Claudeのツール使用
Claudeはツールや関数と対話する能力を持ち、より幅広いタスクを実行するためにClaudeの機能を拡張することができます。
以下は、Messages APIを使用してClaudeにツールを提供する方法の例です:
ツール使用の仕組み
Claudeは2種類のツールをサポートしています:
-
クライアントツール:あなたのシステム上で実行されるツールで、以下を含みます:
- ユーザー定義のカスタムツール(あなたが作成して実装するもの)
- コンピューター使用(ベータ)やテキストエディタなどのAnthropicが定義したツール(クライアント実装が必要)
-
サーバーツール:Anthropicのサーバー上で実行されるツール(ウェブ検索ツールなど)。これらのツールはAPIリクエストで指定する必要がありますが、あなた側での実装は必要ありません。
Anthropicが定義したツールは、モデルバージョン間の互換性を確保するためにバージョン付きの型(例:web_search_20250305
、text_editor_20250124
)を使用しています。
クライアントツール
クライアントツールをClaudeと統合するには、以下の手順に従います:
Claudeにツールとユーザープロンプトを提供する
- クライアントツールの名前、説明、入力スキーマをAPIリクエストで定義します。
- 「サンフランシスコの天気は?」などのように、これらのツールを必要とする可能性のあるユーザープロンプトを含めます。
Claudeがツールを使用することを決定する
- Claudeはユーザーの質問に対して、どのツールが役立つかを評価します。
- もしあれば、Claudeは適切にフォーマットされたツール使用リクエストを構築します。
- クライアントツールの場合、APIレスポンスの
stop_reason
はtool_use
となり、Claudeの意図を示します。
ツールを実行して結果を返す
- Claudeのリクエストからツール名と入力を抽出します
- あなたのシステム上でツールコードを実行します
- 結果を
tool_result
コンテンツブロックを含む新しいuser
メッセージで返します
Claudeがツール結果を使用して応答を作成する
- Claudeはツール結果を分析して、元のユーザープロンプトに対する最終的な応答を作成します。
注意:ステップ3と4はオプションです。一部のワークフローでは、Claudeのツール使用リクエスト(ステップ2)だけで十分で、結果をClaudeに送り返す必要がない場合もあります。
サーバーツール
サーバーツールは異なるワークフローに従います:
Claudeにツールとユーザープロンプトを提供する
- ウェブ検索などのサーバーツールには、それぞれ独自のパラメータがあります。
- 「AIに関する最新ニュースを検索して」などのように、これらのツールを必要とする可能性のあるユーザープロンプトを含めます。
Claudeがサーバーツールを実行する
- Claudeはユーザーの質問に対して、サーバーツールが役立つかどうかを評価します。
- もしそうなら、Claudeはツールを実行し、結果は自動的にClaudeの応答に組み込まれます。
Claudeがサーバーツール結果を使用して応答を作成する
- Claudeはサーバーツール結果を分析して、元のユーザープロンプトに対する最終的な応答を作成します。
- サーバーツールの実行には追加のユーザー操作は必要ありません。
ツール使用の例
以下は、さまざまなツール使用パターンとテクニックを示すコード例です。簡潔にするため、ツールは単純なものであり、ツールの説明は最適なパフォーマンスを確保するために理想的なものよりも短くなっています。
価格
Tool use requests are priced based on:
- The total number of input tokens sent to the model (including in the
tools
parameter) - The number of output tokens generated
- 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 responsestool_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.
Model | Tool choice | Tool use system prompt token count |
---|---|---|
Claude Opus 4 | auto , none any , tool | 346 tokens 313 tokens |
Claude Sonnet 4 | auto , none any , tool | 346 tokens 313 tokens |
Claude Sonnet 3.7 | auto , none any , tool | 346 tokens 313 tokens |
Claude Sonnet 3.5 (Oct) | auto , none any , tool | 346 tokens 313 tokens |
Claude Sonnet 3.5 (June) | auto , none any , tool | 294 tokens 261 tokens |
Claude Haiku 3.5 | auto , none any , tool | 264 tokens 340 tokens |
Claude Opus 3 | auto , none any , tool | 530 tokens 281 tokens |
Claude Sonnet 3 | auto , none any , tool | 159 tokens 235 tokens |
Claude Haiku 3 | auto , 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
メトリクスの一部として入力と出力の両方のトークン数を出力します。
次のステップ
クックブックにある実装可能なツール使用コード例のリポジトリを探索してください: