Planner - joehubert/ai-agent-design-patterns GitHub Wiki

Home::Overview of Patterns

Classification

Architectural Pattern

Intent

The Planner pattern enables AI systems to break down complex tasks into manageable sub-tasks with explicit sequencing, dependencies, and success criteria before execution begins. It provides a structured approach to complex problem-solving by creating execution plans that guide subsequent actions.

Also Known As

Task Decomposer, Strategic Planner, Task Scheduler, Goal Decomposition Engine, Planning Agent

Motivation

Complex AI tasks often require multiple steps that need to be executed in a specific order with clear dependencies. Without proper planning:

  • LLMs may attempt to solve complex problems in a single step, leading to suboptimal results
  • Critical dependencies between subtasks may be overlooked
  • Resources might be inefficiently allocated
  • It becomes difficult to track progress or recover from failures at specific stages

For example, when asked to "create a market analysis report for electric vehicles in Europe," an agent without planning capabilities might produce a generic response missing key components. A Planner would break this into researching market size, identifying key players, analyzing regulatory environment, assessing consumer trends, and synthesizing findings—each with clear success criteria and dependencies.

Applicability

Use the Planner pattern when:

  • Tasks require multiple distinct steps to complete successfully
  • The problem space is complex with non-obvious decomposition strategies
  • Dependencies exist between different parts of the solution
  • Progress needs to be tracked and measured against specific milestones
  • Recovery from failures at specific stages needs to be managed
  • Execution might involve different specialized agents or tools for different subtasks
  • The solution requires resource allocation decisions that benefit from upfront planning

Structure

To do...

Components

  • Task Analyzer: Examines the initial task specification to understand its scope, constraints, and requirements. It identifies the core problem to be solved and extracts key parameters.

  • Decomposition Engine: Breaks down complex tasks into smaller, more manageable subtasks based on logical divisions of work, functional areas, or processing stages.

  • Dependency Manager: Identifies and tracks relationships between subtasks, establishing which tasks must precede others and which can be executed in parallel.

  • Resource Allocator: Determines what tools, models, or agents are best suited for each subtask based on capabilities and requirements.

  • Success Criteria Definer: Establishes clear completion criteria for each subtask and the overall task, enabling verification of progress and results.

  • Plan Synthesizer: Compiles the subtasks, dependencies, resources, and success criteria into a coherent execution plan with clear sequencing.

  • Plan Executor: Manages the execution of the plan, invoking the appropriate tools or agents for each subtask according to the established sequence.

  • Progress Monitor: Tracks execution progress against the plan, detecting completions and failures of individual subtasks.

  • Plan Adaptor: Modifies the plan when necessary based on execution results, unexpected challenges, or new information that emerges during execution.

Interactions

  1. The Task Analyzer receives the initial request and performs preliminary analysis to understand its scope and requirements.

  2. The Decomposition Engine breaks the main task into subtasks, consulting with the Task Analyzer to ensure all requirements are addressed.

  3. The Dependency Manager establishes relationships between subtasks, determining which must be completed before others can begin.

  4. The Resource Allocator assigns appropriate tools, models, or specialized agents to each subtask based on its requirements.

  5. The Success Criteria Definer establishes verification points for each subtask and the overall plan.

  6. The Plan Synthesizer combines all this information into a structured execution plan.

  7. The Plan Executor manages the sequential and/or parallel execution of subtasks according to the plan and dependency structure.

  8. The Progress Monitor tracks completion of subtasks and evaluates results against success criteria.

  9. When subtasks fail or new information emerges, the Plan Adaptor modifies the execution plan accordingly, potentially triggering new decomposition or dependency analysis.

Consequences

Benefits:

  • Enables solving of significantly more complex problems than could be addressed in a single step
  • Improves solution quality by ensuring all aspects of a problem are considered
  • Provides clear tracking of progress through well-defined milestones
  • Facilitates resource optimization by matching specialized tools to specific subtasks
  • Creates natural checkpoints for verification and validation
  • Supports graceful error recovery by isolating failures to specific subtasks
  • Improves explainability by making the problem-solving process explicit and traceable

Limitations:

  • Introduces overhead in terms of processing time and complexity
  • May over-decompose simple problems that could be solved more efficiently in fewer steps
  • Requires sophisticated planning capabilities that may be challenging to implement effectively
  • Static plans may struggle to adapt to highly dynamic environments
  • The quality of the plan directly impacts the quality of the final result
  • May require significant prompt engineering or fine-tuning to achieve reliable planning capabilities

Performance implications:

  • Planning phase adds latency before execution begins
  • Enables potential parallelization of independent subtasks
  • Improves overall efficiency for complex tasks despite planning overhead
  • Computation and token usage increase due to explicit reasoning about the plan

Implementation

  1. Design the planning prompt: Create detailed prompt templates that guide the LLM through structured planning, including task analysis, decomposition, dependency mapping, and success criteria definition.

  2. Implement plan validation: Add verification steps to ensure plans are complete, coherent, and properly address the original task requirements.

  3. Create a plan representation: Design a structured format (e.g., JSON) to represent plans that includes subtasks, dependencies, resources, and success criteria.

  4. Build the execution engine: Develop a system that can interpret the plan, execute each step with appropriate tools or models, and track progress.

  5. Implement feedback loops: Create mechanisms for the executor to report progress, results, and failures back to the planner for plan adaptation.

  6. Establish fallback mechanisms: Design recovery strategies for when subtasks fail or produce unexpected results.

  7. Balance planning depth: Find the appropriate level of task decomposition—neither too granular nor too coarse—for your application domain.

  8. Incorporate learning: When possible, use historical plan execution data to improve future planning, identifying common subtasks and effective decomposition strategies.

Code Examples

To do...

Variations

  • Iterative Planner: Creates an initial high-level plan and progressively refines it as execution proceeds and more information becomes available.

  • Meta-Planner: A two-level approach where a high-level planner creates a strategic plan, and specialized planners develop detailed tactical plans for specific components.

  • Reactive Planner: Emphasizes rapid adaptation to changing conditions by continuously revising the plan during execution based on real-time feedback.

  • Multi-Agent Planner: Distributes planning responsibilities among specialized agents, each focusing on different aspects of the problem space.

  • Template-Based Planner: Uses predefined plan templates for common task types, customizing them for specific instances rather than planning from scratch.

  • Hierarchical Planner: Organizes plans into nested layers of abstraction, with high-level goals decomposed into progressively more detailed subplans.

Real-World Examples

  • AutoGPT and BabyAGI: These autonomous agent systems use planning to break down high-level goals into actionable steps, establishing dependencies and execution sequences.

  • LangChain's Task Decomposition: The LangChain framework implements planning components that break complex tasks into sequences of tool calls and LLM operations.

  • GitHub Copilot for Planning: Advanced code generation systems plan implementation approaches before generating code, breaking down programming tasks into logical components with dependencies.

  • ChatGPT Advanced Data Analysis: When analyzing complex datasets, the system plans analysis approaches before executing queries and visualizations.

  • Anthropic's Constitutional AI: Uses planning to ensure that responses to complex queries follow a structured approach that adheres to defined principles.

Related Patterns

  • Hierarchical Task Decomposition: An extension of the Planner pattern that focuses specifically on creating nested layers of tasks and subtasks.

  • Chain-of-Thought Prompting: Often used within planners to encourage explicit reasoning about task decomposition and sequencing.

  • ReAct: Combines planning with execution in a tight loop, where planning and acting alternate throughout the problem-solving process.

  • Router: Often works with Planners by directing requests to appropriate planning agents based on task type.

  • Tool Use: Planning frequently involves determining which tools to use for which subtasks and in what sequence.

  • Reflection: Planners can incorporate reflection steps to evaluate and refine plans before and during execution.

  • Multi-Agent: Planners often coordinate multiple specialized agents, assigning subtasks based on agent capabilities.