Create and use programmatic subagents with custom behaviors, tools, and models
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.
from claude_agent_sdk import AgentDefinition, ClaudeAgentOptions# Define a code reviewer agentcode_reviewer = AgentDefinition( description="Reviews code for best practices and potential issues", prompt="You are a code reviewer. Analyze code for bugs, performance issues, " "security vulnerabilities, and adherence to best practices. " "Provide constructive feedback.", tools=["Read", "Grep"], model="sonnet")# Configure options with the agentoptions = ClaudeAgentOptions( agents={ "code-reviewer": code_reviewer })
from claude_agent_sdk import AgentDefinitiontest_generator = AgentDefinition( description="Creates comprehensive test suites", prompt="You are a testing expert. Create thorough test suites that:\n" "- Cover normal and edge cases\n" "- Test error handling\n" "- Include clear assertions\n" "- Follow testing best practices\n" "Write tests that are maintainable and comprehensive.", tools=["Read", "Write", "Bash"], model="sonnet")
from claude_agent_sdk import AgentDefinitiondata_analyzer = AgentDefinition( description="Analyzes data files and generates insights", prompt="You are a data analyst. Analyze data to:\n" "- Identify patterns and trends\n" "- Calculate relevant statistics\n" "- Generate visualizations\n" "- Provide actionable insights\n" "Present findings clearly with supporting evidence.", tools=["Read", "Bash", "Write"], model="sonnet")
from claude_agent_sdk import AgentDefinition# Fast agent for simple tasksquick_agent = AgentDefinition( description="Handles simple, quick tasks", prompt="You handle quick tasks efficiently.", model="haiku" # Fast and cost-effective)# Balanced agent for general workgeneral_agent = AgentDefinition( description="Handles general tasks", prompt="You handle a variety of tasks with good quality.", model="sonnet" # Balanced performance)# Powerful agent for complex taskscomplex_agent = AgentDefinition( description="Handles complex, challenging tasks", prompt="You handle complex tasks requiring deep analysis.", model="opus" # Maximum capability)# Inherit model from parentinheriting_agent = AgentDefinition( description="Uses same model as parent", prompt="You inherit configuration from the parent agent.", model="inherit" # Use parent's model)
Limit agents to specific tools for focused functionality:
from claude_agent_sdk import AgentDefinition# Read-only agentreader_agent = AgentDefinition( description="Reads and analyzes files", prompt="You analyze files without making changes.", tools=["Read", "Grep", "Glob"] # No write access)# Write-focused agentwriter_agent = AgentDefinition( description="Creates and modifies files", prompt="You create and edit files.", tools=["Read", "Write", "Edit", "MultiEdit"])# Command execution agentcommand_agent = AgentDefinition( description="Runs system commands", prompt="You execute system commands safely.", tools=["Bash", "Read"] # Limited to bash and reading)# Unrestricted agentfull_agent = AgentDefinition( description="Handles any task", prompt="You have full access to all tools.", tools=None # Inherits all available tools)