Planning
Learn how AI agents break goals into steps, choose what to do next, adapt their plan, and execute tasks safely.
By the end of this lesson, you will be able to:
- Explain what planning means in an AI agent.
- Break a high-level goal into smaller actionable steps.
- Understand fixed plans and adaptive plans.
- Design a simple Python planning loop.
- Use tool results and observations to revise a plan.
- Recognize when planning helps and when a deterministic workflow is better.
What Is Planning?
Planning is the process of deciding which steps an agent should take to reach a goal.
Why Do Agents Need Planning?
Simple requests may need one response. Larger tasks often contain multiple dependent actions.
Turn a complex request into smaller tasks that can be executed and checked.
Complete prerequisite steps before actions that depend on their results.
Revise the next step when a tool fails or an assumption changes.
Use boundaries so the agent does not wander through irrelevant steps.
Goal vs Plan vs Action
βPrepare a weekly sales summary.β
Get sales data β calculate totals β identify trends β create summary.
Call the sales-data tool for the requested date range.
Plan Decomposition
A useful plan is specific enough to execute and verify without becoming unnecessarily detailed.
Research a topic
Define the final information the user needs.
Find sources
Search for relevant sources using approved tools.
Evaluate
Inspect results and identify missing evidence.
Answer
Combine useful findings into the requested format.
Fixed Planning vs Adaptive Planning
Useful when the workflow is predictable and dependencies are stable.
Useful when tool results, failures, or discoveries can change the path.
Python: Represent a Simple Plan
A plan can be represented as structured data that your application can inspect and execute.
plan = [
{"step": 1, "task": "Get sales data", "status": "pending"},
{"step": 2, "task": "Calculate totals", "status": "pending"},
{"step": 3, "task": "Identify trends", "status": "pending"},
{"step": 4, "task": "Write summary", "status": "pending"},
]
for item in plan:
print(item["step"], item["task"], item["status"])In production, the application should validate allowed actions and enforce execution rules even when a model generates the plan.
The Planning Loop
Planning can be a one-time step or an iterative process where observations influence the remaining plan.
Python: Execute the Next Step
def next_step(plan):
for item in plan:
if item["status"] == "pending":
return item
return None
step = next_step(plan)
if step:
print("Execute:", step["task"])
else:
print("Plan complete")Planning with Tool Results
Tool results can change the next action. This is where adaptive planning becomes useful.
A required tool returns the expected data, so the agent proceeds.
A tool fails or returns incomplete data, so the agent may retry, choose another tool, or stop.
A result changes the assumptions, so the remaining steps are updated.
Plan Validation
A model-generated plan should not automatically become permission to execute arbitrary actions.
Only use tools explicitly exposed by the application.
Validate every tool input before execution.
Check that required earlier steps succeeded.
Set maximum steps, time, cost, or retries.
Planning and Memory
The intended sequence of steps toward the goal.
Relevant state, results, and approved context from previous steps.
Keeping these concepts separate makes an agent easier to reason about and debug.
Planning and RAG
RAG can provide information that helps an agent decide how to proceed.
Plan-and-Execute Architecture
Create steps
Turn the goal into a candidate plan.
Run steps
Execute approved actions and tools.
Inspect results
Check whether outcomes match expectations.
Adjust
Change remaining steps when new information requires it.
Planning vs Workflow
Prefer deterministic code when the steps and conditions are predictable.
Useful when the best next step depends on information discovered during execution.
A common production mistake is using an autonomous planner for a process that could be a simple, testable workflow.
Plan Granularity
Not actionable or measurable.
Clear enough to execute and evaluate.
Creates unnecessary complexity and overhead.
Handling Failed Plans
- Identify which step failed.
- Inspect the tool or observation result.
- Decide whether retrying is safe and useful.
- Choose an alternative step if one exists.
- Ask for help or stop when the agent cannot proceed safely.
Planning Costs
Planning and replanning can add extra round trips.
Plans, tool results, and observations consume context and token budget.
Every decision or tool introduces another possible failure.
The benefit should justify the additional complexity.
Common Planning Mistakes
- Creating plans that are too vague to execute.
- Creating unnecessarily long plans for simple tasks.
- Executing every planned action without checking results.
- Allowing unlimited retries or replanning loops.
- Skipping application-side validation.
- Using agent planning where deterministic code would be safer.
Mini Project: Research Agent Planner
Design a simple research agent that receives a topic and creates a plan before using tools.
Turn the request into a clear research goal.
Find relevant sources using an approved search tool.
Inspect results and identify missing evidence.
Produce a grounded response or replan when necessary.
Challenge: Add a maximum of three replanning attempts and stop when required evidence cannot be found.
Quick Quiz
A) Storing every message B) Deciding steps toward a goal C) Generating embeddings
A) When new observations can change the next step B) When nothing can change C) Only for formatting text
A) Ignore it B) Observe and decide whether to continue or revise C) Always restart
A) Unlimited autonomy B) Deterministic workflow C) Random planning
30-Second Recap
Key Takeaways
Start with the simplest architecture that can solve the task. Add adaptive planning only when uncertainty or changing conditions make it valuable.