AI & prompting

Max Tokens vs Context Length: What Each LLM Limit Controls

Context length (the context window) is the total number of tokens a model can handle in one request, counting your input and its reply together. Max tokens (max_tokens) is a separate cap you set on how many tokens the model may generate in that one reply, so it is always a slice of the context window, never extra space on top of it.

If you want the background on the window itself (what fills it, why long contexts degrade and cost more), see What Is a Context Window?. This article is about the output side: what max_tokens does, how it interacts with the window, and how to choose a value.

Max tokens vs context length at a glance

Context length (context window)max_tokens (output limit)
What it limitsInput + output tokens in one requestOutput tokens in one reply
Who sets itThe model (fixed by the provider)You, per request
Includes the prompt?Yes: system prompt, messages, tool definitions, imagesNo
Includes thinking/reasoning tokens?YesYes, on Claude and OpenAI reasoning models
What happens when you hit itRequest rejected, or generation stops at the window edgeReply is cut off, possibly mid-sentence
Typical sizes (Claude, as of October 2026)1M tokens on current models; 200k on older onesUp to 128k per request on the 1M-token models

The Claude figures come from Anthropic's context-window documentation, which lists a 1M-token window and up to 128k output tokens (max_tokens) per request for its current models. Other providers publish different limits per model, and they change often, so check the model page before relying on a number.

What max_tokens actually does

max_tokens is a ceiling, not a target. Anthropic's API reference describes it as "The maximum number of tokens to generate before stopping" and adds that the models "may stop before reaching this maximum". Setting max_tokens: 4000 does not make the model write 4,000 tokens; a two-sentence answer still comes back as two sentences.

What it does do is guarantee an upper bound on output length, and therefore on output cost and on generation time for that request. When the model reaches the limit, it stops right there. Anthropic's latency guide warns that the response "will be cut off, perhaps mid-sentence or mid-word".

In the Claude Messages API, max_tokens is required: it is the only body parameter in the reference without an "optional" label. In OpenAI's Chat Completions API it is optional, and the old max_tokens parameter is deprecated in favour of max_completion_tokens, and the reference says max_tokens "is not compatible with o-series models". max_completion_tokens counts visible output plus reasoning tokens.

The parameter names differ by API

APIOutput limit parameterHow a cut-off reply is reported
Claude Messages APImax_tokens (required)stop_reason: "max_tokens"
OpenAI Chat Completionsmax_completion_tokens (max_tokens deprecated)finish_reason: "length"
OpenAI Responses APImax_output_tokensstatus: "incomplete"
Ollama (local)num_predict optionGeneration stops at the limit

Ollama's Modelfile reference lists num_predict as "Maximum number of tokens to predict when generating text" with a default of -1 (infinite generation). Its context length is a separate option, num_ctx, listed with a default of 2048. With local models the context length is something you configure, because a larger window needs more memory.

How the two limits interact

The basic arithmetic is: input tokens + output tokens must fit in the context window. Suppose a model has a 200,000-token window and your request contains 185,000 tokens of documents and conversation. Even if you set max_tokens: 32000, only 15,000 tokens of room are left for the reply.

What the API does in that situation depends on the provider and model generation. Anthropic documents the behaviour precisely:

  • If the input alone exceeds the context window, every model returns a 400 invalid_request_error ("prompt is too long").
  • On Claude 4.5 models and newer, input + max_tokens may exceed the window. The request is accepted, and if generation reaches the window edge it stops with stop_reason: "model_context_window_exceeded".
  • On earlier Claude models, input + max_tokens above the window is a validation error, so you had to shrink max_tokens to fit.

So on current Claude models you can set a generous max_tokens without computing the exact remaining room. On older models, and on any API that validates the sum, compute it: max_tokens = min(desired_output, context_window - input_tokens). Count input tokens with the provider's token-counting endpoint, or estimate them with a counter such as larely's token counter.

Thinking and reasoning tokens use the same budget

This is where most "empty response" and "truncated answer" bugs come from with reasoning models. The hidden reasoning is output, and it is counted against the same limit as the visible answer:

  • Claude: the context-window docs say thinking tokens "are a subset of your max_tokens parameter, are billed as output tokens, and count toward rate limits."
  • OpenAI: the reasoning guide says the max_output_tokens cap covers reasoning tokens as well as visible output tokens, and that reasoning tokens "still occupy space in the model's context window and are billed as output tokens". When you first experiment with these models, OpenAI advises "reserving at least 25,000 tokens for reasoning and outputs".

Worked example: you set max_tokens: 2000 on a reasoning task and the model spends 1,900 tokens thinking. Only 100 tokens remain for the answer, so you get the start of a reply and a max_tokens stop reason. With thinking enabled, the limit needs to cover the thinking plus the longest answer you expect.

How to choose a max_tokens value

  1. Estimate your longest legitimate reply. A classification label needs a few tokens; a 1,500-word report needs a few thousand. Use real token-per-word counts rather than guessing.
  2. Add headroom so normal variation doesn't cut replies off. If replies are usually 800 tokens, a limit of 1,000 will truncate the occasional long one.
  3. Add the thinking budget if the model reasons before answering.
  4. Check that it fits the context window with your largest expected input, if your API validates the sum.
  5. Use a low limit as a guard where a long reply would be a bug, such as a yes/no classifier or a JSON field extractor. A runaway reply then fails fast and cheaply.

Billing follows the tokens actually generated, not the limit, so a high max_tokens costs nothing extra on a reply that stops naturally. The cost risk is only in the worst case. How LLM API pricing works shows the per-token arithmetic.

Detecting and handling a truncated reply

Always check the stop field before using the output. A truncated JSON object fails to parse; a truncated tool call is unusable. A minimal check with the Anthropic Python SDK:

response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=messages,
)
if response.stop_reason in ("max_tokens", "model_context_window_exceeded"):
    # The reply is incomplete: raise the limit, continue, or flag it
    ...

Anthropic's stop-reason guide gives two remedies: "Raise max_tokens or continue the response". If the reply ends in an incomplete tool call, the guide says to retry the request with a higher limit; a half-written tool call cannot be repaired. For OpenAI Chat Completions the same check is choice.finish_reason == "length".

If replies are still cut off

Look at the token usage the API returns with each response (usage.output_tokens in the Claude API). If output tokens equal your limit, the limit is the cause. If output is well below the limit but the stop reason is the context window, the input is too large: trim conversation history, drop unused tool definitions, or summarise earlier turns. If you run models locally and long prompts seem to be "forgotten", check num_ctx before anything else: the window your runtime is configured with can be far smaller than the maximum the model supports, and the model can only work with what fits inside it.

Frequently asked questions

Does max_tokens include the prompt?
No. In the Claude Messages API and the OpenAI APIs, max_tokens (or max_completion_tokens / max_output_tokens) limits only the tokens the model generates in that response. The prompt counts toward the context window, not toward max_tokens.
Does a high max_tokens cost more?
You are billed for the tokens actually generated, not for the limit you set. A reply that stops naturally after 300 tokens costs the same whether max_tokens was 1,000 or 32,000. What a high limit changes is the worst case: a runaway or very long reply can use all of it.
Why did my response stop mid-sentence?
It most likely hit the output limit. Check the stop field: stop_reason "max_tokens" in the Claude API, finish_reason "length" in OpenAI Chat Completions, or status "incomplete" in the OpenAI Responses API. Raise the limit or ask the model to continue.
Do thinking or reasoning tokens count toward max_tokens?
Yes, on both Claude and OpenAI reasoning models. Claude's thinking tokens are a subset of max_tokens, and OpenAI's max_output_tokens and max_completion_tokens include reasoning tokens. A reasoning model can therefore use up the whole limit before writing any visible text.

Sources