Agentic Engineering from First Principles All chaptersLecturesLabDownload the book
Part V · Engineering Reliable Agents

Chapter 24: Failure and Recovery

Central questionHow should an agent recover from failure without multiplying load, repeating side effects, losing progress, or hiding the original defect?

Lecture

Chapter principle

Make failure survivable. Classify failures, bound retries, diversify fallbacks, identify side effects, persist progress, and validate recovered state. Recovery is part of normal control flow, not an exception added at the end.

Key takeaways

Experiments

Every experiment in the book is small, runnable and printed with its real output. In the Lab you can replay a recorded real run of each one, with no programming, and watch what the model and the agent do.

Experiment 43: Retry, back off, fall back

When a call fails, when should the agent try again?

A retry policy wraps a real model call that asks for the capital of Peru. A provider cannot be made to time out on demand, so timeouts are injected before the real call. A timeout counts as temporary: the policy waits a little longer each time and tries again, up to 3 attempts. A request for a model that does not exist is permanent: the provider refuses it, and the policy stops at once. Four cases are tried: two timeouts then success, a permanent error, an outage with a fallback to a smaller real model, and an outage without one. This is a recorded run; the waits are partly random.

Experiment 44: Retrying a payment safely

If a payment’s reply is lost, is it safe to send it again?

A real agent has a payment tool. The user asks it to charge a customer 100 dollars. The payment server charges the card, but the reply is lost on the way back, so the agent sees only a timeout. The user then says "Please try again." The conversation runs twice: once plainly, and once with an idempotency key attached by the tool, a label that says "this is the same payment as before", so the server can recognize the repeat. This is a recorded run of a real model; a new run may differ.

Experiment 45: Checkpoint and resume

After a crash, can a long task pick up where it left off?

A five-step task (retrieve, analyze, draft, verify, submit) where every step is a real model call. After each step it saves a checkpoint: a small file recording how far it got and which version of the source data it used. It crashes during the fourth step and is started again. Then the same thing happens, but this time the price in the source changes from 80 to 90 while the task is down. The crash is simulated. This is a recorded run of a real model; its wording will differ in a new run.

All the code for this chapter: github.com/agentic-engineering-book/code

This is Chapter 24 of Agentic Engineering from First Principles: thirty chapters, a case study and fifty-nine executable experiments, from a single model call to controlled, self-improving agents.
Download the bookBrowse all chapters
← Chapter 23: ObservabilityChapter 25: Human-in-the-Loop →