🧠Learn GenAIVisual explainers for tired devs
Β·12 min read

Multi-Agent Orchestration: Patterns for Claude Code Swarms

agentsarchitectureclaude-codeorchestrationswarm

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 aggregation

Orchestrator

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.all for 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.50 per worker
  • β€’ Track total spend across swarm
  • β€’ Kill runaway agents early

🎯 Effort Levels

  • β€’ --effort low for simple tasks
  • β€’ --effort high for complex ones
  • β€’ Match effort to task complexity

πŸ”§ Tool Restrictions

  • β€’ --allowedTools per 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

OpenClaw

Supervisor: parallel workers, one orchestrator. Best when subtasks are independent.

Supervisor vs Pipeline

Pipeline: sequential handoff. Best when each step depends on previous output.

vs Debate vs Critic

OpenClaw

Debate: multiple agents argue positions simultaneously. Explores trade-offs broadly.

Debate vs Critic

Critic: produce-then-critique loop. Iteratively refines a single output.

vs MapReduce vs Blackboard

OpenClaw

MapReduce: embarrassingly parallel, simple aggregation. Scales linearly.

MapReduce vs Blackboard

Blackboard: shared mutable state, agents react to changes. More complex but flexible.

vs Hand-coded vs Learned

OpenClaw

Hand-coded: you define the decomposition. Predictable, debuggable, immediate.

Hand-coded vs Learned

Learned (like Kimi K2.5): model discovers decomposition via RL. More autonomous but opaque.

Quick Start

1

Check Claude Code CLI is available

npx @anthropic-ai/claude-code --help
2

Test non-interactive mode with JSON output

claude -p --output-format json 'Hello world'
3

Define your agent personas (see examples above)

# Create agents.json with your specialist definitions
4

Build your first swarm orchestrator

# Write orchestrate.sh to spawn workers in parallel