Create

Sign in to ReadmeX

or

How we handle your data: Privacy Policy

ReadmeX

Your AI reading companion

What would you like to catch up on?

Pick a briefing, or ask your own question.

Your community and people briefings stay personal to you.

Story

How Four Coding Agents Delegate to Subagents

AI summary

A source-code analysis compares how Codex, Gemini CLI, OpenCode and ZCode delegate work from a main agent to subagents. It describes a shared pattern in which a subagent runs with an isolated context and narrowed tool permissions, performs its own agent loop, and returns only a final report. The article also examines each project’s approaches to session tracking, recursion controls, messaging and background execution. It is based on November 2026 source snapshots and cites 17 file-and-line locations across the four codebases.

In-depth insight

The analysis shows how these frameworks manage context isolation, delegation, recursion and permission boundaries. It also highlights trade-offs involved in returning one final report to the parent context, limiting subagent permissions and maintaining reliable subagent state.

系列第九篇。前八篇我们拆过四大 Agent 的主循环、token 压缩、防跑偏、上下文组装、工具/MCP/Skill、系统提示词、永久记忆和授权审批。今天拆最后一个大件:主 Agent 与子 Agent 的协作机制——皇帝如何派钦差大臣出去办案,又不让满朝的奏折把金銮殿塞爆。 本文基于 2026-11 的源码快照:openai/codex(Rust)、google-gemini/gemini-cli(TypeScript)、anomalyco/opencode(TypeScript + Effect)、zai-org/ZCode(TypeScript),全部逐文件读源码并抽查核验 17 处 file:line。


一、为什么皇帝需要钦差?

先说人话:主 Agent(也就是你在终端里对话的那个 AI)有一个致命软肋——上下文窗口是有限的。

让它自己干"研究整个代码库再写篇报告"这种活,它得把几十个文件的原文读进上下文。读完第 20 个文件,前面读的内容已经被压缩/丢弃,而且每读一个文件,最贵的输入 token 就涨一截。让老板亲自跑腿,跑着跑着老板就失忆了。

解法只有一个:外包。皇帝(主 Agent)把任务写成一道圣旨(prompt),派一位钦差大臣(子 Agent)出京。钦差带着自己的幕僚班子(独立的上下文和工具面)在外面跑几十轮、读一百个文件,中间过程一个字都不进宫,回京时只递一份奏折(最终报告)。皇帝的金銮殿(上下文)始终干净。

#Codex#Gemini CLI#OpenCode#ZCode

Original · 34 min readRead the original
Group chat

No comments yet. Start the conversation.