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. The way to detect this is to check the finish_reason field in the response, which 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 one of those API parameters that everyone uses on their first LLM call and very few engineers actually understand correctly. The confusion is consistent: candidates conflate max_tokens 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.
This deep dive defines max_tokens precisely, walks through how it is enforced during decoding, distinguishes it from the context window, and connects it to the cost, latency, and quality decisions that production prompts have to make.
What max_tokens actually caps
max_tokens caps the number of output tokens the model will generate in its reply. 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 input has been fully encoded and the model 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_tokens (or max_completion_tokens on newer models) to cap reply length 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 distinguishes the cap on the visible part vs the total. max_tokens is the legacy parameter still supported for backward compatibility.
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, 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.