Skip to content
mentoor

Topic 3 of 5 · 5 min read

Detecting and recovering stuck or runaway runs

Goal: Read an agent run log, spot the loop and choose to cancel, resume or escalate.

A stuck run repeats actions without making progress, so detect it from the trajectory (the same call, the same result, no new information) and then pick one recovery: cancel, resume from a checkpoint, or escalate to a person.

Check yourself · 1 of 3

Which pattern most clearly signals a stuck run?

Choose one answer

Why it matters

Agents do not get bored. When a tool fails in a way the model does not understand, it often tries again with the same arguments, or with a tiny rewording, until a budget stops it. Budgets limit the damage. Detection catches it earlier, and recovery decides what happens to the work and to any side effects already made.

How to detect

  1. Repeated calls. Hash each tool name plus arguments. The same hash three times in a row is a loop.
  2. No progress. Compare the state after each step: new facts found, new files changed, a todo closed. Five steps with none is stuck.
  3. Retries without change. A retry that sends the same request after the same error can only get the same error.
  4. Oscillation. Two steps that undo each other, such as edit then revert.
function isLooping(steps: Step[]): boolean {
  const last = steps.slice(-3).map(s => hash(s.tool, s.args));
  return last.length === 3 && new Set(last).size === 1;
}

Choosing the recovery

  • Cancel when the task is cheap to redo and no side effects happened. Mark it failed with the reason.
  • Resume from a checkpoint when the work before the loop is good and the cause is fixable: a changed prompt, a repaired tool, a new instruction injected at the checkpoint.
  • Escalate when the task has real consequences, the cause is unclear, or a side effect is half done.

Cleaning up side effects

Before cancelling, read the run's action log. Which emails were sent, which rows were written, which payments were started? A run killed halfway can leave an order created but not paid. Each side effect needs a compensating action or a person to finish it. This is why actions should be idempotent and logged.

Before and after

Before: a run searched find_invoice("INV-2291") seven times, got "not found" each time, and was cancelled at the step cap. After: the loop check stopped it on the third identical call, resumed the run with the hint "invoice lookup failed, ask the user for the right number", and the user supplied it.

What goes wrong

  • Blind retries on the same input. Retrying a failed call unchanged is a loop in slow motion.
  • Cancelling without checking side effects. The run is gone, the half-made refund remains.
  • Resuming without fixing the cause. The run loops again from the same checkpoint.

Examples

Reading a loop in a run log

step 4  find_invoice("INV-2291")  -> not_found
step 5  find_invoice("INV-2291")  -> not_found
step 6  find_invoice("inv-2291")  -> not_found
step 7  find_invoice("INV-2291")  -> not_found

The arguments differ only in letter case. The result never changes and the agent has learned nothing. The right recovery is to resume with a new instruction (ask the user), not to retry.

A half-finished side effect

A refund run was cancelled at step 12. The log shows create_refund succeeded at step 9, and notify_customer never ran. Cancelling alone leaves a refund the customer does not know about. The cleanup is to send the notice, or to escalate with the log so a person completes it.

Key points

  • Detect loops from the trajectory: identical tool calls, no new information, or steps that undo each other.
  • Hash tool name and arguments, and treat three identical calls in a row as a loop.
  • Cancel when redo is cheap, resume when earlier work is good and the cause is fixed, escalate when consequences are real.
  • Before cancelling a run, read its action log and clean up half-finished side effects.
  • Never resume from a checkpoint without changing what caused the loop.

Common mistakes

  • Treating a retry with the same arguments as a new attempt.
  • Cancelling a run without reading what it already did to the outside world.
  • Resuming from a checkpoint with the same prompt and the same broken tool.
  • Detecting only by total step count, which finds loops late.

Check yourself · 2 of 3

A run was cancelled at step 12, after step 9 created a refund and step 10 should have notified the customer. What must happen next?

Choose one answer

Check yourself · 3 of 3

A run loops because a lookup tool rejects the agent's input format. When is resuming from a checkpoint a good choice?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic