Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 39 ยท Planning and Reasoning

Planning and Task Decomposition

Break big goals into small, checkable steps: plans as data, dependencies and ordering, topological sorting, hierarchical plans and when planning is worth its cost.

"Write a market report on electric bikes" is not a step an agent can execute. It is a goal. Between a goal and the first tool call sits planning: turning the goal into smaller tasks, deciding their order, and knowing when each is done. Planning is what lets an agent handle work that takes dozens of steps without wandering. This lesson treats a plan as ordinary data you can inspect, validate and run.

Why plan at all?

A pure ReAct agent picks each next step from the latest observation. That is flexible, but on long tasks it can skip required parts, repeat work or forget the original goal. A plan gives the agent:

  • Coverage: every required part of the goal appears as a task.
  • Order: prerequisites are done before the tasks that need them.
  • Progress: you can show "step 3 of 7" and detect when work is finished.
  • Control: a human can review a plan before the agent acts.

Planning costs an extra model call and can go stale when reality differs from expectations, so use it when the task is long, risky or expensive. For a two-step lookup, skip it.

A plan is data

Ask the model for a structured plan, not prose. Each task gets an id, a description, and the ids it depends on.

JSON
{
  "goal": "Compare two laptops and recommend one",
  "tasks": [
    {"id": "t1", "do": "Fetch specs for Nova 13", "needs": []},
    {"id": "t2", "do": "Fetch specs for Atlas 15", "needs": []},
    {"id": "t3", "do": "Compare price, weight and battery", "needs": ["t1", "t2"]},
    {"id": "t4", "do": "Write the recommendation", "needs": ["t3"]}
  ]
}

Tasks t1 and t2 are independent, so they could run in parallel; t3 must wait for both.

flowchart LR
  t1["t1: specs Nova 13"] --> t3["t3: compare"]
  t2["t2: specs Atlas 15"] --> t3
  t3 --> t4["t4: recommend"]

A plan is a directed acyclic graph (DAG) of tasks.

Validating a plan

Model-written plans can contain mistakes: a missing dependency, a typo in an id, or a cycle (A needs B, B needs A) that would never finish. Validate before executing.

Python Validate a plan and find a safe order

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

graphlib is in Python's standard library. Its static_order gives one valid execution order, and it raises CycleError for impossible plans. If validation fails, send the problems back to the model and ask for a corrected plan.

Running a plan, with parallel waves

Tasks whose dependencies are done can run together. TopologicalSorter can hand out "ready" tasks in waves.

Python Execute a plan wave by wave

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

How to ask for a good plan

A good planning prompt states the goal, the available tools, and the plan format, and asks for tasks that are small, concrete and checkable. A task such as "do research" is too vague; "search for 3 recent articles and note their main claims" can be completed and verified. Include a done criterion for each task when it is not obvious ("table has at least 5 rows").

TEXT
Goal: <goal>
Tools you can use later: search_web, read_url, calculator.
Return JSON: {"tasks": [{"id": "...", "do": "...", "needs": ["..."], "done_when": "..."}]}
Rules: 3 to 8 tasks; each task completable in one or two tool calls; no cycles.

Hierarchical plans

Big goals break down in levels: a goal becomes milestones, each milestone becomes tasks. A hierarchical planner plans coarsely first, then expands only the milestone it is about to work on. This keeps early plans short and lets later details use what has been learned. It is the same idea as a manager handing a project to a team: see the orchestrator-worker pattern.

Plans change

A plan is a hypothesis. Tools fail, results surprise you, the user changes their mind. Keep the plan in state (see agent state), mark tasks pending, running, done or failed, and let the agent revise it. Executing a fixed plan is the subject of plan-and-execute; handling surprises is covered in replanning and recovery.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is a plan better stored as structured data than as prose?

    Structured plans can be checked for missing dependencies and cycles, and run step by step.
  2. 2. What problem does a cycle in a plan cause?

    If A needs B and B needs A, neither can start.
  3. 3. Which task description is best?

    Small, concrete, checkable tasks can be executed and verified.
  4. 4. When is planning probably NOT worth its cost?

    For tiny tasks the extra call adds latency without improving results.

Summary

  • Planning turns a goal into ordered, checkable tasks, giving coverage, order, progress and control.
  • Represent plans as data: id, description, dependencies, optional done criterion.
  • Validate for unknown dependencies and cycles; topological sort gives a safe order and parallel waves.
  • Plan coarse-to-fine for big goals, and treat every plan as revisable.
  • Skip planning for trivial tasks.

Next: The Plan-and-Execute Pattern