Sub topic 6
Setting a thinking token budget
This chapter assumes you can call an LLM API from Python, know what tokens, context windows and max_tokens are, and have read the earlier chapters on reasoning tokens and effort levels. Here you will learn how a thinking token budget works, how to set one, and what can go wrong.
What a thinking budget is
An effort level such as "low" or "high" is a coarse setting. The provider decides how many reasoning tokens that label allows. A thinking token budget is a number you choose. You tell the API roughly how many tokens the model may spend on internal reasoning before it writes the final answer.
Anthropic's Messages API uses this style through the thinking parameter. Google's Gemini 2.5 models offer a similar thinkingBudget setting. OpenAI's reasoning models use effort levels instead. The parameter names differ, but the idea is the same: you cap how much of the output allowance can go to hidden reasoning.
The budget is a target, not a promise. Three points follow from that:
- The model can use fewer tokens than the budget on an easy question.
- The budget can be exceeded slightly, so do not rely on it as a hard cap.
- A larger budget gives the model room to think longer. It does not force the model to.
The basic request
The example below turns on extended thinking with a 2,000-token budget. It also separates the thinking blocks from the final text.
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)Three details matter here.
thinkingis an object withtypeset to"enabled"and abudget_tokensvalue. Leave the parameter out and the model does not use extended thinking.max_tokensis 4096, which is larger thanbudget_tokens. This is required in the standard case.- The response
contentis a list of blocks. Athinkingblock comes first, followed by atextblock. Code that reads onlyresponse.content[0].textwill fail, because the first block is the thinking block.
Rules for the budget value
The API enforces some limits on budget_tokens:
| Rule | Detail |
|---|---|
| Minimum | 1,024 tokens. Smaller values are rejected. |
| Upper bound | Must be less than `max_tokens`. |
| Exception | With interleaved thinking during tool use, the budget can exceed `max_tokens`, because the budget then covers all thinking across the turn. The context window still limits it. |
| Disabling | Omit the `thinking` parameter, or set `{"type": "disabled"}`. |
Other request settings are also restricted while thinking is on. Check the current documentation for your model, since these rules change. At the time of writing, you cannot combine thinking with a custom temperature, and top_p is limited to a narrow range. You also cannot force the model to call a specific tool with tool_choice. If a request that worked before starts returning a 400 error after you add thinking, look at these settings first.
How the budget shares `max_tokens`
Thinking tokens count as output tokens. The max_tokens value limits the thinking and the visible answer together. This is the main source of surprises with budgets.
Suppose you set budget_tokens=3500 and max_tokens=4096. If the model uses its whole budget, only about 600 tokens remain for the answer. A long explanation will be cut off. A practical rule is to set
max_tokens = budget_tokens + expected answer length + some margin
Always check response.stop_reason. A value of "max_tokens" means the output hit the limit. With thinking enabled, that may mean the answer is incomplete even though the request succeeded.
Large max_tokens values also have an operational cost. Anthropic requires streaming for requests that could run long. The SDK raises an error for non-streaming calls when the expected time is too long. If you plan budgets in the tens of thousands of tokens, use client.messages.stream(...).
Cost and usage
Thinking tokens are billed at the output token rate. Anthropic's Claude 4 models return a summary of the thinking, but you are billed for the full thinking tokens, not the summary. The visible thinking text can therefore be much shorter than the amount you pay for.
The usage field reports output_tokens, which includes thinking tokens. This differs from the OpenAI Responses API, where output_tokens_details.reasoning_tokens separates the reasoning part. With a budget, you can work out the thinking share as follows:
- Run the same prompt with thinking disabled and compare
output_tokens. - Run it at several budgets and watch how
output_tokensmoves.
The next section does this in code.
Sweeping budgets
This script runs one prompt at several budgets and prints the output size and latency. It uses only the fields described above.
import time
import anthropic
client = anthropic.Anthropic()
PROMPT = (
"A warehouse ships boxes in groups of 6, 9 or 14. "
"What is the smallest number of boxes that can be split evenly into "
"groups of any one of those three sizes? Give the number and one line of reasoning."
)
def run(budget):
"""budget is None for thinking off, or an int of at least 1024."""
kwargs = {
"model": "claude-sonnet-4-5",
"messages": [{"role": "user", "content": PROMPT}],
}
if budget is None:
kwargs["max_tokens"] = 1024
else:
kwargs["max_tokens"] = budget + 1024
kwargs["thinking"] = {"type": "enabled", "budget_tokens": budget}
start = time.perf_counter()
response = client.messages.create(**kwargs)
seconds = time.perf_counter() - start
answer = "".join(b.text for b in response.content if b.type == "text")
return {
"budget": budget,
"output_tokens": response.usage.output_tokens,
"stop_reason": response.stop_reason,
"seconds": round(seconds, 1),
"answer": answer.strip()[:80],
}
for budget in (None, 1024, 4000, 8000):
print(run(budget))Notice that max_tokens is computed from the budget, so the answer always has 1,024 tokens of room. For a task like this one, expect output_tokens to climb from the no-thinking run to the 1,024 run. After that it will probably level off, because the model stops thinking once it is confident. If the 8,000 budget uses almost the same number of tokens as the 4,000 budget, the extra headroom was unused. Raising the budget further would not change the cost.
Choosing a budget
Start small and increase only when measurements justify it.
| Task type | Starting budget | Notes |
|---|---|---|
| Simple extraction or classification | Thinking off | Reasoning adds cost and delay with little gain. |
| Short arithmetic or logic puzzles | 1,024 to 2,000 | The minimum is often enough. |
| Multi-step analysis, code review | 4,000 to 8,000 | Check whether quality improves over the minimum. |
| Hard math, planning, large refactors | 16,000 or more | Use streaming, and test for diminishing returns. |
The budget is also a latency control. Thinking tokens are generated one at a time, like any output. A request that spends 8,000 thinking tokens takes noticeably longer than one that spends 1,000, before the first answer token appears. For interactive features, set the budget by the delay your users will accept. Do not set it by the maximum quality you could reach.
Common errors
- Budget below the minimum. A
budget_tokensvalue under 1,024 returns a validation error. Use 1,024 or turn thinking off. - Budget not below
max_tokens. This is rejected in the standard case. Raisemax_tokensor lower the budget. - Empty or truncated answer. The model used most of
max_tokensfor thinking. Checkstop_reason, then raisemax_tokens, not only the budget. - Reading the wrong block. Code that assumes
content[0]is text breaks. Filter byblock.type. - Setting
temperature. Custom sampling settings can conflict with thinking. Remove them and retest. - Dropping thinking blocks in tool loops. When the model calls a tool, send the whole assistant message back, including its thinking blocks with their signatures. Rebuilding the message from only the text parts breaks the continuity check.
- Treating the budget as a hard cap. Plan for some overshoot in cost estimates.
Budgets compared with effort levels
Budgets give you finer control and are easier to reason about for cost. You can compute a worst-case thinking cost from the number. Effort levels are simpler and less tied to one model's token counts, so they adapt better when you change models. If your provider offers both, a budget suits cost-sensitive pipelines with a stable model. An effort level suits code that must move between models. Each provider's documentation defines how the two interact, so check it before mixing them in one request.
Exercise
You set max_tokens=2048 and budget_tokens=2000 for a task that needs a 700-token answer. The response succeeds but the answer ends mid-sentence, and stop_reason is "max_tokens". What happened, and what two changes would fix it?
Show answer
Thinking tokens count toward max_tokens. The model spent most of the 2,048 tokens on thinking, so fewer than 700 remained for the answer, and the output was cut off. Raise max_tokens to at least the budget plus the expected answer plus a margin (for example 2000 + 700 + 500 = 3200 or more), or lower budget_tokens so more room is left for the answer. Raising max_tokens is the safer fix when the task needs deep thinking.
Exercise
You run the budget sweep and get output_tokens of 310 (thinking off), 1,150 (budget 1,024), 1,180 (budget 4,000) and 1,170 (budget 8,000). Answers are equally correct at all thinking levels. Which budget should you ship, and why?
Show answer
Ship the 1,024 budget, or test whether thinking is needed at all by comparing accuracy against the no-thinking run on more cases. The 4,000 and 8,000 runs use almost the same tokens as the 1,024 run, so the extra headroom is unused, and a larger budget adds no benefit here. The no-thinking run is cheapest, but one prompt is too small a sample to decide, so measure accuracy on a larger set first.