Skip to content
All patterns
Cost

Reasoning token budgeting

Problem

Frontier reasoning models (OpenAI o1/o3, Claude 3.7 Thinking, Gemini Flash Thinking, DeepSeek-R1) generate chain-of-thought tokens during inference. Without explicit budgeting, queries burn thousands of thinking tokens on overthinking, hit API output token limits, trigger HTTP socket timeouts, and blow cost ceilings.

Pattern

Allocate dedicated reasoning token budgets based on task complexity tiers. Reserve output token headroom so thinking does not truncate the final response, decouple client-side read timeouts to tolerate thinking latency, and strip obsolete 'think step-by-step' prompt wrappers.

Implementation
reasoning_budget.py
from anthropic import Anthropic

client = Anthropic()

# Route task complexity to explicit reasoning token budgets
BUDGET_TIERS = {
    "triage": 1024,       # Simple validation, classification, quick edits
    "refactor": 4096,     # Multi-file code generation and bug fixes
    "architecture": 12288, # Deep formal reasoning, distributed algorithms
}

def solve_with_reasoning(task: str, complexity: str = "refactor") -> dict:
    reasoning_budget = BUDGET_TIERS.get(complexity, 2048)
    # Total max_tokens must accommodate both thinking budget AND the final response
    response_headroom = 4096
    total_max_tokens = reasoning_budget + response_headroom

    # Configure thinking budget explicitly
    response = client.messages.create(
        model="<large-model>",  # Model names change often; check your provider's model list (as of 28 October 2026). Replace <...-model> with a model id from that list.
        max_tokens=total_max_tokens,
        thinking={
            "type": "enabled",
            "budget_tokens": reasoning_budget,
        },
        messages=[{"role": "user", "content": task}],
    )

    thinking_content = ""
    final_text = ""
    for block in response.content:
        if block.type == "thinking":
            thinking_content = block.thinking
        elif block.type == "text":
            final_text = block.text

    return {
        "result": final_text,
        "thinking_tokens_used": getattr(response.usage, "thinking_tokens", None),
        "output_tokens": response.usage.output_tokens,
        "stop_reason": response.stop_reason,
    }
Trade-offs
  • Explicit budgets prevent runaway cost and latency spikes
  • Under-budgeting reasoning tokens can lead to incomplete reasoning on hard logic
  • Reasoning latency delays time-to-first-token (TTFT) for interactive streaming users
  • Requires tuning budget tiers per task domain rather than using a single global value
Production checklist
  • Ensure max_tokens equals thinking budget plus adequate response headroom
  • Set gateway and socket read timeouts to 60-120s to prevent premature client disconnections
  • Handle max_tokens stop reason gracefully by detecting when reasoning was truncated
  • Avoid 'think step by step' instructions in prompts; reasoning models think natively
  • Track thinking token consumption vs output token volume in tracing dashboards