max_tokens caps the number of OUTPUT tokens the model will generate; it does not limit input length (the context window does that).
Imagine you ask a friend a question and add 'reply in at most 100 words'. That instruction does not say anything about how long your question was. You can ask a 5,000-word question with the same 100-word reply limit. max_tokens is that reply limit, except it counts tokens instead of words. Your input can be as long as the model's context window allows. The cap only applies to what the model writes back. If the model is mid-sentence when it hits the cap, it just stops, even mid-word. The reply will look truncated. To detect this, check the finish_reason field in the response. It says 'length' when max_tokens fired.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
max_tokens is often confused with a prompt-length limit, or with the context window, or with the model's maximum output capability. None of those is what max_tokens controls.
What max_tokens caps
max_tokens caps the number of output tokens the model will generate in its reply, and nothing else. It does not bound the input length, it does not bound the context window, and it does not bound any per step internal computation.
The mechanism lives in the decoding loop. After the model encodes the full input, it starts generating. The inference engine produces one token, appends it to the running output, and increments a counter. After each step, the engine checks whether the counter has reached max_tokens. On match, decoding halts immediately. The output is whatever was produced up to that point, possibly mid-sentence, mid-word, or mid-JSON.
The response payload always reports why decoding stopped. OpenAI uses finish_reason with values like stop, length, content_filter. Anthropic uses stop_reason with values like end_turn, stop_sequence, max_tokens. When max_tokens fired, the reason is length (OpenAI) or max_tokens (Anthropic). Production code reads this field and treats a length termination as a quality signal. Either the prompt asked for more than the cap, or the format is unstable and not reaching a natural ending.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- OpenAI's Chat Completions API uses max_completion_tokens (max_tokens is deprecated) to cap reply length, including reasoning tokens, and reports finish_reason='length' when it fires.
- Anthropic's Claude messages API uses max_tokens as a required parameter (no default) and reports stop_reason='max_tokens' on the response when the cap is hit.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhat is the difference between max_tokens and max_completion_tokens in OpenAI's newer API, and why did they introduce the latter?
Reasoning models like o1 generate hidden reasoning tokens before the visible reply; max_completion_tokens caps visible output and reasoning tokens together. max_tokens is deprecated in its favor and is not compatible with o-series models.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Confusing max_tokens with the context-window limit; max_tokens caps the reply length only, and the context window caps total input plus output.
60 second bullets to scan on the way to the call.
What max_tokens caps (output tokens only)
How it differs from the context-window limit (which covers input plus output)
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.