111. Loop Engineering 5 - Budgets, Backoff and Failure Exits

Stop a loop safely: iteration and spend caps, exponential backoff, and exits that leave the system in a known state.

By Jacques Botte, founder of Toptronic®. Last updated 17 October 2026.

The lesson

A budget has four dimensions and they are not interchangeable. Turns bound how much the loop can try; wall-clock time bounds how long a person waits and how much a lease is held; tokens bound the context cost, which grows faster than the turn count because every turn appends; money bounds what the operator will tolerate. Set all four, and make the smallest one the one that actually fires, because that is the one you have thought hardest about.

Backoff is how a loop asks again without becoming a denial-of-service attack on its own dependency. Double the delay between attempts, add jitter so that a hundred parallel loops do not retry in lockstep, cap the delay at something a human would still call reasonable, and give up after a stated number of attempts. The jitter matters more than the doubling: the synchronised retry storm is the classic cause of an outage that survives the original fault.

Distinguish the three kinds of failure, because they need three different answers. A TRANSIENT failure (a timeout, a 429, a locked file) deserves a retry with backoff. A PERMANENT failure (a missing file, a bad schema, a rejected credential) deserves an exit with a clear message - retrying it wastes the entire budget and produces nothing. An UNKNOWN failure deserves one retry, then a stop and a record, because a loop that keeps trying something nobody understands is how a small fault becomes a long incident.

The exit is the part people skip, and it decides whether the loop is safe. A clean exit releases the lease, writes the state file, records the reason and tells somebody. A dirty exit leaves the lock held and the state half-written, and the next run inherits a mess it cannot interpret. Write the exit first: what is true after a success, what is true after a give-up, and what is true if the process is killed mid-turn - and make the state file atomic so that the third answer is a real one.

Finally, make the failure loud and make it boring. One line to a log file that nobody reads is not a failure signal; a summary that appears where the operator already looks, naming the loop, the exit reason, the budget spent and what is left half-done, is. The measure of a good loop is not how rarely it fails but how quickly a person can tell that it did, and what they have to clean up when it has.

Check yourself

Question 1: Why is jitter added to exponential backoff?
  1. To make the log look random
  2. So many loops do not retry in lockstep and stampede the dependency — correct
  3. To increase total retries
  4. To avoid writing a budget

Answer: So many loops do not retry in lockstep and stampede the dependency

Synchronised retries turn one transient fault into a sustained outage. Jitter spreads them out; the doubling alone does not.

Question 2: How should a permanent failure - a missing file or a rejected credential - be handled?
  1. Retry until the budget is spent
  2. Exit with a clear message instead of consuming the budget — correct
  3. Retry with a longer prompt
  4. Ignore it and continue

Answer: Exit with a clear message instead of consuming the budget

Retrying something that cannot succeed burns the whole budget and produces nothing. Transient failures retry; permanent ones stop and say why.

Question 3: What makes a loop's exit safe?
  1. Printing a message
  2. Releasing the lease, writing state atomically, recording the reason and telling someone — correct
  3. Restarting the process
  4. Increasing the budget

Answer: Releasing the lease, writing state atomically, recording the reason and telling someone

A dirty exit leaves a held lock and a half-written state file for the next run to misread. The exit is part of the design.

← Previous lesson · All 134 lessons · Next lesson →

The full course — 134 lessons and 402 quiz questions — ships inside the app. Get TPEE to study it offline.

This lesson is the written summary of a course that ships inside the TPEE prompt editor for Windows - thirteen sections, a 0-100 score before you send anything, and it works with no network connection. The three one-time prices, and a 14-day free trial.