Lesson 47.5 · 5 of 5 · 7 min read · Reviewed 24 Nov 2026
Task decomposition: splitting work an agent can finish
By the end, you can
- Split a large task into subtasks an agent can finish, each with its own done criterion.
- Choose a decomposition strategy (by step, by component or by question) and arrange the subtasks as a small dependency graph.
AgentsAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. fail on big, vague tasks in a typical way: they make progress, run out of context or focus, and stop somewhere in the middle with nothing you can check. Planning (47.1) says what to do in order. Decomposition is the skillSkill: A folder of instructions, scripts and resources that an agent loads when relevant to a task. underneath: cutting the work into pieces an agent can actually finish.
Three ways to split
| Strategy | Split along | Good for | Example |
|---|---|---|---|
| By step | The stages of a process | Pipelines, migrations, reports | Export, transform, load, verify |
| By component | Parts of a system | Code changes, documents with sections | API layer, database layer, UI |
| By question | Sub-questions to answer | Research, analysis, diagnosis | Who are the users? What do rivals charge? What do reviews complain about? |
Research on prompting found that breaking a hard problem into easier sub-problems, then solving them in order, lets models solve tasks they fail on in one go. The same holds for agents.
The right size
A good subtask:
- Fits in one context. The agent can see everything it needs without trimming.
- Takes a handful of tool calls, not dozens.
- Has one owner. One agent or one model call is responsible.
- Ends with a done criterion that a script or a reviewer can check.
Too big, and you are back to the stalled hour. Too small, and every hand-off loses context and adds cost.
Dependencies as a small graph
Write each subtask with what it needs. That forms a directed acyclic graph (a DAG): arrows point from a subtask to the ones that need its output, and there are no loops.
A export schema needs: - done: schema.sql has 14 tables
B port read queries needs: A done: 30 read tests pass
C port write queries needs: A done: 12 write tests pass
D run full test suite needs: B, C done: 0 failures
E write cut-over runbook needs: A done: reviewer signs off
Read it in waves. A runs first. B, C and E only need A, so they can run in parallel. D waits for B and C. If C fails, you know exactly which piece to retry, and B’s work is kept.
Done criteria per subtask
“Port the queries” is a task. “30 read tests pass” is a done criterion. Each subtask should end with something checkable: a test run, a file that exists with the right shape, a count, or a reviewer’s yes. Without it, the agent decides for itself when it is finished, and it is usually optimistic.
When not to decompose
- The task is small. If one call can do it well, splitting adds overhead and new ways to fail.
- The parts are tightly coupled. Writing a short story by paragraph loses the voice. Some work needs one mind holding all of it.
- You cannot state done criteria. If you cannot say what finished looks like for a piece, the split is probably wrong. Rethink the seams.
- The hand-off cost is high. Every subtask needs its own context. If each needs the same large document, the repeated context may cost more than the split saves.
Common mistakes
- Splitting by time (“first half, second half”) instead of by meaning.
- Hidden dependencies: two “independent” subtasks that both edit the same file.
- Done criteria that are just the task restated: “queries are ported”.
Key points
- A good subtask fits in one context, uses a few tool calls and ends with a check you can run.
- Split by step for pipelines, by component for systems, and by question for research.
- Write dependencies as a small graph: subtasks with no unmet dependencies can run in parallel.
- Do not decompose when the task is small, or when the parts depend on each other so tightly that splitting loses the context.
Run a task graph in waves
Turn a decomposed task into a small DAG with a done check per subtask, and run it in dependency order.
You will have: A script that groups five subtasks into waves, checks each against its done criterion, retries only the failed one and rejects a plan with a cycle.
Steps
- Run the starter: it puts every subtask in one wave and ignores dependencies.
- Write waves(): each wave holds the tasks whose needs are already done; raise an error if none are ready.
- Run the waves in order and check every result with the subtask's done function.
- Retry only a subtask that fails its check, once, then hand it to a person.
- Show that a plan where X needs Y and Y needs X is caught.
Done when
- The waves are A, then B, C and E, then D.
- Only C is retried, and B's finished work is kept.
- The cyclic plan raises an error instead of looping.
Open in the Playground No API key needed.
Starter code (Python)
# Split a migration into subtasks with done criteria, then run them in waves of a small DAG.
TASKS = {
"A": {"title": "export schema", "needs": [], "done": lambda r: r["tables"] == 14},
"B": {"title": "port read queries", "needs": ["A"], "done": lambda r: r["failed"] == 0 and r["passed"] == 30},
"C": {"title": "port write queries", "needs": ["A"], "done": lambda r: r["failed"] == 0 and r["passed"] == 12},
"D": {"title": "run full test suite", "needs": ["B", "C"], "done": lambda r: r["failed"] == 0},
"E": {"title": "write cut-over runbook", "needs": ["A"], "done": lambda r: r["approved"]},
}
ATTEMPTS = {}
def run_subtask(tid):
# A simulated agent doing one subtask. Write queries fail 3 tests on the first try.
ATTEMPTS[tid] = ATTEMPTS.get(tid, 0) + 1
return {
"A": {"tables": 14},
"B": {"passed": 30, "failed": 0},
"C": {"passed": 9, "failed": 3} if ATTEMPTS[tid] == 1 else {"passed": 12, "failed": 0},
"D": {"passed": 42, "failed": 0},
"E": {"approved": True},
}[tid]
def waves(tasks):
# TODO: return a list of waves. Each wave is a sorted list of the task ids whose "needs"
# are all in earlier waves. Raise ValueError if no task is ready (a cycle).
# Expected for TASKS: [["A"], ["B", "C", "E"], ["D"]]
return [sorted(tasks)] # wrong: ignores dependencies
plan = waves(TASKS)
for i, wave in enumerate(plan, 1):
print(f"wave {i}: {', '.join(wave)}")
# TODO: run each wave in order. For each subtask, call run_subtask(tid) and check it with
# TASKS[tid]["done"](result). If the check fails, print it and retry only that subtask once.
# If it fails twice, stop and hand it to a person. Print the ids you retried.
# TODO: show that waves() rejects a cycle such as X needs Y, Y needs X.
Show a solution
# Split a migration into subtasks with done criteria, then run them in waves of a small DAG.
TASKS = {
"A": {"title": "export schema", "needs": [], "done": lambda r: r["tables"] == 14},
"B": {"title": "port read queries", "needs": ["A"], "done": lambda r: r["failed"] == 0 and r["passed"] == 30},
"C": {"title": "port write queries", "needs": ["A"], "done": lambda r: r["failed"] == 0 and r["passed"] == 12},
"D": {"title": "run full test suite", "needs": ["B", "C"], "done": lambda r: r["failed"] == 0},
"E": {"title": "write cut-over runbook", "needs": ["A"], "done": lambda r: r["approved"]},
}
ATTEMPTS = {}
def run_subtask(tid):
# A simulated agent doing one subtask. Write queries fail 3 tests on the first try.
ATTEMPTS[tid] = ATTEMPTS.get(tid, 0) + 1
return {
"A": {"tables": 14},
"B": {"passed": 30, "failed": 0},
"C": {"passed": 9, "failed": 3} if ATTEMPTS[tid] == 1 else {"passed": 12, "failed": 0},
"D": {"passed": 42, "failed": 0},
"E": {"approved": True},
}[tid]
def waves(tasks):
# Group subtasks into waves: each wave holds every task whose needs are already done.
done, out = set(), []
while len(done) < len(tasks):
ready = sorted(t for t in tasks if t not in done and all(n in done for n in tasks[t]["needs"]))
if not ready:
raise ValueError("cycle or missing dependency: " + ", ".join(sorted(set(tasks) - done)))
out.append(ready)
done.update(ready)
return out
plan = waves(TASKS)
for i, wave in enumerate(plan, 1):
print(f"wave {i}: {', '.join(wave)}")
retried = []
for wave in plan:
for tid in wave: # tasks in one wave are independent, so they could run in parallel
for attempt in range(1, 3): # one retry per subtask
result = run_subtask(tid)
if TASKS[tid]["done"](result):
break
print(f"{tid} not done: {result}, retrying only {tid}")
retried.append(tid)
else:
raise SystemExit(f"{tid} failed twice: hand it to a person")
print("retried:", ", ".join(retried) or "none")
print("all subtasks done:", all(ATTEMPTS[t] >= 1 for t in TASKS))
try:
waves({"X": {"needs": ["Y"]}, "Y": {"needs": ["X"]}})
except ValueError as e:
print("bad plan caught:", e)
Finished the chapter? Take the chapter check.