AI & Agents

How Agents Plan Before They Act

A peek inside the thinking step that turns a chatbot into a problem-solver.

Scroll to start

Thinking Before Doing

A regular chatbot does one thing: it reads your message and writes a reply. An agent is different. When you give an agent a goal — like "find the cheapest flight to Tokyo next month" — it doesn't just answer. It thinks first, breaks the goal into smaller steps, and then works through them one at a time.

This thinking step is called planning. It's a short list the agent writes for itself: "Step 1: search flights. Step 2: compare prices. Step 3: pick the cheapest one. Step 4: show the result." The plan lives in the agent's memory, hidden from you, but it shapes everything the agent does next.

Planning is what separates an agent from a chatbot. A chatbot can answer a question. An agent can finish a task.

Why Planning Changes Everything

Without a plan, an agent is just guessing. It might try a tool, get an answer, and then forget what it was doing. The next message is a fresh start, and the agent wanders in circles, repeating itself or losing the goal.

With a plan, the agent has a roadmap. It knows where it's going, what step comes next, and when to stop. That's why a planning step is the single most important upgrade you can give an AI system. It's the difference between a tool that responds and a teammate that finishes work.

💡 Key Insight

Planning turns an LLM from a typewriter into a project manager. The model isn't smarter — it just has a written-down plan to follow, so each step builds on the last one instead of starting over.

The Four Steps an Agent Takes

Every planning agent follows the same basic loop. It receives a goal, makes a plan, does the first step, checks the result, and then moves on. If a step fails, the agent rewrites the plan and tries again. Here's the loop in plain terms:

The Agent Planning Loop
🎯
Goal
Read what the user wants done
📝
Plan
Write a list of steps to reach the goal
⚙️
Act
Do the next step using a tool
🔁
Re-Plan
Check the result, update the plan
repeat until done

The "re-plan" step is the secret sauce. Real life is messy — a website might be down, an API might return an error, a search might come up empty. When something fails, the agent looks at what went wrong and rewrites the plan to try a different path. That's why good agents can recover from mistakes instead of giving up.

A Tiny Planning Agent in Python

Here's what a planning loop looks like in real code. The agent gets a goal, asks the LLM to break it into steps, then does each step using a tool. If a step fails, it asks the LLM for a new plan:

tiny_agent.py
# A minimal agent that plans, acts, and re-plans
def run_agent(goal, llm, tools):
    plan = llm(f"Make a step-by-step plan for: {goal}")

    for step in plan:
        result = tools.run(step)        # try the step

        if result.failed():
            # the step broke — ask for a new plan
            plan = llm(
              f"Step failed: {step}\n"
              f"Error: {result.error}\n"
              "Make a new plan to still reach the goal."
            )

    return llm(f"Summarize the final result for: {goal}")

The "magic" is just two calls to the language model — one to plan, one to re-plan when something breaks. The tools.run() part is where the agent reaches out into the real world: searching the web, calling an API, reading a file, or sending an email. The plan keeps it all organized.

Knowledge Check

Test what you learned with this quick quiz.

Quick Quiz — 3 Questions

Question 1
What is the main difference between a chatbot and an agent?
Question 2
What does an agent do FIRST when it gets a goal?
Question 3
What happens when a step in the plan fails?
🏆

You crushed it!

Perfect score on this module.