Skip to content

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.

Taskincident #4521Plannernumbered stepsExecutorone step, toolsReplan?continue / changeReflectcheck the answerAnswerwith evidencePlan once, execute step by stepnext step, or replan if a gap is found
A visible plan makes long tasks steerable and reviewable. Measure it: on short tasks planning only adds cost.

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.

Common misconception: “Add a ‘reflect on your answer’ step and quality goes up.” Often it only adds cost. Reflection helps when the critic has information or checks the generator didn’t.

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
Sample several times, take a voteEach sample reasons independently and gives a final answer. Majority voting is the simplest way to spend more compute at test time.
Wrong samples:

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

n = 160%
n = 372%
n = 586%
n = 995%
n = 1599%
n = 25100%
Guided experiments
  1. 60% right, wrong answers disagree. How much do 9 samples add? Where do extra samples stop being worth their cost?
  2. 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.)
  3. 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)?
  4. 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?
Tip: Measure before adding planning or voting: run your eval set with n = 1, 3 and 5 and plot accuracy against cost. Many tasks stop improving at about n = 3.

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.

In production: Log the plan and every replan in the trace. When a run goes wrong, the first question is whether the plan was wrong or its execution was.

Hands-on: coding window

The coding window and the full VS Code lab are available after you sign in.

Sign in to code

Labs

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

Further reading

GitHub repositories

Checkpoint quiz

1. Main benefit of plan-and-execute over pure ReAct on long tasks?
2. When does an evaluator-optimizer loop help most?
3. Best use of a reasoning model in an agent?
4. A fixed five-step checklist with no branching is best built as…
5. How do you know planning helps?
6. Self-consistency (majority vote over n samples) makes accuracy worse as n grows when…

Interview questions

  1. Compare ReAct and plan-and-execute. When would you use each?
  2. How would you add self-correction to an agent without doubling its cost?
  3. When is a reasoning model worth the extra latency and cost?
  4. Walk through Anthropic’s agent building blocks and give a use for each.
  5. 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).