Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 39 ยท Planning and Reasoning

Replanning and Recovering from Failure

Keep agents on track when reality changes: detect stuck loops, checkpoint progress, replan with a budget, choose alternatives and escalate to a human, with Python demos.

No plan survives contact with the real world untouched. A page has moved, a file is empty, a service is down, the user changes the goal halfway. What distinguishes a useful agent from a demo is what it does next. This lesson covers the practical toolkit for noticing trouble, saving progress, trying a different route, and knowing when to ask for help. It builds on planning, plan-and-execute and tool errors and retries.

Noticing that something is wrong

An agent cannot recover from a problem it does not see. Common signals, from cheap to expensive:

  • A tool error (handled by retries or a message back to the model).
  • Repetition: the same action with the same arguments again and again.
  • No progress: several steps pass without a new fact, a finished task or a changed state.
  • Budget burn: steps, tokens or time are running out compared with progress made.
  • A failed check: a validator, test or critic rejects the result.
  • Contradiction: an observation disagrees with an assumption in the plan.
Python Detect loops and lack of progress

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

When a warning fires, do not simply continue. Interrupt the loop, tell the model what was detected ("you have repeated this search three times"), and ask for a different approach or a final answer with what is known.

Checkpoints: save progress, resume later

For long tasks, store the state after every completed step: the goal, the plan, task statuses and the results gathered. If the process crashes, or you decide to replan, nothing is lost. This is the same state you keep in agent state, written somewhere durable.

Python Checkpoint and resume

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The resumed run skips finished tasks. Make steps idempotent where you can, so redoing one is harmless.

Recovery strategies, in order

  1. Retry transient failures with backoff.
  2. Repair the call: fix arguments using the error message.
  3. Use an alternative tool or source: another search provider, a cached result.
  4. Replan the remaining work: give the planner the goal, the completed results, and the failure; ask for new steps only for what is left.
  5. Reduce the goal: deliver a partial answer and say what is missing.
  6. Escalate: stop and ask the user or an operator, with a clear description of what was tried.
flowchart TD
  F["Failure detected"] --> A{"Transient?"}
  A -- yes --> R["Retry with backoff"]
  A -- no --> B{"Fixable call?"}
  B -- yes --> X["Repair arguments"]
  B -- no --> C{"Alternative route?"}
  C -- yes --> P["Replan remaining steps"]
  C -- no --> E["Partial answer or escalate to human"]

Try cheap fixes first; escalate when options run out.

Bounded replanning with a clear handoff

The recovery logic itself must have limits. Count replans, and when they are used up, hand over with a useful report rather than a vague apology.

Python Replan with a limit, then escalate

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The second call stops after two failures and escalates with a report listing what was tried, which is exactly what a human needs in order to help. Approval and handoff to people is covered in human-in-the-loop.

Honest partial results

When the agent cannot finish, the right output is a truthful partial result: what was found, what is missing, and why. Never let a model fill gaps with invented data to look successful. Make that part of the instructions ("If you cannot verify a number, say so") and of the final answer format, for example a confidence or missing field in the structured output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which is a sign that an agent is stuck?

    Repeated identical actions and a lack of new information indicate no progress.
  2. 2. Why checkpoint progress after each step?

    Saved state allows resuming and replanning only the remaining work.
  3. 3. What should the replanner be given?

    With the completed work and the error, it can plan only what is still needed.
  4. 4. What is the best output when the agent cannot finish?

    Honest reporting lets a user or operator continue the work.

Summary

  • Detect trouble through errors, repetition, no progress, budget burn, failed checks and contradictions.
  • Checkpoint state so work can resume and replans only cover what remains.
  • Recover in order: retry, repair, alternative route, replan, reduce the goal, escalate.
  • Limit replanning itself, and escalate with a report of what was tried.
  • Prefer honest partial results to invented success.

Next: Short-Term Memory and Context Windows