用途:本技能讲解 Claude 的工具调用(函数调用)模式,让 Claude 调用你定义的函数,例如查询数据库、调用 API、读取文件或做数学计算。Claude 决定何时调用工具,由你执行并把结果回传,这是构建 Claude 智能体的方式。
主要能力:定义工具(含名称、描述与 JSON Schema 形式的 input_schema);发送带 tools 的消息;Claude 返回 stop_reason 为 tool_use 及 tool_use 内容块(含 name 与 input);执行函数后以 tool_result 回传;通过循环持续调用直到 stop_reason 为 end_turn,得到最终文本。
使用方式:先完成 clade-model-inference 并了解 JSON Schema。用 @claude-ai/sdk 或 anthropic Python 包创建客户端,定义 tools 数组,调用 client.messages.create 并传入 model、max_tokens、tools、messages。找到 tool_use 块后执行对应函数,再以 role 为 user、type 为 tool_result 的内容回传,tool_use_id 必须与 tool_use 块的 id 一致。智能体循环中把 assistant 响应与工具结果依次追加到 messages。
适用场景:需要让 Claude 触发外部动作、查询数据或串联多步任务的智能体应用。
限制:Anthropic 不提供嵌入 API;嵌入与向量检索需搭配专用嵌入模型(OpenAI、Cohere 或 Voyage)。常见错误包括工具 schema 不合法、Claude 幻觉出不存在的工具名、tool_result 的 id 不匹配,需在定义与执行前校验。资料未提供收费、价格与平台信息。