Sub topic 9
Running subagents in parallel or in sequence
This chapter assumes you can read basic Python, use Git branches, and know that an agent works in a loop of tool calls inside a context window. You should also know what a subagent is and how an orchestrator hands it a brief.
The deciding question
Run two subagents in parallel only if neither needs the other's output and they do not touch the same files. Otherwise run them in sequence. Parallel runs finish sooner. Sequential runs are easier to reason about.
Ask two questions about each pair of tasks:
- Data dependency: does task B need a result, decision, or file that task A produces?
- Write overlap: will both tasks edit the same file, lock file, or generated artifact?
| Data dependency | Write overlap | Choice |
|---|---|---|
| No | No | Parallel |
| No | Yes | Parallel in separate worktrees, or sequential |
| Yes | Either | Sequential |
A mixed plan
Real tasks often mix both. Suppose you add a --verbose flag. Changing parse_args must come first, because the other work depends on its final shape. Docs and tests can then run side by side.
The orchestrator waits for the first stage, then starts the second stage together.
Running a parallel stage
Use asyncio.gather to run independent tasks at once. Setting return_exceptions=True means one failed subagent does not cancel the others. The orchestrator records the failure and decides what to do.
import asyncio
from dataclasses import dataclass
@dataclass
class Task:
name: str
brief: str
async def run_subagent(task: Task) -> dict:
# Placeholder: replace with a call to your agent runtime.
# The subagent receives only the brief, not the parent's history.
await asyncio.sleep(0.1)
return {"name": task.name, "status": "ok", "summary": f"finished {task.name}"}
async def orchestrate(tasks: list[Task]) -> list[dict]:
outcomes = await asyncio.gather(
*(run_subagent(t) for t in tasks), return_exceptions=True
)
results = []
for task, outcome in zip(tasks, outcomes):
if isinstance(outcome, Exception):
results.append({"name": task.name, "status": "failed", "summary": str(outcome)})
else:
results.append(outcome)
return results
if __name__ == "__main__":
tasks = [
Task("update-docs", "Update README usage section for the new --verbose flag."),
Task("add-tests", "Add unit tests for parse_args in tests/test_cli.py."),
]
for result in asyncio.run(orchestrate(tasks)):
print(result)A sequential stage is just an await on one call, followed by a check of its status before the next stage starts.
Common errors
- Hidden dependencies. Two tasks look independent but both rely on a function signature that is still changing. Put the change that fixes that signature in an earlier stage.
- Shared files. Two subagents editing
README.mdorpackage.jsonin one checkout will overwrite each other. Give each its own worktree and branch (see the next chapter), or serialize them. - Too much parallelism. Many simultaneous subagents can hit API rate limits and make review harder. Cap concurrency, for example with
asyncio.Semaphore. - Skipping the gate. Starting stage two when stage one failed wastes work on a broken base. Check status first.
Exercise
You need to (1) rename a function in src/utils.py, (2) update every caller in src/, and (3) write a changelog entry. Which tasks can run in parallel, and which must wait?
Show answer
Task 2 depends on task 1, because callers need the final name, so it runs after task 1 in sequence. Task 3 depends on neither, so it can run in parallel with task 1 and task 2, as long as it edits only the changelog file and no other task touches that file.