Notebooks
A
Anthropic
Extended Thinking With Tool Use

Extended Thinking With Tool Use

Extended Thinking with Tool Use

Table of contents

This notebook demonstrates how to use Claude 3.7 Sonnet's extended thinking feature with tools. The extended thinking feature allows you to see Claude's step-by-step thinking before it provides a final answer, providing transparency into how it decides which tools to use and how it interprets tool results.

When using extended thinking with tool use, the model will show its thinking before making tool requests, but not repeat the thinking process after receiving tool results. Claude will not output another thinking block until after the next non-tool_result user turn. For more information on extended thinking, see our documentation.

Setup

First, let's install the necessary packages and set up our environment.

[ ]
[ ]

Single tool calls with thinking

This example demonstrates how to combine thinking and make a single tool call, with a mock weather tool.

[ ]
=== INITIAL RESPONSE ===
Response ID: msg_01NhR4vE9nVh2sHs5fXbzji8
Stop reason: tool_use
Model: claude-sonnet-4-5
Content blocks: 3 blocks

Block 1: Type = thinking
Thinking content: The user is asking about the current weather in Paris. I can use the `weather` function to get this information.

The `weather` function requires a "l...
Signature available: True

Block 2: Type = text
Text content: I'll check the current weather in Paris for you.

Block 3: Type = tool_use
Tool: weather
Tool input: {'location': 'Paris'}
Tool ID: toolu_01WaeSyitUGJFaaPe68cJuEv
=== END INITIAL RESPONSE ===


=== EXECUTING TOOL ===
Tool name: weather
Location to check: Paris
Result: {'temperature': 65, 'condition': 'Rainy'}
=== TOOL EXECUTION COMPLETE ===


=== SENDING FOLLOW-UP REQUEST WITH TOOL RESULT ===
Follow-up response received. Stop reason: end_turn

==== FULL RESPONSE ====

✓ FINAL ANSWER:
Currently in Paris, it's 65°F (18°C) and rainy. You might want to bring an umbrella if you're heading out!

==== END RESPONSE ====

Multiple tool calls with thinking

This example demonstrates how to handle multiple tool calls, such as a mock news and weather service, while observing the thinking process.

[ ]
=== INITIAL RESPONSE ===
Response ID: msg_01VwqpBMARVoTP1H8Ytvmvsb
Stop reason: tool_use
Model: claude-sonnet-4-5
Content blocks: 3 blocks

Block 1: Type = thinking
Thinking content: The user is asking for two pieces of information:
1. The weather in London
2. The latest news about technology

Let me check what tools I have availab...
Signature available: True

Block 2: Type = text
Text content: I'll get that information for you right away.

Block 3: Type = tool_use
Tool: weather
Tool input: {'location': 'London'}
Tool ID: toolu_016xHQWMR4JsKtWvH9nbsZyA
=== END INITIAL RESPONSE ===


=== TOOL USE ITERATION 1 ===

=== EXECUTING TOOL ===
Tool name: weather
Location to check: London
Result: {'temperature': 62, 'condition': 'Cloudy'}
=== TOOL EXECUTION COMPLETE ===


=== SENDING FOLLOW-UP REQUEST WITH TOOL RESULT ===

=== FOLLOW-UP RESPONSE (ITERATION 1) ===
Response ID: msg_01EhR96Z2Z2t5EDhuWeodUod
Stop reason: tool_use
Content blocks: 1 blocks

Block 1: Type = tool_use
Tool: news
Tool input preview: {'topic': 'technology'}
=== END FOLLOW-UP RESPONSE (ITERATION 1) ===


=== TOOL USE ITERATION 2 ===

=== EXECUTING TOOL ===
Tool name: news
Topic to check: technology
Result: {'headlines': ['New AI breakthrough announced by research lab', 'Tech company releases latest smartphone model', 'Quantum computing reaches milestone achievement']}
=== TOOL EXECUTION COMPLETE ===


=== SENDING FOLLOW-UP REQUEST WITH TOOL RESULT ===

=== FOLLOW-UP RESPONSE (ITERATION 2) ===
Response ID: msg_01WUEfC4UxPFaJaktjVDMJEN
Stop reason: end_turn
Content blocks: 1 blocks

Block 1: Type = text
Text content preview: Here's the information you requested:

## Weather in London
Currently, it's 62°F and cloudy in Londo...
=== END FOLLOW-UP RESPONSE (ITERATION 2) ===


=== FINAL RESPONSE ===

==== FULL RESPONSE ====

✓ FINAL ANSWER:
Here's the information you requested:

## Weather in London
Currently, it's 62°F and cloudy in London.

## Latest Technology News Headlines
- New AI breakthrough announced by research lab
- Tech company releases latest smartphone model
- Quantum computing reaches milestone achievement

==== END RESPONSE ====
=== END FINAL RESPONSE ===

Preserving thinking blocks

When working with extended thinking and tools, make sure to:

  1. Preserve thinking block signatures: Each thinking block contains a cryptographic signature that validates the conversation context. These signatures must be included when passing thinking blocks back to Claude.

  2. Avoid modifying prior context: The API will reject requests if any previous content (including thinking blocks) is modified when submitting a new request with tool results.

  3. Handle both thinking and redacted_thinking blocks: Both types of blocks must be preserved in the conversation history, even if the content of redacted blocks is not human readable.

For more details on extended thinking without tools, see the main "Extended Thinking" notebook.

[ ]
=== INITIAL RESPONSE ===
Response contains:
- 1 thinking blocks
- 1 tool use blocks

Tool called: weather
Location to check: Berlin
Tool result: {'temperature': 60, 'condition': 'Foggy'}

=== TEST 1: WITHOUT thinking block ===
ERROR: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'messages.1.content.0.type: Expected `thinking` or `redacted_thinking`, but found `tool_use`. When `thinking` is enabled, a final `assistant` message must start with a thinking block (preceeding the lastmost set of `tool_use` and `tool_result` blocks). We recommend you include thinking blocks from previous turns. To avoid this requirement, disable `thinking`. Please consult our documentation at https://docs.claude.com/en/docs/build-with-claude/extended-thinking'}}
This demonstrates that thinking blocks must be preserved

=== TEST 2: WITH thinking block (correct approach) ===
SUCCESS: Response received with thinking blocks included

Second response contains:
- 0 thinking blocks
- 1 text blocks

Final answer: Currently in Berlin, it's foggy with a temperature of 60°F (about 15.5°C).

Note: The second response after tool use doesn't contain thinking blocks.
This is expected behavior - thinking is shown before tool use but not after receiving tool results.

Conclusion

This notebook shows how to combine Claude's extended thinking feature with tool use. Key benefits include:

  1. Transparency into Claude's thinking process when using tools
  2. Visibility into how Claude decides when to use tools versus internal knowledge
  3. Better understanding of multi-step tasks that involve multiple tool calls
  4. Insight into how Claude interprets tool results and incorporates them into responses

When using extended thinking with tools, keep in mind:

  • Set appropriate thinking budgets for complex tasks (minimum 1,024 tokens)
  • Always preserve thinking blocks and their signatures when passing tool results
  • Include both normal and redacted thinking blocks in the conversation history
  • Ensure that system prompts, tools, and thinking configurations match between calls
  • Expect that tool result turns will not contain additional thinking blocks
  • Tool use and thinking together can increase token usage and response time