File: subagents.md | Updated: 11/15/2025
Agent Skills are now available! Learn more about extending Claude's capabilities with Agent Skills .
English
Search...
Ctrl K
Search...
Navigation
Guides
Subagents in the SDK
Home Developer Guide API Reference Model Context Protocol (MCP) Resources Release Notes
On this page
Subagents in the Claude Agent SDK are specialized AIs that are orchestrated by the main agent. Use subagents for context management and parallelization. This guide explains how to define and use subagents in the SDK using the agents parameter.
Subagents can be defined in two ways when using the SDK:
agents parameter in your query() options (recommended for SDK applications).claude/agents/)This guide primarily focuses on the programmatic approach using the agents parameter, which provides a more integrated development experience for SDK applications.
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: A research-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 run style-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: A database-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: A doc-reviewer subagent might only have access to Read and Grep tools, ensuring it can analyze but never accidentally modify your documentation files.
Programmatic Definition (Recommended)
Define subagents directly in your code using the agents parameter:
Copy
import { query } from '@anthropic-ai/claude-agent-sdk';
const result = query({
prompt: "Review the authentication module for security issues",
options: {
agents: {
'code-reviewer': {
description: 'Expert code review specialist. Use for quality, security, and maintainability reviews.',
prompt: `You are a code review specialist with expertise in security, performance, and best practices.
When reviewing code:
- Identify security vulnerabilities
- Check for performance issues
- Verify adherence to coding standards
- Suggest specific improvements
Be thorough but concise in your feedback.`,
tools: ['Read', 'Grep', 'Glob'],
model: 'sonnet'
},
'test-runner': {
description: 'Runs and analyzes test suites. Use for test execution and coverage analysis.',
prompt: `You are a test execution specialist. Run tests and provide clear analysis of results.
Focus on:
- Running test commands
- Analyzing test output
- Identifying failing tests
- Suggesting fixes for failures`,
tools: ['Bash', 'Read', 'Grep'],
}
}
}
});
for await (const message of result) {
console.log(message);
}
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:
.claude/agents/*.md - Available only in the current project~/.claude/agents/*.md - Available across all projectsEach subagent is a markdown file with YAML frontmatter:
Copy
---
name: code-reviewer
description: Expert code review specialist. Use for quality, security, and maintainability reviews.
tools: Read, Grep, Glob, Bash
---
Your subagent's system prompt goes here. This defines the subagent's
role, capabilities, and approach to solving problems.
Note: Programmatically defined agents (via the agents parameter) take precedence over filesystem-based agents with the same name.
When using the Claude Agent SDK, subagents can be defined programmatically or loaded from the filesystem. Claude will:
agents parameter in your options.claude/agents/ directories (if not overridden)descriptionProgrammatically defined agents (via agents parameter) take precedence over filesystem-based agents with the same name.
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.
Automatic Invocation
The SDK will automatically invoke appropriate subagents based on the task context. Ensure your agent’s description field clearly indicates when it should be used:
Copy
const result = query({
prompt: "Optimize the database queries in the API layer",
options: {
agents: {
'performance-optimizer': {
description: 'Use PROACTIVELY when code changes might impact performance. MUST BE USED for optimization tasks.',
prompt: 'You are a performance optimization specialist...',
tools: ['Read', 'Edit', 'Bash', 'Grep'],
model: 'sonnet'
}
}
}
});
Explicit Invocation
Users can request specific subagents in their prompts:
Copy
const result = query({
prompt: "Use the code-reviewer agent to check the authentication module",
options: {
agents: {
'code-reviewer': {
description: 'Expert code review specialist',
prompt: 'You are a security-focused code reviewer...',
tools: ['Read', 'Grep', 'Glob']
}
}
}
});
Dynamic Agent Configuration
You can dynamically configure agents based on your application’s needs:
Copy
import { query, type AgentDefinition } from '@anthropic-ai/claude-agent-sdk';
function createSecurityAgent(securityLevel: 'basic' | 'strict'): AgentDefinition {
return {
description: 'Security code reviewer',
prompt: `You are a ${securityLevel === 'strict' ? 'strict' : 'balanced'} security reviewer...`,
tools: ['Read', 'Grep', 'Glob'],
model: securityLevel === 'strict' ? 'opus' : 'sonnet'
};
}
const result = query({
prompt: "Review this PR for security issues",
options: {
agents: {
'security-reviewer': createSecurityAgent('strict')
}
}
});
Subagents can have restricted tool access via the tools field:
Example of a read-only analysis agent:
Copy
const result = query({
prompt: "Analyze the architecture of this codebase",
options: {
agents: {
'code-analyzer': {
description: 'Static code analysis and architecture review',
prompt: `You are a code architecture analyst. Analyze code structure,
identify patterns, and suggest improvements without making changes.`,
tools: ['Read', 'Grep', 'Glob'] // No write or execute permissions
}
}
}
});
Common Tool Combinations
Read-only agents (analysis, review):
Copy
tools: ['Read', 'Grep', 'Glob']
Test execution agents:
Copy
tools: ['Bash', 'Read', 'Grep']
Code modification agents:
Copy
tools: ['Read', 'Edit', 'Write', 'Grep', 'Glob']
Was this page helpful?
YesNo
Custom Tools Slash Commands in the SDK
Assistant
Responses are generated using AI and may contain mistakes.