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. The way to detect this is to check the finish_reason field in the response, which 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 one of those API parameters that everyone uses on their first LLM call and very few engineers actually understand correctly. The confusion is consistent: candidates conflate max_tokens 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.

This deep dive defines max_tokens precisely, walks through how it is enforced during decoding, distinguishes it from the context window, and connects it to the cost, latency, and quality decisions that production prompts have to make.

What max_tokens actually caps

max_tokens caps the number of output tokens the model will generate in its reply. 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 input has been fully encoded and the model 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
Why max_tokens is the cheapest cost lever
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_tokens (or max_completion_tokens on newer models) to cap reply length 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 distinguishes the cap on the visible part vs the total. max_tokens is the legacy parameter still supported for backward compatibility.

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, 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
Flashcard: what is a stop sequence in an LLM API call and what is it used for?
Flashcard·Easy