The Core Idea: Big goals are solved by breaking them into subtasks, ordering them by dependencies, executing them, and replanning when something fails or something new is learned, while keeping the work that is already done.
- Beginner: Why Plan?
Some tasks are too large to handle with one decision at a time. Write a report comparing electric-car markets and battery technology involves research, comparison and writing. Without a plan an agent may wander, repeat itself, or forget a part.
Analogy: A plan is a map. You can still change the route, but you know where you are and where you are going.
- Beginner: Breaking Complex Tasks Into Subtasks
Task decomposition splits a large goal into smaller pieces that are each easy to do and check.
| Good subtask | Why |
|---|---|
| Has one clear outcome | Easy to tell when it is done |
| Small enough for a few tool calls | Fits one focused step |
| States its inputs | Knows what it needs from earlier subtasks |
| Can be checked | A test or a rule says whether it worked |
- Medium: Planning and Sequential Execution
A plan is more than a list: some subtasks depend on others. In the example, compare needs both data subtasks, and write the report needs the comparison. Drawing these links gives a dependency graph.
Sequential execution runs subtasks one after another in a valid order. Independent subtasks (the two data searches) could even run in parallel, which a later lesson covers.
In the code: Python's graph tools list a valid order in which every subtask comes after the ones it depends on.
Two planning styles: Plan first, then execute (predictable, but the plan can go stale) and plan a little, act, plan again (flexible, but more model calls).
- Medium: Dynamic Planning
The world rarely follows the plan exactly. Dynamic planning means the plan is a living document: after each result, the agent can add, remove or reorder subtasks.
| New information | Plan change |
|---|---|
| A search shows the topic is bigger than expected | Add more research subtasks |
| A needed tool is unavailable | Choose another route |
| An early result makes a later step unnecessary | Remove the step |
| The user changes the goal | Rewrite the plan, keep reusable results |
Rule of thumb: Plan in enough detail for the next few steps and keep the rest loose. Over-detailed long plans break first.
- Advanced: Replanning After Failure
When a subtask fails, the agent should not start from zero. Replanning keeps the completed work and changes only what is broken.
Danger: Unlimited replanning is a loop in disguise. Always cap the number of replans.
- Visual Explanation: A Plan With Dependencies and a Replan
The figure shows the dependency structure of the plan in the code, then what happens when one subtask fails and the plan is replaced.
Click to see why we keep finished workCompare ▼
Part 1 shows the order constraints. Part 2 shows the failure and the swap. Open the bar to see why finished work is kept.
- Python Implementation: Dependency Planning and Replanning
This script builds a plan as a dependency graph, finds a valid execution order, runs it until one subtask fails, then replans by swapping in a backup subtask while keeping the completed work.
from graphlib import TopologicalSorter # A plan is a set of subtasks and what each one depends onplan = { "find_market_data": [], "find_tech_data": [], "compare": ["find_market_data", "find_tech_data"], "write_report": ["compare"],}completed = set()broken = {"find_market_data"} # this subtask will fail the first time def execute(plan, completed, broken): ts = TopologicalSorter(plan) ts.prepare() while ts.is_active(): failed = [] for task in sorted(ts.get_ready()): if task in completed: ts.done(task) # already finished earlier: skip elif task in broken: failed.append(task) else: print(" ran:", task) completed.add(task) ts.done(task) if failed: return failed return [] print("Plan 1 order if nothing fails:", list(TopologicalSorter(plan).static_order()))print("Executing plan 1")failed = execute(plan, completed, broken)print(" FAILED:", failed) # Replanning: swap the failed subtask for a backup, keep the finished workdef replace(plan, old, new): return {(new if k == old else k): [new if d == old else d for d in deps] for k, deps in plan.items()} plan2 = replace(plan, "find_market_data", "find_market_data_backup")print("Executing plan 2 (replanned)")failed = execute(plan2, completed, broken)print(" Finished. Everything done:", sorted(completed))The first execution completes the technology search and then stops at the failed market search. The replanned run swaps in the backup subtask and runs only the remaining work: the backup, the comparison and the report.
Knowledge Check
Key Points
The Big Picture
Decompose → order by dependencies → execute → replan the broken parts only
A good plan turns a vague goal into small steps the agent can do and check.
Up Next: Plans work best when the agent thinks, acts and learns from each result. In ReAct, Reflection and Self-Correction you will see how agents reason while acting and fix their own mistakes.