Example: Adding a Calculator to Our Agent › Step 3: Build the Agent with Tool Support (Claude Sonnet 4.5)

Michael BrenndoerferJune 29, 20252 min read
AI Agent HandbookAll chapters
Part 5 of 15Tool Use - Extending the Agent's Abilities
Chapter 14Example: Adding a Calculator to Our Agent › Step 3: Build the Agent with Tool Support (Claude Sonnet 4.5)
Reading section 5 of 16 · Step 3: Build the Agent with Tool Support (Claude Sonnet 4.5) · 2 minSection 5 of 16 · 2 min

Book contents

Section 5 of 16 · 2 min read

Example: Adding a Calculator to Our Agent › Step 3: Build the Agent with Tool Support (Claude Sonnet 4.5)One section · Sections in this chapter5 / 16 ↓

Now we'll create an agent that can use our calculator. We'll use Claude Sonnet 4.5 because of its superior tool use and agent reasoning capabilities:

In[6]:
Code
import os
from anthropic import Anthropic

## Using Claude Sonnet 4.5 for its excellent tool use and agent reasoning
client = Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))

def run_agent_with_calculator(user_message: str) -> str:
    """
    Run the agent with calculator tool support.
    
    Args:
        user_message: The user's question or request
    
    Returns:
        The agent's response
    """
    # Tool registry maps tool names to functions
    tool_registry = {
        "calculate": calculate
    }
    
    # Start the conversation
    messages = [{"role": "user", "content": user_message}]
    
    # Initial agent call with tool description
    response = client.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=1024,
        tools=[calculator_tool],
        messages=messages
    )
    
    # Check if the agent wants to use a tool
    if response.stop_reason == "tool_use":
        # Extract tool use information
        tool_use_block = next(
            block for block in response.content 
            if block.type == "tool_use"
        )
        
        tool_name = tool_use_block.name
        tool_input = tool_use_block.input
        
        print(f"Agent is using tool: {tool_name}")
        print(f"Tool input: {tool_input}")
        
        # Call the tool
        tool_result = tool_registry[tool_name](**tool_input)
        print(f"Tool result: {tool_result}")
        
        # Send the tool result back to the agent
        messages.append({"role": "assistant", "content": response.content})
        messages.append({
            "role": "user",
            "content": [{
                "type": "tool_result",
                "tool_use_id": tool_use_block.id,
                "content": str(tool_result)
            }]
        })
        
        # Get the agent's final response
        final_response = client.messages.create(
            model="claude-sonnet-4-5",
            max_tokens=1024,
            tools=[calculator_tool],
            messages=messages
        )
        
        return final_response.content[0].text
    
    # No tool needed, return direct response
    return response.content[0].text

This function handles the complete tool use workflow:

  1. Sends the user's message to Claude with the calculator tool description
  2. Checks if Claude wants to use the calculator
  3. If yes, calls the calculator function with the extracted parameters
  4. Sends the result back to Claude
  5. Returns Claude's final response to the user

The print statements let us see what's happening under the hood. In production, you'd use proper logging instead.