Module 3 Lesson 2: The Planner–Executor Pattern
Divide and conquer. Using one agent to build the map and another to walk the path.
Planner–Executor: Managing Complexity
In the ReAct pattern, the agent "wings it"—it only thinks one step ahead. For complex projects (like "Write a 10-page research paper"), this leads to a lack of focus.
The Planner–Executor pattern separates "Thinking" from "Doing" by using two distinct roles (or two distinct LLM calls).
1. The Roles
- The Planner: Its only job is to break the big goal into a list of specific, achievable sub-tasks. It does not use tools. It produces a Roadmap.
- The Executor: Its only job is to take a single sub-task from the roadmap and solve it (often using a ReAct loop).
2. The Workflow
- User: "Build me a marketing plan for a new sneaker."
- Planner:
- Step 1: Research competitor pricing.
- Step 2: Identify top 3 social media trends.
- Step 3: Write ad copy.
- Executor: Takes Step 1. Runs search tools. Returns result.
- Executor: Takes Step 2. Runs social media tools. Returns result.
- Finalizer: Combines all results into the final marketing plan.
3. Visualizing the Split
graph TD
User[Complex Goal] --> Plan[Planner Agent]
Plan --> List[Sub-task List]
List --> E1[Executor: Sub-task 1]
List --> E2[Executor: Sub-task 2]
List --> E3[Executor: Sub-task 3]
E1 --> Res[Aggregator]
E2 --> Res
E3 --> Res
Res --> Final[Final Goal Reached]
4. Why This is Better for Production
A. Reliability
The Planner can use a very large, smart model (like GPT-4), while the Executor can use a smaller, faster model (like Llama 8B). This saves money and time.
B. Parallelism
Since the sub-tasks are often independent, you can run multiple Executors at the same time!
C. Re-planning
If an Executor fails at Step 2, the system can go back to the Planner and say: "Step 2 failed because the website was down. Please update the plan." The Planner might add a new step: "Search for a cached version of Step 2 data."
5. Code Example: Conceptual Planner Output
The Planner should always output a machine-readable format:
{
"tasks": [
{"id": 1, "description": "Get CEO name of Nvidia", "tool_required": "search"},
{"id": 2, "description": "Get CEO name of Intel", "tool_required": "search"},
{"id": 3, "description": "Compare their net worth", "tool_required": "calculator"}
]
}
Key Takeaways
- Planner–Executor is for tasks that require more than 3-5 steps.
- The Planner manages the high-level logic and roadmap.
- The Executor focuses on technical completion of a single task.
- This pattern allows for Better error recovery and Parallel execution.