Planning foundations

The planning pattern lets an AI system decompose ambitious goals into structured, executable steps. Instead of trying to solve everything at once, the system first creates a detailed plan, then executes it step by step.

Planning pattern flow

In prompt chaining, you define the steps upfront. In planning, the LLM generates the steps itself based on the goal. This makes planning far more flexible for open-ended tasks where you cannot predict the right steps in advance.

patterns/06_planning.py
python
def create_plan(task, llm):
    """Create a structured plan for the given task."""
    prompt = f"""
    Create a detailed plan for this task: {task}
    Break it down into 3-5 clear, actionable steps.
    Format: Step 1: [Description] ...
    """
    return llm.generate(prompt).content

The first phase: ask the LLM to break a complex task into 3-5 actionable steps.

Each step is executed independently with the original task as context. This lets the LLM focus on one sub-problem at a time, which produces better results than trying to solve everything at once.

patterns/06_planning.py
python
def execute_step(step, context, llm):
    """Execute a single step of the plan."""
    prompt = f"""
    Task context: {context}
    Execute this step: {step}
    Provide a clear, actionable result.
    """
    return llm.generate(prompt).content

Execute each step individually, passing the task context so the LLM understands the bigger picture.

Finally, we combine all step results into a coherent final output. The LLM sees the original task, the plan, and all results, giving it full context to synthesize everything together.

patterns/06_planning.py
python
def combine_results(plan, results, task, llm):
    """Combine all step results into a final output."""
    results_text = "\n".join(
        [f"Step {i+1}: {r}" for i, r in enumerate(results)]
    )
    prompt = f"""
    Original task: {task}
    Plan: {plan}
    Step results: {results_text}
    Combine into a comprehensive final output.
    """
    return llm.generate(prompt).content

# Execute the planning pipeline
plan = create_plan(task, llm)
steps = [line for line in plan.split("\n") if line.strip().startswith("Step")]
results = [execute_step(step, task, llm) for step in steps]
final = combine_results(plan, results, task, llm)

Combine all step results into one comprehensive output.

Quiz: Quiz

Loading practiceโ€ฆ