把子任务交给子循环,再交回一次停止

阅读约需 58分钟 分钟,7节中含4个示例。

版次 2026.11.2 示例于 2026年11月20日 通过 64个示例中有64个通过 变化内容

AI 翻译。由人工以英文核验。 我们如何制作图书

译自英文版图书,版次 2026.11.2。 阅读原文

读完本章,你可以把一个子任务交给拥有独立步数预算的子循环,并得到一份摘要和一个停止原因,而不是子循环的完整记录。你还会让一次停止传到人手中:被停止规则判定为卡住而结束的运行,会交给父循环,由父循环把这次停止所带的问题转给提交该问题单的维护者。

以案例中的任务 T-02 为例:在小型单位换算库 unitcraft 中,负数金额会丢失千位分隔符,维护代理需要修复这个问题。该运行读取 unitcraft/format.py,并对它做了两次编辑。两次编辑都失败了,因为它们所指定的旧文本并不按原样存在于文件中,所以测试结果没有变化。第 2 章规定,一旦规则判定没有进展,就结束这样的运行。在 task-set 记录中,v1 版框架运行 T-02 直到触及步数上限。用第 2 章的两条停止规则重放同样的回复,这次运行会以 no_progress at step 5 of 16 结束。这次停止完成了它的任务,但留给维护者的只有一份日志,没有关于代理当时想做什么的说明。必须有人去读日志才能弄清楚。

规模是第二个问题。要找出舍入规则位于何处,需要读取多个文件,而代理读过的每个文件都会留在它下一步携带的上下文中。这样的旁支任务,更适合放在独立的上下文中完成,只返回父循环所需的内容。Anthropic 的子代理文档为其产品描述了这种划分:子代理在自己的上下文中完成工作,只返回摘要。

本章为这个案例构建子循环。子循环就是同一个循环,只是拥有自己的预算。在本书中,交接(handoff)指一次运行以向人提出问题而不是以完成作为结束:它可以是模型自己发出的 ask_user 调用,也可以是停止之后由框架给出的结构化交接,其中包含停止原因、该运行尝试过什么以及一个问题。子循环把它的交接返回给父循环:它是如何结束的、尝试过什么、读取和修改了哪些文件,以及它的摘要或它停下时所面对的问题。它从不返回自己的记录。结束子循环的停止,会以一个问题的形式传到维护者那里。

这部分尚未翻译,因此以英文显示。

A child is a loop with its own budget

A side task in the case, such as finding where a rounding rule lives, can take steps that the parent's budget should not pay for. A child is the loop from chapter 1, started by a parent in the middle of its own run. It has the same tools and the same stop rules, and it keeps a transcript of its own. What differs is the count. The child's budget counts the child's own model calls, from its first call, so the parent's steps don't pay for the child's work, and a child that loops runs out of its own budget. That is the count chapter 5 promised a child: its own steps, not the parent's. A child's run_tests goes through the same sandboxed test runner as its parent's, with the caps, clean environment and no conftest files that chapter 7 describes, because the child is given the same tool set. The path is run_tests in lib/tools.py, then run_pytest and run_command there, then run_sandboxed in lib/sandbox.py. No example runs a child's command any other way.

Give the child 4 steps and run it on T-01. The task-set recording's first steps, from the model of chapter 1, replay for it, so the child does what the recorded run did at the start.

child-with-budget.py Python 3.12.15 · pydantic 2.14.3 通过
"""A child loop on T-01 with a budget of 4 steps. The budget counts the child's own model calls, and it ends the childas step_cap with no finish. The child's requests match the recording's first four steps, so they replay."""from lib.case import load_tasksfrom lib.models import ReplayModelfrom lib.subagents import run_childfrom lib.tools import V1from lib.trace import IdCounterBUDGET = 4task = load_tasks(only=["T-01"])[0]child = run_child(ReplayModel("task-set", "T-01"), task, V1, budget=BUDGET, ids=IdCounter())print("child budget, steps:", BUDGET)print("child steps:", child.handoff.steps)print("child result:", child.handoff.ended)print("child tools:", ", ".join(child.tool_names))print("child steps, in order:", *child.trace, sep="\n  ")child.run.close()assert child.handoff.ended == "step_cap" and child.handoff.steps == BUDGET
输出python child-with-budget.py
child budget, steps: 4
child steps: 4
child result: step_cap
child tools: list_files, read_file, run_tests, write_file, edit_file, ask_user, finish
child steps, in order:
  step 1: list_files: 25 files
  step 2: read_file: 23 lines
  step 3: edit_file: error: old text not found in unitcraft/format.py
  step 4: read_file: 23 lines

The budget, not the tools, ended this child: it used all its steps and never called finish. Its budget counts model calls, 4 of them here against a budget of 4, and the harness ends the child as step_cap. Its tools are the full tool set v1, list_files, read_file, run_tests, write_file, edit_file, ask_user, finish, so the budget is what holds the child back, not a smaller tool set.

Those 4 steps are the child's whole run. It lists the files, reads one, tries an edit that fails because the old text isn't in the file as written, and reads the file again. Nothing in the budget cares which of those steps was useful. It counts them.

A parent can start the same child itself, through a tool. In the next example the parent is a loop with two tools, start_child and ask_user. It starts the child on T-02 under both stop rules, reads the child's handoff as the tool's result, and asks the person the question the handoff carries.

parent-starts-child.py Python 3.12.15 · pydantic 2.14.3 通过
"""A parent loop starts the child on T-02 through a tool, reads the child's handoff and asks the person the child'squestion. The output counts the parent's steps apart from the child's, and shows that the child's file text stays out ofthe parent's context."""from lib.case import load_tasksfrom lib.loop import Stops, run_taskfrom lib.models import ReplayModelfrom lib.subagents import ParentReadsHandoff, characters, start_child_toolsfrom lib.tools import V1from lib.trace import IdCounterRULES = Stops(repeat_limit=3, no_progress_limit=2)task = load_tasks(only=["T-02"])[0]child_ids = IdCounter()child_ids.next("run")  # the recording numbers its runs in task order, so T-02 is run-0002: skip T-01's numberchildren = []tools = start_child_tools(task, lambda: ReplayModel("task-set", task.id), ids=child_ids, stops=RULES, children=children)parent_model = ParentReadsHandoff(task.id)parent = run_task(parent_model, task, tools, max_steps=tools.step_cap, ids=IdCounter())child = children[0]h = child.handoff# the child's longest tool result is its read of the library file, the text a parent must not receivefile_text = max((m["content"] for m in child.transcript if m["role"] == "tool"), key=len)parent_text = "\n".join(m["content"] for m in parent_model.messages)print("child budget, steps:", V1.step_cap)print("child steps:", h.steps)print("child result:", h.ended)print("parent steps:", parent.steps)print("parent result:", parent.ended)print("parent tools:", ", ".join(t.name for t in tools.tools))print("question asked matches the handoff:", "yes" if parent.question == h.question else "no")print("child transcript, characters:", characters(child.transcript))print("handoff in parent context, characters:", len(h.text))print("parent context at last step, characters:", characters(parent_model.messages))print("child file text in parent context:", "yes" if file_text in parent_text else "no")print("parent steps, in order:", *parent.trace.lines(), sep="\n  ")assert h.ended == "no_progress" and parent.ended == "ask_user" and parent.steps == 2assert parent.question == h.question and file_text not in parent_textparent.close()
输出python parent-starts-child.py
child budget, steps: 16
child steps: 5
child result: no_progress
parent steps: 2
parent result: ask_user
parent tools: start_child, ask_user
question asked matches the handoff: yes
child transcript, characters: 5408
handoff in parent context, characters: 436
parent context at last step, characters: 1041
child file text in parent context: no
parent steps, in order:
  step 1: start_child: child no_progress at step 5
  step 2: ask_user: ask_user

The parent's question is the child's, word for word, and the child's file text is not in the parent's context. The child takes 5 steps of its own budget, and the parent takes 2, one to start the child and one to ask. The parent receives a handoff of 436 characters, where the child's transcript holds 5408.

The end is a stop like any other. A child that reaches its budget ends without a finish, and the handoff in the section on stops carries that end to the parent, which is where the next decision goes.

Working rule: Give each child a budget of its own, counted in its own model calls and never shared with the parent. Set it from runs that finished on the task set, not from a guess. A budget below what finished runs need turns finished work into stops.

包含四个部分的任务说明

任务说明就是子代理收到的任务本身。这里子代理只拿到议题文本:summary-to-parent 中的 T-02 子代理从该任务自己的议题出发,没有任何父代理写下的内容。Anthropic 对其多代理研究系统的介绍指出,每个子代理的任务说明需要包含四个部分:目标、输出格式、关于所用工具和信息来源的指引,以及清晰的任务边界。按这四个部分来写,T-02 子代理的任务说明可以是这样:

Objective: find out why the two edits to unitcraft/format.py left the test result where it was.
Output format: a short summary, then the files read and the files edited.
Tools and sources: read_file and run_tests on the scratch copy; edit_file only on unitcraft/format.py.
Boundaries: do not change a test or any file other than unitcraft/format.py. If the fix needs a public name changed, call ask_user instead of editing.

每个部分都针对 T-02 这一情形容易出现的一种失败。目标让子代理专注于那两处修改,不去碰库中的其余部分。输出格式让父代理一遍就能读完结果,文件列表放在末尾,正是父代理查找的位置。工具和信息来源这一行说明子代理可以编辑哪个文件。这些示例中的子代理拥有完整的 v1 工具集,其中包括 write_file,因此这一行只是父代理写下的文字,这里没有任何机制强制执行。交接内容中的 files changed 一行事后会显示子代理改动了哪些文件,edits failed 一行则显示有多少处修改未能应用。边界是维护者最先检查的部分,因为可见测试就是检验手段,而对测试的修改不需要真正修复问题就能让检验通过。

任务说明是父代理写下的文字,本章并不对它做度量。同一篇介绍把下面这点作为观察而非度量结果提出:收到含糊任务的子代理有时会重复彼此的搜索,而明确的任务说明是该厂商的解决办法。这里没有任何示例测试含糊的任务说明。任务说明能够做到的是让边界可以检查。关于返回内容的那一节中的交接内容列出了子代理读取和修改过的文件,可以逐行与边界一一对照。

工作准则: 在子代理启动之前,把四个部分都写进任务说明,并且把边界写成文件和工具,而不是意图。如果任务说明无法说明子代理何时算完成,或者它可以编辑哪些文件,就还没有准备好发送。

返回什么

T-02 的运行会留下一份记录,其中包含它读过的每个文件、每次失败的编辑和每次测试运行,这远远超出了父级需要取回的内容。父级需要的是子级的结果,以及足以让它判断该结果的那一部分过程。它不需要那些读取记录。被中止的子级也需要同样的处理,只是要再加上中止原因和一个问题。

在两条停止规则下,按任务集的步数上限,把 T-02 作为子级运行,并打印父级收到的内容,同时打印子级记录的大小。

summary-to-parent.py Python 3.12.15 · pydantic 2.14.3 通过
"""T-02's recorded run as a child, with both stop rules and the task set's step cap. The parent gets thehandoff's fields and not the child's transcript; the example prints the size of each."""from lib.case import load_tasksfrom lib.loop import Stopsfrom lib.models import ReplayModelfrom lib.subagents import characters, run_child, tokensfrom lib.tools import V1from lib.trace import IdCounterRULES = Stops(repeat_limit=3, no_progress_limit=2)task = load_tasks(only=["T-02"])[0]ids = IdCounter()ids.next("run")  # the recording numbers its runs in task order, so T-02 is run-0002: skip T-01's numberchild = run_child(ReplayModel("task-set", "T-02"), task, V1, budget=V1.step_cap, ids=ids, stops=RULES)h = child.handoffprint("child result:", h.ended, "at step", h.steps, "of", h.budget)print("transcript messages:", len(child.transcript))print("child transcript, characters:", characters(child.transcript))print("child transcript, estimated tokens:", tokens(characters(child.transcript)))print("parent receives, characters:", len(h.text))print("parent receives, estimated tokens:", tokens(len(h.text)))print("saved by the handoff, characters:", characters(child.transcript) - len(h.text))print("what the parent receives:")print(h.text)child.run.close()assert task.issue not in h.text and len(h.text) < characters(child.transcript)
输出python summary-to-parent.py
child result: no_progress at step 5 of 16
transcript messages: 12
child transcript, characters: 5408
child transcript, estimated tokens: 1803
parent receives, characters: 436
parent receives, estimated tokens: 146
saved by the handoff, characters: 4972
what the parent receives:
child: child-0001
ended: no_progress
steps: 5 of 16
tried: edit_file 2, list_files 1, read_file 2
files read: unitcraft/format.py
files changed: none
edits failed: 2
stop: no_progress (edit_file, 2 edits in a row left the test result unchanged: 8 failed, 22 passed)
question: The last 2 edits left the test result unchanged (last result: 8 failed, 22 passed). Should a person look at unitcraft/format.py, or should the agent keep going?

父级得到的只有交接内容,没有别的。子级的结束状态是 no_progress at step 5 of 16,它保留的记录长达 5408 个字符。父级读到的交接内容是 436 个字符。

交接内容中“已修改文件”一行为 none,“编辑失败”一行为 2。两次编辑都指向 unitcraft/format.py,但该文件的字节没有变化,所以“已修改”一行没有列出它,而失败计数表明这些编辑没有生效。

以词元计,同样的差距是 1803 对 146。这些是用第 5 章中的规则做出的估算,不是模型分词器给出的计数,本章只用它们来比较大小。

交接内容的各行按固定顺序排列:子级的 ID、结束方式、已用步数与预算、尝试过的工具及次数、读过的文件、修改过的文件、编辑失败的次数,然后是中止原因和问题。完成任务的子级给出的是摘要,而不是中止原因和问题。已完成的子级会被计数,但它们的摘要不会被打印,所以这一行不出现在任何输出中。该示例会检查议题的文本没有原样出现在交接内容中,这样父级依据的是子级做了什么,而不是任务的措辞。

父级如何处理交接内容由父级自行选择,本章不做脚本化。本章的代码只构建了一种选择:父级把问题交给维护者。同样的接线也可以启动第二个子级,给它一份更窄的任务说明,其中点出交接内容列出的某个文件,或者仅凭这些字段作答。没有示例构建这两种做法。它们都不会让子级的记录进入父级的上下文,而这正是设置交接内容的原因。

子级自己的记录会被保留,第 11 章追踪的正是这份记录。父级不会打开它。

**工作准则:**向父级返回子级的 ID、结束方式、各项计数、涉及的文件,以及它的中止原因或摘要,不要多返回别的内容。把完整记录留在子级自己的记录中,供追踪读取。

这部分尚未翻译,因此以英文显示。

A stop handed back

The T-02 run ends stuck, and a stop alone doesn't tell the maintainer what to decide. Chapter 2 gave each stop a name and a reason, but someone still has to choose between taking the task from the agent, giving it more steps and trying another approach. That someone is the maintainer who filed the issue. So a stop that the harness can't resolve becomes a handoff to a parent, and the parent asks the maintainer.

The alternative is to log the stop and drop the run. That keeps the parent's context small, and it leaves the decision with nobody. The handoff keeps the decision with the maintainer, and costs the parent one short step in the runs below.

For the three stops that end a child stuck, step_cap, repeated_call and no_progress, the harness builds the handoff from the run's own record: its trace, its streak of repeated calls and its test counts. The stop reason, the counts and the question all come from that record. No model writes them, so a replayed run gives the same handoff every time. The parent is a loop of its own, with a small step budget and two tools, ask_user and finish. In a live design the parent would be a model that words the question to the maintainer. Here a scripted parent passes the question on exactly as the handoff gives it, so the run shows the wiring and not a model's judgement.

A child that ends some other way is not handed back. A run that ends as no_tool_call, for example, gives a handoff with no stop reason and no question, so the parent has nothing to put to the person. No run here ended that way (0 of the runs), so no example shows it.

Run every task-set run as a child under both stop rules. Each child that ends stuck goes to a parent, which asks the person its question.

handoff-on-stuck.py Python 3.12.15 · pydantic 2.14.3 通过
"""The 20 task-set runs as children, with both stop rules. Each child that ended stuck is handed to a parent, whichputs the stop's question to the person with ask_user. Counts only: how the children ended, and what the parents got."""from collections import Counterfrom lib.loop import Stopsfrom lib.models import ScriptedModelfrom lib.subagents import STUCK, asks, characters, hand_to_parent, task_set_childrenfrom lib.trace import IdCounterRULES = Stops(repeat_limit=3, no_progress_limit=2)ENDS = ("finish", "ask_user", "step_cap", "repeated_call", "no_progress", "no_tool_call")children = task_set_children(RULES)stuck = [(task, h, transcript) for task, h, _, transcript in children if h.stuck]ends = Counter(h.ended for _, h, _, _ in children)print("child runs:", len(children))for ended in ENDS:    print(f"  {ended}: {ends[ended]}")print("done with the rules:", sum(1 for _, _, ok, _ in children if ok))print("ended with finish or ask_user:", sum(1 for _, h, _, _ in children if h.ended in ("finish", "ask_user")))print("done among the handed back:", sum(1 for _, h, ok, _ in children if h.stuck and ok))print("handed back to a parent:", len(stuck))handed = Counter(h.ended for _, h, _ in stuck)for ended in STUCK:    print(f"  {ended}: {handed[ended]}")print("handed back, tasks:", ", ".join(task.id for task, _, _ in stuck))parent_ids = IdCounter()  # the parents number their runs apart, so the children's run IDs stay the recording'sparents = [(h, hand_to_parent(ScriptedModel([asks(h.question)]), h, parent_ids)) for _, h, _ in stuck]print("parent asked the person, with the handoff's question:",      sum(1 for h, r in parents if r.ended == "ask_user" and r.question == h.question))print("child steps, all handed back:", sum(h.steps for _, h, _ in stuck))print("parent steps, all handed back:", sum(r.steps for _, r in parents))print("child transcripts, characters:", sum(characters(t) for _, _, t in stuck))print("handoffs the parents read, characters:", sum(len(h.text) for _, h, _ in stuck))print("saved by the handoffs, characters:",      sum(characters(t) for _, _, t in stuck) - sum(len(h.text) for _, h, _ in stuck))assert all(h.stop and h.question for _, h, _ in stuck), "every stuck child gives a stop reason and a question"assert not any(ok for _, h, ok, _ in children if h.stuck), "a done task must not be handed back"
输出python handoff-on-stuck.py
child runs: 20
  finish: 6
  ask_user: 1
  step_cap: 1
  repeated_call: 1
  no_progress: 11
  no_tool_call: 0
done with the rules: 5
ended with finish or ask_user: 7
done among the handed back: 0
handed back to a parent: 13
  step_cap: 1
  repeated_call: 1
  no_progress: 11
handed back, tasks: T-01, T-02, T-03, T-06, T-10, T-11, T-12, T-13, T-15, T-16, T-17, T-19, T-20
parent asked the person, with the handoff's question: 13
child steps, all handed back: 83
parent steps, all handed back: 13
child transcripts, characters: 95252
handoffs the parents read, characters: 5874
saved by the handoffs, characters: 89378

None of the runs a stop handed back was done. The rules hand back 13 of the 20 runs, and none of those is done. Each one reaches a parent, and the parents put the question to the person 13 times, once for each. The stop that comes up most is no_progress, the same stop T-02 ended on.

The stop rules cost no finished work. 7 runs ended with finish (6) or with ask_user (1), and all 5 done runs are among them, because none of the handed-back runs was done. The other runs in that group are not done, and none of them ended on a stop rule, so no handoff covers them. A run that ends with finish or ask_user never reaches a parent under these rules, whether or not it was right to end there.

The handed-back runs are T-01, T-02, T-03, T-06, T-10, T-11, T-12, T-13, T-15, T-16, T-17, T-19, T-20. Between them their children took 83 steps, and their parents took 13 steps, one each, the call that asks the person.

The children's transcripts held 95252 characters, and the parents read 5874 characters of handoff between them. Every handed-back child is checked to give a stop reason and a question. That checks the shape of the handoff, and it doesn't check that the question is the right one to ask.

The question for the T-02 run ends: "Should a person look at unitcraft/format.py, or should the agent keep going?" It names the file that both failed edits named, so the maintainer knows where to look without opening the transcript. The handoff's files changed line says no file changed, so the question points at a file the edits named but never changed.

One caveat, said once: a handoff says that a run stopped and why. It doesn't say the task could not be finished. This book counts a handed-back run as not done, as the example does.

Working rule: Hand every stuck stop (step_cap, repeated_call, no_progress) to a person as a question, with its stop reason, its counts and its files. A parent works from the handoff. The child's record stays with the child, where a trace can read it.

这部分尚未翻译,因此以英文显示。

Two designs, and whose numbers

Which design a split should use depends on what the parent has to do with the child's result. Two designs are in wide use, and they differ in what happens to the conversation. In this chapter's design, the child returns a result and stops. In an SDK handoff, the conversation moves to another agent. The SDK uses the word handoff for the second design, which is not this chapter's meaning of it. Nothing in this section was run here, and its figures belong to their sources.

The OpenAI Agents SDK's documentation on handoffs, for SDK 0.24.0 as fetched in November 2026, describes the second. A handoff is a tool the model calls, named after its target; its example is transfer_to_refund_agent. The receiving agent sees the whole earlier conversation unless an input filter changes what it sees. For a specialist that should return a result without taking over the conversation, the same page recommends wrapping the agent as a tool, with Agent.as_tool. Its nested history, which summarizes the earlier conversation, is an opt-in beta and off by default.

The Claude Code subagent documentation, as fetched in November 2026, describes the first kind. A subagent has a limit on agentic turns, which this book would call steps, set as maxTurns. When a subagent reaches the limit, its output comes back marked partial, and the parent can resume it. The page says the partial marking "requires Claude Code v2.1.246 or later"; nothing here was run at that version. The built-in subagents inherit the parent's permission rules, and most run with a restricted tool set. A custom subagent has its own tools and independent permissions. The shape matches this chapter's budget, with the handoff playing the part of the partial result.

The measurement this section quotes comes from the company that built the system it measures. In June 2025, Anthropic's account of its research system reported that "a multi-agent system with Claude Opus 4 as the lead agent and Claude Sonnet 4 subagents outperformed single-agent Claude Opus 4 by 90.2% on our internal research eval." That comparison sets a multi-agent system against one agent, with a different model in the subagents, on an eval whose metric the post doesn't name. The post says its evaluations favor breadth-first queries, where many independent directions can be pursued at once.

The same post gives a second measurement, on the BrowseComp evaluation: "token usage by itself explains 80% of the variance". It says that multi-agent systems work mainly because they spend enough tokens to solve the problem. Its own data put agents at about four times a chat's tokens and multi-agent systems at about fifteen times. It also says that some domains where agents must share one context, or depend on each other in many ways, are not a good fit, and that its lead agent ran subagents in batches, waiting for each set to finish. A split isn't parallel because it has several agents.

A widely read argument against splitting comes from Walden Yan's essay "Don't Build Multi-Agents", published by Cognition in 2025. Its first principle is "Share context, and share full agent traces, not just individual messages." The handoff breaks it: it passes a child's fields and its summary or stop, and never its full trace. The essay's second principle is that actions carry implicit decisions, so agents working apart can act on conflicting ones. It's an essay, one practitioner's judgement.

The essay's simplest way to follow its principles is one linear agent with a continuous context. For very long tasks it proposes a model that compresses the history into key details, events and decisions, and it says that this is hard to get right. That is close to chapter 5's compaction and to this handoff. The essay also accepts a subagent that answers a well-defined question, usually without writing any code.

This chapter's example child does write code: T-02's child edits unitcraft/format.py, which is the case the essay argues against. Here one child edits and the parent doesn't edit that file, so the conflict the second principle describes doesn't arise in these examples. The child's reasons for its edits still don't reach the parent, and the essay's objection to that stands. The book builds the child anyway, because the T-02 case needs a child that can change the library. The split pays when a result can be used without the reasons behind it, which is the case the product documentation describes.

We recommend a child for a side task whose result stands alone, and one loop for work where the parent and the child would edit the same files.

Working rule: Use a child when its result can be used without its reasons: a fact, a list of files, or a stop with its question. Keep one loop when the parent must act on the files the child would edit. This book doesn't measure a split against one loop, so the rule rests on the argument above, not on a measurement.

生产环境中会出什么问题

拆分工作会带来单个循环不会有的故障。第一种是预算的单位不对。按秒计的预算,会因为测试运行得慢而停掉子智能体,就像因为循环而停掉它一样容易;而且回放无法重现这种停止,因为同一次运行每次耗时都不同。这就是本章按模型调用次数计数、而不按时间计数的原因。

第二种是父智能体替人做决定。handoff-on-stuck 中的父智能体,其工具里既有 finish,也有 ask_user,所以自以为知道答案的模型可以不问任何人就结束一次卡住的运行。在生产环境中,接手停止信号的父智能体应当只具备提问的手段,而传到它这里的每一次停止,都应当以向维护者提出一个问题作为结束。

第三种是父智能体无法调和的拆分。两个编辑同一文件的子智能体,各自都可能交回一份干净的摘要,之后父智能体必须打开两份记录,才能看出它们的编辑相互矛盾。交接本身不会显示这种矛盾,这正是反方观点的要点。

第四种是不显现的成本。子智能体消耗的词元记在它自己名下,而供应商给出的数字表明,多智能体系统消耗的词元约为一次聊天的十五倍。应当像第 11 章那样,把每个子智能体的词元记录在它自己的记录上,否则拆分的成本永远不会进入父智能体的计数。

工作准则: 以模型调用次数计算子智能体的预算,只给它的父智能体提问的手段,并在合并之前,对照其他子智能体改动过的文件,检查每次拆分所做的编辑。把每个子智能体的词元记录在它自己的记录上,使拆分的成本在花费之处计入。

本章做出的决定

对维护者来说,变化在于 T-02 停止现在是以一个关于某个文件的问题送达的。以前它只以一份日志结束,维护者必须自己去读,也没有附带任何待做的决定。按这一设计,它会连同相关文件和一个指明应查看哪个文件的问题一起送到维护者手中。下面的每项选择都可以更改,每项也都会改变运行出错时维护者看到的内容。前三项决定子代理可以花费什么、返回什么,以及停止的原因从哪里来。后三项决定什么时候值得拆出一个子代理,以及子代理无法完成时由谁来回答。

  • 子代理的预算只计算它自己的模型调用,父代理的计数不包含这些调用。
  • 子代理返回各个字段,以及它的摘要或停止信息,而不返回它的对话记录。对话记录留在它自己的记录中。
  • 停止的原因、计数和问题来自这次运行的记录,由代码生成,因此重放会得到相同的交接内容。
  • 卡住的子代理交给一个步数预算很小的父代理,由它通过 ask_user 向提交该问题的维护者提问。
  • 子代理适用于结果可以独立成立的旁支任务。父代理和子代理要编辑相同文件的工作,仍留在同一个循环中。
  • 以问题的形式送达某个人的停止,比以日志结尾的停止更有用。第 12 章讨论另一种情形:拆分和代理都不值得其成本。

本章,在本版次中

版次 2026.11.2示例于 2026年11月20日 通过本章 4个示例 个示例中有 4 个通过

译自英文版图书,版次 2026.11.2。

引用本节

格式

每种格式均引用英文原书,本译本以其为准。

每种格式都会注明该版次。其链接会打开当前版次,节标题处会说明该节文本最后一次修改的时间。访问日期和检索日期即为您打开此页的当天。

本页内容

第 8 章,共 12 章: 把子任务交给子循环,再交回一次停止

  1. A child is a loop with its own budget
  2. 包含四个部分的任务说明
  3. 返回什么
  4. A stop handed back
  5. Two designs, and whose numbers
  6. 生产环境中会出什么问题
  7. 本章做出的决定

返回顶部全部章节