返回播客Claude
Claude Code subagents
如何设计高效的 subagent
Now that you know how to create sub aents, let's look at patterns that lead to effective sub aents.
了解如何创建子智能体之后,来看看哪些模式能打造出高效的子智能体。
First, let's get a better idea of how subit data in the sub aent config file is used.
首先,深入了解子智能体配置文件中的数据是如何被使用的。
Whenever you send a message to the main context window agent, the name and description of each sub aent is included in the system prompt.
每次向主智能体发送消息时,每个子智能体的名称和描述都会被包含在 system prompt 中。
So, if you want to better control when the main agent launches a sub agent automatically, you should modify the name and description.
如果想更好地控制主智能体何时自动启动子智能体,应该修改名称和描述。
Next, remember that when a sub agent is launched, the main agent writes an input prompt.
其次,子智能体启动时,主智能体会写一个输入提示词。
When writing this input prompt, it uses the description as guidance.
主智能体写输入提示词时,会以描述作为指引。
So, if you want to better control when the main agent launches a sub agent automatically, you should modify the name and description.
如果想更好地控制主智能体何时自动启动子智能体,应该修改名称和描述。
Let's consider our review sub aent again.
再来看我们的代码审查子智能体。
Right now, when the main agent runs this sub agent, the sub aent is given an input prompt telling it to use get diff to find the current changes.
目前,主智能体运行这个子智能体时,会给它一个输入提示词,告知它用 get diff 找出当前改动。
If we wanted the main agent to more reliably tell the sub agent exactly which files to review, we would update the description.
如果想让主智能体更可靠地把待审查的文件明确告知子智能体,就需要更新描述。
You must tell the agent precisely which files you want it to review.
必须在描述中精确指定要审查哪些文件。
Now, if we ask claw to run the code reviewer agent, we'll see a different input.
现在,让 Claude 运行代码审查子智能体,就能看到不同的输入内容了。
You can also influence what the main thread tells a sub agent through the description.
还可以通过描述来影响主智能体传给子智能体的内容。
So adding return sources that can be cited to a web search sub aents description causes the main thread to include that instruction when delegating the task.
比如,在网络搜索子智能体的描述中加入返回可引用的来源,主智能体在委派任务时就会把这条指令一并带上。
The most important improvement that you can make is defining an output format in the system prompt.
最重要的改进,是在 system prompt 中定义输出格式。
This creates natural stopping points for the sub aent.
这样能给子智能体创造自然的停止点。
Without a defined output format, sub aents struggle to decide when enough research has been done and they tend to run much much longer than sub agents that are given an output format.
没有定义输出格式时,子智能体难以判断研究何时已经充分,往往会比有输出格式的子智能体运行时间长得多。
When a sub agent discovers a workaround to some issue like solving a dependency issue or finding that a certain command needs particular flags, these details should appear in the summary.
子智能体若发现了某个问题的解决方案,比如解决了依赖问题,或者发现某个命令需要特定参数,这些细节都应当出现在摘要中。
Otherwise, the main thread has to rediscover the same solutions, obstacles encountered, any setup issues, workarounds discovered or environment quirks, commands that needed special flags or configuration, dependencies or imports that cause problems.
否则,主智能体需要重新自己发现这些解决方案:遇到的阻碍、安装问题、找到的变通方法、环境特殊情况、需要特殊参数的命令、引发问题的依赖或导入。
Explicitly asking for obstacle reporting in the output format surfaces this information.
在输出格式中明确要求汇报阻碍,这些信息才能浮现出来。
A readonly sub aent using just glob grap read cannot accidentally modify files.
只使用 glob、grep、read 的只读子智能体,不会意外修改文件。
This constraint clarifies that the sub aents role and prevents unintended side effects.
这个限制明确了子智能体的职责,也防止了意外的副作用。
So think about what sub aents actually needs to do.
想想子智能体实际需要做什么。
If it's just researching it only needs to read files.
如果只是做调研,只需要读文件权限。
So keep it read only.
保持只读模式。
That way it can't accidentally modify anything while exploring.
这样在探索时就不会意外修改任何内容。
A reviewer needs to run get diff to see what changed.
审查子智能体需运行 get diff 查看改动。
So give it bash access, but it still doesn't need to edit files.
赋予 bash 权限即可,但仍然不需要编辑文件。
Only give edit and write to sub aents that should actually change your code, like a styling agent applying CSS updates.
只给真正需要改代码的子智能体授予 edit 和 write,比如应用 CSS 更新的样式子智能体。
This also helps clarify what each sub aent is for when you have several of them.
当有多个子智能体时,这也有助于明确每个子智能体的职责。
So effective sub aents use structured output report obstacles have specific descriptions and limit tool access.
高效的子智能体应使用结构化输出、汇报阻碍、有明确的描述,并限制工具访问权限。