理解 Subagents 的分工模型
说明 Codex subagents 如何并行探索、实现或分析任务,以及什么时候应该拆分给子 Agent。
Codex 可以通过 spawning specialized agents in parallel 来运行 subagent workflows,让它们并发 explore、tackle 或 analyze work。
这篇解释核心概念和取舍。setup、agent configuration 和 examples 见 Subagents。
Subagents 只适合能独立拆分、结果能汇总、写入范围能隔离的任务。不要为了“看起来并行”把主线程下一步马上需要的阻塞工作交给子 agent。
为什么 subagent 工作流有帮助
即便有 large context windows,模型仍然有边界。
如果你把 main conversation,也就是定义 requirements、constraints 和 decisions 的地方,塞满 noisy intermediate output,例如 exploration notes、test logs、stack traces、command output,session 会随着时间变得不稳定。
这通常被描述为:
| 概念 | 含义 |
|---|---|
| Context pollution | 有用信息被 noisy intermediate output 淹没。 |
| Context rot | conversation 填满低相关细节后,performance 下降。 |
背景说明可看 Chroma 关于 context rot 的文章。
Subagent workflows 的作用是把 noisy work 从 main thread 中移走:
- 让 main agent 专注 requirements、decisions 和 final outputs。
- 让 specialized subagents 并行处理 exploration、tests 或 log analysis。
- 让 subagents 返回 summaries,而不是 raw intermediate output。
当工作可以独立并行时,subagent workflows 也可以节省时间。它们还能把 larger-shaped tasks 拆成 bounded pieces,让任务更容易处理。
例如,Codex 可以把 multi-million-token document 的分析拆成小问题,再把提炼后的 takeaways 返回给 main thread。
起步时,优先把 parallel agents 用在 read-heavy tasks,例如 exploration、tests、triage 和 summarization。
parallel write-heavy workflows 要更谨慎,因为多个 agents 同时编辑 code 可能造成 conflicts,并增加 coordination overhead。
核心术语
Codex 在 subagent workflows 中使用几个相关术语:
| 术语 | 含义 |
|---|---|
| Subagent workflow | Codex 运行 parallel agents,并整合它们结果的 workflow。 |
| Subagent | Codex 启动来处理具体 task 的 delegated agent。 |
| Agent thread | 某个 agent 的 CLI thread,可以通过 /agent inspect 和 switch。 |
触发 subagent 工作流
Codex 不会自动 spawn subagents。只有当你明确要求 subagents 或 parallel agent work 时,它才应该使用 subagents。
实际触发方式是直接写清楚,例如:
- "spawn two agents"
- "delegate this work in parallel"
- "use one agent per point"
Subagent workflows 会比类似 single-agent runs 消耗更多 tokens,因为每个 subagent 都会做自己的 model 和 tool work。
好的 subagent prompt 应该说明:
- 如何 divide work。
- Codex 是否应该等待所有 agents 完成后再继续。
- 需要返回什么 summary 或 output。
示例:
请用 parallel subagents review 当前分支。分别启动一个 subagent 检查 security risks,一个检查 test gaps,一个检查 maintainability。等待三个 subagents 全部完成后,按类别汇总 findings,并附 file references。选择模型和推理强度
不同 agents 需要不同 model 和 reasoning settings,但具体模型名称、可用性和价格属于高波动事实,不适合写成长期推荐表。
更稳的选择方法:
- 探索、日志归纳、文件定位这类 read-heavy sidecar task,可以优先选择更快、更省的配置。
- 安全审查、复杂逻辑推理、跨模块设计和高风险变更,需要更强推理和更明确验证。
- 写代码的 subagent 必须有清晰 ownership,避免多个 agent 写同一批文件。
- 需要 pin model 或 reasoning effort 时,把原因写进 agent file 或 prompt,不要只写“用最强模型”。
- 具体模型名称、reasoning effort 支持情况和 pricing 回官方 Models / Config 页面核验。
更高 reasoning effort 会增加 response time 和 token usage,但可能提升复杂任务质量。团队工作流里要把质量、延迟、成本和冲突风险一起评估。
更多细节见:
- Models:https://developers.openai.com/codex/models
- Config basics:https://developers.openai.com/codex/config-basic
- Configuration Reference:https://developers.openai.com/codex/config-reference