CLAUDE LABJP
FORK — Claude Code 2.1.212 changes what /fork does: it copies your conversation into a new background session with its own row in claude agents, so you can keep working. The old in-session subagent is now /subtaskLIMITS — WebSearch calls are now capped at 200 per session by default, and subagent spawns get the same 200 ceiling, so a runaway search or delegation loop stops on its ownMCPBG — MCP tool calls running past two minutes now move to the background automatically, keeping the session usable. Tune the threshold with CLAUDE_CODE_MCP_AUTO_BACKGROUND_MSPLANFIX — Fixed plan mode auto-running file-modifying Bash commands such as touch and rm without a permission prompt or an SDK canUseTool callbackSONNET5 — Claude Sonnet 5 is running on introductory pricing of $2 per million input tokens and $10 per million output. After August 31 it moves to $3 and $15IPO — Bankers are reportedly lining up investor meetings for Anthropic ahead of a possible public listing as soon as OctoberFORK — Claude Code 2.1.212 changes what /fork does: it copies your conversation into a new background session with its own row in claude agents, so you can keep working. The old in-session subagent is now /subtaskLIMITS — WebSearch calls are now capped at 200 per session by default, and subagent spawns get the same 200 ceiling, so a runaway search or delegation loop stops on its ownMCPBG — MCP tool calls running past two minutes now move to the background automatically, keeping the session usable. Tune the threshold with CLAUDE_CODE_MCP_AUTO_BACKGROUND_MSPLANFIX — Fixed plan mode auto-running file-modifying Bash commands such as touch and rm without a permission prompt or an SDK canUseTool callbackSONNET5 — Claude Sonnet 5 is running on introductory pricing of $2 per million input tokens and $10 per million output. After August 31 it moves to $3 and $15IPO — Bankers are reportedly lining up investor meetings for Anthropic ahead of a possible public listing as soon as October
Articles/API & SDK
API & SDK/2026-05-05Advanced

Building a 'Think-and-Search' AI Agent — Claude API Extended Thinking × Tool Use

A deep dive into combining Claude API Extended Thinking and Tool Use. Covers frequent errors, a complete research agent implementation in Python, plus cost estimation, timeout design, and error recovery for production use.

extended-thinking7tool-use22claude-api81python22agent13api-sdk13

Premium Article

When Claude Sonnet 4.6 launched, it brought something I had been waiting for: Extended Thinking and Tool Use working together in the same API call. Before this, you had to choose — either let Claude think deeply, or let it call external tools. Not both. With that limitation removed, it became possible to build agents that genuinely reason about what to search, plan a sequence of tool calls, and then interpret the results — all in one coherent loop.

Getting there took a frustrating couple of hours, though. The max_tokens setting kept tripping me up, I had a wrong mental model of what budget_tokens actually means, and handling thinking_delta events during streaming was completely undocumented in the examples I could find. This guide is the resource I wish had existed when I started.

The focus here is working code and the reasoning behind each design choice, not theory. I'm assuming you have already made basic Anthropic API calls and want to go further.

What Becomes Possible When You Combine Both

A quick framing before the code.

Extended Thinking lets Claude run a long internal reasoning process before returning an answer. That reasoning appears in the response as thinking blocks. It excels at multi-step problems where getting to the right answer requires working through intermediate conclusions.

Tool Use lets Claude call external functions during a conversation — web search, database queries, calculations, file reads. Claude signals which tool to call (and with what arguments), your code runs it, and you feed the result back.

Together, the flow looks like this:

  1. Receive user task
  2. Extended Thinking: plan which tools to call, and in what order
  3. Tool Use: execute the planned tools
  4. Extended Thinking: interpret results, decide whether to continue or conclude
  5. Repeat steps 3–4 if needed
  6. Generate final answer

This is qualitatively different from simple tool use. Claude is not just pattern-matching "user asked about prices, call price_lookup tool." It is reasoning about strategy, noticing when a result is incomplete, and adapting its approach mid-task.

Constraints to Understand Before Writing Any Code

The combination has specific constraints that will cause errors if you ignore them. These are worth learning upfront.

Supported Models

Extended Thinking with Tool Use requires one of:

  • claude-opus-4-6 — highest reasoning quality, highest cost
  • claude-sonnet-4-6 — strong balance of quality and cost, the practical default

claude-haiku-4-5 does not support Extended Thinking. For prototypes and development, start with claude-sonnet-4-6 and upgrade to Opus only if you need deeper reasoning on genuinely complex tasks.

The max_tokens Trap

This is where most people first hit an error.

When Extended Thinking is active, max_tokens must accommodate both the thinking tokens and the output tokens. If max_tokens is smaller than budget_tokens, the API rejects the request.

# This will error
response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,           # too small
    thinking={
        "type": "enabled",
        "budget_tokens": 5000  # budget_tokens > max_tokens → error
    },
    tools=[...],
    messages=[...]
)
# Error: thinking budget_tokens must be less than max_tokens
 
# Correct pattern
response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=16000,           # budget_tokens + room for output
    thinking={
        "type": "enabled",
        "budget_tokens": 10000  # stays within max_tokens
    },
    tools=[...],
    messages=[...]
)

budget_tokens is the ceiling on thinking tokens Claude may use — not a guarantee it will use that many. Check response.usage to see actual consumption and tune from there.

tool_choice Recommendation

Use {"type": "auto"} rather than {"type": "any"}. The any option forces a tool call regardless of whether one is needed. When Extended Thinking is active, you want Claude to reason freely and potentially decide no tool is necessary. Forcing tool use interferes with that judgment.

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
Measured data showing thinking-token usage plateaus even as you raise budget_tokens
Concrete fixes for history-management pitfalls, including preserving thinking blocks after stop_reason=tool_use
Complete runnable code for both research and code-review agents, plus production cost and timeout design
Secure payment via Stripe · Cancel anytime

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Claude Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • Copy-paste ready implementation code
  • New advanced guides published daily
  • $5/mo or $10 for lifetime access
View Membership →

Related Articles

API & SDK2026-05-05
Let Claude Diagnose Its Own Tool Errors — Building a Self-Correction Loop with the Anthropic API
Learn how to handle Tool Use failures gracefully by feeding error details back to Claude using the is_error flag, enabling self-diagnosis and automatic retry. Includes working Python code and production antipatterns to avoid.
API & SDK2026-07-09
Same Output, Different Path — Guarding Agent Trajectories with Invariants
When the default model changes, your final output can stay correct while the path your agent takes quietly shifts. Here is a trajectory regression harness built on recorded tool traces and deterministic invariants, with working code and measured numbers.
API & SDK2026-06-29
When Context Editing Made My Agent Re-run the Same Search — Field Notes on Clear Boundaries and Cache Invalidation
After turning on Context Editing to auto-clear tool results, the agent forgot what it had just read, re-ran the same tool, and the cache rebuilt every turn so costs went up. Field notes on instrumenting the silent regression and setting trigger, keep, and clear_at_least from measured data.
📚RECOMMENDED BOOKS
Build a Large Language Model (From Scratch)
Sebastian Raschka
LLM Dev
Prompt Engineering for LLMs
Berryman & Ziegler
Prompting
AI Engineering
Chip Huyen
AI Eng
* Contains affiliate links
See all →