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.
Plan once. Execute the list.
An agent first generates a complete plan and then executes the steps sequentially or in a controlled manner.
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,
)When to reach for it
When it backfires
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.
Countermeasure
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.
Countermeasure
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.
Countermeasure
Constrain the plan length in the planner prompt. Reward terseness; penalize speculative steps.
Threat exposure
Adopting this pattern opens these attack surfaces. Each links to its entry in the threat model.
Full threat model →Next pattern
Search patterns, frameworks, and pages.