Claude Tool Use and Function Calling: The Developer’s Guide

Claude tool use (also called function calling) is the capability that transforms Claude from a conversational AI into an agentic system that can interact with external services, execute code, query databases, and take real-world actions. This guide covers how tool use works, the three execution modes, the built-in server tools, and practical implementation examples.

What Is Tool Use?

Tool use lets you define functions that Claude can call during a conversation. When Claude determines that a tool would help answer a user’s request, it generates a tool call (specifying the tool name and arguments), your code executes the function, and the result is returned to Claude to continue the conversation.

Example flow: User asks “What’s the weather in Seattle?” → Claude calls your get_weather function with {"location": "Seattle"} → Your code calls a weather API → Returns data to Claude → Claude generates a natural language response incorporating the weather data.

Defining Tools

tools = [
    {
        "name": "get_stock_price",
        "description": "Get the current stock price for a given ticker symbol",
        "input_schema": {
            "type": "object",
            "properties": {
                "ticker": {
                    "type": "string",
                    "description": "The stock ticker symbol (e.g., AAPL, GOOGL)"
                }
            },
            "required": ["ticker"]
        }
    }
]

response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    tools=tools,
    messages=[{"role": "user", "content": "What's Apple's current stock price?"}]
)

The Three Execution Modes

1. Client-Side Execution

Your application receives the tool call, executes the function locally or via external APIs, and returns the result. This is the standard pattern — you control the execution environment and can call any service.

2. Server-Side Execution (Built-in Tools)

Anthropic provides built-in tools that Claude can execute server-side without your code doing anything:

  • web_search: Real-time web search
  • code_execution: Execute Python code in a sandbox
  • bash: Run shell commands
  • text_editor: Read and edit files (used in Claude Code)

3. Tool Runner SDK (Programmatic)

Anthropic’s Tool Runner SDK automates the tool call/execute/return loop, letting you build agentic workflows without writing the orchestration loop manually.

Handling Tool Results

# After receiving a tool_use block from Claude
if response.stop_reason == "tool_use":
    tool_use = next(block for block in response.content if block.type == "tool_use")
    tool_name = tool_use.name
    tool_input = tool_use.input
    
    # Execute your function
    result = your_function(tool_input)
    
    # Return result to Claude
    follow_up = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        tools=tools,
        messages=[
            {"role": "user", "content": "What's Apple's stock price?"},
            {"role": "assistant", "content": response.content},
            {"role": "user", "content": [{"type": "tool_result", "tool_use_id": tool_use.id, "content": str(result)}]}
        ]
    )

Frequently Asked Questions

What is the difference between tool use and function calling?

They’re the same thing — Anthropic uses “tool use” as the preferred term, while “function calling” is the term OpenAI popularized. Both describe the same capability: letting an AI model invoke defined functions during a conversation.

How many tools can I define for Claude?

Claude supports up to several hundred tools in a single request, though performance is best with a focused set relevant to the task. Each tool definition consumes input tokens, so large tool sets have a cost impact.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *