D4 · Prompt Engineering › Cluster 5 · Chain of Thought & Extended Thinking

5.4 · Thinking with Tool Use

Reason across tool results

CONCEPT Reasoning about which tool, and about the result

Extended thinking pays off most in agentic loops. Before a tool call, Claude reasons about which tool to use and with what arguments; after a result comes back, it reasons about what the result means and what to do next. Pairing thinking with tool use turns a blind "call tool → get result → call tool" loop into a reflective one where each step is deliberated.

🔑 The value: the model plans the tool call and interprets the result, instead of reflexively chaining calls. That's fewer wasted calls and better recovery when a result is surprising.

CONCEPT Interleaved thinking

Interleaved thinking lets Claude emit a fresh thinking block between tool calls — reasoning after each result before deciding the next action. On current models this is automatic with adaptive thinking (no beta header). Without it, the model reasons once up front, then chains tool calls with no reflection in between.

Without interleavingWith interleaved thinking
Turn shape[think] → tool → result → tool → result → text[think] → tool → result → [think] → tool → result → [think] → text
ReflectionOnly before the first callAfter every result
Best forSimple, known tool sequencesMulti-step tasks where each result changes the plan
⚠️ Two hard constraints when thinking + tools run together: (1) pass the model's thinking blocks (with signature) back unmodified alongside the tool_use block and the tool_result, or reasoning continuity breaks; (2) tool_choice must be auto or none — forcing a specific tool or any is not supported with extended thinking.

DIAGRAM The reflective tool loop

flowchart TD A["User request"] --> T1["🔵 thinking: which tool?"] T1 --> C1["tool_use call"] C1 --> R1["tool_result"] R1 --> T2["🔵 thinking: interpret result,
pick next step"] T2 --> D{"Done?"} D -->|"no"| C1 D -->|"yes"| ANS["🟢 text answer"] R1 -.->|"replay: thinking + tool_use
+ result, unmodified"| KEEP["🟠 preserve signatures"] classDef ok fill:#d4edda,stroke:#28a745,color:#155724; classDef mid fill:#cfe2ff,stroke:#0d6efd,color:#084298; classDef gate fill:#ffe8cc,stroke:#ff9f40,color:#9a4a0a; class ANS ok; class T1 mid; class T2 mid; class KEEP gate;

CODE Preserve thinking blocks across the tool round-trip

When you send the tool result back, the assistant turn must include the original thinking block and the tool_use block — verbatim — so the model's reasoning carries forward.

# 1) First call — Claude thinks, then requests a tool.
resp = client.messages.create(
    model="claude-opus-4-8", max_tokens=16000,
    thinking={"type": "adaptive"},        # interleaved thinking is automatic
    tools=[weather_tool],                # tool_choice stays "auto" (default)
    messages=[{"role": "user", "content": "What's the weather in Paris?"}],
)
thinking_block = next(b for b in resp.content if b.type == "thinking")
tool_use       = next(b for b in resp.content if b.type == "tool_use")

# 2) Send result back — MUST replay the thinking block unmodified.
follow = client.messages.create(
    model="claude-opus-4-8", max_tokens=16000,
    thinking={"type": "adaptive"}, tools=[weather_tool],
    messages=[
        {"role": "user", "content": "What's the weather in Paris?"},
        {"role": "assistant", "content": [thinking_block, tool_use]},   # verbatim!
        {"role": "user", "content": [{
            "type": "tool_result",
            "tool_use_id": tool_use.id,
            "content": "18°C, clear",
        }]},
    ],
)

TRADE-OFFS Reflective loop vs. plain tool chaining

✅ Thinking + tools (interleaved)

  • Reasons about tool choice and about each result
  • Recovers from surprising / empty results
  • Fewer wasted or redundant tool calls on hard tasks

⚠️ Cost & rules

  • Thinking tokens between every call → more cost/latency
  • Must replay thinking blocks + signatures unmodified
  • tool_choice limited to auto/none

EXAM TRAPS

Trap: Dropping the thinking block when returning a tool result. → Reasoning continuity breaks. Replay the thinking block and its signature unmodified in the assistant turn alongside the tool_use.
Trap: Forcing a specific tool with thinking on. → tool_choice must be auto or none with extended thinking; {type:"tool"} or any isn't supported.
Trap: Expecting interleaving to need a beta header on current models. → It's automatic with adaptive thinking; the older interleaved-thinking beta header is only for legacy models.
Trap: Assuming thinking is free inside the loop. → A thinking block after every result multiplies token cost and latency — worth it for hard multi-step tasks, wasteful for a fixed two-call sequence.

PRACTICE

Q1 · What must the assistant turn contain when you send a tool result back with thinking on?

The original thinking block (with its signature) and the tool_use block, both unmodified, followed by the tool_result in the next user turn. Dropping the thinking block breaks reasoning continuity.

Q2 · What does interleaved thinking add over plain up-front reasoning?

A fresh thinking block between tool calls — the model reflects on each result before choosing the next action, instead of reasoning once and then blindly chaining calls. It's automatic with adaptive thinking on current models.

Q3 · Which tool_choice values are allowed with extended thinking?

Only auto (default) or none. Forcing a specific tool ({type:"tool"}) or any is not supported while extended thinking is enabled.