Example: Adding a Calculator to Our Agent › Adding Multiple Tools

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 › Adding Multiple Tools
Reading section 11 of 16 · Adding Multiple Tools · 2 minSection 11 of 16 · 2 min

Book contents

Section 11 of 16 · 2 min read

Example: Adding a Calculator to Our Agent › Adding Multiple ToolsOne section · Sections in this chapter11 / 16 ↓

Now that you have a calculator working, adding more tools follows the same pattern. Let's add a simple unit converter:

In[16]:
Code
def convert_temperature(value: float, from_unit: str, to_unit: str) -> dict:
    """Convert temperature between Celsius, Fahrenheit, and Kelvin."""
    try:
        # Convert to Celsius first
        if from_unit.lower() == "fahrenheit":
            celsius = (value - 32) * 5/9
        elif from_unit.lower() == "kelvin":
            celsius = value - 273.15
        else:  # Already Celsius
            celsius = value
        
        # Convert from Celsius to target
        if to_unit.lower() == "fahrenheit":
            result = celsius * 9/5 + 32
        elif to_unit.lower() == "kelvin":
            result = celsius + 273.15
        else:  # Stay in Celsius
            result = celsius
        
        return {
            "success": True,
            "result": round(result, 2),
            "from_value": value,
            "from_unit": from_unit,
            "to_unit": to_unit
        }
    except Exception as e:
        return {
            "success": False,
            "error": str(e)
        }

## Tool description
temperature_tool = {
    "name": "convert_temperature",
    "description": "Convert temperature between Celsius, Fahrenheit, and Kelvin",
    "input_schema": {
        "type": "object",
        "properties": {
            "value": {
                "type": "number",
                "description": "The temperature value to convert"
            },
            "from_unit": {
                "type": "string",
                "description": "The unit to convert from: 'celsius', 'fahrenheit', or 'kelvin'"
            },
            "to_unit": {
                "type": "string",
                "description": "The unit to convert to: 'celsius', 'fahrenheit', or 'kelvin'"
            }
        },
        "required": ["value", "from_unit", "to_unit"]
    }
}

## Update the agent to include both tools
def run_agent_with_tools(user_message: str) -> str:
    """Run agent with calculator and temperature converter."""
    tool_registry = {
        "calculate": calculate,
        "convert_temperature": convert_temperature
    }
    
    messages = [{"role": "user", "content": user_message}]
    
    response = client.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=1024,
        tools=[calculator_tool, temperature_tool],
        messages=messages
    )
    
    # Same tool use logic as before...
    # (Full implementation omitted for brevity)

Now your agent can handle both calculations and temperature conversions:

In[17]:
Code
print(run_agent_with_tools("What's 100 Celsius in Fahrenheit?"))
print(run_agent_with_tools("If I buy 15 items at $\$12.99$ each, what's my total?"))
Out[17]:
Console
None
None
100 Celsius is 212 Fahrenheit.
Your total would be $194.85.

The agent automatically chooses the right tool for each question.