Constructing Plan-and-Execute Agent Systems
Pure ReAct agents wander off course on long, multi-step tasks. In this lesson, we build a Plan-and-Execute agent architecture in LangGraph: decoupling high-level strategic roadmapping from tactical tool execution with continuous replanning.
1. Core Mechanics of Plan-and-Execute
Select an architecture node to inspectPlan-and-Execute Implementation
# 1. THE STRATEGIC PLANNER NODE
from pydantic import BaseModel, Field
from langchain_openai import ChatOpenAI
class Plan(BaseModel):
"""Structured plan containing sequential action steps."""
steps: list[str] = Field(
description="Sequential list of distinct subtasks required to solve the goal."
)
planner_llm = ChatOpenAI(model="gpt-4o", temperature=0)
planner = planner_llm.with_structured_output(Plan)
def plan_node(state: PlanExecuteState) -> dict:
"""Generates initial itinerary from user query."""
user_query = state["input"]
plan = planner.invoke(f"Decompose this task into distinct steps: {user_query}")
return {"plan": plan.steps}2. Interactive Plan-and-Execute Studio
Step-by-Step Execution GraphPlan-and-Execute Agent Architecture Studio
Contrast single-step ReAct loops with structured Architect-Executor-Replanner workflows
Decomposes goal into ordered steps
Executes current step using tools
Evaluates status: replan or complete
web_search(query='competitor SaaS pricing')data_formatter(schema='PricingTable')llm_writer(tone='executive')Common Engineering Traps
Generating an 8-step plan and executing it blindly without a replanning step is fatal. If Step 2 discovers that an API endpoint requires authentication or a file does not exist, steps 3 through 8 will blindly execute on invalid assumptions. Always include a replanner node.
Feeding the executor's full tool schema descriptions into the planner prompts causes the planner to hallucinate tool parameters rather than writing strategic natural language tasks. Let the planner write human-like directives, and let the executor choose the tools.
Key Architectural Takeaways
- 1.Role Specialization: Separate strategic reasoning (Planner) from tactical tool calling (Executor). Smaller, cheaper models can run individual steps safely.
- 2.Dynamic Replanning: The replanner inspects intermediate observations and dynamically prunes, inserts, or finishes steps as real-world data arrives.
- 3.State Schema Isolation: Tracking state['plan'] and state['past_steps'] keeps the context clean, avoiding token exhaustion over long tasks.
Implementing Deep Planning Agents
Scale from simple lists to hierarchical tree search. Explore Monte Carlo Tree Search (MCTS), beam search, and backtracking for non-linear agent problem solving.