◉CLAUDE LABJP
●2.1.301 — Adds the vim % motion, LaTeX math rendering in the VS Code panel, and a retry-watchdog limit for usage-limit waits (10/15)●2.1.299 — Conversations over 200K tokens left idle about 54 minutes get compacted; MCP allow rules now match only the server they name●SONNET 4.5 — Retires on the Claude API on 11/30, 45 days left. Migrate to Sonnet 5.5●WINDOWS — Reports say hooks break silently only on Japanese-locale Windows; check the character encoding first●FAST MODE — Opus 5.5 fast mode is out of research preview and no longer needs the beta header (10/15)●NEW — I Put a Ten-Line Promise File at the To…●2.1.301 — Adds the vim % motion, LaTeX math rendering in the VS Code panel, and a retry-watchdog limit for usage-limit waits (10/15)●2.1.299 — Conversations over 200K tokens left idle about 54 minutes get compacted; MCP allow rules now match only the server they name●SONNET 4.5 — Retires on the Claude API on 11/30, 45 days left. Migrate to Sonnet 5.5●WINDOWS — Reports say hooks break silently only on Japanese-locale Windows; check the character encoding first●FAST MODE — Opus 5.5 fast mode is out of research preview and no longer needs the beta header (10/15)●NEW — I Put a Ten-Line Promise File at the To…
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-thinking6tool-use24claude-api83python22agent14api-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
The Real Cost of Claude API Extended Thinking in Production — ROI Data by Task Type
Three months of measured cost, quality, and speed data for Extended Thinking across five task categories. Learn exactly when extended thinking is worth it—and when it's not.
⬡ API & SDK2026-09-03
Don't Hand Your Agent the Tool That Commits — Sorting Tools Into Advise, Prepare, and Commit
Before you wrap irreversible actions in an approval gate, decide which ones never belong in the tool list at all. A three-tier inventory, a rewritten tool definition, and a small script that flags commits hiding in your schema.
⬡ API & SDK2026-06-12
Handing Monthly Revenue CSVs to the Claude API Code Execution Tool — Files API Wiring, Container Reuse, and Billing Traps
How I moved monthly revenue-CSV reconciliation into the Claude API Code Execution tool — a pre-upload sanity check, Files API integration, container reuse, the 5-minute billing minimum, and the file recovery that pause_turn quietly drops.