max_tokens caps how many output tokens the model may generate in one response. It does not limit your input, and it does not change the context window. Anthropic requires it on every request. OpenAI and Gemini treat it as optional.
When the cap is hit, generation just stops, often mid-sentence. You get no error, and you still pay for the tokens produced. So always check the stop field before you use the text:
- Anthropic sets
stop_reasontomax_tokens. - OpenAI sets
finish_reasontolength. - Gemini sets
finishReasontoMAX_TOKENS.
Truncated JSON will fail to parse, so treat that value as a real failure.
On reasoning models, hidden reasoning tokens count against the same budget. A tight cap can then return no visible text at all. OpenAI names that parameter max_completion_tokens on Chat Completions and max_output_tokens on the Responses API.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
Why there's no diagram: “”
The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓
The diagram below the answer is the concept . Jump to it ↓