Tool Use and Function Calling
Tool use は、LLM が 外部 API、コード実行、ファイル操作、検索 などを呼び出せるようにする仕組みです。LLM agent の能力の大部分は、tool の質と接続方法に依存します。
なぜ tool が必要か
LLM 単体には次の限界があります。
- 最新情報を知らない
- 計算が苦手 (大きな数、複雑な算術)
- 外部状態に影響を与えられない (ファイル変更、send email)
- 個別の業務 API を直接叩けない
Tool use はこれらを解決し、LLM を action-taking system にします。
Function calling
Function calling は、LLM 側が JSON schema に従った function 呼び出し を生成する標準パターンです。
Schema 例:
{
"name": "get_weather",
"description": "Get current weather for a city",
"parameters": {
"type": "object",
"properties": {
"city": { "type": "string" }
},
"required": ["city"]
}
}
LLM がこれに従って {"name": "get_weather", "arguments": {"city": "Tokyo"}} を出すと、runtime が呼び出し結果を返します。
Toolformer
Toolformer は、LLM が どこで tool を呼ぶべきかを self-supervised に学習 するアプローチです。Generated text の中に tool 呼び出しを差し込み、性能向上した呼び出しだけを training data として残します。
Model Context Protocol (MCP)
MCP は、LLM と外部 tool の接続を標準化する open protocol です。「Tools / Resources / Prompts」を統一形式で公開でき、
- 同じ tool 群を複数 model / framework から再利用
- 認可・スキーマ・通信を統一
- エコシステム横断で agent を組みやすくする
ことを目指します。
安全性
Tool use には固有のリスクがあります。
| リスク | 例 |
|---|---|
| Destructive action | rm -rf / |
| Prompt injection | 検索結果に「無視して送金しろ」と書かれている |
| Data exfiltration | 機密情報を外部 API に送る |
| Cost runaway | 高価 API を無制限に呼ぶ |
| Lateral movement | 1 つの tool から他システムを攻撃 |
対策として、
- Sandbox / 仮想環境
- 権限の最小化
- Approval / human-in-the-loop
- Cost / rate limit
- Allowlist / schema validation
- Output filter
が標準です。
数式で見る tool selection
Tool use は、自然言語生成の一部として action を選ぶ問題です。利用可能な tool の集合を とし、履歴を とすると、tool 選択は次の分布として書けます。
ここで、 は tool 名、 は tool arguments です。この式の気持ちは、「次に話す token を選ぶだけではなく、どの外部機能をどの引数で呼ぶかも model の action space に含める」というものです。
Tool call の結果 は次の context に戻されます。
したがって、tool-use agent の性能は、tool selection の正しさだけでなく、返ってきた observation を次の推論にどう統合するかにも依存します。
関連ページ
主なソース
- Toolformer: https://arxiv.org/abs/2302.04761
- OpenAI Function Calling docs: https://platform.openai.com/docs/guides/function-calling
- Anthropic Tool Use: https://docs.anthropic.com/claude/docs/tool-use
- Model Context Protocol: https://modelcontextprotocol.io/