AI・機械学習
中級
Tool Use(Anthropic)(ツールユース)
AnthropicのClaude APIにおける外部ツール呼び出し機能。OpenAIのFunction Callingに相当するが、独自のコンテンツブロック形式を採用。
0 回閲覧
0 いいね
2026/6/7 更新
関連タグ
tool-use
claude
anthropic
llm
function-calling
Tool Use(Anthropic)
概要
Tool Use(ツール使用)は、AnthropicのClaude APIにおける外部ツール・関数呼び出し機能です。OpenAIのFunction Callingに相当しますが、APIの設計と応答形式が異なります。Claude 3(Haiku・Sonnet・Opus)以降の全モデルが対応しており、Anthropic Python/Node.js SDKを通じて利用できます。
API形式
リクエスト
import anthropic
client = anthropic.Anthropic()
tools = [
{
"name": "get_stock_price",
"description": "指定銘柄の現在の株価を取得する",
"input_schema": {
"type": "object",
"properties": {
"ticker": {
"type": "string",
"description": "株式ティッカーシンボル(例: AAPL)"
}
},
"required": ["ticker"]
}
}
]
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
tools=tools,
messages=[{"role": "user", "content": "Appleの株価を教えて"}]
)
# tool_useブロックの確認
for block in response.content:
if block.type == "tool_use":
print(block.name) # "get_stock_price"
print(block.input) # {"ticker": "AAPL"}
レスポンス構造
{
"type": "message",
"stop_reason": "tool_use",
"content": [
{
"type": "text",
"text": "Appleの株価を調べます。"
},
{
"type": "tool_use",
"id": "toolu_01234",
"name": "get_stock_price",
"input": {"ticker": "AAPL"}
}
]
}
ツール結果のフィードバック
# ツールを実行して結果をAPIに返す
tool_result = get_stock_price("AAPL") # 実際の関数実行
follow_up = client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
tools=tools,
messages=[
{"role": "user", "content": "Appleの株価を教えて"},
{"role": "assistant", "content": response.content},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_01234",
"content": f"現在の株価: ${tool_result}"
}
]
}
]
)
tool_choiceの制御
# 自動判断(デフォルト)
tool_choice={"type": "auto"}
# 常にどれかのツールを使用
tool_choice={"type": "any"}
# 特定ツールを強制
tool_choice={"type": "tool", "name": "get_stock_price"}
# ツール使用禁止
tool_choice={"type": "none"}
OpenAI Function Callingとの比較
| 項目 | Anthropic Tool Use | OpenAI Function Calling |
|---|---|---|
| スキーマ形式 | input_schema | parameters |
| 結果フィードバック | tool_resultコンテンツブロック | role: toolメッセージ |
| ツール強制 | tool_choice: {type: "tool"} | tool_choice: {function: {...}} |
| 並列呼び出し | 対応 | 対応 |
| ストリーミング | 対応 | 対応 |
Extended Thinkingとの組み合わせ
Claude 3.7/3.5以降では、Tool Useと拡張思考(Extended Thinking)を組み合わせることで、複雑な推論を必要とするツール選択が可能になりました。thinkingコンテンツブロックがツール呼び出し前に挿入され、モデルの判断プロセスが可視化されます。
セキュリティ考慮事項
- ツール入力は必ずサーバーサイドでバリデーション
disable_fallbackを使用して意図しない動作を防止- 機密操作は人間確認(Human-in-the-loop)パターンを実装