Claudeはツールや関数と相互作用する能力があり、Claudeの機能を拡張してより幅広いタスクを実行できます。
新しいコースの一部として、Claudeでのツール使用をマスターするために必要なすべてを学びましょう!このフォームを使用してアイデアや提案を共有し続けてください。
Messages APIを使用してClaudeにツールを提供する方法の例を以下に示します:
curl https://api.anthropic.com/v1/messages \
  -H "content-type: application/json" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "claude-sonnet-4-5",
    "max_tokens": 1024,
    "tools": [
      {
        "name": "get_weather",
        "description": "Get the current weather in a given location",
        "input_schema": {
          "type": "object",
          "properties": {
            "location": {
              "type": "string",
              "description": "The city and state, e.g. San Francisco, CA"
            }
          },
          "required": ["location"]
        }
      }
    ],
    "messages": [
      {
        "role": "user",
        "content": "What is the weather like in San Francisco?"
      }
    ]
  }'

ツール使用の仕組み

Claudeは2種類のツールをサポートしています:
  1. クライアントツール:あなたのシステム上で実行されるツールで、以下が含まれます:
  2. サーバーツールウェブ検索ウェブフェッチツールなど、Anthropicのサーバー上で実行されるツール。これらのツールはAPIリクエストで指定する必要がありますが、あなたの側での実装は不要です。
Anthropicが定義したツールは、モデルバージョン間での互換性を確保するためにバージョン付きタイプ(例:web_search_20250305text_editor_20250124)を使用します。

クライアントツール

以下の手順でクライアントツールをClaudeと統合します:
1

Claudeにツールとユーザープロンプトを提供

  • APIリクエストで名前、説明、入力スキーマを含むクライアントツールを定義します。
  • これらのツールが必要になる可能性のあるユーザープロンプトを含めます(例:「サンフランシスコの天気はどうですか?」)
2

Claudeがツールの使用を決定

  • Claudeはユーザーのクエリに対してツールが役立つかどうかを評価します。
  • 役立つ場合、Claudeは適切にフォーマットされたツール使用リクエストを構築します。
  • クライアントツールの場合、APIレスポンスのstop_reasontool_useになり、Claudeの意図を示します。
3

ツールを実行して結果を返す

  • Claudeのリクエストからツール名と入力を抽出
  • あなたのシステム上でツールコードを実行
  • tool_resultコンテンツブロックを含む新しいuserメッセージで結果を返す
4

Claudeがツール結果を使用してレスポンスを作成

  • Claudeはツール結果を分析して、元のユーザープロンプトに対する最終的なレスポンスを作成します。
注意:手順3と4はオプションです。一部のワークフローでは、Claudeのツール使用リクエスト(手順2)だけで十分で、結果をClaudeに送り返す必要がない場合があります。

サーバーツール

サーバーツールは異なるワークフローに従います:
1

Claudeにツールとユーザープロンプトを提供

  • ウェブ検索ウェブフェッチなどのサーバーツールには、独自のパラメータがあります。
  • これらのツールが必要になる可能性のあるユーザープロンプトを含めます(例:「AIに関する最新ニュースを検索して」や「このURLのコンテンツを分析して」)
2

Claudeがサーバーツールを実行

  • Claudeはサーバーツールがユーザーのクエリに役立つかどうかを評価します。
  • 役立つ場合、Claudeはツールを実行し、結果が自動的にClaudeのレスポンスに組み込まれます。
3

Claudeがサーバーツール結果を使用してレスポンスを作成

  • Claudeはサーバーツール結果を分析して、元のユーザープロンプトに対する最終的なレスポンスを作成します。
  • サーバーツールの実行には追加のユーザーインタラクションは必要ありません。

ツール使用の例

様々なツール使用パターンと技術を実演するコード例をいくつか紹介します。簡潔にするため、ツールはシンプルなツールで、ツールの説明は最高のパフォーマンスを確保するために理想的な長さよりも短くしています。
curl https://api.anthropic.com/v1/messages \
     --header "x-api-key: $ANTHROPIC_API_KEY" \
     --header "anthropic-version: 2023-06-01" \
     --header "content-type: application/json" \
     --data \
'{
    "model": "claude-sonnet-4-5",
    "max_tokens": 1024,
    "tools": [{
        "name": "get_weather",
        "description": "Get the current weather in a given location",
        "input_schema": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "The city and state, e.g. San Francisco, CA"
                },
                "unit": {
                    "type": "string",
                    "enum": ["celsius", "fahrenheit"],
                    "description": "The unit of temperature, either \"celsius\" or \"fahrenheit\""
                }
            },
            "required": ["location"]
        }
    }],
    "messages": [{"role": "user", "content": "What is the weather like in San Francisco?"}]
}'
Claudeは以下のようなレスポンスを返します:
JSON
{
  "id": "msg_01Aq9w938a90dw8q",
  "model": "claude-sonnet-4-5",
  "stop_reason": "tool_use",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "I'll check the current weather in San Francisco for you."
    },
    {
      "type": "tool_use",
      "id": "toolu_01A09q90qw90lq917835lq9",
      "name": "get_weather",
      "input": {"location": "San Francisco, CA", "unit": "celsius"}
    }
  ]
}
その後、提供された入力でget_weather関数を実行し、新しいuserメッセージで結果を返す必要があります:
curl https://api.anthropic.com/v1/messages \
     --header "x-api-key: $ANTHROPIC_API_KEY" \
     --header "anthropic-version: 2023-06-01" \
     --header "content-type: application/json" \
     --data \
'{
    "model": "claude-sonnet-4-5",
    "max_tokens": 1024,
    "tools": [
        {
            "name": "get_weather",
            "description": "Get the current weather in a given location",
            "input_schema": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city and state, e.g. San Francisco, CA"
                    },
                    "unit": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"],
                        "description": "The unit of temperature, either \"celsius\" or \"fahrenheit\""
                    }
                },
                "required": ["location"]
            }
        }
    ],
    "messages": [
        {
            "role": "user",
            "content": "What is the weather like in San Francisco?"
        },
        {
            "role": "assistant",
            "content": [
                {
                    "type": "text",
                    "text": "I'll check the current weather in San Francisco for you."
                },
                {
                    "type": "tool_use",
                    "id": "toolu_01A09q90qw90lq917835lq9",
                    "name": "get_weather",
                    "input": {
                        "location": "San Francisco, CA",
                        "unit": "celsius"
                    }
                }
            ]
        },
        {
            "role": "user",
            "content": [
                {
                    "type": "tool_result",
                    "tool_use_id": "toolu_01A09q90qw90lq917835lq9",
                    "content": "15 degrees"
                }
            ]
        }
    ]
}'
これにより、天気データを組み込んだClaudeの最終レスポンスが出力されます:
JSON
{
  "id": "msg_01Aq9w938a90dw8q",
  "model": "claude-sonnet-4-5",
  "stop_reason": "stop_sequence",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "The current weather in San Francisco is 15 degrees Celsius (59 degrees Fahrenheit). It's a cool day in the city by the bay!"
    }
  ]
}
Claudeは単一のレスポンス内で複数のツールを並列で呼び出すことができ、これは複数の独立した操作を必要とするタスクに有用です。並列ツールを使用する場合、すべてのtool_useブロックが単一のassistantメッセージに含まれ、対応するすべてのtool_resultブロックが後続のuserメッセージで提供される必要があります。
重要:APIエラーを避け、Claudeが並列ツールを継続して使用できるよう、ツール結果は正しくフォーマットされている必要があります。詳細なフォーマット要件と完全なコード例については、実装ガイドをご覧ください。
並列ツール呼び出しの実装に関する包括的な例、テストスクリプト、ベストプラクティスについては、実装ガイドの並列ツール使用セクションをご覧ください。
単一のリクエストでClaudeに選択できる複数のツールを提供できます。以下はget_weatherget_timeツールの両方を含む例で、両方を求めるユーザークエリも含まれています。
curl https://api.anthropic.com/v1/messages \
     --header "x-api-key: $ANTHROPIC_API_KEY" \
     --header "anthropic-version: 2023-06-01" \
     --header "content-type: application/json" \
     --data \
'{
    "model": "claude-sonnet-4-5",
    "max_tokens": 1024,
    "tools": [{
        "name": "get_weather",
        "description": "Get the current weather in a given location",
        "input_schema": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "The city and state, e.g. San Francisco, CA"
                },
                "unit": {
                    "type": "string",
                    "enum": ["celsius", "fahrenheit"],
                    "description": "The unit of temperature, either 'celsius' or 'fahrenheit'"
                }
            },
            "required": ["location"]
        }
    },
    {
        "name": "get_time",
        "description": "Get the current time in a given time zone",
        "input_schema": {
            "type": "object",
            "properties": {
                "timezone": {
                    "type": "string",
                    "description": "The IANA time zone name, e.g. America/Los_Angeles"
                }
            },
            "required": ["timezone"]
        }
    }],
    "messages": [{
        "role": "user",
        "content": "What is the weather like right now in New York? Also what time is it there?"
    }]
}'
この場合、Claudeは以下のいずれかを行う可能性があります:
  • ツールを順次使用(一度に一つ)— 最初にget_weatherを呼び出し、天気の結果を受け取った後にget_timeを呼び出す
  • 並列ツール呼び出しを使用 — 操作が独立している場合、単一のレスポンスで複数のtool_useブロックを出力
Claudeが並列ツール呼び出しを行う場合、すべてのツール結果を単一のuserメッセージで返す必要があり、各結果は独自のtool_resultブロックに含める必要があります。
ユーザーのプロンプトにツールの必須パラメータをすべて埋めるのに十分な情報が含まれていない場合、Claude Opusはパラメータが不足していることを認識し、それを求める可能性が高くなります。Claude Sonnetも、特にツールリクエストを出力する前に考えるよう促された場合は求める可能性があります。しかし、合理的な値を推測しようとする場合もあります。例えば、上記のget_weatherツールを使用して、場所を指定せずに「天気はどうですか?」とClaudeに尋ねた場合、特にClaude Sonnetは、ツール入力について推測を行う可能性があります:
JSON
{
  "type": "tool_use",
  "id": "toolu_01A09q90qw90lq917835lq9",
  "name": "get_weather",
  "input": {"location": "New York, NY", "unit": "fahrenheit"}
}
この動作は保証されておらず、特により曖昧なプロンプトや知能の低いモデルの場合はそうです。Claude Opusが必須パラメータを埋めるのに十分なコンテキストを持っていない場合、ツール呼び出しを行う代わりに明確化の質問で応答する可能性がはるかに高くなります。
一部のタスクでは、あるツールの出力を別のツールの入力として使用して、複数のツールを順次呼び出す必要がある場合があります。そのような場合、Claudeは一度に一つのツールを呼び出します。すべてのツールを一度に呼び出すよう促された場合、Claudeは上流のツールのツール結果に依存している下流のツールのパラメータを推測する可能性があります。以下は、get_locationツールを使用してユーザーの場所を取得し、その場所をget_weatherツールに渡す例です:
curl https://api.anthropic.com/v1/messages \
     --header "x-api-key: $ANTHROPIC_API_KEY" \
     --header "anthropic-version: 2023-06-01" \
     --header "content-type: application/json" \
     --data \
'{
    "model": "claude-sonnet-4-5",
    "max_tokens": 1024,
    "tools": [
        {
            "name": "get_location",
            "description": "Get the current user location based on their IP address. This tool has no parameters or arguments.",
            "input_schema": {
                "type": "object",
                "properties": {}
            }
        },
        {
            "name": "get_weather",
            "description": "Get the current weather in a given location",
            "input_schema": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city and state, e.g. San Francisco, CA"
                    },
                    "unit": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"],
                        "description": "The unit of temperature, either 'celsius' or 'fahrenheit'"
                    }
                },
                "required": ["location"]
            }
        }
    ],
    "messages": [{
        "role": "user",
        "content": "What is the weather like where I am?"
    }]
}'
この場合、Claudeは最初にget_locationツールを呼び出してユーザーの場所を取得します。tool_resultで場所を返した後、Claudeはその場所でget_weatherを呼び出して最終的な答えを得ます。完全な会話は以下のようになる可能性があります:
役割内容
User私がいる場所の天気はどうですか?
Assistantまず現在の場所を見つけて、そこの天気を確認します。[get_locationのツール使用]
User[一致するidとSan Francisco, CAの結果を含むget_locationのツール結果]
Assistant[以下の入力でget_weatherのツール使用]{ “location”: “San Francisco, CA”, “unit”: “fahrenheit” }
User[一致するidと”59°F (15°C), mostly cloudy”の結果を含むget_weatherのツール結果]
Assistantサンフランシスコ、カリフォルニア州の現在の場所に基づくと、現在の天気は59°F(15°C)で、ほぼ曇りです。街では比較的涼しく曇った日です。外出する場合は軽いジャケットを持参することをお勧めします。
この例は、Claudeが異なるソースからデータを収集する必要がある質問に答えるために、複数のツール呼び出しを連鎖させる方法を実演しています。主要な手順は以下の通りです:
  1. Claudeは最初に天気の質問に答えるためにユーザーの場所が必要であることを認識し、get_locationツールを呼び出します。
  2. ユーザー(つまりクライアントコード)が実際のget_location関数を実行し、tool_resultブロックで結果「San Francisco, CA」を返します。
  3. 場所が判明したので、Claudeはget_weatherツールの呼び出しに進み、locationパラメータとして「San Francisco, CA」を渡します(unitは必須パラメータではないため、推測されたunitパラメータも含む)。
  4. ユーザーは再び提供された引数で実際のget_weather関数を実行し、別のtool_resultブロックで天気データを返します。
  5. 最後に、Claudeは天気データを元の質問への自然言語レスポンスに組み込みます。
デフォルトでは、Claude Opusはツール使用クエリに答える前に考えるよう促されており、ツールが必要かどうか、どのツールを使用するか、適切なパラメータを最適に決定します。Claude SonnetとClaude Haikuは可能な限りツールを使用するよう促されており、不要なツールを呼び出したり、不足しているパラメータを推測したりする可能性が高くなります。SonnetやHaikuにツール呼び出しを行う前にユーザークエリをより適切に評価するよう促すには、以下のプロンプトを使用できます:思考の連鎖プロンプト関連するツール(利用可能な場合)を使用してユーザーのリクエストに答えてください。ツールを呼び出す前に、いくつかの分析を行ってください。まず、提供されたツールのうち、ユーザーのリクエストに答えるのに関連するツールはどれかを考えてください。次に、関連するツールの各必須パラメータを確認し、ユーザーが直接提供したか、値を推測するのに十分な情報を与えているかを判断してください。パラメータが推測できるかどうかを決定する際は、特定の値をサポートするかどうかを慎重にすべてのコンテキストを考慮してください。すべての必須パラメータが存在するか、合理的に推測できる場合は、ツール呼び出しを続行してください。しかし、必須パラメータの値のいずれかが不足している場合は、関数を呼び出さず(不足しているパラメータの代替値を使用してでも)、代わりにユーザーに不足しているパラメータを提供するよう求めてください。提供されていない場合、オプションパラメータについてより多くの情報を求めないでください。
ツールを使用して、そのツールや関数を通じて出力を実行する意図がなくても、スキーマに従うJSON出力をClaudeに生成させることができます。この方法でツールを使用する場合:
  • 通常は単一のツールを提供したいでしょう
  • tool_choiceを設定して(ツール使用の強制を参照)、モデルにそのツールを明示的に使用するよう指示する必要があります
  • モデルがinputをツールに渡すことを覚えておいてください。そのため、ツールの名前と説明はモデルの視点から記述する必要があります。
以下は、record_summaryツールを使用して特定のフォーマットに従って画像を説明する例です。
#!/bin/bash
IMAGE_URL="https://upload.wikimedia.org/wikipedia/commons/a/a7/Camponotus_flavomarginatus_ant.jpg"
IMAGE_MEDIA_TYPE="image/jpeg"
IMAGE_BASE64=$(curl "$IMAGE_URL" | base64)

curl https://api.anthropic.com/v1/messages \
     --header "content-type: application/json" \
     --header "x-api-key: $ANTHROPIC_API_KEY" \
     --header "anthropic-version: 2023-06-01" \
     --data \
'{
    "model": "claude-sonnet-4-5",
    "max_tokens": 1024,
    "tools": [{
        "name": "record_summary",
        "description": "Record summary of an image using well-structured JSON.",
        "input_schema": {
            "type": "object",
            "properties": {
                "key_colors": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "properties": {
"r": { "type": "number", "description": "red value [0.0, 1.0]" },
"g": { "type": "number", "description": "green value [0.0, 1.0]" },
"b": { "type": "number", "description": "blue value [0.0, 1.0]" },
"name": { "type": "string", "description": "Human-readable color name in snake_case, e.g. \"olive_green\" or \"turquoise\"" }
                        },
                        "required": [ "r", "g", "b", "name" ]
                    },
                    "description": "Key colors in the image. Limit to less than four."
                },
                "description": {
                    "type": "string",
                    "description": "Image description. One to two sentences max."
                },
                "estimated_year": {
                    "type": "integer",
                    "description": "Estimated year that the image was taken, if it is a photo. Only set this if the image appears to be non-fictional. Rough estimates are okay!"
                }
            },
            "required": [ "key_colors", "description" ]
        }
    }],
    "tool_choice": {"type": "tool", "name": "record_summary"},
    "messages": [
        {"role": "user", "content": [
            {"type": "image", "source": {
                "type": "base64",
                "media_type": "'$IMAGE_MEDIA_TYPE'",
                "data": "'$IMAGE_BASE64'"
            }},
            {"type": "text", "text": "Describe this image."}
        ]}
    ]
}'

価格

Tool use requests are priced based on:
  1. The total number of input tokens sent to the model (including in the tools parameter)
  2. The number of output tokens generated
  3. For server-side tools, additional usage-based pricing (e.g., web search charges per search performed)
Client-side tools are priced the same as any other Claude API request, while server-side tools may incur additional charges based on their specific usage. The additional tokens from tool use come from:
  • The tools parameter in API requests (tool names, descriptions, and schemas)
  • tool_use content blocks in API requests and responses
  • tool_result content blocks in API requests
When you use tools, we also automatically include a special system prompt for the model which enables tool use. The number of tool use tokens required for each model are listed below (excluding the additional tokens listed above). Note that the table assumes at least 1 tool is provided. If no tools are provided, then a tool choice of none uses 0 additional system prompt tokens.
ModelTool choiceTool use system prompt token count
Claude Opus 4.1auto, none
any, tool
346 tokens
313 tokens
Claude Opus 4auto, none
any, tool
346 tokens
313 tokens
Claude Sonnet 4.5auto, none
any, tool
346 tokens
313 tokens
Claude Sonnet 4auto, none
any, tool
346 tokens
313 tokens
Claude Sonnet 3.7auto, none
any, tool
346 tokens
313 tokens
Claude Sonnet 3.5 (Oct) (deprecated)auto, none
any, tool
346 tokens
313 tokens
Claude Sonnet 3.5 (June) (deprecated)auto, none
any, tool
294 tokens
261 tokens
Claude Haiku 4.5auto, none
any, tool
346 tokens
313 tokens
Claude Haiku 3.5auto, none
any, tool
264 tokens
340 tokens
Claude Opus 3 (deprecated)auto, none
any, tool
530 tokens
281 tokens
Claude Sonnet 3auto, none
any, tool
159 tokens
235 tokens
Claude Haiku 3auto, none
any, tool
264 tokens
340 tokens
These token counts are added to your normal input and output tokens to calculate the total cost of a request. 現在のモデル別価格については、モデル概要表をご参照ください。 ツール使用プロンプトを送信する場合、他のAPIリクエストと同様に、レスポンスは報告されるusageメトリクスの一部として入力トークンと出力トークンの両方のカウントを出力します。

次のステップ

クックブックで、すぐに実装できるツール使用コード例のリポジトリを探索してください: