Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 39 ยท Planning and Reasoning

The Plan-and-Execute Pattern

Separate planning from doing: a planner writes the steps, an executor runs them with tools, and a checker decides whether to continue. Includes a runnable Python stand-in.

In a ReAct agent one model call decides every next move. In the plan-and-execute pattern the work is split into two roles. A planner reads the goal once and writes the full list of steps. An executor then works through the steps, one at a time, using tools. A third, optional part, the replanner or checker, looks at progress after each step and decides whether to keep going, edit the plan or finish.

This is a very common production design because it is predictable: the plan is visible, the steps are small, and the expensive "big thinking" happens only once or occasionally.

The shape of the pattern

flowchart TD
  G["Goal"] --> P["Planner: write steps"]
  P --> Q["Plan: step list"]
  Q --> E["Executor: do next step with tools"]
  E --> R["Record result"]
  R --> C{"Checker: done or plan still valid?"}
  C -- "more steps" --> E
  C -- "needs change" --> P
  C -- "finished" --> F["Final answer"]

Planner and executor are separate calls, which can even use different models.

Why split planning from execution?

  • Cost control. A strong, slower model writes the plan. A cheaper, faster model executes each small step.
  • Focus. The executor sees one step plus the results it needs, not the whole history, so its context stays small.
  • Inspectability. You can log, show or approve the plan before anything runs. See human-in-the-loop.
  • Testing. You can test the executor on single steps and the planner on goals separately.

The costs: less flexibility when a step reveals a surprise (that is what replanning is for), and one more component to build.

A minimal implementation

Below, the planner and the executor's "model" are scripted stand-ins, so the demo is deterministic. The structure is what you would keep with real models: plan as data, loop over steps, store results, validate each step.

Python Planner, executor and checker

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.

Two details deserve attention. The $1 placeholders let later steps use earlier results without the model copying values, which avoids transcription errors. And when anything goes wrong the executor returns "replan" instead of guessing.

Adding a replanning step

When the executor fails, hand the planner the goal, the steps already completed with their results, and the problem. Ask it for a revised list for the remaining work only.

Python Replan after a failed step

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.

The attempt counter is essential: a replanner can fail forever just as an agent loop can. See stop conditions and budgets.

Real-world shape (read-only)

With a real library the same pattern appears as a graph with three nodes: plan, execute and replan, connected by a conditional edge that returns to execute until the plan is empty. Libraries that model agents as graphs are described in structured agent workflows; the real code needs a provider key and cannot run here, which is why this lesson uses stand-ins.

When to choose it

Choose plan-and-execute when the task is long but mostly predictable, steps can be small and independent, you want a reviewable plan, or you want to save cost with a cheaper executor. Prefer plain ReAct for short, exploratory tasks where each step depends on surprising results, and prefer a fixed workflow when the steps never change at all.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main idea of plan-and-execute?

    Planning and doing are separate roles, often handled by separate calls or models.
  2. 2. Why use placeholders like $1 for earlier results?

    Referencing results avoids copy errors and keeps the executor's input small.
  3. 3. What should happen when a step fails?

    A bounded replanning step lets the agent repair a plan without looping forever.
  4. 4. A benefit of separating planner and executor is:

    Costly reasoning happens once; routine steps can use faster models.

Summary

  • Plan-and-execute splits a planner (writes steps) from an executor (runs them), with a checker deciding what is next.
  • It gives predictability, inspectability, cost control and smaller contexts.
  • Plans are data; placeholders pass earlier results to later steps.
  • Failures trigger bounded replanning with the completed work and the error as input.
  • Use it for long, mostly predictable tasks; use ReAct for exploratory ones.

Next: Reflection and Self-Critique