Sub topic 5
Breaking a Coding Task into Delegable Units
This chapter assumes you have used a coding agent in a terminal or editor, know Git branches and the command line, and can read basic Python. It also assumes you know what a context window is and how tool calls work. By the end, you should be able to split a coding task into pieces that separate subagents can complete without stepping on each other.
Why decomposition comes first
A subagent starts with an empty context. It sees only the brief you give it. If you hand it a vague or oversized piece of work, it will guess, and the guesses will not match what the orchestrator expected. Decomposition is the step that decides whether delegation helps or just adds cost.
A good unit of work is one that a capable engineer could finish from a written note alone, without asking questions. Four properties make a unit delegable.
- Self-contained. The subagent can finish it using the brief and the repository, with no knowledge of the parent's conversation.
- Bounded. It touches a known, small set of files or directories.
- Checkable. A command or an observable result shows it is done, such as a passing test or a successful build.
- Summarizable. The result fits in a short report, so the parent's context stays small.
If a unit fails any of these, either split it further or keep it with the orchestrator.
Find the seams
A seam is a place where the code or the work divides cleanly. Look for seams in these places.
| Seam | Example split | Why it works |
|---|---|---|
| Directory or module | `src/api/` and `src/billing/` | Different files, few shared edits |
| Layer | implementation, tests, docs | Each output lives in different files |
| Activity | research, edit, verify | Each needs different tools |
| Independent item | migrate 12 files to a new API | Same recipe, no cross-dependencies |
Splitting by activity deserves a note. Research tasks, such as "find every caller of parse_args and list how they pass options", are often the best first delegation. They read a lot of code but write nothing, so they carry no conflict risk. The subagent reads many files and returns a short list, and the parent never loads those files.
Map the dependencies
After you list candidate units, ask of each pair: does unit B need anything that unit A produces? Draw the answer as a graph.
In this graph, the tests unit and the docs unit do not depend on each other. They can run at the same time once the implementation exists. The implementation cannot start before the research is done, if the research changes the design. Those edges tell you what must run in sequence and what can run in parallel. A later chapter covers that choice in detail. For now, the point is that you cannot see it until you have decomposed the work and written the dependencies down.
There are three kinds of dependency to look for.
- Data dependency. B needs a file, function, or answer that A creates.
- Interface dependency. A and B can run together only if both agree on a signature or format first. Fix the interface in the brief, for example "the function is
parse_args(argv: list[str]) -> Namespace", and both units can proceed. - Write conflict. A and B edit the same file. They have no logical dependency, but running them at once risks overwritten work. Either merge them into one unit, run them in sequence, or give each its own workspace.
Worked example
Suppose the task is: "Add a --verbose flag to our CLI, test it, and document it." A single agent could do this in one pass. Decomposing it anyway shows the method.
- List the outputs. Changed code in
src/cli.py, new tests intests/test_cli.py, an edited section inREADME.md. - Assign each output to one owner. Each file has exactly one unit that may edit it. This removes write conflicts.
- Record dependencies. Tests and docs both need to know the flag's name and behavior. Both depend on the implementation, or on a fixed interface.
- Write a done condition for each unit. For tests:
pytest tests/test_cli.pypasses. For docs: the README usage section lists--verbosewith a one-line description.
Because the flag's name and meaning are already decided in the task, the interface is fixed, and the tests and docs units can run beside the implementation. That gives you three units, with the orchestrator doing the review at the end.
The Python below shows the units as data. The orchestrator can inspect them before running anything, which makes decomposition an explicit step rather than something that happens implicitly in a prompt.
from dataclasses import dataclass, field
@dataclass
class Unit:
name: str
owns: set[str] # paths this unit is allowed to edit
depends_on: set[str] = field(default_factory=set)
done_when: str = ""
def find_write_conflicts(units: list[Unit]) -> list[tuple[str, str, str]]:
conflicts = []
for i, a in enumerate(units):
for b in units[i + 1:]:
shared = a.owns & b.owns
for path in sorted(shared):
conflicts.append((a.name, b.name, path))
return conflicts
def ready_to_run(units: list[Unit], finished: set[str]) -> list[str]:
return [
u.name
for u in units
if u.name not in finished and u.depends_on <= finished
]
if __name__ == "__main__":
units = [
Unit("implement", {"src/cli.py"}, done_when="python -m src.cli --help shows --verbose"),
Unit("tests", {"tests/test_cli.py"}, done_when="pytest tests/test_cli.py passes"),
Unit("docs", {"README.md"}, done_when="README lists --verbose"),
]
print("conflicts:", find_write_conflicts(units))
print("ready now:", ready_to_run(units, finished=set()))Running it prints an empty conflict list and all three units as ready, because no unit shares a file or depends on another. If you add src/cli.py to the docs unit's owns set, the conflict check reports it before any subagent starts.
Sizing units
Granularity is a trade-off. Each subagent costs a fresh context, a brief to write, and a result to read and check.
- Too large. The subagent runs for a long time, fills its own context, and returns something hard to review. A failure wastes a lot of work.
- Too small. The overhead of the brief and the review exceeds the work itself. Renaming one variable does not need a subagent.
A practical rule is to size a unit so that its brief takes a few sentences to write and its result takes a few sentences to check. If the brief needs several pages, the unit is probably several units. If the brief is shorter than the result you expect to review, the unit is probably too small to delegate.
Common mistakes
- Splitting by file count rather than by meaning. Dividing 20 files into four groups of five is only safe if the files do not depend on each other. Check imports first.
- Hidden shared state. Two units that both run a test suite writing to the same database file, or both update a lock file, can interfere without touching the same source files.
- Leaving the decision to the subagent. If a design choice affects several units, the orchestrator must make it and put it in each brief. A subagent cannot see its siblings' decisions.
- No done condition. Without a check the subagent can run, "done" means whatever the subagent decides. Always write one.
- Delegating the thinking. Handing off "figure out the best architecture" produces an answer that the parent must re-verify from scratch. Delegate the gathering of facts and the execution of decided work. Keep the judgment calls with the orchestrator.
Checklist before delegating
- Each unit has one owner for every file it edits.
- Every dependency between units is written down as a data, interface, or write-conflict edge.
- Shared interfaces are fixed in the briefs.
- Each unit has a done condition a command can check.
- Each unit is large enough to justify its brief and small enough to review quickly.
Exercise
A task asks you to rename a function load_config to read_config across a repository, update its three unit tests, and update the docs page that mentions it. The function is defined in src/config.py and called from 15 files in src/. Propose a decomposition. Name the units, which files each owns, which can run in parallel, and one risk you would check before starting.
Show answer
One reasonable split: (1) a research unit that lists every caller and every textual mention of load_config, owning no files (read only); (2) a rename unit that edits src/config.py and the 15 caller files, which should be a single unit or be split by directory only if no caller file is shared; (3) a tests unit owning the test file; (4) a docs unit owning the docs page. After the research unit finishes, the tests and docs units can run in parallel with each other because they own different files and the new name is fixed in the brief. The rename unit should run before the full test suite check, which the orchestrator runs last. Risks to check first: callers that use the name dynamically (strings, getattr, config files) that a text search for the identifier would miss, and whether two caller files import from each other so that splitting the rename across subagents would leave the code temporarily broken.
Exercise
Two proposed units are "add input validation to src/api/users.py" and "add logging to src/api/users.py". Are they independent? What are your options?
Show answer
They are not independent, because both own src/api/users.py and so have a write conflict, even though the changes may touch different lines. Options: merge them into one unit; run them in sequence so the second starts from the first's result; or run them in parallel in separate git worktrees on separate branches and merge afterward, accepting that the orchestrator may have to resolve a merge conflict. For two small edits to one file, merging them into one unit is usually cheapest.