When to reach for it
- The overall goal can be clearly broken down into testable subtasks.
- Execution needs to be traceable and centrally controllable.
- Cost or latency matters; you want to minimize LLM calls.
/pattern/plan-and-execute/
An agent first generates a complete plan and then executes the steps sequentially or in a controlled manner.
When to reach for it
When it backfires
The tradeoff
Better structure and testability compared to ReAct, but carries the risk of the agent clinging to outdated or unsuitable plans.
Plan first — in one LLM call — then execute the steps you wrote down. The shape of the work is fixed before any tool runs.
class State(TypedDict):
goal: str
plan: list[str]
done: list[tuple[str, str]]
result: str | None
def make_plan(state):
plan = planner_llm.invoke(state["goal"]).steps
return {"plan": plan}
def run_step(state):
step = state["plan"][0]
output = executor.invoke(step, context=state["done"])
return {
"plan": state["plan"][1:],
"done": state["done"] + [(step, output)],
}
graph = StateGraph(State)
graph.add_node("planner", make_plan)
graph.add_node("executor", run_step)
graph.add_edge("planner", "executor")
graph.add_conditional_edges(
"executor",
lambda s: "executor" if s["plan"] else END,
)An early step returns something the planner didn't predict — an empty list, a 404, a different schema. The plan still runs, with the wrong inputs.
Fix · Add a replan trigger. Validate each step's output against the planner's assumption; if invalidated, hand control back to the planner.
Step 2 misreads step 1's output. Step 3 builds on the misread. By step 4, the agent is solving a different problem.
Fix · Pydantic schemas on every step boundary. Cheap validators are worth more than expensive replanning.
The planner writes an ambitious 12-step plan for a task that probably needed three. The cost of executing the plan dwarfs what you saved over ReAct.
Fix · Constrain the plan length in the planner prompt. Reward terseness; penalise speculative steps.
Next pattern
Search patterns, frameworks, and pages.