max_tokens vs stop: pair each parameter with the cap it enforces on the completion.
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
`max_tokens` is a hard numeric ceiling that always fires; `stop` is a list of strings that only halts generation when the model happens to emit one.
Picture two ways to tell a storyteller when to stop. The first way is a kitchen timer. You set it to 60 seconds, and no matter what the storyteller is saying when it dings, they have to put down the microphone. That is `max_tokens`. The second way is a code word. You say 'when you reach the end of the story, say THE END, and we will know to clap.' If the storyteller never says the code word, you keep listening forever. That is `stop`. Production systems use both. The timer protects you from an endless tale that runs up the bill. The code word lets the storyteller end naturally at the right narrative beat instead of getting cut off mid-sentence by the timer.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
3 min: numeric cap vs string sentinel + unconditional vs conditional firing + cost-ceiling vs structural-boundary roles + the always-set-max_tokens rule + streaming tail-buffering subtlety.
| Aspect | max_tokens | stop |
|---|---|---|
| Type | Integer | List of strings |
| Trigger condition | Unconditional, count reached | Conditional, string matched |
| Cost guarantee | Yes, hard ceiling | No, depends on model output |
| Output includes trigger | N/A, counts tokens | No, sentinel is stripped |
| Primary use | Bound spend and latency | Shape structured boundaries |
Real products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
Relying on `stop` alone to bound cost. If the model never emits the sentinel string, decoding runs until the model's context limit, which can cost dollars per request.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.