Skip to main content
The Claude Agent SDK supports programmatic subagents - specialized agents with custom prompts, tools, and models that can be invoked by Claude to handle specific tasks.

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)
Subagents run as separate agent sessions and report progress through hook events.

Creating Custom Agents

Define agents using the AgentDefinition class:

Agent Definition Fields

description
str
required
Brief description that helps Claude understand when to use this agent
prompt
str
required
System prompt that defines the agent’s behavior and expertise
tools
list[str]
default:"None"
List of allowed tools. If None, the agent inherits tools from the parent
model
str
default:"None"
Model to use: "sonnet", "opus", "haiku", or "inherit". If None, uses default

Using 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

Agent descriptions should clearly indicate when to use each agent. Be specific about their capabilities and purpose.
Agent prompts define behavior. Include guidelines, constraints, and examples to ensure consistent results.
Limit agents to only the tools they need. This improves security and helps Claude choose the right agent.
Use haiku for simple tasks, sonnet for general work, and opus for complex analysis. This optimizes cost and performance.
Use SubagentStart and SubagentStop hooks to track agent activity, especially in complex multi-agent workflows.
Test each agent separately before combining them in workflows to ensure they behave as expected.