Zenaique

What does the max_tokens parameter control in an LLM API call?

MCQ·Easy·4.0 · 0·~1 min·Asked atGraphcorePineconeUber·Relevant atAnthropic
Attempt it
TL;DR

max_tokens caps the number of OUTPUT tokens the model will generate; it does not limit input length (the context window does that).

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

Imagine you ask a friend a question and add 'reply in at most 100 words'. That instruction does not say anything about how long your question was. You can ask a 5,000-word question with the same 100-word reply limit. max_tokens is that reply limit, except it counts tokens instead of words. Your input can be as long as the model's context window allows. The cap only applies to what the model writes back. If the model is mid-sentence when it hits the cap, it just stops, even mid-word. The reply will look truncated. To detect this, check the finish_reason field in the response. It says 'length' when max_tokens fired.

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

max_tokens is often confused with a prompt-length limit, or with the context window, or with the model's maximum output capability. None of those is what max_tokens controls.

What max_tokens caps

max_tokens caps the number of output tokens the model will generate in its reply, and nothing else. It does not bound the input length, it does not bound the context window, and it does not bound any per step internal computation.

The mechanism lives in the decoding loop. After the model encodes the full input, it starts generating. The inference engine produces one token, appends it to the running output, and increments a counter. After each step, the engine checks whether the counter has reached max_tokens. On match, decoding halts immediately. The output is whatever was produced up to that point, possibly mid-sentence, mid-word, or mid-JSON.

The response payload always reports why decoding stopped. OpenAI uses finish_reason with values like stop, length, content_filter. Anthropic uses stop_reason with values like end_turn, stop_sequence, max_tokens. When max_tokens fired, the reason is length (OpenAI) or max_tokens (Anthropic). Production code reads this field and treats a length termination as a quality signal. Either the prompt asked for more than the cap, or the format is unstable and not reaching a natural ending.

max_tokens vs the context window
max_tokens as a cost and latency ceiling
max_tokens, stop sequences, and quality signal
Sizing max_tokens for real workloads
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.

Real products, models, and research that use this idea.

  • OpenAI's Chat Completions API uses max_completion_tokens (max_tokens is deprecated) to cap reply length, including reasoning tokens, and reports finish_reason='length' when it fires.
  • Anthropic's Claude messages API uses max_tokens as a required parameter (no default) and reports stop_reason='max_tokens' on the response when the cap is hit.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QWhat is the difference between max_tokens and max_completion_tokens in OpenAI's newer API, and why did they introduce the latter?
A

Reasoning models like o1 generate hidden reasoning tokens before the visible reply; max_completion_tokens caps visible output and reasoning tokens together. max_tokens is deprecated in its favor and is not compatible with o-series models.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Confusing max_tokens with the context-window limit; max_tokens caps the reply length only, and the context window caps total input plus output.

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • What max_tokens caps (output tokens only)

  • How it differs from the context-window limit (which covers input plus output)

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
Pair each decoding strategy with its determinism and diversity profile.
Match pairs·Easy