Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 15

Planning for Execution: A Structured Approach

Recommended resources

Introduction

In our last lesson, we implemented Chain-of-Thought reasoning as a tightly-coupled observe-think-act loop. While effective, this interleaved approach requires an LLM call for every single step, which can be slow and costly. Each step is decided "locally," without a global, strategic view of the entire task. For a systems architect, this resembles a process that makes a network call for every single instruction—functional, but far from optimal.

Today, we will explore a more deliberate and efficient architectural pattern: plan-then-execute. We will decouple the reasoning phase from the action phase. The agent will first create a comprehensive, structured plan to solve a user's request and only then begin executing the steps in that plan. This separation allows for more strategic thinking, reduces LLM calls, and critically, opens the door for optimizations like parallel execution—a concept you'll find very familiar from your work in distributed systems.


ReAct vs. Plan-Then-Execute

To understand the shift we're making, it's useful to contrast the ReAct pattern from our last lesson with the plan-then-execute model.

The ReAct loop is fundamentally iterative and myopic. It decides on one action at a time based on the immediate context.

Python: Create a ReAct Agent from Scratch

As a quick refresher, this video explains the ReAct pattern.

Watch the initial explanation of the thought-action-observation loop. Notice how the agent thinks, performs one action, gets an observation, and then feeds that back in to decide the next single thought. This is the tight coupling we are about to break.

Plan-then-execute introduces a higher level of orchestration. It operates in two distinct phases:

  1. Planning: The agent makes a single, high-level call to an LLM (the "Planner") to decompose the entire problem into a series of explicit, executable steps. The output is a structured data object, like a JSON array or a list.
  2. Execution: A separate component (the "Executor") iterates through this pre-defined plan, carrying out each step. This phase typically involves tool calls and processing, often without any further LLM intervention unless a step explicitly requires it.

This approach has significant engineering advantages.

LangGraph: Planning Agents

This video from LangChain provides an excellent overview of why plan-and-execute is a powerful pattern for building more production-ready agents.

Watch the introductory segment that contrasts ReAct with plan-and-execute, from the beginning. The speaker highlights the key benefits: faster execution, lower token costs, and improved reliability by avoiding the "short-sightedness" of single-step decisions.


The Planner-Executor Architecture

At its core, the plan-then-execute pattern is a simple separation of concerns, embodied by two primary components: a Planner and an Executor.

This flowchart illustrates a simple plan-then-execute style workflow. The "Planner" creates a structured task, which the "Coder" (our Executor) then executes. The validation and review loops represent more advanced patterns we will explore later.

This flowchart illustrates a simple plan-then-execute style workflow. The "Planner" creates a structured task, which the "Coder" (our Executor) then executes. The validation and review loops represent more advanced patterns we will explore later.

The Planner: The Brain of the Operation

The Planner's sole responsibility is to translate a high-level user goal into a structured, machine-readable plan. The engineering challenge here lies in prompt design and schema enforcement. The prompt must instruct the LLM to:

  1. Think strategically about the entire problem.
  2. Break it down into logical, discrete sub-tasks.
  3. Output this plan in a strict format that our system can parse and execute.

A common format is a JSON array where each object represents a step.

Plan–Code–Execute: Designing Agents That Create Their Own Tools

This article provides an excellent conceptual breakdown and a from-scratch implementation of a plan-then-execute system.

First, read the description of the Planner agent to understand its role. Then, examine the Python code provided for the planner agent. Pay close attention to the system_prompt, which rigorously defines the expected JSON output format, including fields like step_id, name, description, and type. Finally, review the example plan generated for a "Spatial Analysis" task to see the structured output in practice.

The Executor: The Hands of the Operation

The Executor is a much simpler component. It is a programmatic loop that receives the plan generated by the Planner. It iterates through each step and executes it.

  • If a step involves a tool call (e.g., type: "CODE"), the Executor invokes the appropriate tool with the specified parameters.
  • If a step involves summarization or data transformation, it might make a targeted LLM call with a very specific prompt.

Crucially, the Executor's logic is deterministic; it follows the plan without deviation.


Implementing from First Principles

Let's ground this in a practical, framework-free implementation. We'll need two core functions: one to interact with the Planner LLM and another to manage the execution loop. The contract between them is the structured plan.

The following resource provides a minimalist but complete set of prompts for a PlanAndSolveAgent.

hello-agents/docs/chapter7/Chapter7-Building-Your-Agent-Framework.md at main · datawhalechina/hello-agents

This document from the hello-agents project is dedicated to building agent components from scratch, perfectly aligning with our goal. We'll focus on their implementation of the plan-then-execute pattern.

Read the section 7.4.4 PlanAndSolveAgent. Focus on the two prompt templates: DEFAULT_PLANNER_PROMPT and DEFAULT_EXECUTOR_PROMPT. The <tf start="DEFAULT_PLANNER_PROMPT" end='."]

To implement this, your system would:

  1. create_plan(question) function:

    • Takes the user's question.
    • Inserts it into the DEFAULT_PLANNER_PROMPT.
    • Makes a single LLM call.
    • Parses the response to extract the Python list of steps. Returns this list.
  2. execute_plan(question, plan) function:

    • Initializes an empty history list to store results.
    • Loops through each current_step in the plan.
    • For each step, it constructs a prompt using the DEFAULT_EXECUTOR_PROMPT, filling in the question, plan, history, and current_step.
    • Makes an LLM call to get the result for that step.
    • Appends the result to the history.
    • After the loop, the final result is likely the last item in the history.

This two-function design cleanly separates planning from execution and serves as the foundational implementation of this powerful pattern.


From Sequential Lists to Parallel Graphs

A simple list of steps implies a linear, sequential execution. However, what if some steps are independent? For example, if a task is "Compare the population of Paris and Tokyo," the two sub-tasks—"find population of Paris" and "find population of Tokyo"—can be executed in parallel. This is where your background in distributed systems becomes directly applicable.

By defining dependencies between tasks, the plan evolves from a simple list into a Directed Acyclic Graph (DAG). This is the core insight behind more advanced, production-grade agent architectures.

This diagram shows the architecture of LLMCompiler, a system that treats agentic workflows as a compilation problem. The Planner generates a "Task DAG" which is then passed to a "Task Fetching Unit" that schedules and executes tasks in parallel, much like a job scheduler in a distributed system.

This diagram shows the architecture of LLMCompiler, a system that treats agentic workflows as a compilation problem. The Planner generates a "Task DAG" which is then passed to a "Task Fetching Unit" that schedules and executes tasks in parallel, much like a job scheduler in a distributed system.

This architecture introduces several key optimizations that should resonate with your engineering experience.

LangGraph: Planning Agents

The same LangChain video provides an excellent summary of two research papers that advance the plan-and-execute pattern.

First, watch the segment on the REWOO paper (Reasoning without Observation). This introduces the idea of variable substitution, where the planner can define dependencies directly in the plan (e.g., step_2_input = output_of_step_1). This allows a sequence of tool calls to run without an LLM call in between, a major efficiency gain. Next, watch the segment on the LLMCompiler paper (LLM Compiler). This demonstrates how to achieve true parallelism. The planner streams a DAG of tasks, and an execution unit runs independent tasks concurrently. This is a powerful fusion of LLM reasoning and classical concurrent systems engineering.

These advanced patterns show the natural evolution of plan-then-execute: from a simple list to a full-blown execution graph that can be optimized for latency and cost.


Conclusion

Today, we've made a crucial architectural shift from the reactive, step-by-step nature of ReAct to the deliberate, strategic approach of plan-then-execute. By separating the "thinking" from the "doing," we build agents that are not only more efficient but also more robust.

Your key takeaways are:

  • Decoupling Planning and Execution: Plan-then-execute consists of two distinct phases: an initial LLM call to generate a complete, structured plan, followed by a programmatic loop that executes that plan.
  • Structured Plans are Key: The interface between the Planner and Executor is a structured data format (e.g., JSON, list). The design of this schema is a critical part of the system's architecture.
  • Path to Optimization: This pattern moves beyond simple sequential execution. By representing the plan as a DAG with explicit dependencies, we can unlock parallel execution and other performance optimizations familiar from distributed computing.

Our current plan-then-execute model is static. It assumes the world remains unchanged during execution and that every step will succeed. But what happens when a tool fails or an API returns an unexpected result? The pre-computed plan becomes invalid. In our next lesson, we will address this by implementing dynamic re-planning, allowing our agent to adapt and correct its course when faced with the unpredictability of the real world.