Skip to content

Anthropic Tool Use

Anthropic 把这套能力叫 tool use

工具定义结构

json
{
  "name": "get_weather",
  "description": "获取指定城市天气",
  "input_schema": {
    "type": "object",
    "properties": {
      "city": { "type": "string" }
    },
    "required": ["city"]
  }
}

响应风格

Anthropic 常见做法不是给你一个统一的 tool_calls 数组,而是在 content blocks 中混合返回:

  • text
  • tool_use
  • tool_result

伪结构示意:

json
{
  "content": [
    {
      "type": "tool_use",
      "id": "toolu_01",
      "name": "get_weather",
      "input": {"city": "Shanghai"}
    }
  ]
}

处理思路

  1. 扫描 content blocks
  2. 找出所有 tool_use
  3. 执行对应工具
  4. tool_result 作为后续消息内容回传
  5. 继续请求模型生成最终回答

和 OpenAI 最大差异

  • OpenAI 偏“消息对象里挂一个 tool_calls 数组”
  • Anthropic 偏“多种内容块混排”

所以如果你在做跨 provider 适配器,解析层必须拆开写。

工程建议

内部统一抽象时,至少把这些字段归一化:

  • id
  • name
  • input
  • provider
  • raw

否则一到多 provider 支持阶段,代码会很乱。

聚焦 OpenClaw、tool_calls、function calling 与 agent 实战。