Sub topic 6
Setting a thinking token budget
This chapter assumes you have called an LLM API from Python and know what tokens, context windows and max_tokens mean. It also assumes you know that a reasoning model produces hidden reasoning tokens before its final answer. Here you will set a thinking budget in the Anthropic Messages API and read the result.
What a thinking budget is
An effort level is a coarse label such as low or high. A thinking token budget is a number. It tells the model roughly how many tokens it may spend on reasoning before it answers. A larger budget allows longer reasoning on hard problems. A smaller budget keeps cost and latency down.
The budget is a target, not a promise. The model can stop thinking earlier when the problem is easy. The budget also limits only the reasoning. The final answer is counted separately, as described below.
Enabling extended thinking
In the Anthropic Messages API you turn on thinking with the thinking parameter. You set type to "enabled" and give budget_tokens.
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=4096,
thinking={"type": "enabled", "budget_tokens": 2000},
messages=[
{"role": "user", "content": "Is 3599 a prime number? Explain briefly."}
],
)
for block in response.content:
if block.type == "thinking":
print("[thinking]", block.thinking)
elif block.type == "text":
print("[answer]", block.text)The response content is a list of blocks. Thinking blocks have type == "thinking". The final answer is in blocks with type == "text". Loop over the blocks and check the type instead of assuming the first block is the answer.
The rule: max_tokens must exceed the budget
max_tokens caps all output, which includes the thinking and the visible answer. The API requires max_tokens to be larger than budget_tokens. In the example, max_tokens=4096 and budget_tokens=2000 leave at least about 2,000 tokens of room for the answer.
The budget also has a minimum. The API rejects values below 1,024 tokens, so budget_tokens=500 fails with a validation error.
Common errors
- Setting
budget_tokensequal to or abovemax_tokens. The request is rejected. - Setting a budget under the minimum of 1,024 tokens.
- Setting
max_tokensonly a little above the budget. If the model uses most of the budget, the answer can be cut off, and the response stops withstop_reasonequal to"max_tokens". - Treating the budget as exact. Actual thinking can be shorter, and the budget is not a guaranteed length.
- Forgetting that thinking tokens are billed as output tokens, even if you do not display them.
Choosing a budget
Start small and raise the budget only when measurements show a gain. The table gives starting points, not rules.
| Task | Starting budget |
|---|---|
| Simple lookup or rewrite | Thinking off |
| Short arithmetic or logic check | 1,024 to 2,000 |
| Multi-step analysis or code debugging | 4,000 to 8,000 |
| Hard math or planning problems | 10,000 or more, with a larger `max_tokens` |
Reasoning models differ in how the budget is read. Some newer models choose their own thinking depth and use an effort setting instead. Check the documentation for your model before you copy a number from another setup. The last chapters of this tutorial show how to compare settings on your own cases.
Reading usage
The response includes a usage object. Its output_tokens field covers the thinking and the visible answer together.
print("output tokens:", response.usage.output_tokens)
print("stop reason:", response.stop_reason)Compare output_tokens with the length of the visible text to see how much went to thinking. If stop_reason is "max_tokens", raise max_tokens or lower the budget.
Exercise
A request sets max_tokens=3000 and budget_tokens=3000. What happens, and how do you fix it?
Show answer
The API rejects it, because max_tokens must be larger than budget_tokens. Raise max_tokens (for example to 5000) or lower the budget (for example to 2000) so there is room left for the visible answer.
Exercise
A response ends with stop_reason equal to "max_tokens" and the answer stops mid-sentence. The budget was 4000 and max_tokens was 4200. What is the likely cause?
Show answer
The model used most of its 4000-token budget on thinking, leaving about 200 tokens for the answer. Increase max_tokens well above the budget, or lower the budget.