C Claude

How to implement tool calling on Claude

intermediate 12 min read Updated 2026-03-18
Quick Answer

Tool calling on Claude allows the AI to interact with external functions and APIs to perform specific tasks. You implement it by defining tool schemas in your API requests and handling the tool use responses from Claude.

Prerequisites

  • Basic understanding of API integration
  • Python or JavaScript programming knowledge
  • Claude API access key
  • Familiarity with JSON data structures

Step-by-Step Instructions

1

Set up Claude API client

Install the Anthropic Python SDK using pip install anthropic. Initialize the client with your API key:

import anthropic

client = anthropic.Anthropic(
    api_key="your-api-key-here"
)

Alternatively, set your API key as an environment variable ANTHROPIC_API_KEY.
Store your API key securely using environment variables instead of hardcoding it in your source code.
2

Define your tool functions

Create the actual functions that Claude will call. Each function should have clear parameters and return structured data:

def get_weather(location: str) -> dict:
    # Your weather API logic here
    return {
        "location": location,
        "temperature": "22°C",
        "condition": "sunny"
    }

def calculate_sum(a: float, b: float) -> float:
    return a + b
Keep tool functions focused on single responsibilities and ensure they handle errors gracefully.
3

Create tool schemas

Define tool schemas that describe your functions to Claude using JSON schema format:

tools = [
    {
        "name": "get_weather",
        "description": "Get current weather for a location",
        "input_schema": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "City name or location"
                }
            },
            "required": ["location"]
        }
    }
]
Write detailed descriptions and specify required parameters clearly to help Claude understand when and how to use each tool.
4

Send message with tools

Make an API call to Claude including your tools array and a message:

response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    tools=tools,
    messages=[
        {
            "role": "user",
            "content": "What's the weather like in Paris?"
        }
    ]
)

Claude will analyze your message and determine if any tools should be used.
5

Handle tool use responses

Check if Claude wants to use a tool by examining the response content:

if response.content[0].type == "tool_use":
    tool_use = response.content[0]
    tool_name = tool_use.name
    tool_input = tool_use.input
    tool_use_id = tool_use.id
    
    # Execute the corresponding function
    if tool_name == "get_weather":
        result = get_weather(**tool_input)
    
    print(f"Tool {tool_name} called with: {tool_input}")
    print(f"Result: {result}")
Always validate tool inputs before executing functions to prevent errors or security issues.
6

Send tool results back to Claude

Create a follow-up message containing the tool results so Claude can provide a final response:

follow_up = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "What's the weather like in Paris?"},
        {"role": "assistant", "content": response.content},
        {
            "role": "user",
            "content": [
                {
                    "type": "tool_result",
                    "tool_use_id": tool_use_id,
                    "content": str(result)
                }
            ]
        }
    ],
    tools=tools
)
Ensure the tool_use_id matches exactly with the ID from Claude's tool use request.
7

Implement conversation loop

Create a loop to handle multiple tool calls in a single conversation:

def chat_with_tools(user_message):
    messages = [{"role": "user", "content": user_message}]
    
    while True:
        response = client.messages.create(
            model="claude-3-5-sonnet-20241022",
            max_tokens=1024,
            tools=tools,
            messages=messages
        )
        
        if response.content[0].type == "text":
            return response.content[0].text
            
        # Handle tool use and continue loop
        # ... tool execution logic ...
Set a maximum iteration limit to prevent infinite loops in case of unexpected behavior.
8

Add error handling and logging

Implement comprehensive error handling for API calls and tool execution:

import logging

try:
    response = client.messages.create(
        model="claude-3-5-sonnet-20241022",
        max_tokens=1024,
        tools=tools,
        messages=messages
    )
except anthropic.APIError as e:
    logging.error(f"Claude API error: {e}")
    return "Sorry, I encountered an error."
except Exception as e:
    logging.error(f"Tool execution error: {e}")
    return "Tool execution failed."
Log both successful tool calls and errors for debugging and monitoring purposes.

Common Issues & Troubleshooting

Claude doesn't call the tool when expected

Check your tool description and input schema. Make sure the description clearly explains when to use the tool and that parameter types match exactly. Also verify your user message provides enough context for Claude to understand a tool is needed.

Getting 'tool_use_id not found' errors

Ensure you're passing the exact tool_use_id from Claude's response back in your tool result. The ID must match character-for-character, and you must include it in the same conversation thread.

Tool parameters are incorrect or missing

Validate your JSON schema in the input_schema field. Make sure all required parameters are marked in the required array and parameter types are correctly specified as string, number, boolean, etc.

API rate limiting or timeout errors

Implement exponential backoff retry logic and respect rate limits. Add timeout handling to your tool functions and consider caching results for expensive operations. Use time.sleep() between requests if hitting rate limits.

Prices mentioned in this guide are pulled from current plan data and may change. Always verify on the official Claude website before purchasing.