把子任務交給子迴圈,再把停止交回去

閱讀約需 52 分鐘 分鐘,7 節 中共有 4 個範例。

版次 2026.11.2 範例於 2026年11 月 20 通過測試 64 個範例中有 64 個通過 變更內容

由 AI 翻譯。經人工以英文驗證。 我們如何製作書籍

譯自英文書,版次 2026.11.2。 閱讀原文

讀完本章,你就能把一個子任務交給擁有自己步數預算的子迴圈,並取回一份摘要和一個停止原因,而不是子迴圈的整份對話紀錄。你也會讓停止傳達給真人:被停止規則判定為卡住而結束的執行,會交給父迴圈,由父迴圈把停止所帶的問題提給提出該 issue 的維護者。

以案例中的任務 T-02 為例:在小型單位函式庫 unitcraft 中,負數金額會遺失千分位分隔符號,維護代理必須修正這個問題。這次執行讀取了 unitcraft/format.py,並對它做了兩次編輯。兩次編輯都失敗,因為它們所指定的舊文字並未以該寫法出現在檔案中,所以測試結果維持原狀。第 2 章說明,一旦規則判定沒有進展,這樣的執行就會結束。在 task-set 的錄製中,v1 版 harness 執行 T-02 直到步數上限。套用第 2 章的兩條停止規則重播時,相同的回覆會讓這次執行以 no_progress at step 5 of 16 結束。停止發揮了它的作用,但交給維護者的只有一份日誌,沒有說明代理當時想做什麼。必須有人讀完日誌才能知道。

規模是第二個問題。要找出捨入規則位於何處,需要讀取多個檔案,而代理讀過的每個檔案,都會留在它下一步所攜帶的上下文中。這類附帶任務,最好放在獨立的上下文中完成,只回傳父層所需的內容。Anthropic 的子代理文件針對其產品描述了這種分工:子代理在自己的上下文中完成工作,只回傳摘要。

本章為這個案例建立子迴圈。子迴圈就是同一個迴圈,只是擁有自己的預算。在本書中,交接(handoff)是指執行結束時向真人回問一個問題,而不是完成任務:可能是模型自己呼叫 ask_user,也可能是停止之後由 harness 產生的結構化交接,其中包含停止原因、該次執行嘗試過的事項,以及一個問題。子迴圈把它的交接回傳給父迴圈:它如何結束、嘗試過什麼、讀取並修改了哪些檔案,以及它的摘要或它停下時所面對的問題。它絕不回傳自己的對話紀錄。結束子迴圈的停止,會以問題的形式傳達給維護者。

此部分尚未翻譯,因此以英文呈現。

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.

包含四個部分的任務說明

任務說明(brief)就是子代理收到的工作內容。在這裡,子代理只拿到議題(issue)文字: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 個字元。

交接內容中「files changed」一行的值是 none,「edits failed」一行的值是 2。兩次編輯都指向 unitcraft/format.py,但該檔案的位元組並沒有改變,所以已變更那一行不列出它,而失敗次數則說明這些編輯沒有生效。

以 token 來看,同樣的差距是 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.

兩種設計,以及數字屬於誰

拆分應採用哪一種設計,取決於父代理要如何運用子代理的結果。目前廣泛使用的設計有兩種,差別在於對話會發生什麼事。在本章的設計中,子代理回傳結果後即停止。在 SDK 的移交(handoff)中,對話則轉移給另一個代理。SDK 以 handoff 一詞稱呼第二種設計,這與本章對該詞的用法不同。本節的內容沒有在這裡執行過,其中的數字屬於各自的來源。

OpenAI Agents SDK 關於移交的文件(SDK 0.24.0,於 2026 年 11 月擷取)描述的是第二種。移交是模型所呼叫的一個工具,以其目標命名;文件中的範例是 transfer_to_refund_agent。除非以輸入過濾器改變接收端所見的內容,否則接收的代理會看到先前的整段對話。對於只需回傳結果、而不接管對話的專家型代理,同一頁建議以 Agent.as_tool 將代理包裝成工具。其巢狀歷程會將先前的對話摘要化,屬於需自行選擇啟用的 beta 功能,預設為關閉。

Claude Code 子代理文件(於 2026 年 11 月擷取)描述的是第一種。子代理對代理式回合數有上限,本書稱之為步驟,以 maxTurns 設定。子代理達到上限時,其輸出會標示為部分結果並回傳,父代理可以讓它繼續執行。該頁面指出部分結果的標示「requires Claude Code v2.1.246 or later」;這裡沒有在該版本上執行過任何內容。內建的子代理繼承父代理的權限規則,且多數以受限的工具組執行。自訂子代理則有自己的工具與獨立的權限。這個形態與本章的預算相符,只是由移交扮演部分結果的角色。

本節引用的這項量測,出自建構該系統的公司本身。2025 年 6 月,Anthropic 對其研究系統的說明報告:「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.」這項比較是把多代理系統與單一代理相比,子代理使用的模型也不同,且評測所用的指標,該文並未指明。該文指出其評測偏重廣度優先的查詢,這類查詢可以同時追求許多彼此獨立的方向。

同一篇文章還給出第二項量測,出自 BrowseComp 評測:「token usage by itself explains 80% of the variance」。文章說,多代理系統之所以有效,主要是因為花費了足夠的 token 來解決問題。其自身的資料顯示,代理的 token 用量約為一般聊天的四倍,多代理系統則約為十五倍。文章也指出,有些領域要求代理共用同一份脈絡,或彼此之間有許多相依關係,並不適合這種做法;而其主導代理是分批執行子代理,等每一批完成後才繼續。拆分並不會因為有多個代理就成為平行。

一篇廣為流傳的反對拆分論點,出自 Cognition 於 2025 年發表的 Walden Yan 文章〈Don't Build Multi-Agents〉。其第一項原則是「Share context, and share full agent traces, not just individual messages.」移交違反了這項原則:它傳遞的是子代理的欄位以及其摘要或停止狀態,從不傳遞完整的軌跡。該文的第二項原則是,行動帶有隱含的決策,因此各自獨立運作的代理可能依據相互衝突的決策行事。這是一篇文章,是一位實務工作者的判斷。

該文遵循其原則的最簡單方式,是採用一個具有連續脈絡的線性代理。對於非常長的任務,它提出以一個模型將歷程壓縮為關鍵細節、事件與決策,並指出要做好這點很困難。這與第 5 章的壓縮(compaction)以及本章的移交相近。該文也接受這樣的子代理:它回答一個定義明確的問題,且通常不撰寫任何程式碼。

本章範例中的子代理確實會撰寫程式碼:T-02 的子代理編輯 unitcraft/format.py,這正是該文所反對的情況。這裡是由一個子代理進行編輯,而父代理不編輯該檔案,因此第二項原則所描述的衝突在這些範例中不會發生。子代理做出這些編輯的理由仍然不會傳到父代理,該文對此的反對意見依然成立。本書仍然建構這個子代理,因為 T-02 的情況需要一個能修改函式庫的子代理。當結果可以在不附帶其背後理由的情況下使用時,拆分才划算,這正是產品文件所描述的情況。

我們建議將子代理用於結果能獨立成立的旁支任務,而在父代理與子代理會編輯相同檔案的工作上,則使用單一迴圈。

實用原則: 當結果可以在不附帶理由的情況下使用時,才使用子代理:例如一項事實、一份檔案清單,或一個附帶問題的停止。當父代理必須對子代理將編輯的檔案採取行動時,則維持單一迴圈。本書並未量測拆分與單一迴圈的差異,因此這項原則依據的是上述論證,而非量測結果。

此部分尚未翻譯,因此以英文呈現。

What goes wrong in production

Splitting work brings failures that a single loop doesn't have. The first is a budget in the wrong unit. A budget counted in seconds stops a child for a slow test run as readily as for a loop, and a replay can't repeat it, because the same run takes a different time each time. That is why this chapter counts model calls and no time.

The second is a parent that decides for the person. The parent in handoff-on-stuck has finish as well as ask_user among its tools, so a model that thinks it knows the answer can end a stuck run without asking anyone. In production, a parent handed a stop should have only the means to ask, and a stop that reaches it should always end with a question to the maintainer.

The third is a split the parent can't reconcile. Two children that edit the same file can each hand back a clean summary, and the parent then has to open both records to see that their edits disagree. The handoff doesn't show that disagreement by itself, which is the point of the counter-view.

The fourth is cost that doesn't show. A child's tokens are its own, and the vendor's figure puts a multi-agent system at about fifteen times a chat's tokens. Trace each child's tokens on its own record, as chapter 11 does, or the cost of a split never reaches the parent's count.

Working rule: Count a child's budget in model calls, give its parent only the means to ask, and check each split's edits against the files the other children changed before you merge them. Trace each child's tokens on its own record, so a split's cost is counted where it is spent.

本章做出的決定

對維護者來說,改變在於 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. 兩種設計,以及數字屬於誰
  6. What goes wrong in production
  7. 本章做出的決定

返回頂端所有章節