# Implementing Extended Thinking with Tool Use in Claude: A Production-Ready Guide

> Unlock Claude's extended thinking with tool use. This guide shows how to implement internal reasoning between tool calls for production-ready applications. Preserve and forward thinking blocks.

- Repository: [Anthropic/claude-cookbooks](https://github.com/anthropics/claude-cookbooks)
- Tags: how-to-guide
- Published: 2026-04-14

---

**Claude's extended thinking beta allows the model to consume additional tokens for internal reasoning between tool calls, provided you preserve and forward the thinking blocks (including their signatures) back to the API in subsequent conversation turns.**

Anthropic's extended thinking capability enables Claude to perform complex, multi-step reasoning before executing tools. The `anthropic/claude-cookbooks` repository provides production-ready reference implementations demonstrating how to integrate this feature with client-side tool execution, ensuring the model maintains its internal chain of thought across conversation turns.

## How Extended Thinking Works with Tool Use

When **extended thinking** is enabled, Claude allocates a separate token budget for internal computation—planning, reasoning, and evaluating approaches—before generating visible text or tool calls. According to the source code in [`tool_use/memory_demo/demo_helpers.py`](https://github.com/anthropics/claude-cookbooks/blob/main/tool_use/memory_demo/demo_helpers.py), the critical requirement is that thinking blocks must be captured from the API response and re-injected into the next request's message history.

The architecture centers on three components:

- **`run_conversation_turn`** (lines 31-78 in [`demo_helpers.py`](https://github.com/anthropics/claude-cookbooks/blob/main/demo_helpers.py)): Orchestrates a single API call, optionally injecting the `thinking` parameter and parsing the response for content blocks.
- **Thinking block preservation**: When the response contains `type: "thinking"` content, the code constructs a dictionary with the thinking text and optional signature, appending it to the assistant's message content.
- **`run_conversation_loop`** (lines 122-180): Manages the multi-turn cycle, forwarding preserved thinking blocks while executing tool calls via `MemoryToolHandler`.

## Constructing Requests with Extended Thinking

To enable extended thinking, include a `thinking` dictionary in your request parameters. The implementation in [`demo_helpers.py`](https://github.com/anthropics/claude-cookbooks/blob/main/demo_helpers.py) shows this conditional injection:

```python
request_params = {
    "model": model,
    "max_tokens": max_tokens,
    "system": system,
    "messages": messages,
    "tools": [memory_tool],
    "betas": ["context-management-2025-06-27"],
}
if thinking:
    request_params["thinking"] = thinking

```

The `thinking` parameter expects a configuration object specifying the budget:

```python
thinking_cfg = {"type": "enabled", "budget_tokens": 10_000}

```

This allocates 10,000 tokens for Claude's internal reasoning chain, separate from the output token limit.

## Preserving Thinking Blocks Across Turns

When Claude uses extended thinking with tools, the API returns a thinking block that must be preserved in the conversation history. The parsing logic in [`demo_helpers.py`](https://github.com/anthropics/claude-cookbooks/blob/main/demo_helpers.py) (lines 82-95) handles this extraction:

```python
for content in response.content:
    if content.type == "thinking":
        # Preserve the block for the next turn

        thinking_block = {"type": "thinking", "thinking": content.thinking}
        if hasattr(content, "signature") and content.signature:
            thinking_block["signature"] = content.signature
        assistant_content.append(thinking_block)

```

The `signature` field is cryptographically bound to the thinking content. If you omit it when forwarding the block back to the API, Claude will reject the payload with a validation error.

## Integrating with the Memory Tool

The reference implementation combines extended thinking with a **memory tool** that performs filesystem operations. The `MemoryToolHandler` class in [`tool_use/memory_tool.py`](https://github.com/anthropics/claude-cookbooks/blob/main/tool_use/memory_tool.py) (lines 76-118) executes commands like `view`, `create`, and `edit` based on Claude's tool calls.

During the conversation loop, Claude might emit thinking followed by a tool use request:

```json
{
  "type": "thinking",
  "thinking": "I need to view the notes file to answer the user. The file is at /memories/project/notes.txt."
}

```

The helper code forwards this block (with signature) back to Claude on the next turn, executes the `view` command via `MemoryToolHandler._view`, and returns the result, allowing Claude to continue its reasoning with the new information.

## Complete Implementation Example

Below is a runnable example using the utilities from [`demo_helpers.py`](https://github.com/anthropics/claude-cookbooks/blob/main/demo_helpers.py):

```python
from anthropic import Anthropic
from tool_use.memory_demo.demo_helpers import run_conversation_loop
from tool_use.memory_tool import MemoryToolHandler

# Initialize client

client = Anthropic()

# System prompt encouraging planning

system_prompt = """
You are an assistant that plans before acting.
If a tool call is needed, think first and include a thinking block.
"""

# Enable extended thinking

thinking_cfg = {"type": "enabled", "budget_tokens": 10_000}

# Initial user message

messages = [{"role": "user", "content": "Summarize the contents of /memories/project/notes.txt"}]

# Create memory handler (stores files under ./memory_storage)

memory = MemoryToolHandler()

# Run the conversation loop

response = run_conversation_loop(
    client=client,
    model="claude-3-sonnet-20240229",
    messages=messages,
    memory_handler=memory,
    system=system_prompt,
    thinking=thinking_cfg,
    max_turns=5,
    verbose=True,
)

print("\n=== Final Claude response ===")
print(response)

```

This implementation:
- Injects `thinking_cfg` into each API request via `run_conversation_loop`
- Automatically extracts and preserves thinking blocks from responses
- Handles tool execution through `MemoryToolHandler.execute`
- Continues the loop until Claude stops requesting tools or hits `max_turns`

## Context Management Considerations

The reference implementation also supports the `context-management-2025-06-27` beta (referenced in lines 183-200 of [`demo_helpers.py`](https://github.com/anthropics/claude-cookbooks/blob/main/demo_helpers.py)). When enabled, the `print_context_management_info` function displays notices about context edits applied during the conversation, which is useful for long-running sessions where extended thinking consumes significant context window space.

## Summary

- **Extended thinking** requires forwarding thinking blocks (with signatures) back to the API in subsequent turns to maintain Claude's reasoning chain.
- The **`run_conversation_loop`** function in [`demo_helpers.py`](https://github.com/anthropics/claude-cookbooks/blob/main/demo_helpers.py) handles the complete lifecycle: injecting thinking configurations, parsing response blocks, and managing tool execution.
- **Signatures** on thinking blocks are mandatory for continued use; omitting them causes API validation failures.
- The **memory tool** implementation demonstrates secure client-side execution while preserving the model's internal plan across multiple turns.

## Frequently Asked Questions

### What is extended thinking in Claude?

Extended thinking is a beta feature that allocates a separate token budget for Claude's internal reasoning before generating responses or tool calls. When enabled via `{"type": "enabled", "budget_tokens": N}`, the model can perform complex planning and evaluation steps that remain invisible to the end user but are captured in `thinking` blocks within the API response.

### Why must thinking blocks include signatures?

The `signature` field provides cryptographic verification of the thinking content's integrity. According to the implementation in [`demo_helpers.py`](https://github.com/anthropics/claude-cookbooks/blob/main/demo_helpers.py), this signature must be preserved and sent back to the API in subsequent messages. Without it, Claude cannot verify that the thinking content hasn't been tampered with, and the API will reject requests missing this field.

### How does the conversation loop handle multiple tool calls?

The `run_conversation_loop` function (lines 122-180) aggregates all content blocks from the assistant—including thinking blocks and tool use requests—into the `assistant_content` list. After executing tools via `execute_tool`, it appends both the assistant's message and the tool results to the shared `messages` list, then initiates the next turn. This continues until the response contains no tool calls or the `max_turns` limit is reached.

### Can extended thinking work with custom tools beyond the memory tool?

Yes. While the `claude-cookbooks` repository demonstrates extended thinking with the `memory` tool implemented in [`memory_tool.py`](https://github.com/anthropics/claude-cookbooks/blob/main/memory_tool.py), the pattern in [`demo_helpers.py`](https://github.com/anthropics/claude-cookbooks/blob/main/demo_helpers.py) is generic. Any tool definition can be passed in the `tools` array, and the thinking block handling remains identical regardless of which specific tools Claude invokes during its reasoning process.