Short answer
max_tokens limits how many tokens the model can generate in one response. Different APIs and models use different field names, required settings, and default behavior.
For stable output, set an explicit output limit for long answers, code generation, and documentation tasks. For Claude / Anthropic-compatible API calls, you usually need to send max_tokens explicitly.
Core concepts
If the output limit is too small, the response may be cut off. If it is very large, the model will not always generate that many tokens, but long-output tasks may cost more.
Different parameter names
If you use Codex CLI or another tool based on the Responses API, follow the field currently supported by that tool or provider. Do not hard-code the same parameter name for every model.
What happens if you do not set it
Behavior differs by provider and API:
The same code can produce different output lengths after you switch models. To reduce surprises in production, set an explicit output limit.
Recommended ranges
Check the model plaza and upstream model docs for the actual maximum. Model output limits can change by version.
What to do when output is truncated
If the response containsfinish_reason: "length", the model usually hit the output limit.
Troubleshoot in this order:
- Increase the output limit field supported by the current API.
- Check whether you used the correct parameter name.
- Make the prompt more focused to reduce unnecessary output.
- Use a model with a larger output window.
- Split the task into multiple steps.
Common mistakes
- Assuming a larger
max_tokensvalue always makes the model write more. - Retrying after truncation without checking
finish_reason. - Using an old field name with a reasoning model.
- Ignoring hidden reasoning tokens when estimating context and cost.
- Ignoring the model’s own maximum output limit.