Skip to content
AgenticAIWiki
  1. Learn
  2. Part 9
  3. 47. Plan-and-execute and ReWOO
  4. 47.3

Lesson 47.3 · 3 of 5 · 4 min read · Reviewed 24 Nov 2026

Replanning when a step fails

By the end, you can

  • Design replanning for failed or surprising steps.
  • Keep replanning bounded.

Triggers

  • A step fails (an error, a timeout, a tool returning is_error).
  • A result breaks an assumption (“no refunds last month”).
  • A checkpoint is reached: after every N steps, check the plan still makes sense.

What the planner needs

Goal:            Summarise last month's refunds for finance.
Done:            1. Exported orders (4,210 rows).
Failed:          2. Group by reason: the reasons service returned 503.
Remaining plan:  3. Draft summary  4. Email finance.
Task:            Revise the remaining steps.

A good revision might be: “2a. Use the reason codes already in the export. 2b. Note in the summary that free-text reasons were unavailable.”

Bounds

Guard Why
Max replans per task (for example, 3) Stops endless re-planning
Don’t repeat a failed approach Keep a “tried” list
Escalate after the limit A human decides (chapter 45)
Preserve completed work Don’t redo successful steps

Honest reporting

When the final result is partial, say so: what was done, what wasn’t, and why. Silent partial success is one of the most damaging agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. failures (43.4).

Key points

  • Replanning updates the remaining plan when a step fails or returns something unexpected.
  • Trigger it on errors, on results that break the plan's assumptions, or at checkpoints.
  • Pass the planner the goal, what's done, what failed, and why.
  • Limit replans, and escalate to a human when the plan keeps failing.
Hands-on labRuns in the Playground

Replan around a failed step

Update the remaining plan after a failure without redoing finished work, and escalate at the limit.

You will have: A plan runner with bounded replanning and an honest escalation message.

Steps

  1. Run the starter: both goals fail at their first broken step.
  2. When a step fails, ask replan() for a new remaining plan and keep the done list.
  3. Count replans and stop at MAX_REPLANS.
  4. Escalate with what was done and what still fails.
  5. Check that the second goal never re-runs fetch_sales_v2 or build_table.

Done when

  • The monthly report finishes with 1 replan.
  • The upload goal escalates after 2 replans and lists its finished steps.
  • No failed approach is tried twice.

Open in the Playground No API key needed.

Starter code (Python)
# Replan around a failed step: keep finished work, avoid failed approaches, and escalate at the limit.
WORKS = {"fetch_sales_v1": False, "fetch_sales_v2": True, "build_table": True,
         "upload_s3": False, "upload_sftp": False, "upload_drive": False, "export_csv": True}
ALTERNATIVES = {"fetch_sales_v1": ["fetch_sales_v2"], "upload_s3": ["upload_sftp"], "upload_sftp": ["upload_drive"]}
MAX_REPLANS = 2


def replan(failed, remaining, tried):
    """Simulated planner: swaps the failed step for an alternative it has not tried."""
    for alt in ALTERNATIVES.get(failed, []):
        if alt not in tried:
            return [alt] + remaining
    return None


def run(goal, plan):
    done, tried, replans = [], set(), 0
    plan = list(plan)
    while plan:
        step = plan.pop(0)
        tried.add(step)
        if WORKS[step]:
            done.append(step)
            continue
        # TODO: if replans has reached MAX_REPLANS, return an escalation string with done and the failing step
        # TODO: otherwise ask replan() for a new remaining plan (keep `done`), count the replan, and carry on
        return f"{goal}: FAILED at {step}"
    return f"{goal}: done in {len(done)} steps, replans: {replans}"


print(run("monthly report", ["fetch_sales_v1", "build_table", "export_csv"]))
print(run("upload report", ["fetch_sales_v2", "build_table", "upload_s3"]))
Show a solution
# Replan around a failed step: keep finished work, avoid failed approaches, and escalate at the limit.
WORKS = {"fetch_sales_v1": False, "fetch_sales_v2": True, "build_table": True,
         "upload_s3": False, "upload_sftp": False, "upload_drive": False, "export_csv": True}
ALTERNATIVES = {"fetch_sales_v1": ["fetch_sales_v2"], "upload_s3": ["upload_sftp"], "upload_sftp": ["upload_drive"]}
MAX_REPLANS = 2


def replan(failed, remaining, tried):
    """Simulated planner: swaps the failed step for an alternative it has not tried."""
    for alt in ALTERNATIVES.get(failed, []):
        if alt not in tried:
            return [alt] + remaining
    return None


def run(goal, plan):
    done, tried, replans = [], set(), 0
    plan = list(plan)
    while plan:
        step = plan.pop(0)
        tried.add(step)
        if WORKS[step]:
            done.append(step)
            continue
        new = replan(step, plan, tried) if replans < MAX_REPLANS else None
        if new is None:
            return f"{goal}: ESCALATE after {replans} replans. done={done}, still failing={step}"
        replans += 1
        print(f"  replan {replans}: {step} failed, new plan {new}")
        plan = new
    return f"{goal}: done in {len(done)} steps, replans: {replans}"


print(run("monthly report", ["fetch_sales_v1", "build_table", "export_csv"]))
print(run("upload report", ["fetch_sales_v2", "build_table", "upload_s3"]))
Quick checkStep 2 of 6 fails because the API it needs is down. What should a well-designed agent do?
How sure are you?
Pick an answer and how sure you are