How Many max_tokens Should You Set? A 4-Step Method by Task (2026)

Blog / AI concepts

How Many max_tokens Should You Set? A 4-Step Method by Task (2026)

There is no single right answer for how many max_tokens to set. It is the upper limit on how many tokens the model generates in one reply, and it should be a bit larger than the reply length you expect, with some headroom. Set it too low and replies get cut off midway. Set it too high and you usually won't pay more, but you may run into the context window limit, or let a runaway output go on far too long. Below: what it is, how the two major APIs treat it, and how to pick a value per task.

What exactly does max_tokens limit?

It limits the number of output tokens in this one reply. It does not limit the input you send. A token is the basic unit a model uses to process text; in Chinese, one common character is roughly one to two tokens, depending on the tokenizer. For details, see "What is a token in an LLM, and why are you billed per token?"

Two points are easy to mix up:

  • It is a cap, not a target. The model stops when it thinks it is done. Setting 4096 does not mean you will get 4096 tokens.
  • Billing is based on the tokens actually generated, not the cap you set. A high cap does not make your bill bigger by itself. Output tokens usually cost more than input tokens; see "Why output tokens cost more than input" for the reason.

How does it relate to the context window?

The context window is the total amount of input plus output that fits in one request. Think of it like this:

input tokens + output tokens ≤ context window

So max_tokens can't grow without limit. If your input already takes most of the window, there is little room left for output, and even a very high max_tokens only lets the model write until the window runs out. Some APIs return a parameter error outright when input plus max_tokens exceeds the window; others truncate to the remaining space. Check each provider's official docs for the exact behavior.

Each model also has its own maximum output per request, often smaller than the context window. If you set a value above that limit, you will usually get an error or be capped at the limit. Again, the official docs are the authority.

Is max_tokens required in the OpenAI and Anthropic APIs?

ItemAnthropic Messages APIOpenAI Chat Completions API
Required?Required; omitting it returns an errorOptional; if omitted, the model and server defaults apply
Parameter namemax_tokensNewer docs recommend max_completion_tokens; the older max_tokens no longer applies to some models
Sign of truncationstop_reason in the response is max_tokensfinish_reason in the response is length

Notes:

  • On the Anthropic side the field is required, and forgetting it is the most common error for newcomers. For a full side-by-side of the two request formats, see "Anthropic API vs. OpenAI API: what's different?"
  • On the OpenAI side, for reasoning models max_completion_tokens usually counts the model's internal thinking tokens too. That means if you set 1000, the visible reply may be only a few hundred tokens, with the rest used up by thinking. Whether this applies depends on the official docs for the specific model.
  • When you go through a third-party "OpenAI-compatible" gateway, support for parameter names may differ. If a field isn't recognized, check the gateway's docs first. For what compatibility means, see "What does an OpenAI-compatible API mean?"

How do you choose max_tokens for a task? Four steps

  1. Estimate the reply length you want. For Chinese replies, estimate tokens as the character count times 1 to 1.5. For example, a summary of up to 500 characters is about 500 to 750 tokens. This is a rough estimate; for better accuracy, run a few requests of the same kind first and check the actual output tokens in the usage field of the response.
  2. Add 30% to 50% headroom on top of that. For the summary example above, you could set about 1000. The headroom keeps an occasionally longer reply from being cut off.
  3. Check that input plus output stays within the window. Be especially careful with long-document Q&A and conversations with a lot of history. If the input is too long, trim the history first rather than blindly raising max_tokens.
  4. Watch the truncation rate after launch. Track the share of responses where stop_reason is max_tokens or finish_reason is length. If the share is clearly not zero, either the cap is too low or your prompt makes the model write too much; adjust one of the two.

Rules of thumb for common tasks

TaskApproach
Classification, tagging, yes/no decisionsVery small; a few dozen tokens is enough, which also keeps the model from rambling
One-sentence answers, title generationOn the order of 100 to 300, based on the length you want
Summarizing, rewritingBased on source length and target length, usually a few hundred to a thousand or so
Code generation, long-form writingLarger, starting from a thousand to several thousand; also check whether you hit the model's per-request output limit
Returning JSONLeave plenty of headroom; truncated JSON becomes unparseable half-text. See "Getting LLMs to output JSON reliably"

These are rules of thumb, not official recommended values. Test with your own real requests to get actual numbers.

What if the reply gets cut off?

First confirm that max_tokens is the cause:

// Anthropic response snippet
{ "stop_reason": "max_tokens" }

// OpenAI response snippet
{ "choices": [ { "finish_reason": "length" } ] }

If you see one of these two values, you hit the output cap. Try the following in order:

  1. Raise max_tokens, as long as you stay within the model's per-request output limit and the context window.
  2. State the length requirement in the prompt, for example "keep it under 300 words," so the model wraps up on its own.
  3. If the content really is long, split it into several requests: have the model produce an outline first, then generate each section.
  4. With streaming, truncation also shows up in the last event. See "What is stream=true?"

Do you need special settings when using a gateway?

When you use an aggregation gateway or a compatible API, max_tokens is passed in basically the same way, but the gateway may translate parameters for different backend models, so check the gateway's docs for the details.

If you want a way to connect that is billed in RMB, you can look at AGI Plan. It is a multi-provider LLM aggregation gateway with monthly subscriptions, payment in RMB, and no need for an overseas credit card. It is compatible with both the OpenAI and Anthropic API formats, and tools such as Claude Code and Codex only need a changed endpoint URL. Note that it is not an officially authorized channel, and some models are marked as overseas-only; the available models and regions are as listed at agiplan.dev/models. AGI Plan is a commercial partner of cdnping, and its getting-started guide is at agiplan.dev/startup.

To compare the availability and latency of different models across inference providers, see cdnping's /en/models. If you suspect a slow API is caused by the network, start with /en/learn/ai-api-latency-china.

Summary and next steps

  • First work out which API you are using: Anthropic requires it, while OpenAI makes it optional and the parameter name is changing.
  • Set it to "expected length plus headroom," then verify with real requests instead of setting it to the maximum by feel.
  • Log how often stop_reason / finish_reason shows truncation; it is the most direct basis for tuning.
  • If you haven't started yet, read "How to estimate your monthly token usage" first to get a feel for your input and output volumes, then decide on a plan and a cap.

Related articles

Keep reading