Tool Restrictions Per Agent
Different agents can have different tools. This enforces specialization.
Example: Security Reviewer Agent
securityreviewer = AgentDefinition(
name="security-reviewer",
description="Audits code for security vulnerabilities",
system
prompt="""Review for:
- SQL injection, XSS vulnerabilities
Insecure cryptography
Exposed secrets
Authentication bypass risks""",
tools=["Read", "Grep", "WebSearch"], # No Bash, no Edit
model="claude-3-5-sonnet"
)
This agent can't run arbitrary commands (Bash) or edit files ā only review.
Example: Implementer Agent
implementer = AgentDefinition(
name="implementer",
description="Codes new features",
systemprompt="""When implementing:
Read existing patterns
Follow the code style
Write tests
Verify with tests before reporting done""",
tools=["Read", "Edit", "Write", "Bash", "Grep"], # Has Bash + Edit
model="claude-3-5-sonnet"
)
This agent can code and test, but not browse the web (focused on local work).
---
Context Isolation Benefits
Each agent has its own context window. This saves tokens and keeps the main agent lean.
Without Agents
Main agent researches (reads 15 files)
ā All 15 files in main agent's context
ā Context is now full
ā Can't do much else
ā Total tokens: ~50K
With Subagents
Main agent spawns researcher (reads 15 files)
ā Researcher's context: ~20K tokens used
ā Researcher summarizes: "Found X pattern in 3 files, Y pattern in 5 files"
ā Main agent gets 100-word summary
ā Main context: still lean
ā Total tokens: ~25K (50% savings)
Real Workflow
async for message in query(
prompt="Research auth patterns and design a refactor plan",
options={
"agents": {
"researcher": researcheragent,
"planner": planneragent
}
}
):
# Claude automatically decides when to use each agent
# Researcher explores (keeps findings brief)
# Planner designs based on summary (context stays lean)
if isinstance(message, ResultMessage):
print(message.result)
---
Specialization Patterns
The Team Pattern
Create agents for different roles, work together:
agents = {
"explorer": AgentDefinition(
description="Searches and discovers patterns",
tools=["Glob", "Grep", "Read", "WebSearch"]
),
"designer": AgentDefinition(
description="Plans architecture and solutions",
tools=["Read", "Grep"],
systemprompt="Design solutions that are scalable, maintainable, and follow existing patterns"
),
"builder": AgentDefinition(
description="Implements code and features",
tools=["Read", "Write", "Edit", "Bash"]
),
"validator": AgentDefinition(
description="Tests and validates work",
tools=["Bash", "Grep", "Read"]
)
}
Use by delegating:
options = ClaudeAgentOptions(agents=agents)
async for message in query(
prompt="Implement user authentication: research best practices, design it, code it, validate tests pass",
options=options
):
# Claude automatically delegates to appropriate agents
pass
The Focus Pattern
Create agents with very narrow focus:
findtodos = AgentDefinition(
description="Find all TODO comments in code",
tools=["Grep", "Read"]
)
run
tests = AgentDefinition(
description="Run test suites and report status",
tools=["Bash"]
)
checktypes = AgentDefinition(
description="Run TypeScript type checker",
tools=["Bash"]
)
Each agent does one thing well.
---
When to Use Subagents
Use Subagents When:
- Researching code + writing tests
- Can happen in parallel
- Security reviewer knows vulnerabilities
- Test writer knows test patterns
- Long research tasks
- Multiple file reads
- Explorer (read-only)
- Builder (read + write + execute)
Don't Use Subagents When:
- Just "Read a file"
- Subagent overhead > benefit
- Task 2 depends on results of Task 1
- Parallelization doesn't help
- Subagent's summary still large
- Limited token savings
---
Designing Effective Agents
Principle 1: Single Responsibility
ā Bad: Vague responsibility
broadagent = AgentDefinition(
description="General helper"
)
ā Good: Specific responsibility
securityauditor = AgentDefinition(
description="Audits TypeScript code for security vulnerabilities"
)
Be specific. "Security auditor" is better than "helper."
Principle 2: Clear System Prompt
systemprompt="""You are a database schema reviewer.
FOCUS ON:
Proper indexing for query performance
Referential integrity and constraints
Normalization (avoid redundancy)
Scalability for 1M+ records
IGNORE:
Application code
Frontend concerns
DevOps infrastructure
OUTPUT FORMAT:
Issue: [what's wrong]
Impact: [why it matters]
Fix: [what to change]
"""
Clear instructions = better results.
Principle 3: Right Tool Set
ā Too many tools (confusing)
tools=["Read", "Write", "Edit", "Bash", "Grep", "WebSearch", ...]
ā Focused toolset
tools=["Read", "Grep"] # Code review agent doesn't need to write
Give agents only the tools they need.
---
Key Gotchas
Gotcha #1: Agents can't delegate to agents
Gotcha #2: Context doesn't transfer
Gotcha #3: Tools visible in schema
Gotcha #4: Cost scales with agent count
---
Next Steps
You've mastered creating specialized agents. Next module: Multi-Agent Orchestration ā how agents work together at scale!