rumblr Work in progressWIP

● The AI Primer · Lesson 47 · Part 6: building systems people rely on

How an AI agent plans a long task

multi-step planning, checks and retries

This lesson covers Why a twenty-step task fails when a three-step demo worked, and how a plan survives it

Members · open during launch 20 min6 figures and diagrams

What this is

What is multi-step planning?

Multi-step planning is how an AI agent, a program that has an AI language model act one step at a time, gets a long job done when each step is only mostly reliable. The chances multiply: a step that succeeds 95% of the time gives a ten-step task a 60% chance of finishing, and a twenty-step task 36%. An agent that finishes long jobs writes a plan and revises it when a step fails. It checks each step with something other than the language model, such as a test or a database query, and saves each result so that a failure costs one step.

Why it matters: You need it the moment a task takes more than a handful of calls to a language model in a row. The tell is an agent that finishes the small cases and fails the long ones, with a bill that shows it starting again from step one after every stumble.

At a glance

Key takeaways

  1. Success compounds: . At 95% per step, a ten-step task finishes 60% of the time.
  2. Plan first, run the plan step by step, and when a step fails, replan from that step and keep the work already done.
  3. Split the task into subtasks that each have a check, and save each result, so a failure costs one retry and not the whole run.
  4. A model reviewing its own work shares its blind spots; a test, a schema or a database query does not.
  5. A retry happens only after a failure is detected, so the check matters more than the number of retries.

How it works, from scratch

An agent that does one thing is easy to get right. An agent that has to do twenty things in a row fails far more often, for a reason that is plain arithmetic.

Chapter 1

Compounding error: why long tasks fail

Suppose each step of an agent succeeds 95% of the time, and the task needs every step to succeed. Two steps in a row both succeed with a chance of 0.95 × 0.95, and each further step multiplies the chance by 0.95 again:

steps chance all succeed
1 0.95
2 0.95 × 0.95 = 0.9025
10 0.95¹⁰ ≈ 0.599
20 0.95²⁰ ≈ 0.358
The formula and its symbols

Symbols

Symbol Meaning
chance a single step succeeds
number of steps the task needs

In words: multiply the per-step success rate by itself once per step.

New to Σ, ∂ or ‖x‖? The symbols reference decodes each one as a few lines of Python.

On the example: and . A 95%-reliable step is a 60%-reliable 10-step task.

In Python:

p = 0.95
# ten steps that must all succeed
round(p ** 10, 2)  # → 0.6
round(p ** 20, 2)  # → 0.36

Figure 1 · Chart

0 5 10 15 20 25 30 steps in the task (n) 0.0 0.2 0.4 0.6 0.8 1.0 P(task succeeds) = p^n Compounding error: reliable steps, unreliable tasks 90% per step 95% per step 99% per step

At 95% per step a 10-step task succeeds only about 60% of the time, and every curve keeps sliding toward zero as steps are added

Reading it: the x-axis is the number of steps in the task, and the y-axis is the chance the whole task succeeds. The three curves are three per-step rates, and the lower the rate, the faster its curve falls. Follow the dotted line at 10 steps up to the 95% curve: about 0.6. On the same curve a three-step demo sits at 0.86 and a twenty-step job at 0.36.

In code: end_to_end_success multiplies the per-step rate by itself once per step. View source

Every fix works on one of the formula's two numbers. Fewer steps lower : a higher-level tool (see Tools) can do in one call what took several. A check that catches a failed step, followed by a retry of only that step, raises , and so does a person reviewing the work at the points where a mistake is expensive.

Chapter 2

Plan-and-execute, with replanning

Before anything can be checked or retried, the agent needs an order of work. An agent that decides one step at a time (a pure ReAct loop, see The agent loop) drifts on a long task. Plan-and-execute splits the job in two: a planner writes the whole list of steps first, and the agent then runs them in order. The plan keeps the agent on track, and a person can read it before anything happens.

The risk is following a plan that has stopped being true, like a cook who reads the whole recipe, finds halfway through that there are no eggs, and carries on as if there were. A sensible cook rewrites the rest of the recipe around what is in the cupboard and keeps what is already chopped. Replanning does the same. When a step fails, the planner is told what is done and why the step failed, and it writes a new plan for the rest.

Here the goal is to reconcile the third quarter's invoices against the payments received, and the planner's first plan calls an invoices API that has been retired:

planner  -> {"steps": ["fetch_payments", "fetch_invoices_v1", "match", "draft_summary"]}
execute  fetch_payments      ok
execute  fetch_invoices_v1   failed: invoices API v1 was retired on 2026-07-01; use fetch_invoices_v2
planner  <- "Completed so far: fetch_payments. Step fetch_invoices_v1 failed: ...retired..."
planner  -> {"steps": ["fetch_invoices_v2", "match", "draft_summary"]}
execute  fetch_invoices_v2   ok
execute  match               ok
execute  draft_summary       ok     (fetch_payments was NOT run again)

Figure 2 · Diagram

Reading it: the inner loop (execute, check, next) is the plan being followed. The outer loop back to Planner runs only when a check fails, and it carries what is already done, so that work is kept, and why the step failed, so the new plan can route around it. The Replans left? diamond stops a planner that cannot adapt from looping forever.

In code: plan_and_execute asks the planner for a plan, runs each step, skips steps already done, and asks for a new plan when a step fails. It returns a PlanRun: every plan, each step's result and the replan count. View source

Chapter 3

Decomposition into verifiable subtasks

Replanning starts when a step's result fails a check, so every step needs a check. "Reconcile Q3 invoices" cannot be ticked off as a whole; "fetch the invoices" can, once you say what done means. Decomposition splits a task into subtasks that each come with a check, written in code, that defines when the subtask is done:

subtask output check (definition of done)
fetch_invoices 4 invoices at least one invoice returned
fetch_payments 3 payments at least one payment returned
match 4 rows: invoiced vs paid each invoice appears exactly once
list_mismatches INV-102 (1200 vs 1100), INV-104 (450 vs 0) none
draft_summary "Q3 reconciliation: 4 invoices, 3 payments, 2 mismatches (INV-102 short by 100, INV-104 unpaid)." mentions the mismatches

Each subtask's output is saved once its check passes. A saved output is a checkpoint, and it means a failure costs only the step that failed. When the payments API times out once, only fetch_payments runs again: attempts are {fetch_invoices: 1, fetch_payments: 2, match: 1, ...}.

Figure 3 · Diagram

Reading it: arrows show which outputs feed which step. The dotted loop on fetch_payments is the retry after the timeout. fetch_invoices has already been saved, so the retry does not redo it.

In code: Subtask pairs a step's work with its check; run_subtasks runs them in order, retries only the step whose check failed, and returns a RunReport of outputs and attempts. reconcile_q3 builds the five subtasks in the table. View source

Checkpoints: how much work a failure costs

Without checkpoints, a failure anywhere sends the run back to step one. With them, only the failed step is retried. The expected number of step executions to finish an -step task:

The formula and its symbols

Symbols

Symbol Meaning
chance a single step attempt succeeds (failures are noticed)
number of steps

In words: with checkpoints each step needs on average attempts, so steps need . Restarting from scratch needs successes in a row, and the expected wait for a run of successes grows roughly like .

On the example: , : with checkpoints step runs; restarting from scratch . At it's about 52.6 vs. 240.

In Python:

def with_checkpoints(n, p):
    # each step needs 1/p attempts on average
    return n / p
def restart_from_scratch(n, p):
    return (1 - p ** n) / ((1 - p) * p ** n)
round(with_checkpoints(10, 0.95), 1), round(restart_from_scratch(10, 0.95), 1)  # → (10.5, 13.4)
round(with_checkpoints(50, 0.95), 1), round(restart_from_scratch(50, 0.95))  # → (52.6, 240)

Figure 4 · Chart

0 10 20 30 40 50 60 steps in the task (n) 1 0 0 1 0 1 1 0 2 expected step executions (log scale) What a failure costs (p = 0.95 per step) checkpoints: retry the failed step (n/p) no checkpoints: restart from step 1

On the log scale, restarting from scratch climbs as a straight line (exponential growth) while checkpoints stay close to one run per step

Reading it: the x-axis is task length, and the y-axis is the number of step executions to expect, on a log scale where each gridline is ten times the one below. On that scale exponential growth draws a straight line, and the restart line is straight: each extra step multiplies its cost by about . The checkpoint line () grows in proportion to , so on a log scale it flattens: going from 10 to 20 steps rises no more than going from 5 to 10. At 50 steps the gap is about 240 step runs against 53.

So a long task without checkpoints is expensive as well as unreliable. Workflow engines that save each step's result, so that a crashed run resumes where it stopped, call this durable execution.

In code: expected_step_runs evaluates both formulas: with checkpoints, the restart formula without. View source

Chapter 4

Reflection vs. external verification

Every check in the invoice task is code, such as "each invoice appears exactly once". The tempting shortcut is to ask the model whether its own step was right, which is called reflection. It fails the way proofreading your own essay fails: you read what you meant to write, so your blind spots stay blind. External verification hands the work to something that does not share them, such as a test, a schema validator or a database query.

Say the model writes a leap-year function:

def is_leap(year):
    return year % 4 == 0        # forgets that 1900 was not a leap year

Asked to review it, the model replies "Looks correct: years divisible by 4 are leap years." Run against known answers, the function produces is_leap(1900) returned True, expected False. Given that failure, the model's second draft passes every case.

Figure 5 · Diagram

Reading it: two paths leave the same first draft. The top path stays inside the model and ends at "approved, still wrong". The bottom path goes through something outside the model, and what comes back is a concrete failure the model can fix.

In code: self_review is the top path (a model reads the code and approves or not); run_checks is the bottom path (it runs the code against known answers); generate_until_checks_pass loops the bottom path, feeding each failure back to the model until the checks pass. View source

A check is also what sets off a retry, and a failure it misses gets none. With a check that catches a share of failed attempts and up to retries, the chance that a step succeeds becomes:

The formula and its symbols

Symbols

Symbol Meaning
chance one attempt succeeds
chance the check detects a failed attempt
retries allowed after a detected failure

In words: you succeed on the first try, or fail and notice and succeed on the next try, and so on. Failures you don't notice get no retry.

On the example: , perfect check , one retry: per step, so ten steps succeed of the time instead of 60%. With a check that catches only half the failures (): .

In Python:

def p_step(p, d, r):
    # p Σ_{i=0}^{r} ((1 - p) d)^i
    return p * sum(((1 - p) * d) ** i for i in range(r + 1))
# a perfect check, one retry
round(p_step(0.95, 1, 1), 4)  # → 0.9975
# ten such steps
round(p_step(0.95, 1, 1) ** 10, 3)  # → 0.975
# a check that catches half the failures
round(p_step(0.95, 0.5, 1), 5)  # → 0.97375
round(p_step(0.95, 0.5, 1) ** 10, 2)  # → 0.77

Figure 6 · Chart

0 5 10 15 20 25 30 steps in the task (n) 0.0 0.2 0.4 0.6 0.8 1.0 P(task succeeds) Same 95% step, different verification no checks (step = 0.9500) check catches half, 1 retry (step = 0.9737) check catches all, 1 retry (step = 0.9975) check catches all, 2 retries (step = 0.9999)

With the same 95% step, a ten-step task succeeds 97.5% of the time when a check catches every failure and allows one retry, 77% when the check catches half, and 60% with no checks

Reading it: every curve uses the same 95%-reliable step. The bottom one has no checks. The two middle ones allow one retry, with a check that catches half or all of the failures. The top one catches all and allows two retries. At 10 steps, going from a check that catches half to one that catches all lifts the task from 77% to 97.5%; a second retry adds only about two points more. A retry is only as good as the check that triggers it.

In code: step_success computes from , and ; feed its result into end_to_end_success to get the ten-step curves. View source

Test yourself

4 questions

Answer each one out loud or on paper before you open it.

Question 1Q: Each step of your agent is 95% reliable. How reliable is a 20-step task, and what do you do about it?Think it through, then reveal

A: . Cut the steps (higher-level tools), add external checks after key steps with a retry of just that step, checkpoint so failures don't restart the run, and put human review where a mistake is expensive. With perfect checks and one retry, per-step reliability becomes 99.75%, and 20 steps succeed about 95% of the time.

Question 2Q: Why isn't "ask the model to double-check its work" enough?Think it through, then reveal

A: The reviewer shares the author's blind spots, and self-critique can confidently reinforce a wrong answer. Checks outside the model (tests, schema validation, a query against the source of truth, a separate grader model with a rubric) produce concrete failures the model can act on.

Question 3Q: When is plan-and-execute better than deciding one step at a time?Think it through, then reveal

A: For long, multi-part tasks where staying on track matters and where showing the plan to a person before acting is valuable. Always pair it with replanning. A plan is a hypothesis, and the first failed step is evidence.

Question 4Q: What makes a subtask "good"?Think it through, then reveal

A: It's small, concrete and verifiable. It has a definition of done that code can check, and its output is saved so a later failure doesn't redo it.

Primary sources

The papers behind this lesson

Wang et al., Plan-and-Solve Prompting: Improving Zero-Shot Chain-of-Thought Reasoning by Large Language Models (2023).

It showed that asking a model to first devise a plan and then carry it out step by step reduces missed steps compared with reasoning straight through.

The paper
Shinn et al., Reflexion: Language Agents with Verbal Reinforcement Learning (2023).

Agents that turn feedback from the environment (failed tests, wrong answers) into written lessons and retry improve markedly, and the gains come from external signals.

Read on rumblr →The paper

Researcher's shelf

Further reading

  • Anthropic, Building effective agents: https://www.anthropic.com/engineering/building-effective-agents
  • Lilian Weng, LLM Powered Autonomous Agents (planning, reflection): https://lilianweng.github.io/posts/2023-06-23-agent/
  • Temporal, durable execution: https://docs.temporal.io/
  • LangGraph docs (persistence and checkpoints): https://langchain-ai.github.io/langgraph/

About this lesson. This lesson is built from the open-source AI Primer ↗. Its text, figures and numbers are generated from the Primer's source at commit f8b43d1, so the two always agree: the explanation, the code that builds it and the tests that prove it. Its hand-built interactive figures are still to come: the status page shows which lessons have them.