Plan-and-Execute
Author: ranian963
Peer Review:
Proofread : Chaeyoon Kim
This is a part of LangChain Open Tutorial
Overview
This tutorial introduces how to create a plan-and-execute style agent and explains, step by step, how to implement it using LangGraph.
The plan-and-execute approach is useful for tackling complex tasks by first establishing a long-term plan, then executing each step of that plan, and revising it as needed.

Table of Contents
References
Environment Setup
Setting up your environment is the first step. See the Environment Setup guide for more details.
[Note]
The langchain-opentutorial is a package of easy-to-use environment setup guidance, useful functions and utilities for tutorials.
Check out the langchain-opentutorial for more details.
You can set API keys in a .env file or set them manually.
[Note] If you’re not using the .env file, no worries! Just enter the keys directly in the cell below, and you’re good to go.
Plan-and-Execute Definition
A plan-and-execute approach is characterized by:
Long-Term Planning: Before performing a complex task, it first establishes a high-level plan.
Step-by-Step Execution and Replanning: It carries out the plan in stages, checking at each step whether the plan is still valid and updating it if necessary.
This method is inspired by the Plan-and-Solve Peper and the Baby-AGI Project. Unlike the more traditional ReAct Style, which focuses on short-term reasoning one step at a time, plan-and-execute explicitly emphasizes long-term planning.
Advantages:
Clear Long-Term Structure: Even powerful LLMs can struggle to handle extended plans in a single pass. By explicitly defining a long-term plan, the process becomes more robust.
Efficient Model Usage: A larger or more powerful model can be used for the planning phase, while a smaller or lighter model can handle the execution phase, optimizing resource utilization.
The sections below explain how to implement a plan-and-execute agent in LangGraph, step by step.
Defining the Model Name for the Examples
We will define the model name to be used in these demonstrations.
Note
Since
MODEL_NAMEappears frequently, we declare it as a separate variable.It is recommended to run this with a model such as
gpt-4o(or another GPT-4-level model). If you use a smaller model likegpt-40-mini, you may encounter frequent replanning.
Defining Tools
We first define the tools to be used.
In this simple example, we will use the built-in search tool provided by Tavily. Of course, it is equally straightforward to create your own custom tools as needed.
For more details, refer to the Tools documentation.
Defining the Execution Agent
We now create the execution agent responsible for performing tasks.
In this example, the same execution agent will be used for each task, but that is not mandatory.
State Definition
input: User’s inputplan: The current planpast_steps: The plan and results of previous executionsresponse: The final response
Plan Step
We will generate a long-term plan using function calling . Specifically, we define a Plan model and a prompt for the planner that instructs the LLM to produce an itemized plan of steps needed to solve the user's request. We keep each step focused and avoid adding unnecessary detail.
We will run planner to verify the plan generation result.
Re-plan Step
Based on the results of previous steps, we create a stage that can revise the original plan. If a tool call or execution indicates that additional steps are needed, we update the plan accordingly; otherwise, we finalize the response.
Creating the Graph
We now build the LangGraph workflow by connecting the defined nodes:
planner : Generates the plan.
execute : Uses the
execution agentto perform the next step.replan : Decides whether to continue with a new plan or provide the final answer.
final_report : Summarizes all steps and provides a polished final response.
After defining the nodes and edges, we compile the graph. You can visualize the workflow to better understand how data moves between these steps.
Graph Visualization
Running the Graph
Finally, we run the entire workflow by providing user input. The workflow proceeds as follows:
The Planner step creates an initial plan.
The Execute step executes the first item in the plan and returns results.
The Re-plan step checks if more actions are needed. If so, it updates the plan and goes back to Execute ; otherwise, it proceeds to Final Report .
The Final Report step generates a comprehensive markdown summary of all executed steps and the final answer.
By following these steps, you can build a plan-and-execute agent in LangGraph, enabling structured, multi-step problem-solving with explicit long-term planning and flexible re-planning capabilities.
Checking the Final Report
Last updated