Chapter 6: Planning β One Step at a Time
Why This Matters
By the end of this chapter, you'll understand two patterns that turn a one-step agent into a multi-step planner: chain-of-thought (thinking out loud before acting) and ReAct (reasoning and acting in a loop). You'll build an agent that breaks a big task into steps, works through them, and β crucially β recovers when a step fails. This is the difference between an agent that does one thing and an agent that finishes the job.
The Problem: One-and-Done
Our agent loop from Chapter 3 has a hidden limitation. It works great when the task needs one or two tool calls. But give it a task that needs planning β "do A, then B, then C, then check the result" β and it often does A and stops. Why?
Because the LLM, without guidance, tends to answer the immediate question and consider itself done. It found flights. Job done, right? No β but the model doesn't naturally think "wait, the user asked for the whole trip, I've only done part of it." It needs to be told to think through the whole task first.
Chain-of-Thought: Think Before You Act
The first fix is simple and powerful: ask the LLM to think out loud before it acts. Instead of jumping straight to a tool call, it first writes out its reasoning: "The user wants a full trip. I need flights, a hotel, and an itinerary. Let me start with flights, then use the result to find a hotel, then build the itinerary, then check the budget."
This is called chain-of-thought, and it's the single most effective prompt technique for multi-step tasks. By forcing the LLM to articulate its plan before acting, you make it consider the whole task, not just the first move.
That's it. The same agent loop, the same tools β but now the system prompt tells the LLM to plan ahead and keep going. Watch the difference:
ReAct: Reason, Act, Observe β and Repeat
Chain-of-thought tells the agent to think before acting. ReAct (Reason + Act) is the full pattern: the agent reasons about what to do, acts by calling a tool, observes the result, and then reasons again about what to do next. Sound familiar? It should β it's the agent loop from Chapter 3, with explicit reasoning at each step.
The good news: our agent loop already is a ReAct loop. The LLM thinks (reasons), calls a tool (acts), sees the result (observes), and thinks again. The only addition is making the reasoning visible β asking the LLM to state its plan and its next step in words before each tool call.
When Steps Fail: Recovery
Here's where planning gets real. Steps fail. The flight search returns nothing. The hotel API is down. The calculator gets a bad expression. A naive agent crashes or gives up. A planning agent reasons about the failure and tries something else.
An agent is planning a research task: "Find the top 3 AI startups in
Lisbon and summarise what each does." It calls a
web_search tool and gets back zero results. Trace the
ReAct loop: what does the agent REASON, what might it ACT differently,
and what does it OBSERVE?
Think: what are the alternative actions? Different search terms? A broader query? Asking the user? The agent should reason about WHY the search failed before trying again.
Planning Ahead: The Plan-Then-Execute Pattern
For really complex tasks, you can take planning further: ask the LLM to produce a full plan upfront, then execute it step by step. This is called plan-then-execute, and it's useful when the task has many dependent steps.
The trade-off: a plan made upfront might be wrong if early results change what's possible. (What if there are no flights in October? The plan said "search hotels" next, but maybe the dates need to change.) Pure ReAct β reasoning at each step β is more flexible. Plan-then-execute is more structured. Most real agents blend both: make a rough plan, then adapt it as they go.
max_turns β the safety net should catch runaway loops. We'll go deeper on this in Chapter 10 (Guardrails).Where People Come Unstuck
Mistake #1: No planning prompt
The agent does one step and stops. The fix is almost always the system prompt: tell it to think step by step, consider the whole task, and keep going until done. One line can transform a one-and-done agent into a multi-step planner.
Mistake #2: Stopping at the first failure
A tool returns an error, and the agent gives up β "I couldn't find flights, sorry." A planning agent reasons about the failure and tries an alternative. Make sure your system prompt tells the agent to recover, not just report. "If a step fails, try a different approach" is a line that saves hours of frustration.
Mistake #3: Overplanning
Not every task needs a 10-step plan. "What's the weather in Lisbon?" is one step. Forcing the agent to plan upfront for trivial tasks wastes tokens and time. Let the LLM decide β if you tell it to "plan when the task is complex, act directly when it's simple," it'll usually get the balance right.
Think about a research agent that answers: "Compare the battery life of the latest iPhone and Samsung Galaxy, and recommend which is better for someone who travels a lot."
Sketch the plan the agent should make. What steps? What tools might it need? Where might a step fail β and what would recovery look like? Would you use pure ReAct (reason at each step) or plan-then-execute (plan upfront)? Why?
There's no single right answer. The point is to start thinking in plans and recovery β the mental model this chapter is building.
Chapter Summary
- Without planning, agents tend to do one step and stop β they answer the immediate question without considering the whole task.
- Chain-of-thought is a prompt technique: ask the LLM to "think step by step" before acting. It's a prompt change, not a code change.
- ReAct (Reason + Act) is the full pattern: reason about what to do, act by calling a tool, observe the result, reason again. Our agent loop is already a ReAct loop β we just make the reasoning explicit.
- Recovery is a natural consequence of ReAct: when a tool fails, the error goes back as an observation, and the LLM reasons about what to try instead. The loop is self-correcting by design.
- Plan-then-execute makes a full plan upfront, then executes step by step. More structured but less flexible. Most real agents blend planning with adaptive ReAct reasoning.
- The system prompt is the lever: "think step by step, consider the whole task, keep going until done, recover from failures." One good prompt turns a one-step agent into a multi-step planner.
The Research Agent. Take your agent loop and give it
two tools: web_search(query) (fake it β return hardcoded
results) and calculate(expression). Add the ReAct system
prompt from this chapter.
Give it this task: "Find the population of Lisbon and Porto, then calculate which is larger and by how many people."
Watch the agent: plan the steps, search for both cities, calculate the difference, and report. Then break one of your fake search results β make it return "No results found" for Porto β and watch the agent recover. Does it try a different query? Does it tell you it couldn't find one? That's planning and recovery, working together.