agents
parameter.
Overview
Subagents can be defined in two ways when using the SDK:- Programmatically - Using the
agents
parameter in yourquery()
options (recommended for SDK applications) - Filesystem-based - Placing markdown files with YAML frontmatter in designated directories (
.claude/agents/
)
agents
parameter, which provides a more integrated development experience for SDK applications.
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
Programmatic Definition (Recommended)
Define subagents directly in your code using theagents
parameter:
AgentDefinition Configuration
Field | Type | Required | Description |
---|---|---|---|
description | string | Yes | Natural language description of when to use this agent |
prompt | string | Yes | The agent’s system prompt defining its role and behavior |
tools | string[] | No | Array of allowed tool names. If omitted, inherits all tools |
model | 'sonnet' | 'opus' | 'haiku' | 'inherit' | No | Model override for this agent. Defaults to main model if omitted |
Filesystem-Based Definition (Alternative)
You can also define subagents 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
agents
parameter) take precedence over filesystem-based agents with the same name.
How the SDK Uses Subagents
When using the Claude Agent SDK, subagents can be defined programmatically or loaded from the filesystem. Claude will:- Load programmatic agents from the
agents
parameter in your options - Auto-detect filesystem agents from
.claude/agents/
directories (if not overridden) - Invoke them automatically based on task matching and the agent’s
description
- Use their specialized prompts and tool restrictions
- Maintain separate context for each subagent invocation
agents
parameter) take precedence over filesystem-based agents with the same name.
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 agent’sdescription
field clearly indicates when it should be used:
Explicit Invocation
Users can request specific subagents in their prompts:Dynamic Agent Configuration
You can dynamically configure agents based on your application’s needs:Tool Restrictions
Subagents can have restricted tool access via thetools
field:
- Omit the field - Agent inherits all available tools (default)
- Specify tools - Agent can only use listed tools
Common Tool Combinations
Read-only agents (analysis, review):Related Documentation
- Main Subagents Guide - Comprehensive subagent documentation
- SDK Configuration Guide - Overview of configuration approaches
- Settings - Configuration file reference
- Slash Commands - Custom command creation