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種類のツールをサポートしています:
クライアントツール:あなたのシステム上で実行されるツールで、以下が含まれます:
サーバーツール:ウェブ検索やウェブフェッチツールなど、Anthropicのサーバー上で実行されるツール。これらのツールはAPIリクエストで指定する必要がありますが、あなたの側での実装は不要です。
Anthropicが定義したツールは、モデルバージョン間での互換性を確保するためにバージョン付きタイプ(例:web_search_20250305、text_editor_20250124)を使用します。
以下の手順でクライアントツールをClaudeと統合します:
Claudeにツールとユーザープロンプトを提供
Claudeがツールの使用を決定
stop_reasonがtool_useになり、Claudeの意図を示します。ツールを実行して結果を返す
tool_resultコンテンツブロックを含む新しいuserメッセージで結果を返すClaudeがツール結果を使用してレスポンスを作成
注意:手順3と4はオプションです。一部のワークフローでは、Claudeのツール使用リクエスト(手順2)だけで十分で、結果をClaudeに送り返す必要がない場合があります。
サーバーツールは異なるワークフローに従います:
Claudeにツールとユーザープロンプトを提供
Claudeがサーバーツールを実行
Claudeがサーバーツール結果を使用してレスポンスを作成
様々なツール使用パターンと技術を実演するコード例をいくつか紹介します。簡潔にするため、ツールはシンプルなツールで、ツールの説明は最高のパフォーマンスを確保するために理想的な長さよりも短くしています。
Tool use requests are priced based on:
tools parameter)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:
tools parameter in API requests (tool names, descriptions, and schemas)tool_use content blocks in API requests and responsestool_result content blocks in API requestsWhen 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.
| Model | Tool choice | Tool use system prompt token count |
|---|---|---|
| Claude Opus 4.5 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Opus 4.1 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Opus 4 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Sonnet 4.5 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Sonnet 4 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Sonnet 3.7 (deprecated) | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Haiku 4.5 | auto, noneany, tool | 346 tokens 313 tokens |
| Claude Haiku 3.5 | auto, noneany, tool | 264 tokens 340 tokens |
| Claude Opus 3 (deprecated) | auto, noneany, tool | 530 tokens 281 tokens |
| Claude Sonnet 3 | auto, noneany, tool | 159 tokens 235 tokens |
| Claude Haiku 3 | auto, noneany, 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メトリクスの一部として入力トークンと出力トークンの両方のカウントを出力します。
クックブックで、すぐに実装できるツール使用コード例のリポジトリを探索してください: