Multi-Agent Orchestration: Patterns for Claude Code Swarms
What is Multi-Agent Orchestration?
Multi-agent orchestration is the art of coordinating multiple AI agents to solve complex problems that would overwhelm a single agent. Instead of one agent doing everything sequentially, you decompose work across specialized agents working in parallel.
Think of it like a software team: you wouldn't have one developer do security review, type checking, architecture review, and testing sequentially. You'd parallelize across specialists. The same principle applies to AI agents.
Why "Swarm"?
Like bees in a hive, each agent has a specialized role but works toward a collective goal. No single agent needs to understand the whole problem β the orchestrator coordinates and synthesizes.
β‘
Parallelism
Run independent tasks simultaneously
π―
Specialization
Agents tuned for specific tasks
β
Verification
Agents check each other's work
Claude Code CLI Primitives
Claude Code provides all the primitives you need to build multi-agent systems. The key is the claude -p (print) mode which enables non-interactive, scriptable agent execution.
# Core primitives
-p, --print Non-interactive mode (for workers)
--output-format json Structured output for parsing
--model sonnet|opus Different models per agent
--append-system-prompt Customize agent behavior
--allowedTools Restrict tools per specialist
--max-budget-usd Cost control per worker
# Agent definitions
--agents <json> Define custom agent personas
--agent <name> Select which agent to use
--effort low|med|high Control thinking depth
π‘ Key Insight
Each claude -p invocation is an independent agent. Your orchestrator (a shell script, Node.js, or Python) spawns these agents in parallel, collects their JSON outputs, and synthesizes the final result.
Key Concepts
Why Build a Swarm?
Single-agent execution hits hard limits. Context windows fill up. Latency compounds with each tool call. Complex problems require knowledge no single prompt can capture.
π Single Agent Limits
- β’ Sequential tool calls (1-3s each)
- β’ Context window fills with history
- β’ No specialization β jack of all trades
- β’ One perspective, potential blind spots
π Swarm Advantages
- β’ Parallel execution (3-5x speedup)
- β’ Fresh context per worker
- β’ Specialized prompts per task type
- β’ Multiple perspectives, cross-check
π― Best Use Cases
β’ Code review (security + types + arch)
β’ Research synthesis (multiple sources)
β’ Complex debugging (hypotheses)
β’ Content generation (draft + edit + fact-check)
β’ Data analysis (parallel processing)
β’ Planning (options + critique + decide)
Building Your First Swarm
ποΈSwarm Architecture
A minimal swarm has three components: an orchestrator script, agent definitions, and output aggregation. Here's the basic structure:
swarm/
βββ agents.json # Agent personas
βββ orchestrate.sh # Main coordinator
βββ tasks/
β βββ code-review.sh # Code review swarm
β βββ research.sh # Research swarm
βββ lib/
βββ spawn.sh # Worker helpers
βββ merge.sh # Output aggregationOrchestrator
A shell or Node script that: (1) parses the incoming task, (2) spawns worker agents in parallel, (3) waits for completion, (4) calls a synthesizer agent to merge results.
Workers
Independent claude -p processes, each with a specialized system prompt and restricted tool access. Output JSON for easy parsing.
Synthesizer
A final agent that receives all worker outputs and produces the merged, prioritized, deduplicated final result.
πDefining Agents
Claude Code's --agents flag lets you define reusable agent personas. Each agent gets a description and custom system prompt:
{
"security": {
"description": "Security auditor focused on vulnerabilities",
"prompt": "You are a security expert. Focus ONLY on: auth bugs, input validation, secrets exposure, injection risks. Ignore style issues. Output JSON: {findings: [{severity: 'critical'|'high'|'medium'|'low', file, line, issue, fix}]}"
},
"types": {
"description": "TypeScript type safety specialist",
"prompt": "You are a TypeScript expert. Focus ONLY on: any types, missing null checks, unsafe casts, error boundary gaps. Output JSON: {findings: [{file, line, issue, fix}]}"
},
"architect": {
"description": "Software architecture reviewer",
"prompt": "You are a senior architect. Focus ONLY on: coupling, abstraction leaks, pattern violations, scalability concerns. Output JSON: {findings: [{file, issue, recommendation}]}"
},
"synthesizer": {
"description": "Merges and prioritizes findings from specialists",
"prompt": "You receive findings from security, types, and architecture reviewers. Merge them, remove duplicates, and prioritize. Output a concise summary with the top 5 issues to fix first."
}
}π― Prompt Engineering Tips
- β’ Be explicit about scope: "Focus ONLY on..."
- β’ Specify output format: JSON schema helps parsing
- β’ Include severity levels for prioritization
- β’ Tell agents what to ignore to reduce noise
β‘Parallel Execution
The magic happens when you spawn workers in parallel. Here's a bash example that runs three reviewers simultaneously:
#!/bin/bash # code-review-swarm.sh CODE=$(cat "$1") # File or directory to review AGENTS_FILE="./agents.json" # Spawn workers in parallel (& backgrounds each) echo "π Security review..." claude -p --output-format json \ --agents "$(cat $AGENTS_FILE)" --agent security \ --model sonnet --max-budget-usd 0.50 \ "Review this code: $CODE" > /tmp/security.json & echo "π Type safety review..." claude -p --output-format json \ --agents "$(cat $AGENTS_FILE)" --agent types \ --model sonnet --max-budget-usd 0.50 \ "Review this code: $CODE" > /tmp/types.json & echo "ποΈ Architecture review..." claude -p --output-format json \ --agents "$(cat $AGENTS_FILE)" --agent architect \ --model sonnet --max-budget-usd 0.50 \ "Review this code: $CODE" > /tmp/arch.json & # Wait for all workers to complete wait echo "β All reviews complete" # Synthesize results echo "π Synthesizing..." claude -p --agents "$(cat $AGENTS_FILE)" --agent synthesizer \ "Merge these code reviews: Security: $(cat /tmp/security.json) Types: $(cat /tmp/types.json) Architecture: $(cat /tmp/arch.json)"
β±οΈ Sequential: ~45 seconds
3 reviews Γ 15s each = 45s total
β‘ Parallel: ~15 seconds
All 3 run simultaneously = 15s + 5s synthesis
π’Node.js Orchestrator
For more control, use Node.js with child processes. This gives you better error handling, streaming output, and complex coordination:
import { spawn } from 'child_process';
import { promisify } from 'util';
const agents = {
security: { prompt: "You are a security auditor..." },
types: { prompt: "You are a TypeScript expert..." },
architect: { prompt: "You are a senior architect..." },
};
async function runAgent(name: string, code: string): Promise<string> {
return new Promise((resolve, reject) => {
const proc = spawn('claude', [
'-p',
'--output-format', 'json',
'--append-system-prompt', agents[name].prompt,
'--model', 'sonnet',
`Review this code: ${code}`
]);
let output = '';
proc.stdout.on('data', (data) => output += data);
proc.on('close', (code) => code === 0 ? resolve(output) : reject());
});
}
async function swarmReview(code: string) {
// Run all agents in parallel
const [security, types, arch] = await Promise.all([
runAgent('security', code),
runAgent('types', code),
runAgent('architect', code),
]);
// Synthesize
return runAgent('synthesizer',
`Merge: Security: ${security} Types: ${types} Arch: ${arch}`);
}π§ Node.js Advantages
- β’
Promise.allfor clean parallel execution - β’ Stream processing for large outputs
- β’ Error handling and retries
- β’ Integration with existing tooling
πAdvanced Patterns
Once you've mastered the basics, explore these advanced orchestration patterns:
π Iterative Refinement
Run a critic agent on the synthesized output. If quality score < threshold, loop with the critique as additional context. Cap iterations to prevent runaway.
π Dynamic Decomposition
Have a "planner" agent analyze the task and decide which specialists to invoke. Not all tasks need all agents β save cost by skipping irrelevant specialists.
π³οΈ Voting & Consensus
Run the same task through multiple agents (or same agent multiple times) and use voting to select the best output. Reduces variance on critical decisions.
π³ Hierarchical Swarms
Swarms can spawn sub-swarms. A top-level orchestrator delegates to mid-level orchestrators, each managing their own workers. Good for very large codebases.
π°Cost & Performance
Multi-agent systems can get expensive fast. Use these strategies to control costs:
ποΈ Model Selection
- β’ Workers: Sonnet (fast, cheap)
- β’ Synthesizer: Opus (quality merge)
- β’ Simple tasks: Haiku (very cheap)
π΅ Budget Caps
- β’
--max-budget-usd 0.50per worker - β’ Track total spend across swarm
- β’ Kill runaway agents early
π― Effort Levels
- β’
--effort lowfor simple tasks - β’
--effort highfor complex ones - β’ Match effort to task complexity
π§ Tool Restrictions
- β’
--allowedToolsper agent - β’ Security agent: read-only
- β’ Fewer tools = faster, cheaper
π Example Cost Breakdown
Code review swarm (3 workers + synthesizer):
β’ 3 Γ Sonnet workers @ ~$0.15 each = $0.45
β’ 1 Γ Opus synthesizer @ ~$0.30 = $0.30
β’ Total: ~$0.75 per review
Pattern Comparison
vs Supervisor vs Pipeline
Supervisor: parallel workers, one orchestrator. Best when subtasks are independent.
Pipeline: sequential handoff. Best when each step depends on previous output.
vs Debate vs Critic
Debate: multiple agents argue positions simultaneously. Explores trade-offs broadly.
Critic: produce-then-critique loop. Iteratively refines a single output.
vs MapReduce vs Blackboard
MapReduce: embarrassingly parallel, simple aggregation. Scales linearly.
Blackboard: shared mutable state, agents react to changes. More complex but flexible.
vs Hand-coded vs Learned
Hand-coded: you define the decomposition. Predictable, debuggable, immediate.
Learned (like Kimi K2.5): model discovers decomposition via RL. More autonomous but opaque.
Quick Start
Check Claude Code CLI is available
npx @anthropic-ai/claude-code --helpTest non-interactive mode with JSON output
claude -p --output-format json 'Hello world'Define your agent personas (see examples above)
# Create agents.json with your specialist definitionsBuild your first swarm orchestrator
# Write orchestrate.sh to spawn workers in parallel