Skip to content
47% off: the complete course for $79 (reg. $149)Order now →
AI Builder

AI builder glossary

Max tokens

The required cap on how many tokens a model may generate in one response, which limits output length, cost, and runtime but can truncate answers.

Max tokens is a ceiling, not a target. The model writes until it's done or hits the cap, whichever comes first, and you pay only for tokens actually generated. On the Claude API the max_tokens parameter is required on every request, and on reasoning models it covers thinking tokens as well as the visible answer.

Setting it is a balance. Too high and a runaway response, or an abusive user, can burn money and keep a request open for a long time. Too low and you get truncated output: half a summary, or JSON that won't parse. With thinking enabled, a tight cap is especially risky, because the model may spend most of it reasoning and run out before finishing the answer.

A good default is generous for open-ended features and small only where the output is genuinely tiny, like a single classification label. Pair it with a stop-reason check so truncation is handled rather than silently shown. Very large output budgets may also require streaming so the connection doesn't time out.

Max tokens is also a cost and abuse control. Per-route caps, alongside rate limits and per-user budgets, keep one request from becoming an expensive surprise.

Example

A sentiment tagger uses a small cap because it only returns a label, while the reply drafter uses a generous one; both check for a max_tokens stop reason and log it as an error if it ever appears.

Learn more in the course

Related terms

Go from definitions to doing

Learn to build AI-powered apps in 132 practical lessons.