Multi-Agent Systems
Multi-agent systems solve complex tasks through collaboration and division of labor.
SDK Focus
agents{}
AgentDefinition
model: haiku|sonnet|opus
maxTurns
hooks.SubagentStart/Stop
Why Multi-Agent?
- Specialization - Different agents focus on different domains
- Parallelism - Multiple agents work at the same time
- Robustness - No single point of failure
- Scalability - Add agents as needed
Collaboration Patterns
Hierarchical (Manager-Worker)
┌─────────────┐
│ Manager │ ← Assign tasks, aggregate results
└──────┬──────┘
│
┌────┼────┐
↓ ↓ ↓
┌───┐┌───┐┌───┐
│ W1││ W2││ W3│ ← Execute specific sub-tasks
└───┘└───┘└───┘
Peer-to-Peer
┌───┐ ┌───┐
│ A │ ←→ │ B │
└───┘ └───┘
↑ ╲ ╱ ↑
│ ╲╱ │
│ ╱╲ │
↓ ╱ ╲ ↓
┌───┐ ┌───┐
│ C │ ←→ │ D │
└───┘ └───┘
Pipeline
Input → [Agent A] → [Agent B] → [Agent C] → Output
(analysis) (design) (implementation)
Inter-Agent Communication
# Message passing
message = {
"from": "planner_agent",
"to": "coder_agent",
"type": "task",
"content": {
"action": "implement",
"spec": "Create login API endpoint"
}
}
# Shared state
shared_state = {
"project_context": {...},
"completed_tasks": [...],
"current_blockers": [...]
}
Practical Example: Software Development Team
Architect Agent
├── Analyze requirements and design architecture
└── Assign tasks to other agents
Developer Agent (x3)
├── Implement features
└── Write unit tests
Reviewer Agent
├── Code Review
└── Suggest improvements
QA Agent
└── Run tests and report issues
Subagent Orchestration
multi-agent.ts
import { query } from "@anthropic-ai/claude-agent-sdk";
const response = query({
prompt: "Write a REST API for user management, with tests",
options: {
agents: {
"planner": {
description: "Breaks tasks into implementation steps",
prompt: "You create detailed implementation plans.",
model: "sonnet",
tools: ["Read", "Grep", "Glob"],
maxTurns: 5
},
"coder": {
description: "Writes and edits code",
prompt: "You implement code based on plans.",
model: "sonnet",
tools: ["Read", "Write", "Edit", "Bash"]
},
"reviewer": {
description: "Reviews code for quality and security",
prompt: "You review code. Be thorough but constructive.",
model: "haiku",
tools: ["Read", "Grep"],
maxTurns: 3
}
},
hooks: {
SubagentStart: async (input) => {
console.log(`[SPAWN] ${input.agentName} started`);
},
SubagentStop: async (input) => {
console.log(`[DONE] ${input.agentName} finished`);
},
Stop: async () => {
console.log("[CLEANUP] Ensuring all subagents stopped");
}
}
}
});
Subagent Cleanup Warning
Known Issue: Subagents don't auto-stop when the parent stops. Always implement cleanup in the Stop hook to prevent orphaned processes and resource leaks.
Challenges & Solutions
- Coordination overhead → Define clear responsibilities, reduce unnecessary communication
- Consistency issues → Use shared state and locking mechanisms
- Deadlocks → Set timeouts and fallback strategies
- Debugging difficulty → Full logs and tracing
Next Steps
- Evaluation - How to evaluate multi-agent systems
Try It: Writer + Reviewer Pipeline
Build a generate → critique → revise cycle using subagents.
- Define a "writer" agent (Sonnet) and "reviewer" agent (Haiku)
- Writer generates code, reviewer critiques it, writer revises
- Use
maxTurnsto limit each agent - Track SubagentStart/Stop hooks and implement cleanup
Gate: P9 Complete — 2+ agents run, SubagentStart/Stop hooks fire, cleanup implemented.