Overview
Subagents are created exclusively through the filesystem-based approach by placing markdown files with YAML frontmatter in designated directories. The SDK can then invoke these pre-defined subagents during execution.Benefits of Using Subagents
Context Management
Subagents maintain separate context from the main agent, preventing information overload and keeping interactions focused. This isolation ensures that specialized tasks don’t pollute the main conversation context with irrelevant details. Example: Aresearch-assistant
subagent can explore dozens of files and documentation pages without cluttering the main conversation with all the intermediate search results - only returning the relevant findings.
Parallelization
Multiple subagents can run concurrently, dramatically speeding up complex workflows. Example: During a code review, you can runstyle-checker
, security-scanner
, and test-coverage
subagents simultaneously, reducing review time from minutes to seconds.
Specialized Instructions and Knowledge
Each subagent can have tailored system prompts with specific expertise, best practices, and constraints. Example: Adatabase-migration
subagent can have detailed knowledge about SQL best practices, rollback strategies, and data integrity checks that would be unnecessary noise in the main agent’s instructions.
Tool Restrictions
Subagents can be limited to specific tools, reducing the risk of unintended actions. Example: Adoc-reviewer
subagent might only have access to Read and Grep tools, ensuring it can analyze but never accidentally modify your documentation files.
Creating Subagents
Subagents are defined as markdown files in specific directories:- Project-level:
.claude/agents/*.md
- Available only in the current project - User-level:
~/.claude/agents/*.md
- Available across all projects
File Format
Each subagent is a markdown file with YAML frontmatter:Configuration Fields
Field | Required | Description |
---|---|---|
name | Yes | Unique identifier using lowercase letters and hyphens |
description | Yes | Natural language description of when to use this subagent |
tools | No | Comma-separated list of allowed tools. If omitted, inherits all tools |
How the SDK Uses Subagents
When using the Claude Code SDK, subagents defined in the filesystem are automatically available. Claude Code will:- Auto-detect subagents from
.claude/agents/
directories - Invoke them automatically based on task matching
- Use their specialized prompts and tool restrictions
- Maintain separate context for each subagent invocation
Example Subagents
For comprehensive examples of subagents including code reviewers, test runners, debuggers, and security auditors, see the main Subagents guide. The guide includes detailed configurations and best practices for creating effective subagents.SDK Integration Patterns
Automatic Invocation
The SDK will automatically invoke appropriate subagents based on the task context. Ensure your subagent’sdescription
field clearly indicates when it should be used:
Explicit Invocation
Users can request specific subagents in their prompts:Tool Restrictions
Subagents can have restricted tool access via thetools
field:
- Omit the field - Subagent inherits all available tools (default)
- Specify tools - Subagent can only use listed tools
Related Documentation
- Main Subagents Guide - Comprehensive subagent documentation
- SDK Configuration Guide - Overview of configuration approaches
- Settings - Configuration file reference
- Slash Commands - Custom command creation