Sub topic 12
Parallel fan-out and merging results
This chapter assumes you have read the orchestrator and agents-as-tools chapters. You should be comfortable with Python, JSON, and the idea that an orchestrator calls specialists as ordinary tools.
What fan-out is
Fan-out means the orchestrator starts several independent subtasks at the same time instead of one after another. Merging is the step where it combines the returned results into one answer.
Fan-out helps only when the subtasks do not depend on each other. If the writer needs the researcher's output, the calls must run in sequence. If three researchers each look at a different source, they can run together.
Running workers in parallel
Model calls spend most of their time waiting on the network, so asyncio works well. The sketch below uses a stand-in worker so it runs as shown. In a real system, the worker would call a model.
import asyncio
async def worker(name: str, task: str) -> str:
await asyncio.sleep(0.1) # stands in for a model call
if name == "B":
raise ValueError("source unavailable")
return f"{name}: findings for '{task}'"
async def fan_out(tasks: dict[str, str]) -> dict[str, str]:
results = await asyncio.gather(
*(worker(n, t) for n, t in tasks.items()),
return_exceptions=True,
)
merged = {}
for name, result in zip(tasks, results):
if isinstance(result, Exception):
merged[name] = f"FAILED: {result}"
else:
merged[name] = result
return merged
tasks = {"A": "pricing", "B": "reviews", "C": "competitors"}
print(asyncio.run(fan_out(tasks)))return_exceptions=True matters. Without it, the first failure raises out of gather, and you lose the results of workers that succeeded. With it, each failure comes back as a value that you can handle.
Merging results
There are three common ways to merge.
| Strategy | How it works | Best for |
|---|---|---|
| Concatenate | Join the outputs in a fixed order | Independent sections of a report |
| Model synthesis | Give all outputs to an agent that writes one answer | Overlapping or conflicting findings |
| Vote or select | Pick the majority answer or the best-scored one | Checking a single factual answer |
Whatever you choose, label each result with the worker that produced it. A synthesis agent can then say which source a claim came from, and it can notice when a worker failed.
Common errors
- Hidden dependencies. Two "independent" workers both edit the same file or record. Parallel runs then overwrite each other. Give each worker its own output.
- Silent partial results. If one worker fails and the merge step does not know, the final answer reads as complete when it is not. Pass failures into the merge step explicitly, as the code above does.
- No concurrency limit. Fanning out to 50 workers can hit provider rate limits. Use
asyncio.Semaphoreto cap how many run at once. - Merge order changes meaning. Results arrive in completion order in some designs. Keep a stable key per worker.
Exercise
A fan-out of four workers uses asyncio.gather without return_exceptions=True. Worker 3 raises an error after the other three have finished successfully. What happens to the three good results, and how do you fix it?
Show answer
The exception propagates out of gather, so the orchestrator never receives the three good results. Pass return_exceptions=True, then check each returned value with isinstance(result, Exception) and either retry the failed worker or tell the merge step that it failed.