Skip to content
mentoor

Topic 5 of 5 · 5 min read

Recovering from errors mid-task

Goal: Choose the right recovery action (retry differently, roll back, ask or stop) for a described mid-task error.

Match the recovery to the error: retry only what is transient, change approach when the same attempt keeps failing, roll back when the state is damaged, and ask or stop when the next move is not yours to make. The default, "try again", is right less often than agents think.

Check yourself · 1 of 3

A request to read an order status returns 503 once. What is the best recovery?

Choose one answer

Why it matters

An agent runs many steps with real side effects: files written, packages installed, rows changed, emails sent. A mid-task error leaves the world in a state the plan did not expect. A blind retry can repeat the damage, loop until the budget is gone, or "fix" the problem by deleting the failing check.

The five moves

  1. Retry as is. For transient failures: a timeout, a 503, a rate limit. Back off, cap the attempts (three is common) and make sure the call is safe to repeat. A retry of "create invoice" without an idempotency key can bill twice.
  2. Retry differently. The same attempt failed twice, so change something: read the error text, check the docs, use another tool. In Reflexion, the agent writes a short note on why it failed and uses that note in the next try. That works better than the same call again.
  3. Roll back to a checkpoint. When the working state is damaged, such as half-applied edits or a broken build, return to the last known good point: git stash, git checkout of the files the agent touched, or a restored snapshot. Commit often so there is a checkpoint worth returning to. Never use a destructive reset on work you did not make.
  4. Ask. The error needs a decision or a permission the agent does not have: a missing credential, an ambiguous requirement, a change that touches production.
  5. Stop. The task is unsafe or impossible as described. Stop, report what was done, what failed and the current state.

Before and after

Log:
step 4: npm test  -> 3 failed (ValidationError in orders.spec.ts)
step 5: npm test  -> 3 failed (same)
step 6: edit orders.spec.ts: remove the three assertions
step 7: npm test  -> passed

The agent removed the check to remove the error. A better log: after the second identical failure, read the stack trace, find the validator that rejects a new field, fix it, rerun. If it cannot, stop and report the three failing tests.

What goes wrong

  • Retry loops. No cap and no change between attempts.
  • Deleting the check. The error goes away because the test did.
  • Rollback of too much. A hard reset wipes a teammate's uncommitted work.
  • Pushing on after damage. Later steps run on a broken base and bury the first error.
  • Silent give-up. The agent skips the failure and reports success.

Examples

Retry or not

POST /charges times out. Without an idempotency key, retrying could charge the customer twice, so the agent first checks whether the charge exists, and only then retries with a key. A failed GET /status with a 503 can simply be retried with backoff.

Roll back, then try again

git status --short
git diff --stat
git checkout -- src/orders/

A codemod rewrote 14 files in src/orders/ and left the build broken. The agent lists what changed, restores only the folder it touched, and reruns with a narrower pattern. It does not run a hard reset on the whole repo.

Key points

  • Retry as-is only for transient errors such as timeouts and rate limits, with a cap and with calls that are safe to repeat.
  • After the same failure twice, change the approach: read the error, check the docs, or try another tool.
  • Roll back to a known-good checkpoint when the working state is damaged, and commit often so one exists.
  • Ask when the next move needs a decision or permission the agent does not have; stop when the task is unsafe or impossible.
  • Never make an error disappear by deleting or weakening the check that found it.

Common mistakes

  • Retrying a failing call without a cap, without a change and without reading the error.
  • Retrying a non-idempotent action such as charging or sending without checking if it already happened.
  • Rolling back with a broad destructive command that also discards work the agent did not make.
  • Treating a deleted or loosened test as a fix for the failure it reported.

Check yourself · 2 of 3

A codemod edited 14 files and now the build is broken. The agent is on its own branch with the changes uncommitted. What is the best recovery?

Choose one answer

Check yourself · 3 of 3

Mid-task, the agent finds it needs a production database password it was not given. What should it do?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic