Skip to main content

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. 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 contains finish_reason: "length", the model usually hit the output limit. Troubleshoot in this order:
  1. Increase the output limit field supported by the current API.
  2. Check whether you used the correct parameter name.
  3. Make the prompt more focused to reduce unnecessary output.
  4. Use a model with a larger output window.
  5. Split the task into multiple steps.

Common mistakes

  • Assuming a larger max_tokens value 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.

About BetterToken

BetterToken records usage at the API Key and model request level. You can use the dashboard to observe token usage across models and tasks. The output limit field is still determined by the API format and model you use. When configuring it, check the model, Base URL, API format, and parameter name together.