Skip to content

Lesson 12: Multi-Agent Patterns: Agent Swarms

Play

Watch on YouTube

Code for this lesson: samples/12-swarms

In a swarm, agents hand off to each other autonomously with no predefined structure. There’s no orchestrator deciding who runs next and no graph fixing the order. The execution path emerges from what each agent discovers.

This is the right fit when you can’t draw the workflow ahead of time: debugging an incident, open-ended research, or any problem where the next step depends on what the last step found.

from strands import Agent
from strands.multiagent import Swarm
triage = Agent(
name="triage",
system_prompt="Initial assessment. Hand off to the relevant specialist.",
)
log_analyst = Agent(
name="log_analyst",
system_prompt="Analyze application logs. Hand off if you find infra/deployment issues.",
tools=[check_application_logs],
)
metrics_analyst = Agent(
name="metrics_analyst",
system_prompt="Analyze system metrics. Hand off based on findings.",
tools=[check_metrics_dashboard],
)
deployment_reviewer = Agent(
name="deployment_reviewer",
system_prompt="Review recent deployments and infrastructure changes.",
tools=[check_recent_deployments, check_infrastructure_status],
)
debugging_swarm = Swarm(
[triage, log_analyst, metrics_analyst, deployment_reviewer],
entry_point=triage,
max_handoffs=10,
max_iterations=10,
execution_timeout=300.0,
node_timeout=120.0,
repetitive_handoff_detection_window=6,
repetitive_handoff_min_unique_agents=2,
)
result = debugging_swarm(
"Payment service returning 500 errors for 25% of requests. Started 30 min ago."
)

📂 debugging_swarm.py

  • Handoff is a full transfer of control, not a function call. When log_analyst hands off to deployment_reviewer, the log analyst is done; it doesn’t get the result back.
  • Agents share accumulated context. Each agent sees what the previous agents found, so the investigation builds on itself.
  • Strands injects a handoff_to_agent tool automatically. You don’t write handoff logic; each agent’s system prompt tells it when to hand off, and the model decides.

Because nothing structural stops a swarm from bouncing between agents, always configure limits. Without them, swarms can ping-pong forever.

SettingPurpose
max_handoffsTotal handoff cap
max_iterationsTotal agent execution cap
execution_timeoutWall-clock time limit for the entire swarm
node_timeoutTime limit per agent turn
repetitive_handoff_detection_windowDetect ping-pong loops between the same few agents

The three multi-agent patterns nest: a swarm can be a node in a graph, a graph can contain agents-as-tools, and an agent-as-tool can internally run a graph. Start with the simplest pattern that fits your coordination needs and compose up from there.

PatternStructureUse When
Agents as ToolsHub-and-spoke. Orchestrator calls specialistsClear manager-specialist relationship, isolated context
GraphDAG with explicit edgesYou can draw the workflow on a whiteboard
SwarmAutonomous handoffs, no predefined structureThe team needs to figure it out together