ptrnsai

Plan-and-Execute

IntermediateπŸ—ΊοΈ Planning PatternsLangChain / Academic Research

Intent

Separate planning from execution: first create a complete plan, then execute it step by step.

Problem

ReAct-style agents interleave planning and execution, which can lead to getting lost in details or losing sight of the big picture. For complex multi-step tasks, the agent needs to think ahead about the full strategy before diving into individual steps.

Solution

Split the agent into two phases. The Planner receives the task and creates a structured, step-by-step plan. The Executor then carries out each step, potentially using tools and sub-agents. After each step, results can be fed back to an optional Re-planner that adjusts the remaining plan based on what was learned. This mirrors how human experts work: outline the approach first, then execute, adjusting the plan as you go.

Diagram

Task β†’ [Planner LLM]
              ↓
         Plan: [Step 1, Step 2, Step 3, Step 4]
              ↓
         [Execute Step 1] β†’ result₁
              ↓
         [Re-plan?] β†’ Updated plan
              ↓
         [Execute Step 2] β†’ resultβ‚‚
              ↓
         ...continue until done

When to Use

  • Complex tasks that benefit from upfront strategy
  • When you want to review/approve the plan before execution
  • Tasks with multiple independent steps
  • When you want to use a large model for planning and a smaller model for execution

When NOT to Use

  • Simple tasks that don't need planning
  • Highly dynamic environments where plans become obsolete immediately
  • When the overhead of planning isn't justified

Pros & Cons

Pros

  • Separates strategic thinking from tactical execution
  • Plans can be reviewed by humans before execution
  • Large model for planning, small model for execution saves cost
  • Re-planning adapts to new information

Cons

  • Initial plan may be wrong, cascading errors to execution
  • Re-planning adds overhead
  • Rigid plans may not adapt well to unexpected situations
  • Two-phase approach is more complex to implement

Implementation Steps

  1. 1Design the planner prompt to produce structured, numbered steps
  2. 2Define the output format for plans (JSON, numbered list, etc.)
  3. 3Build the executor that processes one step at a time
  4. 4Optionally build a re-planner that updates the plan after each step
  5. 5Add human-in-the-loop approval after the planning phase
  6. 6Implement plan validation β€” reject plans that are too vague or too long

Real-World Example

Research Report Generation

Task: 'Write a competitive analysis report.' Planner creates: 1) Identify top 5 competitors, 2) Research each competitor's product, 3) Analyze pricing strategies, 4) Compare feature sets, 5) Synthesize findings into report. Each step is executed with web search and analysis tools.

PythonPlanner-Executor Pattern
from openai import OpenAI
import json

client = OpenAI()

def plan_and_execute(goal: str) -> list[dict]:
    # Phase 1: Plan
    plan = client.chat.completions.create(
        model="gpt-4o",
        response_format={"type": "json_object"},
        messages=[{"role": "user", "content": f"Break this into 3-5 steps. Return JSON: {{\"steps\": [{{\"action\": str, \"input\": str}}]}}\n\nGoal: {goal}"}],
    )
    steps = json.loads(plan.choices[0].message.content)["steps"]

    # Phase 2: Execute each step
    results = []
    for step in steps:
        result = client.chat.completions.create(
            model="gpt-4o",
            messages=[{"role": "user", "content": f"Execute: {step['action']}\nInput: {step['input']}"}],
        )
        results.append({"step": step["action"], "result": result.choices[0].message.content})

    return results

References