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
| Concept | Meaning |
|---|---|
| Input tokens | Your messages, context, file content, tool results, and other input |
| Output tokens | The visible response generated by the model |
| Reasoning tokens | Hidden reasoning tokens used internally by some reasoning models |
| Context window | The shared limit used by input, output, and some internal tokens |
| Output limit | The maximum visible output generated in one response |
Different parameter names
| Parameter | Common use case |
|---|---|
max_tokens | Claude / Anthropic Messages API, and some Chat Completions setups |
max_completion_tokens | Some OpenAI reasoning models through Chat Completions |
max_output_tokens | Common output limit field for OpenAI Responses API |
What happens if you do not set it
Behavior differs by provider and API:| Scenario | Possible result |
|---|---|
| Claude / Anthropic-compatible API | May require an explicit max_tokens value |
| OpenAI-compatible Chat Completions | May use the model’s default behavior |
| OpenAI Responses API | May use max_output_tokens or the tool’s default |
| Other models | May have their own default output limits |
Recommended ranges
| Task | Suggested range |
|---|---|
| General Q&A | 1024 - 4096 |
| Code explanation / small edits | 4096 - 8192 |
| Long-form writing / complex coding tasks | 8192 or higher, depending on the model limit |
| Batch tasks | Use a smaller limit to avoid unexpectedly long responses |
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.

