研究助手现在只会搜索;这一章给它加第二个工具 —— fetch_page,把搜到的网页抓回来读。借它把工具的三件事讲透:schema 怎么定义能力、错误怎么回传决定鲁棒性、并行怎么调决定速度。模型够不到你的函数实现,只看得到你写的 schema 和描述。The research assistant can only search; this chapter gives it a second tool — fetch_page, to pull back and read the pages it found. Through it, the three things about tools: how the schema defines capability, how errors feed back to determine robustness, and how parallelism determines speed. The model can't reach your implementation — only the schema and description you wrote.
— I
工具的定义就是它的契约A Tool's Definition Is Its Contract.
模型不读你的函数实现,只读你的 schema 和描述。这份契约写得好不好,直接决定它调得对不对。The model doesn't read your implementation — it reads your schema and description. How well you write that contract decides whether it calls correctly.一个工具就三块:name、description、入参 schema。description 不是文档,是 prompt —— 模型靠它决定「什么时候、用什么参数」调这个工具。1注 1Note 1Anthropic 文档 · Tool use —— 工具由 name / description / input_schema(JSON Schema)定义;strict: true 保证调用严格符合 schema;tool_choice 可选 auto / any / tool / none;tool_result 可带 is_error 把失败回传给模型。截至 2026-05。Anthropic docs · Tool use — tools are defined by name / description / input_schema (JSON Schema); strict: true guarantees calls conform to the schema; tool_choice can be auto / any / tool / none; tool_result can carry is_error to feed failures back to the model. As of 2026-05.模型能只凭描述和少量示例就学会怎么调,靠的是 in-context learning —— GPT-3 那篇早已证明,大模型「无需梯度更新或微调」、仅凭提示里的示例就能改变行为。4注 4Note 4Brown et al. · 「Language Models are Few-Shot Learners」(GPT-3,2020,arXiv:2005.14165)—— in-context / few-shot learning:大模型「无需任何梯度更新或微调,仅凭文本里的少量示例」就能执行新任务。这是工具描述与示例为何能改变模型行为的根。Brown et al. · 'Language Models are Few-Shot Learners' (GPT-3, 2020, arXiv:2005.14165) — in-context / few-shot learning: a large model performs new tasks 'without any gradient updates or fine-tuning, with tasks and few-shot demonstrations specified purely via text interaction.' The root of why tool descriptions and examples change model behavior.同一个工具,两家的写法几乎一样:A tool is three parts: name, description, and an input schema. The description isn't documentation — it's a prompt; the model uses it to decide 'when, and with what arguments' to call.1注 1Note 1Anthropic 文档 · Tool use —— 工具由 name / description / input_schema(JSON Schema)定义;strict: true 保证调用严格符合 schema;tool_choice 可选 auto / any / tool / none;tool_result 可带 is_error 把失败回传给模型。截至 2026-05。Anthropic docs · Tool use — tools are defined by name / description / input_schema (JSON Schema); strict: true guarantees calls conform to the schema; tool_choice can be auto / any / tool / none; tool_result can carry is_error to feed failures back to the model. As of 2026-05. That it can learn the call from nothing but a description and a few examples is in-context learning — GPT-3 showed a large model changes behavior from prompt examples alone, 'without any gradient updates or fine-tuning.'4注 4Note 4Brown et al. · 「Language Models are Few-Shot Learners」(GPT-3,2020,arXiv:2005.14165)—— in-context / few-shot learning:大模型「无需任何梯度更新或微调,仅凭文本里的少量示例」就能执行新任务。这是工具描述与示例为何能改变模型行为的根。Brown et al. · 'Language Models are Few-Shot Learners' (GPT-3, 2020, arXiv:2005.14165) — in-context / few-shot learning: a large model performs new tasks 'without any gradient updates or fine-tuning, with tasks and few-shot demonstrations specified purely via text interaction.' The root of why tool descriptions and examples change model behavior. The same tool looks nearly identical across both vendors:
研究助手的 fetch_page,两家 API 的定义The research assistant's fetch_page, in both APIs
两边都支持 strict —— 强制模型的调用严格符合 schema,省掉一整类「参数缺失 / 类型不对」的脏活。1注 1Note 1Anthropic 文档 · Tool use —— 工具由 name / description / input_schema(JSON Schema)定义;strict: true 保证调用严格符合 schema;tool_choice 可选 auto / any / tool / none;tool_result 可带 is_error 把失败回传给模型。截至 2026-05。Anthropic docs · Tool use — tools are defined by name / description / input_schema (JSON Schema); strict: true guarantees calls conform to the schema; tool_choice can be auto / any / tool / none; tool_result can carry is_error to feed failures back to the model. As of 2026-05.2注 2Note 2OpenAI 文档 · Function calling —— 工具用 {type: function, function: {name, description, parameters, strict}} 定义;响应在 message.tool_calls,结果以 {role: tool, tool_call_id, content} 回传。形态稳定,作对照。OpenAI docs · Function calling — tools defined as {type: function, function: {name, description, parameters, strict}}; calls arrive in message.tool_calls and results return as {role: tool, tool_call_id, content}. Stable shape, used as a contrast.能开就开。Both support strict — forcing the call to conform exactly to the schema, removing a whole class of 'missing arg / wrong type' grunt work.1注 1Note 1Anthropic 文档 · Tool use —— 工具由 name / description / input_schema(JSON Schema)定义;strict: true 保证调用严格符合 schema;tool_choice 可选 auto / any / tool / none;tool_result 可带 is_error 把失败回传给模型。截至 2026-05。Anthropic docs · Tool use — tools are defined by name / description / input_schema (JSON Schema); strict: true guarantees calls conform to the schema; tool_choice can be auto / any / tool / none; tool_result can carry is_error to feed failures back to the model. As of 2026-05.2注 2Note 2OpenAI 文档 · Function calling —— 工具用 {type: function, function: {name, description, parameters, strict}} 定义;响应在 message.tool_calls,结果以 {role: tool, tool_call_id, content} 回传。形态稳定,作对照。OpenAI docs · Function calling — tools defined as {type: function, function: {name, description, parameters, strict}}; calls arrive in message.tool_calls and results return as {role: tool, tool_call_id, content}. Stable shape, used as a contrast. Turn it on when you can.反过来,过度暴露能力是这一章最该警惕的反模式:一个 run_sql(query) 工具,等于把整个数据库的权限交给了模型。工具的边界就是权限的边界 —— schema 越窄越安全。3注 3Note 3JSON Schema —— 工具入参 schema 的规范来源(type / properties / required / enum 等)。模型读的是这份契约,不是你的函数实现。JSON Schema — the spec behind tool input schemas (type / properties / required / enum, etc.). The model reads this contract, not your function implementation.这条线第 9 章还会再画一次。Conversely, over-exposing capability is the anti-pattern to watch here: a run_sql(query) tool hands the model your whole database. A tool's boundary is a permission boundary — the narrower the schema, the safer.3注 3Note 3JSON Schema —— 工具入参 schema 的规范来源(type / properties / required / enum 等)。模型读的是这份契约,不是你的函数实现。JSON Schema — the spec behind tool input schemas (type / properties / required / enum, etc.). The model reads this contract, not your function implementation. Chapter 9 draws this line again.
— II
错误处理决定鲁棒性Error Handling Determines Robustness.
工具一定会失败 —— fetch_page 尤其会:404、超时、抓回一页渲染不出的 JS。agent 的鲁棒性不在 happy path,在你怎么把失败回传给模型。Tools will fail — fetch_page especially: 404s, timeouts, a page of JS that won't render. An agent's robustness isn't on the happy path; it's in how you feed failure back to the model.关键一招:别让工具的异常炸掉整个循环,把错误当成一个普通的 tool_result 传回去(带 is_error)。模型看到错误,就能改参数重试,或者体面地告诉用户做不到。1注 1Note 1Anthropic 文档 · Tool use —— 工具由 name / description / input_schema(JSON Schema)定义;strict: true 保证调用严格符合 schema;tool_choice 可选 auto / any / tool / none;tool_result 可带 is_error 把失败回传给模型。截至 2026-05。Anthropic docs · Tool use — tools are defined by name / description / input_schema (JSON Schema); strict: true guarantees calls conform to the schema; tool_choice can be auto / any / tool / none; tool_result can carry is_error to feed failures back to the model. As of 2026-05.The key move: don't let a tool exception blow up the loop — return the error as an ordinary tool_result (with is_error). Seeing the error, the model can fix its arguments and retry, or tell the user gracefully that it can't.1注 1Note 1Anthropic 文档 · Tool use —— 工具由 name / description / input_schema(JSON Schema)定义;strict: true 保证调用严格符合 schema;tool_choice 可选 auto / any / tool / none;tool_result 可带 is_error 把失败回传给模型。截至 2026-05。Anthropic docs · Tool use — tools are defined by name / description / input_schema (JSON Schema); strict: true guarantees calls conform to the schema; tool_choice can be auto / any / tool / none; tool_result can carry is_error to feed failures back to the model. As of 2026-05.
把失败当结果回传,而不是抛异常Return failure as a result, not an exception
但别让它无限重试。给每个工具一个重试上限,撞到上限还失败,就把失败如实交给用户。盲目重试既烧钱又卡死 —— 这笔账第 11 章细算。But don't let it retry forever. Give each tool a retry cap; if it still fails at the cap, hand the failure to the user honestly. Blind retries both burn money and hang — chapter 11 does that math.
— III
并行调用决定速度Parallel Calls Determine Speed.
研究助手常常想一次搜好几个角度、或一次抓好几个页面 —— 模型在一轮里给出多个工具调用。串行执行,是在浪费它的并行意图。The research assistant often wants to search several angles or fetch several pages at once — the model emits multiple tool calls in one turn. Running them serially wastes its parallel intent.一个 assistant 轮次里可以有多个 tool_use 块。把它们并发执行,再把所有 tool_result 放进同一个 user 轮次回传 —— 每个结果带对自己的 tool_use_id 就行,顺序无所谓。1注 1Note 1Anthropic 文档 · Tool use —— 工具由 name / description / input_schema(JSON Schema)定义;strict: true 保证调用严格符合 schema;tool_choice 可选 auto / any / tool / none;tool_result 可带 is_error 把失败回传给模型。截至 2026-05。Anthropic docs · Tool use — tools are defined by name / description / input_schema (JSON Schema); strict: true guarantees calls conform to the schema; tool_choice can be auto / any / tool / none; tool_result can carry is_error to feed failures back to the model. As of 2026-05.A single assistant turn can contain multiple tool_use blocks. Run them concurrently, then return all tool_results in one user turn — each result just carries its own tool_use_id; order doesn't matter.1注 1Note 1Anthropic 文档 · Tool use —— 工具由 name / description / input_schema(JSON Schema)定义;strict: true 保证调用严格符合 schema;tool_choice 可选 auto / any / tool / none;tool_result 可带 is_error 把失败回传给模型。截至 2026-05。Anthropic docs · Tool use — tools are defined by name / description / input_schema (JSON Schema); strict: true guarantees calls conform to the schema; tool_choice can be auto / any / tool / none; tool_result can carry is_error to feed failures back to the model. As of 2026-05.
import asyncioasync def run_calls(blocks): async def one(b): out = await run_tool_async(b.name, b.input) return {"type": "tool_result", "tool_use_id": b.id, "content": out} return await asyncio.gather(*[one(b) for b in blocks]) # 一起跑,不排队
并发执行一轮里的多个工具调用Run a turn's tool calls concurrently
但并行有前提:工具之间无依赖,且都能安全并发。读操作放心并行;有副作用的工具(写文件、下单、转账)要想清楚 —— 并发的副作用是最难复现的 bug。But parallelism has conditions: the tools must be independent and safe to run concurrently. Parallelize reads freely; think twice about tools with side effects (writing files, placing orders, transfers) — concurrent side effects are the hardest bugs to reproduce.动手 · 给你第二章那个工具做三件事:Hands-on · do three things to the tool from chapter 2:
把它的 description 当 prompt 重写一遍,测模型调得准不准 —— 改前改后各跑 5 次,数调对的次数。Rewrite its description as a prompt and test call accuracy — run 5 times before and after, count the correct calls.
把它从「抛异常」改成「返回带 is_error 的结果」,看模型遇错会不会自己改参数重试。Change it from 'throw' to 'return a result with is_error,' and watch whether the model fixes its arguments and retries on error.
如果你有两个以上互不依赖的工具,把它们改成并发执行,量一下一轮快了多少。If you have two or more independent tools, switch them to concurrent execution and measure how much a round speeds up.
工具的 schema 就是它的权限,
描述就是它的说明书。.
A tool's schema is its permission;
its description is its manual..
Aklman Library
— 讨论Discussion
讨论Discussion.
评论区初始化中…Initializing comments…
01 / 01
没有匹配结果No matches.
换个关键词,或按 Esc 回到页面Try another keyword, or press Esc to return