Knowledges 9 分钟

VB-04:Subagents 从入门到深入——原来我一直理解错了!

我以为自己已经搞懂了 Claude Code 的 Subagents,结果完全理解错了 😅 本文深入聊聊 Context Management、Context Rot,以及我如何把 subagent 团队重组为“Context Collector Army”,终于搞定了卡了近 3 天的测试套件。

其他语言: English · Tiếng Việt · 한국어 · 日本語

这第 4 篇本来要讲 Claude Code 的“Commands”,但我发现自己在使用 Subagents 的过程中犯了不少错误,所以决定把 Commands 那篇挪到第 05 篇 😅

这篇文章讲什么?

  • Claude Code Subagents 概览
  • Subagents 能带来的好处
  • 几个相当有用的 repo
  • Time to Deep-Dive!
  • 我读到的研究以及如何应用
  • 重建 subagent 团队及其成果
  • 老规矩:文中散落着几个“野路子小技巧” 😂
Claude Code Subagents:从入门到深入

Claude Code Subagents 概览

如何创建 Subagent

用 CC 的 /agents 命令最快完成初始化,然后打开 MD 文件按自己的需要定制。

text# 打开 sub agents 界面
/agents

# 选择 "Create New Agent"
# 选择 project-level 或 user-level

理解“委派”(delegation)

默认情况下,Claude Code 本身就是 Main Agent,会执行所有事情。创建 subagents 之后,按照 Claude 文档的说法,CC 会在需要时“智能地”自动召唤相关 agent 并委派任务(智能到你几乎看不到它召唤 subagent,main agent 全都自己搞定,就像在说:“这还不简单,我来!” 😂)

所以,想更有效地召唤 subagents,我们有 2 种方法:

1/ 自动(比默认那个“智能”更靠谱):

在 CLAUDE.md 中加入以下指令,提醒 CC 它手下还有一支队伍:

text...
During the implementation process, you will delegate tasks to the following subagents based on their expertise and capabilities

- Use `planner-researcher` agent to plan for the implementation plan.
- Use `database-admin` agent to run tests and analyze the summary report.
- Use `tester` agent to run tests and analyze the summary report.
- Use `debugger` agent to collect logs in server or github actions to analyze the summary report.
- Use `code-reviewer` agent to review code.
- Use `docs-manager` agent to update docs in `./docs` directory if any.
...

2/ 纯手动(Manual): 直接写进 prompt,或者使用 Command(user prompt)。

接下来要知道,召唤 subagents 有 2 种方式:“顺序”(Chaining) 和 “并行”(Parallel)。

Chaining Sub-Agents

text# 复杂工作流示例
> 先用 code-analyzer subagent 找出性能问题,
> 再用 optimizer subagent 修复它们

Parallel Execution

text# 相互竞争的方案
> 并行部署 2 个 subagents:
> Subagent 1 - Incremental Modernizer:设计渐进式迁移策略
> Subagent 2 - Complete Rewrite Advocate:提出全栈现代化方案

注意,每个 sub-agent 都有:

独立的 Context window

不受主对话影响

自定义 System prompt

为特定角色“训练”

独立的 Tool 权限

只允许使用必要的工具

深度专业化

100% 专注于领域专长

Subagents 能带来的好处

好处含义
Context Preservation(保留上下文)不用每次切换任务都从头解释,每个 sub-agent 保留自己的 context。主对话始终干净、聚焦。
Specialized Expertise(专业化)一个针对安全 fine-tune 过的 sub-agent,在审查代码安全性时总会比通用 AI 更好。成功率明显更高。
Reusability & Team Sharing(可复用)一次创建,永久使用。分享给团队,工作流保持一致。
Parallel Processing(并行处理)不必按顺序 analyze → code → test → review,你可以同时并行运行多个 sub-agents。

几个相当有用的 repo

可供参考的 subagents 列表(参考它们的 description 和 workflow 来定制你自己的 agents 就好,别原封不动地照抄):

wshobson/agentsgithub.com

Claude Sub-Agent Spec Workflow System: 这里面也有很多不错的工作流,我搭建自己的 subagents 时也参考了它。

zhsama/claude-sub-agentgithub.com

Time to Deep-Dive!

有没有兄弟和我遇到过一样的情况:手握一支“神级”AI agents 团队,什么都能做,但真用起来……时灵时不灵?

有时它表现好到烧 token 的速度堪比火箭,有时结果却不如预期,或者陷入无限轮回,bug 怎么修都修不完?

最近几天我就陷入了这种处境,感觉用 Subagents 也没什么效果,只觉得费 token、费时间……而且还在 Test Suite 上卡了快 3 天没解决。

锁定章节: Context Management 才是关键!

锁定章节: 上下文管理(Context Management):用文件系统作为 Memory(具体来说是 Markdown 文件)

锁定章节: Context Rot

锁定章节: 1. Prioritize Semantic Proximity

锁定章节: 2. Minimize Distractors

锁定章节: 3. Structure Over Coherence

锁定章节: 4. Length Optimization

锁定章节: 我对 sub-agents 的理解也错了!

锁定章节: Sub Agents 最初的问题

锁定章节: 慢吞吞

锁定章节: 狂吃 token

锁定章节: 结果并没更好

锁定章节: Claude Code 为什么需要 Sub Agents?

锁定章节: 常见错误:用 Sub Agents 来做 Implementation

锁定章节: 给每个 agent 配对的工具

锁定章节: Best Practice:Sub Agents 是负责收集有用信息的大军!(Context Collector Army)

锁定章节: 这是我重新组织 subagent 团队的方式

锁定章节: 1. Explore

锁定章节: 2. Planning

锁定章节: 3. Execute

锁定章节: 野路子小技巧:Double Escape——神级操作 =))

锁定章节: 结果

锁定章节: Key takeaways

锁定章节: 写在最后!

锁定章节: 参考资料

其余内容仅限 Knowledges 会员

另有 25 个章节(约 6 分钟)仅限会员

你正在阅读约前三分之一。Knowledges、组合和社区方案可阅读付费文章全文;Zuey AI 方案仅可阅读预览。