Hierarchical Task Decomposition
Top-level goal -> sub-goals -> sub-sub-goals — the only way humans (and agents) tackle big tasks.
Hierarchical Task Decomposition is a free AI Agents lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
When Tasks Get Big
"Refactor the auth module" is too big for one prompt. The agent must break it into smaller, manageable pieces.
Hierarchical task decomposition (HTD) is how humans and agents tackle this.
The Hierarchy
- Goal — the high-level objective
- Sub-goals — major phases
- Tasks — concrete steps
- Actions — individual tool calls
Example Decomposition
Goal: Refactor authentication module to use OAuth.
Sub-goal 1: Set up OAuth dependencies
Task: Add dependency to requirements.txt
Task: Configure OAuth client
Sub-goal 2: Replace existing auth flow
Task: Replace login endpoint
Task: Replace session middleware
Task: Update tests
Sub-goal 3: Migration and rollout
Task: Write migration script
Task: Update documentationLLM-Driven Decomposition
DECOMPOSE_PROMPT = '''
Decompose this goal into 3-6 sub-goals. For each sub-goal, list 2-5 concrete tasks.
Return JSON: {sub_goals: [{name, tasks: [str]}]}
Goal: {goal}
'''Recursive Decomposition
If a task is still too big, decompose IT further. Stop when a task fits in a single LLM step (~3-5 tool calls).
When to Stop Decomposing
Heuristics:
- Task description fits in one sentence
- Task has clear acceptance criteria
- Task touches a small number of files
- Task is verifiable by a specific test
Tree Structures
Represent the decomposition as a tree. Each node is a task with: description, status (pending/running/done/failed), children:
@dataclass
class TaskNode:
description: str
status: Literal['pending', 'running', 'done', 'failed']
children: list['TaskNode']
result: str | None = NoneExecute Depth-First
Walk the tree depth-first. Complete each leaf before moving on:
def execute(node):
if node.children:
for child in node.children:
execute(child)
else:
node.status = 'running'
try:
node.result = run_leaf(node)
node.status = 'done'
except Exception as e:
node.status = 'failed'Aggregating Results
After children complete, aggregate at the parent:
def aggregate(parent):
summaries = [c.result for c in parent.children if c.status == 'done']
parent.result = llm.invoke(f'Summarise these sub-task results: {summaries}').contentSurface the Tree to Users
For long-running agents, show users the tree with live status updates. Trust and transparency:
[x] Add OAuth dependency
[x] Configure OAuth client
[~] Replace login endpoint <-- in progress
[ ] Replace session middleware
[ ] Update testsAllow Tree Editing
Power users may want to skip, add, or reorder tasks. Support manual edits to the plan tree.
Re-Plan on Failure
If a sub-task fails repeatedly, kick back up to the parent and re-decompose:
if failed_count(node) > 2:
parent = node.parent
parent.children = llm_replan(parent, failure_context=node.last_error)Cost of Decomposition
Each decomposition is an LLM call. Caching helps — same goal -> same initial decomposition.
When to Stop Splitting
When should the agent stop decomposing tasks?
Recap
Goals -> sub-goals -> tasks -> actions. Build a tree, execute depth-first, aggregate results, replan on failure. The standard pattern for long-horizon work.
Frequently asked questions
Is the “Hierarchical Task Decomposition” lesson free?
Yes — the full text of “Hierarchical Task Decomposition” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.
What will I learn in “Hierarchical Task Decomposition”?
Top-level goal -> sub-goals -> sub-sub-goals — the only way humans (and agents) tackle big tasks. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Agents?
No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Hierarchical Task Decomposition” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Agents lesson?
Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Hierarchical Task Decomposition
- Goal Stacks and Backtracking
- World Models and Lookahead
- Reflection and Self-Critique Loops