返回播客Claude
Claude Code subagents
创建子代理
In the previous video, we covered what sub aents are and how they work.
在上一期视频中,我们介绍了子代理是什么以及它的工作原理。
Cloud code includes built-in sub aents, but you can also create your own sub aents that specialize in certain tasks.
Claude Code 内置了子代理,但你也可以创建专注于特定任务的自定义子代理。
Custom sub aents are markdown files with YAML front matter.
自定义子代理是含 YAML 元数据的 markdown 文件。
These markdown files contain configuration that helps claude understand when to use the sub aent and provides directions to the sub aent itself.
这些 markdown 文件包含配置信息,帮助 Claude 判断何时使用该子代理,并向子代理本身提供指导。
Now, the easiest way to create a sub agent is with the / agents command.
创建子代理最简单的方式,就是使用 /agents 命令。
This panel is the main interface for managing your sub aents.
这个面板是管理子代理的主界面。
Once here, select create new agent.
进入后,选择创建新代理。
You'll then be asked if you want to create a sub aent for the current project or a sub aent that will be shared between all the projects on your machine.
你会被询问是为当前项目创建子代理,还是创建一个在机器上所有项目间共享的子代理。
Next, you can create a sub aent manually, but we recommend using claw code to automatically generate it for you.
你可以手动创建子代理,但我们建议使用 Claude Code 自动生成。
Our first sub aent will be a code reviewer.
第一个子代理将是代码审查器。
I'll ask Claude to make a sub agent that reviews code quality and security issues.
我会让 Claude 创建一个审查代码质量和安全问题的子代理。
Claude will use your input to generate a name, description, and system prompt for the sub agent.
Claude 会根据你的输入,为子代理生成名称、描述和系统提示。
We'll see those in a moment, but before we do, we get the opportunity to customize the tools that this sub agent has access to.
稍后我们会看到这些内容,但在此之前,你可以自定义该子代理能访问的工具。
Now, given that our sub agent is only responsible for reviewing code, you might decide to disallow tools for editing, but I'll leave an execution to allow the sub agent to more easily identify pending changes.
由于子代理只负责审查代码,你可能会禁用编辑工具,但我保留了执行权限,让子代理更容易识别待处理的更改。
Next, you'll be prompted to select the model that powers this sub agent.
接下来,你会被提示选择为该子代理提供动力的模型。
And finally, a color.
最后,选择一个颜色。
This color is used in the UI to help you better identify the sub agent.
这个颜色在 UI 中帮助你更容易识别该子代理。
And it also just adds a little bit of personal flare.
同时也增添一点个人风格。
Now at the summary window, we see that the sub aent config file will be saved into my current project at this path.
在摘要窗口,我们可以看到子代理配置文件将保存到当前项目的路径。
Now let's open up that file to get a better idea of what's going on.
现在来打开这个文件,看看里面有什么。
The name field is a unique identifier.
名称字段是唯一标识符。
This is how you reference the sub agent either by asking claw directly or using at agent code quality reviewer in your message.
你可以直接让 Claude 调用它,或在消息中使用 @agent code-quality-reviewer 来引用该子代理。
The description controls when cla decides to use the sub aent.
描述字段控制 Claude 何时使用该子代理。
The description must be on a single line.
描述必须写在单独一行。
Notice that there are escaped new line characters in there.
注意其中有转义的换行符。
If you want Claude to use the sub agent automatically more often, add in the word proactively to the description.
如果你希望 Claude 更频繁地自动使用子代理,在描述中加入 proactively 这个词。
You can also add example conversations to help Claude understand when it should be using the sub agent.
你还可以添加示例对话,帮助 Claude 理解何时应使用该子代理。
The tools field lists which tools the sub agent can access.
工具字段列出了子代理可以访问的工具。
The list of tools will match the tools we granted access to during the agent generation, but you can further edit the list of tools in this file if you want.
工具列表与代理生成时授权的工具一致,但你也可以在这个文件里进一步编辑工具列表。
The model field specifies which claw model to use.
模型字段指定使用哪个 Claude 模型。
Sonnet, opus, haiku or inherit.
Sonnet、Opus、Haiku 或 inherit。
Use haiku for fast tasks.
快速任务用 Haiku。
Opus for complex analysis and sonnet if you need something between the two.
复杂分析用 Opus,介于两者之间用 Sonnet。
Inherit will use the same model as your main conversation.
inherit 会使用与主对话相同的模型。
The body of the file contains the system prompt that is given to the sub aent.
文件正文包含提供给子代理的系统提示。
The system prompt will provide guidance to the sub agent, helping it understand how to complete its task and how it should return information back to the main agent.
系统提示为子代理提供指导,帮助它理解如何完成任务,以及如何将信息返回给主代理。
Now for the fun part.
现在进入有趣的部分。
After creating your sub agent, test it by making some code changes and asking claw to review them.
创建子代理后,做一些代码改动,让 Claude 审查,来测试它。
If the sub agent isn't being used when you expect, check your description.
如果子代理没有在预期时触发,检查你的描述字段。
Adding more specific examples helps Claude understand when to delegate.
添加更具体的示例,帮助 Claude 理解何时应该进行委托。