Tool Use (Function Calling)

Summary — Key Takeaways from This Lesson

  • Tool Use is the mechanism that allows Claude to call user-defined external functions. The fundamental division of responsibility is: Claude decides, your app executes.
  • Tools are defined using JSON Schema. Three fields are required: name, description, and input_schema.
  • When Claude returns a tool_use response, your app executes the function and sends the result back as a tool_result. This back-and-forth is called the agentic loop.
  • Server-side tools provided by Anthropic (such as web_search) are executed on Anthropic's side, so no agentic loop implementation is required.
  • The code examples here are based on the official documentation as the primary source.
目次 (6)

How Tool Use Works

Claude is a language model specialized in text generation. However, with Tool Use, Claude can "request" real-time data retrieval, computation, and operations on external services. The actual processing is handled by your app, while Claude's role is to decide what to call and when source.

Defining Tools (JSON Schema)

Tools are passed as an array in the tools parameter. The input_schema for each tool is written in JSON Schema format source.

# Example tool definition (Python)
tools = [
    {
        "name": "get_weather",
        "description": "指定した都市の現在の天気を取得します。",
        "input_schema": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "都市名。例: Tokyo, Japan"
                },
                "unit": {
                    "type": "string",
                    "enum": ["celsius", "fahrenheit"],
                    "description": "温度単位"
                }
            },
            "required": ["location"]
        }
    }
]

Python — Minimal Code Example

The following is based on the code example from the official documentation source.

import anthropic

client = anthropic.Anthropic()

response = client.messages.create(
    model="claude-opus-4-7",
    max_tokens=1024,
    tools=tools,  # 上記で定義したツール
    messages=[{"role": "user", "content": "東京の天気を教えてください"}]
)

# stop_reason が "tool_use" のとき Claude がツール呼び出しを要求している
print(response.stop_reason)   # => "tool_use"
print(response.content)       # => tool_use ブロックを含む配列

The Agentic Loop Flow

When using Tool Use, the basic sequence of interactions is as follows.

  1. Your app sends a request to Claude along with the tool definitions
  2. Claude returns stop_reason: "tool_use" along with a tool_use block
  3. Your app reads the name and input from the tool_use block and executes the corresponding function
  4. The execution result is added to the message array as a tool_result and sent to the API again
  5. Claude generates and returns the final answer text

For detailed implementation steps, refer to the official documentation.

Controlling Tool Use with tool_choice

The tool_choice parameter lets you control how Claude selects tools.

  • "auto" (default) — Claude decides whether to use a tool
  • "any" — Claude must use one of the provided tools
  • "tool" + "name" — Claude must use the specified tool
  • "none" — Claude does not use any tools

Tips for Designing Tools

The description field is the primary basis Claude uses to decide when to use a tool. Writing 3–4 or more sentences clearly explaining "what it does," "when to use it," and "what it returns" significantly improves accuracy source.

参考になったら ♡
Clauder Navi 編集部
@clauder_navi

Anthropic の Claude / Claude Code を中心に、日本のエンジニア向けに最新動向と実務 を毎日発信。 運営方針 は メディアについて をご覧ください。