Resources / AI agents

Claude tool use (function calling) explained, with a working example

How tool use works in the Claude API: defining tools with JSON Schema, the tool_use / tool_result loop, stop_reason, errors and parallel calls — with Python code.

Tool use is Anthropic's name for function calling: you describe functions to Claude, Claude decides when to call one and with which arguments, your code runs it, and you send the result back. Claude never runs your code itself — it only asks.

1. Define a tool

A tool is a name, a description and a JSON Schema for its input:

weather_tool = {
    "name": "get_weather",
    "description": "Current weather for a city. Use when the user asks about weather right now.",
    "input_schema": {
        "type": "object",
        "properties": {
            "city": {"type": "string", "description": "City name, e.g. 'Tel Aviv'"},
            "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
        },
        "required": ["city"],
    },
}

Claude chooses tools by reading the descriptions, so write them for a new colleague: what the tool does, when to use it, and what each field means.

2. The loop

import anthropic, json

client = anthropic.Anthropic()  # reads ANTHROPIC_API_KEY
MODEL = "claude-sonnet-5-5"     # any current Claude model

def get_weather(city, unit="celsius"):
    return {"city": city, "temp": 21, "unit": unit, "conditions": "clear"}

messages = [{"role": "user", "content": "Do I need a jacket in Tel Aviv tonight?"}]

while True:
    response = client.messages.create(
        model=MODEL, max_tokens=1024, tools=[weather_tool], messages=messages
    )
    messages.append({"role": "assistant", "content": response.content})

    if response.stop_reason != "tool_use":
        break  # end_turn: Claude has its final answer

    results = []
    for block in response.content:
        if block.type == "tool_use":
            try:
                output = get_weather(**block.input)
                results.append({"type": "tool_result", "tool_use_id": block.id, "content": json.dumps(output)})
            except Exception as e:
                results.append({"type": "tool_result", "tool_use_id": block.id, "content": str(e), "is_error": True})
    messages.append({"role": "user", "content": results})

print(response.content[-1].text)

What happens:

  1. You send the user's message plus the tools list.
  2. Claude replies with stop_reason: "tool_use" and one or more tool_use blocks, each with an id, the tool name and an input that matches your schema.
  3. You run each tool and send back a user message containing one tool_result block per call, matched by tool_use_id.
  4. Repeat while stop_reason is "tool_use". When it's "end_turn", Claude has finished.

Rules that trip people up

Parallel tool calls

Claude may request several tools at once ("weather in Paris and London"). Run them concurrently if they're independent, then return all results together.

Tool use vs MCP

Tool use is how you give tools to Claude in your own API calls. The Model Context Protocol is how a tool server offers tools to any client — Claude apps, ChatGPT, Cursor and others — without each one writing this loop. Same idea, different packaging: if more than one AI client should use your tools, put them behind an MCP server. The general concept across providers is covered in function calling explained.

On Wisp

Each Wisp endpoint has an input schema, so it can be a Claude tool as is: point your tool's handler at the endpoint URL, or skip the loop entirely and connect the project's built-in MCP server to Claude.

Build it with Wisp — describe an API, get it live.

Related