Skip to main content

Creating a subagent

TL;DR

  • Custom sub-agents extend Claude's capabilities by specializing in specific tasks, configured through Markdown files with YAML front matter.
  • The /agents command in Claude Code provides an easy way to generate and manage these sub-agents, which can be tailored for project-specific or shared use.
  • Key configurations include setting a clear description for activation, assigning relevant tools, choosing an appropriate AI model, and defining a precise system prompt for task execution.

Takeaways

  • Create custom sub-agents using the /agents command in Claude Code, choosing between project-specific or shared scope.
  • Sub-agents are configured via Markdown files with YAML front matter, which define their behavior and parameters.
  • Leverage Claude Code's automatic generation feature to quickly set up a sub-agent's initial name, description, and system prompt.
  • Carefully customize the tools a sub-agent can access; for instance, a code reviewer might only need execution tools to identify changes, not editing tools.
  • Select the appropriate Anthropic model: Haiku for speed, Opus for complex analysis, Sonnet for balanced tasks, or Inherit to match the main conversation's model.
  • The sub-agent's description is critical for Claude's delegation decisions; make it a single line, add "proactively" for more frequent use, and include example conversations.
  • The system prompt in the file body guides the sub-agent on how to complete its task and format its output.
  • Always test your sub-agents after creation, and if unexpected behavior occurs, refine the description with more specific examples to improve Claude's delegation accuracy.

Vocabulary

sub-agent — A specialized AI agent designed to perform specific tasks, extending the capabilities of a main AI. YAML front matter — A block of YAML key-value pairs at the beginning of a Markdown file, used for configuration metadata. system prompt — The initial instructions given to an AI model to define its role, task, and desired output format. Anthropic models (Haiku, Sonnet, Opus) — Different large language models developed by Anthropic, each optimized for varying levels of speed, complexity, and cost. delegation — The process by which a main AI decides to hand off a specific task to a specialized sub-agent based on its description and context. Claude Code — The integrated development environment or interface used to create, manage, and interact with Claude sub-agents.

Transcript

In the previous video, we covered what sub aents are and how they work. Claude Code includes built-in sub aents, but you can also create your own sub aents that specialize in certain tasks. Custom sub aents are markdown files with YAML front matter. These markdown files contain configuration that helps claude understand when to use the sub aent and provides directions to the sub aent itself. Now, the easiest way to create a sub agent is with the / agents command. 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. 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 will use your input to generate a name, description, and system prompt for the sub agent. 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. 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. The description controls when cla decides to use the sub aent. 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. You can also add example conversations to help Claude understand when it should be using the sub agent. 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. Sonnet, opus, haiku or inherit. Use haiku for fast tasks. Opus for complex analysis and sonnet if you need something between the two. Inherit will use the same model as your main conversation. 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. If the sub agent isn't being used when you expect, check your description. Adding more specific examples helps Claude understand when to delegate.

Feedback / ReportSpotted an issue or have an improvement idea?