Codex Subagent :如何隔离 Coding Agent 的上下文
Subagent: 管理上下文的一种方法
阅读笔记
- 直接对话的Agent, 是Main Agent(全部上下文). Main Agent 在适合的时间创建 Sub Agent
- 技术本质: 打开一个新的
messages数组(上下文) - Main Agent 没有 Sub Agent 执行过程的上下文, 只有结论
- 探索搜索 / 独立性子任务 / 独立审查意见
- Codex 内置的 Subagent : default / worker(生成代码) / explorer(定位代码). 默认同时最多有6个
- Subagent 只能有一层, 不能无羡套.
- 可以类似自定义 Skill 进行自定义 Subagent (也是只有自定义工作流的时候, 才需要自定义 Subagent, 比如每次写完代码的独立审查)
name = "reviewer"
description = "PR reviewer focused on correctness and security."
developer_instructions = """
Review code like an owner.
Prioritize correctness, security, and missing test coverage.
"""
- Subagent fields
- required: name; description(when to use); developer_instructions(subagent behavior)
- optional: nickname_candidates(many same subagents name); model; model_reasoning_effort;
- project:
.codex/agent; global:~/.codex/agents/ - configuration: agents_max_threads(6);agents_max_depth(1);agents_job_max_runtime_seconds(1800)
- how to use: ask directly;
AGENT.md; skill instructions; - why help: noisy intermediate output make context large, model limit; context pollution; context rot;
- prompt: spawn two agents; delegate this work in parallel; use one agent per point;
- good subagent prompt: explain how to work; whether should wait; what return;
- Subagent的继承: explicit value ⇒ [agent] default(只要不设置) ⇒ parent’s value
Subagent 的概念
Subagent是一个执行具体任务, 返回具体结果, 干净上下文的agent.
主要用来解决Context pollution; Context rot;
Sub Agent 的使用
什么情况下使用 / 如何使用 / 实践
使用背景:
- 当Agent需要多个任务并行开发时;
- 当Agent执行某个具体的任务, 只需要知道结果, 又不希望上下文影响这个任务时.
AI提供的使用场景:
- 主 Agent 搜索过程太长,开始忘记原始要求;
- 大量测试、日志把上下文塞满;
- 一个 PR 需要从多个独立角度审查;
- 多个模块可以并行调查;
- 想让一个独立 Agent 对主方案提出反对意见。
如何使用: 直接派发 / 项目中的agent.md 派发 / skill 进行派发
实践:
I would like to review the following points on the current PR (this branch vs main). Spawn one agent per point, wait for all of them, and summarize the result for each point.
1. Security issue
2. Code quality
3. Bugs
4. Race
5. Test flakiness
6. Maintainability of the code
!image.png
!image.png
参考资料
https://learn.chatgpt.com/docs/agent-configuration/subagents?surface=app
https://labuladong.online/zh/ai-coding/basics/sub-agent/
https://www.youtube.com/watch?v=1XB22Jx2YXI
https://github.com/VoltAgent/awesome-codex-subagents
疑问延伸:
- VoltAgent https://github.com/VoltAgent/voltagent
- Mastra https://github.com/mastra-ai/mastra
- 思考: 能否用Mastra做一个PR审查平台, 或者能否监控AI使用效率Eval.

浙公网安备 33010602011771号