📚Academy
likeone
online

Tool Use Basics

Give Claude superpowers by connecting it to external tools — with complete working code

What Is Tool Use?

Claude can process text and images, and can search the web. With tool use (also called function calling), Claude can do much more — query databases, call APIs, run code, send emails, and interact with any system you connect. You define the tools. Claude decides when and how to use them.

Think of tools as giving Claude hands to go with its brain. Without tools, Claude can only tell you what it would do. With tools, it can actually do it.

Key insight: Claude does not execute tools itself. It outputs a structured request saying "I want to call this tool with these parameters." YOUR code executes the actual function and returns the result to Claude. This separation is a security feature — you always control what actions are taken.

The Tool Use Flow

Every tool use interaction follows the same 5-step pattern:

1
User sends a message"What's the weather in Tokyo?"
Waiting
2
Claude decides to use a toolRecognizes it needs real-time weather data
Waiting
3
Claude outputs a structured tool callget_weather(location: "Tokyo", units: "celsius")
Waiting
4
Your app executes the tool and returns the result{"temp": 22, "condition": "Partly Cloudy", "humidity": 65}
Waiting
5
Claude responds with natural language"It's currently 22C and partly cloudy in Tokyo with 65% humidity."
Waiting

Complete Working Example

Here is a fully functional tool use implementation. This code actually works — you can run it with your API key:

Python — complete tool use implementation
import anthropic, json

client = anthropic.Anthropic()

# Step 1: Define your tools
tools = [
    {
        "name": "get_weather",
        "description": "Get current weather for a location.",
        "input_schema": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "City name, e.g. Tokyo"
                },
                "units": {
                    "type": "string",
                    "enum": ["celsius", "fahrenheit"],
                    "description": "Temperature units"
                }
            },
            "required": ["location"]
        }
    }
]

# Step 2: Your tool implementation (replace with real API)
def execute_tool(name: str, params: dict) -> str:
    if name == "get_weather":
        # In production: call a real weather API here
        return json.dumps({
            "temp": 22,
            "condition": "Partly Cloudy",
            "humidity": 65
        })
    raise ValueError(f"Unknown tool: {name}")

# Step 3: Send message with tools
response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    tools=tools,
    messages=[{"role": "user", "content": "What's the weather in Tokyo?"}]
)

# Step 4: Check if Claude wants to use a tool
if response.stop_reason == "tool_use":
    # Find the tool_use block
    tool_block = next(
        b for b in response.content
        if b.type == "tool_use"
    )
    print(f"Claude wants to call: {tool_block.name}")
    print(f"With params: {tool_block.input}")

    # Execute the tool
    result = execute_tool(tool_block.name, tool_block.input)

    # Step 5: Send result back to Claude
    final = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        tools=tools,
        messages=[
            {"role": "user", "content": "What's the weather in Tokyo?"},
            {"role": "assistant", "content": response.content},
            {"role": "user", "content": [{
                "type": "tool_result",
                "tool_use_id": tool_block.id,
                "content": result
            }]}
        ]
    )
    print(final.content[0].text)
    # "It's currently 22C and partly cloudy in Tokyo
    #  with 65% humidity."
🔒

This lesson is for Pro members

Unlock all 520+ lessons across 52 courses with Academy Pro.

Already a member? Sign in to access your lessons.

Academy
Built with soul — likeone.ai