Swarm and map-reduce

1 min read
On trails:AGPATstep 5/6

The swarm pattern is parallelisation. When the job needs parallel processing, not sequential, such as summarising 10,000 articles, a queen agent spawns a worker agent for each task. The workers are interchangeable, so any of them can be swapped. Microsoft’s guide files the same idea under concurrent orchestration, also called fan-out and fan-in.

Map and reduce

The shape is map-reduce. Map: take the list as input and spawn a worker for each task. Reduce: wait for every worker to finish, collect the results and finalise.

A list of articles goes to a queen agent that spawns one worker per article. The workers run in parallel, and a reduce step collects their results into one result. 10,000 articles Queen map: spawn Worker 1 Worker 2 Worker 3 Worker n Reduce merge results Result
Map spawns the workers. Reduce waits for all of them.

In LangGraph

The map step returns one Send per item, so the number of workers is decided at run time. A reducer on the state field (operator.add) merges what the parallel workers write, and the reduce node runs only after all of them finish.

Python
import operatorfrom typing import Annotated, TypedDictfrom langgraph.graph import StateGraph, START, ENDfrom langgraph.types import Send class State(TypedDict):    articles: list[str]    summaries: Annotated[list[str], operator.add]   # reducer merges parallel writes    digest: str class Job(TypedDict):    article: str def spawn(state: State):                      # map: one worker per article    return [Send("worker", {"article": a}) for a in state["articles"]] def worker(job: Job):    return {"summaries": [job["article"][:20] + "..."]}   # an LLM summary in practice def reduce(state: State):                     # reduce: runs after every worker is done    return {"digest": "\n".join(state["summaries"])} builder = StateGraph(State)builder.add_node("worker", worker)builder.add_node("reduce", reduce)builder.add_conditional_edges(START, spawn, ["worker"])builder.add_edge("worker", "reduce")builder.add_edge("reduce", END)graph = builder.compile()

Why use it

Massive speed and scale. Anthropic reports that running subagents in parallel cut research time by up to 90% on complex queries.

The catch

Expensive. Every worker pays for its own context, and multi-agent runs used about 15× the tokens of a chat in Anthropic’s system. It only fits jobs that split into independent parts, so work where each step needs the last one belongs in a sequenceHandoff patternAgents in a fixed sequence, each passing its output to the next.Opens in a new tab. And it adds complexity: merging results, and deciding what happens when one worker fails.


Sources