Courses / Agentic AI / Module 5
Module 5: Planning and reasoning: plan-and-execute, reflection, reasoning models
8 h · theory 2 · hands-on 3 · project 2 · revision 1
You will be able to
- Use plan-and-execute for multi-step tasks
- Add reflection and self-correction where it pays
- Use reasoning models and budget their tokens
- Measure whether planning helps
Capstone increment: A planner step that drafts an investigation plan before the tools run.
Job-description skills: Planning and reasoning · Lab tools: langgraph, openai
1. Plan, then act
A ReAct agent decides one step at a time. For longer tasks it often wanders: it repeats checks, forgets a sub-goal, or stops early. Plan-and-execute separates the two jobs: a planner writes numbered steps first, an executor runs them one by one, and a replanner revises the plan when a step fails or reveals something new.
A visible plan is also a control point. A person can review or edit it before anything runs, and evals can grade it on its own.
2. Reflection and self-correction
Reflexion (Shinn and others, 2023) has an agent write feedback on its failed attempts and retry with that feedback in context. Anthropic’s evaluator-optimizer pattern does the same with two roles: one generates, the other checks and explains what to fix, in a loop with a cap.
It pays off when the check is objective: a schema, a test suite, a linter, limits in a config. When a model grades its own free-text output, it tends to approve itself; calibrate that kind of judge against people (Module 11) before you rely on it.
3. Reasoning models
Reasoning models spend extra tokens thinking before they answer (OpenAI’s o-series, Claude with extended thinking, DeepSeek-R1, Gemini’s thinking models). They plan better on hard multi-step problems and can think between tool calls, but they cost more and respond more slowly.
Control the budget: most APIs expose a reasoning effort or a thinking-token budget. Don’t add “think step by step” instructions for them; give clear goals, constraints and tools instead. Use a reasoning model for the planner or the hard step, and a fast model for simple tool calls.
Deep dive
Deep dive: test-time compute, voting and when it backfires
There are two ways to spend more compute on a hard question at answer time. Think longer: reasoning models are trained with reinforcement learning to write a long chain of thought before answering, and their accuracy rises with the thinking budget. Sample more: generate n answers independently and pick one by majority vote (self-consistency), with a verifier (tests, a checker model) or best-of-n with a reward model.
Voting works when right answers agree with each other and wrong ones scatter: then more samples push accuracy up, even for a model that is right less than half the time. It backfires when the model’s most common answer is wrong: then more samples make the wrong answer more certain. A verifier you trust (unit tests, a schema, a calculator) beats voting whenever you have one.
Worked example: one common mistake: a model right 45% of the time, majority vote over 15 samples
each sample: right with p = 0.45, otherwise the same wrong answer (0.55) the vote is right only if at least 8 of 15 samples are right P(X ≥ 8) with X ~ Binomial(15, 0.45) ≈ 0.35 → voting turned a 45% model into a 35% one, at 15 times the cost
Majority vote is right 95% of the time (one sample: 60%) · cost ≈ 9 × 800 output tokens ≈ ₹4.32 per question at ₹0.60 per 1,000 tokens · latency ≈ one sample if run in parallel
Guided experiments
- 60% right, wrong answers disagree. How much do 9 samples add? Where do extra samples stop being worth their cost?
- Drop to 40%. Voting still helps a lot. Why can a model that is usually wrong win the vote? (Its mistakes split the vote; its right answer doesn’t.)
- Now 45% with “Repeat one common mistake”. Accuracy falls as n grows. Voting amplifies the most likely answer, right or wrong. Which kinds of question produce one common mistake (trick questions, a misleading document)?
- Voting needs answers you can compare, like numbers or labels. For open-ended answers, what would you use instead: a verifier, a judge model or tests you can run?
4. Choose the simplest pattern that works
Anthropic’s “Building effective agents” lists the building blocks most systems need: prompt chaining (fixed steps), routing (classify then dispatch), parallelisation (independent calls at once), orchestrator-workers (a lead splits work dynamically), and evaluator-optimizer. Many “agents” in production are one of these with a single model-driven step.
Pick per task: a fixed investigation checklist is a chain, not an agent; an incident whose next step depends on what the logs say needs a loop or a planner.
5. Measure whether planning helps
Run the same eval scenarios with and without the planner, and with a fast versus a reasoning model, at k trials each. Compare task success, steps, tokens, cost and latency. Planning usually helps on tasks with five or more dependent steps and adds cost on short ones.
Keep the result in an ADR and re-test when models change: a stronger base model can make a planner unnecessary.
Hands-on: coding window
The coding window and the full VS Code lab are available after you sign in.
Sign in to codeLabs
Lab 5.1: Planner node
Add a planner node to the capstone graph that writes an investigation plan as structured output, an executor that runs one step at a time, and a replanner on failure.
Expected: Traces show the plan, each step and any replans.
Lab 5.2: Does planning help?
Run 10 incident scenarios 3 times each with and without the planner. Compare task success, steps, tokens and latency.
Expected: A results table and a decision recorded in an ADR.
Lab 5.3: Reasoning budget (capstone increment)
Use a reasoning model only for the planner with a capped thinking budget and a fast model for tool steps. Compare cost and success with using the reasoning model everywhere.
Expected: Cost per incident and success for both setups.
Go deeper
Videos
- LangGraph: Planning Agents · LangChain · ~34 min. Plan-and-execute and related planning agents built in LangGraph, with the trade-offs.
- Why AI Models Pause to Think: Test Time Compute Explained · IBM Technology · ~11 min. What test-time compute is and why reasoning models pause to think.
- Self-Consistency Improves Chain of Thought Reasoning in Language Models · Vivek Haldar · ~6 min. The self-consistency paper summarised in six minutes.
Further reading
- Paper Chain-of-Thought Prompting Elicits Reasoning in Large Language Models · Wei et al., 2022. The original evidence that writing out steps helps on multi-step problems.
- Paper Self-Consistency Improves Chain of Thought Reasoning in Language Models · Wang et al., 2022. Sample several reasoning paths and vote; the method in this module's deep dive.
- Paper Reflexion: Language Agents with Verbal Reinforcement Learning · Shinn et al., 2023. Agents that write a reflection after a failed attempt and use it on the next try.
- Paper Plan-and-Solve Prompting · Wang et al., 2023. Ask for a plan first, then execute it: the simplest planning pattern, measured.
- Paper Tree of Thoughts: Deliberate Problem Solving with Large Language Models · Yao et al., 2023. Search over partial solutions with evaluation and backtracking, and what it costs.
- Paper Scaling LLM Test-Time Compute Optimally can be More Effective than Scaling Model Parameters · Snell et al., 2024. When more thinking or more samples beats a bigger model, and when it doesn't.
- Paper DeepSeek-R1: Incentivizing Reasoning Capability in LLMs via Reinforcement Learning · DeepSeek-AI, 2025. How a reasoning model is trained to think longer, in an open report.
GitHub repositories
- langchain-ai/langgraph: The plan-and-execute and reflection tutorials in the docs.
- noahshinn/reflexion: The Reflexion paper’s code: how verbal feedback is stored and reused.
- anthropics/claude-cookbooks: The agent patterns notebooks: chaining, routing, orchestrator-workers and evaluator-optimizer.
- stanfordnlp/dspy: Programs instead of prompts: optimising multi-step pipelines against a metric.
Checkpoint quiz
Interview questions
- Compare ReAct and plan-and-execute. When would you use each?
- How would you add self-correction to an agent without doubling its cost?
- When is a reasoning model worth the extra latency and cost?
- Walk through Anthropic’s agent building blocks and give a use for each.
- How do you test whether a planner improves your agent?
Ask the professor
Sign in to ask questions about this module.
Discussion
Community discussion appears here once giscus is configured (see README).