Claude API Tool Use and Function Calling

How to use Claude's tool use (function calling) API. Define tools in JSON schema, handle tool_use responses, and close the loop in Python and JavaScript.

🔥 Launch tonight — Power Prompts PDF 50p (just 50p tonight)30 battle-tested Claude Code prompts · 8 pages · paste into CLAUDE.md · price reverts to £5

Claude's tool use API (also called function calling) lets Claude decide when to call an external function, construct the correct arguments, and integrate the result into its response. This page walks through the complete request-response loop in Python and TypeScript.

How it works

  1. You define one or more tools with a JSON Schema description.
  2. Claude's response may be tool_use instead of text.
  3. You execute the function locally and return the result in the next messages turn.
  4. Claude reads the result and produces a final text response.

Minimal Python example

import anthropic, json

client = anthropic.Anthropic()

tools = [
    {
        "name": "get_weather",
        "description": "Return current temperature and conditions for a city.",
        "input_schema": {
            "type": "object",
            "properties": {
                "city": {"type": "string", "description": "City name, e.g. London"}
            },
            "required": ["city"]
        }
    }
]

messages = [{"role": "user", "content": "What is the weather in Tokyo?"}]

response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    tools=tools,
    messages=messages
)

if response.stop_reason == "tool_use":
    tool_block = next(b for b in response.content if b.type == "tool_use")
    city = tool_block.input["city"]

    # Call your real function here
    weather_result = {"city": city, "temp_c": 22, "conditions": "Sunny"}

    messages.append({"role": "assistant", "content": response.content})
    messages.append({
        "role": "user",
        "content": [{
            "type": "tool_result",
            "tool_use_id": tool_block.id,
            "content": json.dumps(weather_result)
        }]
    })

    final = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=1024,
        tools=tools,
        messages=messages
    )
    print(final.content[0].text)

TypeScript version

import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic();

const tools: Anthropic.Tool[] = [{
    name: "get_weather",
    description: "Return current temperature and conditions for a city.",
    input_schema: {
        type: "object",
        properties: { city: { type: "string" } },
        required: ["city"]
    }
}];

let messages: Anthropic.MessageParam[] = [
    { role: "user", content: "What is the weather in Tokyo?" }
];

const r1 = await client.messages.create({
    model: "claude-sonnet-4-6", max_tokens: 1024, tools, messages
});

if (r1.stop_reason === "tool_use") {
    const toolBlock = r1.content.find(b => b.type === "tool_use") as Anthropic.ToolUseBlock;
    const weatherResult = { city: toolBlock.input.city, temp_c: 22, conditions: "Sunny" };

    messages = [
        ...messages,
        { role: "assistant", content: r1.content },
        { role: "user", content: [{ type: "tool_result", tool_use_id: toolBlock.id, content: JSON.stringify(weatherResult) }] }
    ];

    const r2 = await client.messages.create({ model: "claude-sonnet-4-6", max_tokens: 1024, tools, messages });
    console.log((r2.content[0] as Anthropic.TextBlock).text);
}

Tool choice modes

tool_choiceBehaviourWhen to use
auto (default)Claude picks whether to call a tool or respond in textMost cases
{"type":"any"}Claude must call at least one toolForcing structured extraction
{"type":"tool","name":"..."}Claude must call this specific toolGuaranteed JSON schema output

Pricing note

Tool definitions and tool_result messages count as input tokens. A complex tool schema with 5 tools adds ~500–1,000 input tokens per request. Use the Claude Cost Calculator to model the incremental cost, and the Prompt-Pricing Recommender to decide whether Haiku or Sonnet is right for your tool-use workflow.

Common mistakes

Frequently asked questions

What is the difference between Claude tool use and function calling?
They are the same feature — 'function calling' is the OpenAI term; Anthropic calls it 'tool use.' Both let the model output a structured JSON payload to invoke an external function, then incorporate the result into its response. The API shape differs slightly but the concept is identical.
Can Claude call multiple tools in one response?
Yes. When stop_reason is tool_use, response.content may contain multiple ToolUseBlock items. Loop over them, execute each function, and return all results as separate tool_result entries in the next messages turn.
How many tools can I define for Claude?
Anthropic supports up to 128 tool definitions per request. In practice, performance degrades when Claude has too many similar tools to choose from — 5–15 well-differentiated tools is the practical sweet spot for reliable routing.
Can I use Claude tool use to guarantee JSON output?
Yes — set tool_choice to {type: 'tool', name: 'your_tool'} and define the tool's input_schema as the JSON structure you want. Claude will always return the schema-conforming JSON as the tool input, even if it doesn't actually 'call' any external function. This is the most reliable way to get structured output from Claude.
Does tool use cost more than regular messages?
Tool definitions and tool_result messages are billed as regular input tokens. A typical tool schema adds 200–500 input tokens per request. This is modest relative to most prompts — but for very high-volume workloads, trimming verbose tool descriptions saves meaningfully.

Free tools

Cost Calculator → Prompt-Pricing Recommender → Diff Summarizer → Skills Browser →

Related

Claude Opus 4.7 vs Sonnet 4.6 Pricing (2026 Comparison)How Much Does Claude Cost? (2026 API Pricing Guide)Claude Prompt Caching: 90% Cost Savings Explained (2026)Claude API Cost Calculator: Estimate Your Anthropic BillClaude vs GPT-4 Pricing: 2026 API Cost Comparison