What are Subagents?
Subagents are task-focused agents that Claude can delegate work to. Each subagent has:- A specific description that tells Claude when to use it
- A custom prompt that defines its behavior
- Restricted tools for focused functionality
- Optional model selection (sonnet, opus, haiku)
Creating Custom Agents
Define agents using theAgentDefinition class:
Agent Definition Fields
Brief description that helps Claude understand when to use this agent
System prompt that defines the agent’s behavior and expertise
List of allowed tools. If None, the agent inherits tools from the parent
Model to use:
"sonnet", "opus", "haiku", or "inherit". If None, uses defaultUsing Agents in Conversations
Claude automatically invokes agents based on task descriptions:Agent Examples
Code Reviewer Agent
Documentation Writer Agent
Test Generator Agent
Data Analyzer Agent
Multiple Agents Working Together
Agent Model Selection
Choose the appropriate model for each agent:Agent Tool Restrictions
Limit agents to specific tools for focused functionality:Monitoring Agent Activity
Use hooks to monitor when agents start and stop:Agent Setting Sources
Control which configuration sources agents use:Complete Agent Workflow Example
Best Practices
Write focused agent descriptions
Write focused agent descriptions
Agent descriptions should clearly indicate when to use each agent. Be specific about their capabilities and purpose.
Provide detailed prompts
Provide detailed prompts
Agent prompts define behavior. Include guidelines, constraints, and examples to ensure consistent results.
Restrict tools appropriately
Restrict tools appropriately
Limit agents to only the tools they need. This improves security and helps Claude choose the right agent.
Choose models based on task complexity
Choose models based on task complexity
Use
haiku for simple tasks, sonnet for general work, and opus for complex analysis. This optimizes cost and performance.Monitor agent execution
Monitor agent execution
Use SubagentStart and SubagentStop hooks to track agent activity, especially in complex multi-agent workflows.
Test agents independently
Test agents independently
Test each agent separately before combining them in workflows to ensure they behave as expected.