AI / Claude Models Basics Interview Questions
What is Claude's max_tokens parameter and how does it relate to the context window?
The max_tokens parameter sets the maximum number of output tokens Claude will generate in a single response. It is a hard cap — Claude will stop generating once it reaches this limit, potentially truncating its response mid-sentence.
# max_tokens is required in the Messages API response = client.messages.create( model="claude-opus-4-8", max_tokens=1024, # Claude generates at most 1024 output tokens messages=[{"role": "user", "content": "Write a detailed essay."}] ) # Check if Claude stopped due to max_tokens if response.stop_reason == "max_tokens": print("Response was cut off â increase max_tokens or use a longer window") elif response.stop_reason == "end_turn": print("Claude naturally finished its response") # Relationship: # context_window = input_tokens + max_tokens (reserved output) # Available input = context_window - max_tokens # e.g. for Opus 4.8: 1,000,000 - 1024 = 998,976 tokens available for input
| Model | Maximum allowed max_tokens | Default if not set |
|---|---|---|
| Claude Fable 5 | 128,000 | N/A — required parameter |
| Claude Opus 4.8 | 128,000 | N/A — required parameter |
| Claude Sonnet 5 | 128,000 | N/A — required parameter |
| Claude Haiku 4.5 | 64,000 | N/A — required parameter |
max_tokens is a required parameter in the Messages API — the request will fail without it. Setting it to the maximum value is usually wasteful; choose a value appropriate for the expected response length. The stop_reason field in the response tells you why Claude stopped generating.
More Related questions...