Skip to content

Bound an agent loop so it stops instead of spiralling

Add termination conditions to a retrying agent so it fails loudly at a known point rather than burning budget quietly.

by Kelv · published Sep 6, 2026

Not yet verifiedno reports yet

An agent that retries is useful. An agent that retries the same failing call with slightly different wording forty times is an outage with a bill attached. This adds explicit termination: a hard step ceiling, a repeat detector that catches near identical attempts, and a rule that a failure escalates rather than being retried differently. The important part is that stopping produces a structured failure the caller can act on, not silence.

Target models
claude-opus-4.5, claude-sonnet-4.5
Time
45 min
Difficulty
Hard
Cost
6,000 input tokens, ~2,000 output tokens, plus replay cost

Tools / stack

Claude API with tool useyour agent runtimea token budget counter

Inputs required

The agent’s system prompt and loop code, plus a trace of a run that spiralled

Steps

  1. 1.Set a hard maximum step count enforced in code, not requested in the prompt. A prompt level limit is a suggestion.
  2. 2.Add a repeat detector that compares each tool call against the previous three, treating near identical calls as a repeat rather than requiring exact matches.
  3. 3.On a detected repeat, stop rather than nudging. A nudged agent usually produces a fourth variation of the same call.
  4. 4.Add a token budget per run, checked between steps, with a hard stop when exceeded.
  5. 5.Make every termination produce a structured failure containing the reason, the step count, and the last attempted call.
  6. 6.Replay the spiral trace against the bounded loop and confirm it now stops, and record at which step.

Expected output

A bounded agent loop, plus a replay of the original spiral showing where it now terminates and what structured failure it returns

Golden Example

Input
Trace of an agent that made 61 near identical search calls before hitting the platform timeout, plus the loop code
Output
Bounded to 12 steps with a repeat detector on a three call window. Replay terminated at step 7 with reason “repeated_call”, the step count, and the last attempted arguments. Token spend on the replay was 4 percent of the original run.

Sign in to verify this playbook: run it and log what happened.

Reports

0 reports

Raw reproduction reports: evidence, not a verified badge yet.

No reports yet.

Run it and log the result. Be the first to prove it works.