LESSON 63 Β· AI AGENTS

Planning

Learn how AI agents break goals into steps, choose what to do next, adapt their plan, and execute tasks safely.

30 min readβ€’Intermediateβ€’Generative AI

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.
1

What Is Planning?

Planning is the process of deciding which steps an agent should take to reach a goal.

Simple definition:A plan turns a goal into actionable steps, while an agent can revise the remaining steps when new information appears.
GoalWhat should be achieved?
β†’
PlanWhat steps could achieve it?
β†’
ActExecute the next step
β†’
ObserveCheck the result
β†’
ReplanAdjust if needed
2

Why Do Agents Need Planning?

Simple requests may need one response. Larger tasks often contain multiple dependent actions.

DECOMPOSEBreak large goals apart

Turn a complex request into smaller tasks that can be executed and checked.

ORDERRespect dependencies

Complete prerequisite steps before actions that depend on their results.

ADAPTReact to new information

Revise the next step when a tool fails or an assumption changes.

CONTROLLimit unnecessary actions

Use boundaries so the agent does not wander through irrelevant steps.

3

Goal vs Plan vs Action

GOALDesired outcome

β€œPrepare a weekly sales summary.”

PLANSteps toward the outcome

Get sales data β†’ calculate totals β†’ identify trends β†’ create summary.

ACTIONOne executable step

Call the sales-data tool for the requested date range.

4

Plan Decomposition

A useful plan is specific enough to execute and verify without becoming unnecessarily detailed.

GOAL

Research a topic

Define the final information the user needs.

STEP 1

Find sources

Search for relevant sources using approved tools.

STEP 2

Evaluate

Inspect results and identify missing evidence.

STEP 3

Answer

Combine useful findings into the requested format.

5

Fixed Planning vs Adaptive Planning

FIXED PLANDecide steps upfront

Useful when the workflow is predictable and dependencies are stable.

ADAPTIVE PLANReplan during execution

Useful when tool results, failures, or discoveries can change the path.

Practical rule:Do not replan just because you can. Replanning adds model calls and latency, so use it when new information actually changes the path.
6

Python: Represent a Simple Plan

A plan can be represented as structured data that your application can inspect and execute.

</> Python
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.

7

The Planning Loop

1. GoalUnderstand the request
β†’
2. PlanCreate candidate steps
β†’
3. ExecuteRun the next action
β†’
4. ObserveRead the result
β†’
5. ReplanContinue or revise

Planning can be a one-time step or an iterative process where observations influence the remaining plan.

8

Python: Execute the Next Step

</> Python
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")
9

Planning with Tool Results

Tool results can change the next action. This is where adaptive planning becomes useful.

SUCCESSContinue

A required tool returns the expected data, so the agent proceeds.

FAILURERevise

A tool fails or returns incomplete data, so the agent may retry, choose another tool, or stop.

NEW INFORMATIONReplan

A result changes the assumptions, so the remaining steps are updated.

10

Plan Validation

A model-generated plan should not automatically become permission to execute arbitrary actions.

Allowed tools

Only use tools explicitly exposed by the application.

Valid arguments

Validate every tool input before execution.

Dependencies

Check that required earlier steps succeeded.

Limits

Set maximum steps, time, cost, or retries.

11

Planning and Memory

PLANWhat should happen?

The intended sequence of steps toward the goal.

MEMORYWhat has happened?

Relevant state, results, and approved context from previous steps.

Keeping these concepts separate makes an agent easier to reason about and debug.

12

Planning and RAG

RAG can provide information that helps an agent decide how to proceed.

Example:An agent retrieves a company policy with RAG, then plans the next response or tool action using that context.
13

Plan-and-Execute Architecture

PLANNER

Create steps

Turn the goal into a candidate plan.

EXECUTOR

Run steps

Execute approved actions and tools.

OBSERVER

Inspect results

Check whether outcomes match expectations.

REPLANNER

Adjust

Change remaining steps when new information requires it.

14

Planning vs Workflow

WORKFLOWKnown path

Prefer deterministic code when the steps and conditions are predictable.

AGENT PLANNINGVariable path

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.

15

Plan Granularity

TOO BROADβ€œFinish the project.”

Not actionable or measurable.

USEFULβ€œCollect β†’ validate β†’ analyze β†’ summarize.”

Clear enough to execute and evaluate.

TOO DETAILEDEvery tiny action is a plan item.

Creates unnecessary complexity and overhead.

16

Handling Failed Plans

  1. Identify which step failed.
  2. Inspect the tool or observation result.
  3. Decide whether retrying is safe and useful.
  4. Choose an alternative step if one exists.
  5. Ask for help or stop when the agent cannot proceed safely.
Do not loop forever:Use step limits, retry limits, timeouts, and explicit stop conditions.
17

Planning Costs

LATENCYMore model calls

Planning and replanning can add extra round trips.

TOKENSLarger context

Plans, tool results, and observations consume context and token budget.

RELIABILITYMore moving parts

Every decision or tool introduces another possible failure.

VALUEUse planning when it helps

The benefit should justify the additional complexity.

18

Common Planning Mistakes

  1. Creating plans that are too vague to execute.
  2. Creating unnecessarily long plans for simple tasks.
  3. Executing every planned action without checking results.
  4. Allowing unlimited retries or replanning loops.
  5. Skipping application-side validation.
  6. Using agent planning where deterministic code would be safer.
19

Mini Project: Research Agent Planner

Design a simple research agent that receives a topic and creates a plan before using tools.

STEP 1Define the question

Turn the request into a clear research goal.

STEP 2Search

Find relevant sources using an approved search tool.

STEP 3Evaluate

Inspect results and identify missing evidence.

STEP 4Answer

Produce a grounded response or replan when necessary.

Challenge: Add a maximum of three replanning attempts and stop when required evidence cannot be found.

20

Quick Quiz

1. What is planning?

A) Storing every message   B) Deciding steps toward a goal   C) Generating embeddings

2. When is adaptive planning useful?

A) When new observations can change the next step   B) When nothing can change   C) Only for formatting text

3. What should happen after a tool result?

A) Ignore it   B) Observe and decide whether to continue or revise   C) Always restart

4. What is often better for a predictable process?

A) Unlimited autonomy   B) Deterministic workflow   C) Random planning

21

30-Second Recap

1Planning turns a goal into actionable steps.
2Agents can execute a plan and use observations to adapt it.
3Tool results can cause the agent to continue, retry, switch strategy, or stop.
4Plans should be validated and bounded by application-side controls.
5Use deterministic workflows when the path is already known.
22

Key Takeaways

Remember:Good agent planning is not about making the longest plan. It is about choosing useful next steps, checking results, adapting when necessary, and staying within clear execution limits.

Start with the simplest architecture that can solve the task. Add adaptive planning only when uncertainty or changing conditions make it valuable.