Lesson 6 of 9 · Patterns
Plan and Execute
The plan-and-execute agent pattern: a planner writes the plan, an executor runs each step and the agent replans on failure. When it beats ReAct, in Python.
- 5 min read
- Intermediate
- Reviewed
- Explain the plan-and-execute pattern and its two roles
- Run a plan, detect a failed step and replan
- Check a generated plan before executing it
- Choose between plan-and-execute and step-by-step ReAct
Think first, then do
ReAct decides one step at a time. For long tasks, that can drift. Plan and execute splits the work:
- The planner sees the whole goal and thinks about order and dependencies once.
- The executor handles one simple step at a time, which is cheaper and easier to check.
- Replanning fixes the plan when reality doesn't match it.
A plan that has to change
# ---- Plan and execute: one planning call, then simple steps, replanning if a step fails ----
def planner(goal, failed_step=None):
"""Stands in for an LLM that writes a plan (and a revised plan after a failure)."""
if failed_step is None:
return ["find venue near office", "check venue has projector", "book venue", "email team"]
return ["find a different venue near office", "check venue has projector", "book venue", "email team"]
VENUES = {"find venue near office": "Riverside Room", "find a different venue near office": "Harbour Hall"}
HAS_PROJECTOR = {"Riverside Room": False, "Harbour Hall": True}
def executor(step, notes):
"""Runs one simple step. A small, cheap model or plain code is often enough here."""
if step.startswith("find"):
notes["venue"] = VENUES[step]
return True, notes["venue"]
if step == "check venue has projector":
ok = HAS_PROJECTOR[notes["venue"]]
return ok, "projector: yes" if ok else "no projector"
return True, f"{step}: done ({notes['venue']})"
goal = "Book a meeting room with a projector for Friday"
plan, notes, replans = planner(goal), {}, 0
print("Plan:", plan)
i = 0
while i < len(plan):
ok, result = executor(plan[i], notes)
print(f" {'OK ' if ok else 'FAIL'} {plan[i]} -> {result}")
if not ok:
if replans == 2:
print("Giving up after 2 replans"); break
replans += 1
plan, i = planner(goal, failed_step=plan[i]), 0
print("Replanned:", plan)
continue
i += 1The first venue had no projector, so the check failed. Instead of pushing on and booking the wrong room, the agent asked the planner for a new plan, which found a different venue, and the rest of the steps ran normally. A limit of two replans stops it from replanning forever.
Plan and execute vs ReAct
| Plan and execute | ReAct (step by step) | |
|---|---|---|
| Best for | Long tasks with predictable structure | Investigations where each step depends on the last |
| Cost | One big planning call, cheap execution steps | A full model call for every step |
| Staying on track | Good: the plan keeps the goal in view | Can drift on long tasks |
| Handling surprises | Needs replanning | Natural: it decides after every observation |
| Transparency | High: the plan can be shown and approved | Lower: the path emerges as it runs |
Many real agents combine them: a plan at the top level, with each step handled by a small ReAct loop.
Common mistakes
Check a generated plan for steps that come before the steps they depend on.
# A plan from the planner: each step lists the steps it needs to be done first
plan = [
("get flight prices", []),
("choose cheapest flight", ["get flight prices"]),
("email itinerary", ["book flight"]),
("book flight", ["choose cheapest flight"]),
]
# Check the plan before executing it. For each step, every step it needs must appear
# EARLIER in the list. Print the problems like:
# 'email itinerary' needs 'book flight' first
# If there are none, print: Plan OK
When is plan-and-execute usually a better fit than step-by-step ReAct?
Summary
- Plan and execute separates a planner (whole task) from an executor (one step at a time).
- Check each step and replan when one fails, with a limit on replans.
- It is cheaper and more transparent for long, structured tasks; ReAct handles surprises more naturally.
- Show plans to users, and combine patterns where it helps.
Next: reflection, letting an agent check and improve its own work.
Finished reading? Tick it off to track your progress.