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.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
