AI Agents
Swarms and pipelines are the two dominant multi-agent architectures. How they differ, when each one wins.
Building your first multi-agent system, you face an architectural choice that will define how the system scales, fails, and is debugged: do you connect agents in a pipeline, or do you let them operate in parallel as a swarm? Both patterns appear in production systems, both have legitimate use cases, and choosing the wrong one does not break your system outright - it just makes it harder to reason about, slower to debug, and more expensive to run than necessary. The choice comes down to whether your task is a sequence or a search.
A pipeline is a directed sequence of agents where each agent's output is the next agent's input. Work flows in one direction. Steps are ordered because each step depends on the previous one. A swarm is a set of agents that work simultaneously on different aspects of the same goal, coordinating through a shared state or an orchestrator that aggregates their outputs. Work is parallelised because steps are independent of each other.
| Dimension | Pipeline | Swarm |
|---|---|---|
| Task structure | Sequential, each step depends on previous | Parallel, steps are independent |
| Latency | Additive (step 1 + step 2 + step 3) | Limited by slowest parallel agent |
| Debugging | Straightforward - inspect each step's input/output | Complex - parallel execution, race conditions |
| Cost | Proportional to task length | Proportional to task breadth × parallel agents |
| Failure handling | One failure stops the chain | One failure may not affect others |
| State management | Each agent's output is the next agent's full context | Shared state requires coordination |
| Best for | Transform, refine, validate workflows | Research, coverage, parallel execution |
Choose the pipeline when the task has a natural sequence that cannot be reordered. The canonical signal: each step transforms the output of the previous step. If step 2 cannot start until step 1 is complete - not as a matter of architecture, but as a matter of logic - you have a pipeline.
Common pipeline use cases:
// Pipeline: linear, each step receives previous step's output
async function runPipeline(input: string): Promise {
const researched = await runAgent('researcher', input);
const drafted = await runAgent('writer', researched);
const edited = await runAgent('editor', drafted);
const finalised = await runAgent('publisher', edited);
return finalised;
}
// Total time: time(researcher) + time(writer) + time(editor) + time(publisher)
Choose the swarm when the task involves coverage or breadth - when you want multiple agents exploring different aspects of the same problem simultaneously, and the goal is to synthesise their independent findings. The canonical signal: agents can start working immediately without waiting for any other agent's output.
Common swarm use cases:
// Swarm: parallel, all agents start simultaneously
async function runSwarm(goal: string, agents: string[]): Promise {
// All agents start at the same time
const results = await Promise.all(
agents.map(agentType => runAgent(agentType, goal))
);
// Synthesise parallel outputs
return synthesise(goal, results);
}
// Total time: max(time(agent1), time(agent2)..., time(agentN)) + time(synthesise)
Most real-world multi-agent systems are neither pure pipelines nor pure swarms. They are pipelines where some stages themselves run agents in parallel. A research-to-report workflow might have a sequential structure (plan → research → write → review) but a parallel research stage (five research agents working simultaneously on different sub-topics):
async function runHybrid(goal: string): Promise {
// Sequential phase 1: planning
const plan = await runAgent('planner', goal);
const subtopics = parsePlan(plan); // Returns ['topic A', 'topic B', 'topic C']
// Parallel phase 2: research
const researchResults = await Promise.all(
subtopics.map(topic => runAgent('researcher', topic))
);
// Sequential phase 3: synthesis
const draft = await runAgent('writer', formatResearch(goal, researchResults));
const final = await runAgent('editor', draft);
return final;
}
// Total time: time(planner) + max(researcher×N) + time(writer) + time(editor)
This hybrid collapses the most expensive phase (research) from additive to parallel while keeping the steps that genuinely depend on sequence in their correct order.
Pipeline failures are simple: one agent fails, the chain stops. This is appropriate when downstream steps cannot produce meaningful output without upstream input. Handle pipeline failures by catching at the pipeline level and deciding whether to retry from the failed step, skip it, or abort.
Swarm failures are more complex: one agent fails, others may continue. The aggregation step must handle partial results gracefully - not assume all agents will complete successfully. Design swarm aggregators to produce a meaningful partial output when some agents fail:
// Swarm aggregation with partial failure handling
const settled = await Promise.allSettled(
agents.map(agentType => runAgent(agentType, goal))
);
const successful = settled
.filter((r): r is PromiseFulfilledResult => r.status === 'fulfilled')
.map(r => r.value);
const failed = settled.filter(r => r.status === 'rejected').length;
if (successful.length === 0) {
throw new Error('All swarm agents failed');
}
// Synthesise from whatever succeeded
const result = await synthesise(goal, successful);
if (failed > 0) {
return `${result}
[Note: ${failed} agent(s) failed to complete - results may be incomplete]`;
}
return result;
Use this three-question heuristic:
When in doubt, start with a pipeline. Pipelines are easier to implement correctly, easier to debug, and easier to explain. Refactor to a swarm when profiling reveals that latency is dominated by a sequential stage that could run in parallel - not before.
Both swarms and pipelines can incorporate the supervisor pattern for orchestration - the supervisor manages routing in either architecture. For understanding how individual agents in a swarm or pipeline handle their internal execution, see the agentic loop explained.