What this is
Why do AI agents fail?
An AI agent, a program that lets an AI language model work through a task in many steps, often works in a demo and fails on real tasks. One reason is arithmetic. If each step succeeds 95% of the time, a ten-step task succeeds 60% of the time and a twenty-step task 36%. Most of the other failures come from the system around the language model: flaky outside services, loops, bad retrieval, instructions hidden in documents, an agent nobody uses. Each has a known fix, and the lesson builds the ones that are plain engineering.
Why it matters: You need it the day an agent that passed every demo starts failing in production and nobody can say why. A demo may take three steps where a real task takes twenty, and at 95% per step any chain longer than two steps succeeds less than nine times in ten.
At a glance
Key takeaways
- Agents fail in production in ten recognisable ways, each with a known fix, and most fixes change the system around the language model.
- Steps multiply: at 95% per step, 10 steps succeed 60% of the time. Use fewer steps and check the key ones.
- Retry a transient failure after a wait that doubles up to a cap, plus random jitter. Never retry an invalid request; make a write idempotent first.
- Put a circuit breaker around each outside service, so an outage makes requests fail at once instead of piling up behind timeouts.
- Detect loops (same tool, same arguments) and set hard limits on steps and tokens.
How it works, from scratch
The catalogue below lists ten ways an agent fails in production, each with its symptom, its fix and the lesson that builds the fix. The chapters after it take three rows further: compounding error, brittle integrations and loops.
| Failure | Symptom | Fix | Lesson |
|---|---|---|---|
| Compounding error | Long tasks fail far more often than short ones | Fewer steps, checks after key steps, checkpoints | How an AI agent plans a long task |
| Bad retrieval | Confident, wrong answers | Hybrid search, reranking, retrieval evals | Retrieval-augmented generation |
| Ambiguous tools | Wrong tool, or bad arguments | Precise descriptions, fewer tools, validation | Tools |
| Context rot | Quality drops as a session grows | Summarize, trim, restart with a state handoff | Context engineering |
| Loops and runaway | The same call repeated; cost spikes | Step budgets, loop detection, stop conditions | The agent loop |
| No evals | Regressions ship silently | Golden sets in CI, online monitoring | Evaluation |
| Prompt injection | The agent obeys instructions found in data | Untrusted-content boundaries, privilege separation | Guardrails |
| Messy enterprise data | Garbled tables, missing permissions | Invest in parsing; permission-aware retrieval | Retrieval-augmented generation |
| Brittle integrations | Timeouts, expired credentials, API changes | Retries with backoff, circuit breakers, contract tests | Why AI agents fail in production |
| No adoption | It works, and nobody uses it | Build with users, show sources, easy human handoff | Safe deployment |
In code: FAILURES is this table as data, one Failure record per row. View source
Chapter 1
Compounding error
An agent's task is a chain of steps, and the task succeeds only if every step does. If each step succeeds 95% of the time, independently of the others, the chances multiply:
| Steps | Chance every step succeeds |
|---|---|
| 1 | 0.95 |
| 2 | 0.95 × 0.95 = 0.9025 |
| 10 | 0.95¹⁰ ≈ 0.599 |
| 20 | 0.95²⁰ ≈ 0.358 |
The formula and its symbols
Symbols
| Symbol | Meaning here | Worked example |
|---|---|---|
| chance a single step succeeds | 0.95 | |
| number of steps that must all succeed | 10 | |
| multiplied by itself times | 0.599 |
In words: the chance that every step succeeds is the per-step chance multiplied together once per step.
On the worked example: 0.95 multiplied by itself 10 times is 0.599, so a ten-step task at 95% per step fails four times in ten.
In Python
p, n = 0.95, 10
# p multiplied by itself n times
round(p ** n, 3) # → 0.599
# fails about four times in ten
round(1 - p ** n, 1) # → 0.4
New to Σ, ∂ or ‖x‖? The symbols reference decodes each one as a few lines of Python.
There are two ways out: fewer steps, or more reliable ones. Checking a step's result where it happens, and retrying just that step when the check fails, keeps an error from travelling down the chain. Checkpoints let a failed task resume from the middle, and a person can review the steps where a mistake would cost most.
Figure 1 · Diagram
flowchart LR S1[Step 1<br/>95%] --> S2[Step 2<br/>95%] --> S3[...] --> S10[Step 10<br/>95%] --> D[Done:<br/>60% of the time] S2 -.->|verify, retry<br/>just this step| S2
Figure 2 · Chart
Twenty steps succeed 82% of the time at 99% per step but only 36% at 95% and 12% at 90%
In code: chain_success is ; max_steps_for runs it backwards,
returning the most steps you can chain and still reach a target success rate. View source
Chapter 2
Brittle integrations: retries with backoff
Many of an agent's steps call an outside service, such as a search API or a database. When the call times out or the connection drops, the failure is often transient: the same call a moment later may work, so retrying it makes the step more reliable. Wait between attempts, twice as long each time (exponential backoff), so an overloaded service has room to recover. Add a random part to each wait (jitter), so that clients that failed together do not all retry together.
Say a service times out twice and then answers. With a base delay of 0.1 s the waits are 0.1 s, then 0.2 s, and the third attempt succeeds. A cap of 10 s keeps any wait from growing past 10 s. An invalid request fails the same way every time, so it is raised at once: retrying would only add load.
The formula and its symbols
Symbols
| Symbol | Meaning here | Worked example |
|---|---|---|
| wait before retry number | = 0.1 s, = 0.2 s | |
| how many retries have already happened | 0, 1 | |
| base delay | 0.1 s | |
| doubles with every retry | 1, 2, 4, … | |
| the cap on any single wait | 10 s |
In words: each wait doubles the previous one, starting from the base delay, but never exceeds the cap.
On the worked example: = min(10, 0.1 × 1) = 0.1 s and = min(10, 0.1 × 2) = 0.2 s.
In Python
b, D_max = 0.1, 10
# d_0 and d_1
[min(D_max, b * 2 ** k) for k in range(2)] # → [0.1, 0.2]
Figure 3 · Diagram
sequenceDiagram participant A as Agent tool participant S as Upstream API A->>S: request S--xA: timeout Note over A: wait 0.1 s A->>S: retry 1 S--xA: timeout Note over A: wait 0.2 s A->>S: retry 2 S-->>A: 200 OK
Figure 4 · Chart
The delay doubles each attempt until it hits the 10-second cap, and jitter scatters five clients' retries across the whole range below it
A retried write must be idempotent, meaning that sending it twice has the same effect as sending it once, or a retried "create order" makes two orders. A unique idempotency key sent with each request lets the service recognise a repeat and skip it.
In code: retry_with_backoff retries only timeouts and dropped
connections, waiting backoff_delays between attempts (with full jitter if
asked), and raises any other error at once. View source
Chapter 3
Brittle integrations: circuit breakers
Retries handle a blip in one request. During an outage that lasts minutes, every request retries and fails in turn, the agent hangs on timeouts, and the retries load a service that is trying to recover. A circuit breaker stops the calls instead. The name comes from the breaker in a fuse box, which cuts the power to a circuit that keeps shorting.
A breaker wraps every call to one outside service. While closed, it lets calls through and counts failures in a row. After a set number it opens: calls fail at once without touching the service (fail fast), so the agent can fall back to a cached answer, tell the user or hand off to a person. After a cool-down it goes half-open and lets one trial call through: success closes it, failure opens it again.
With a threshold of 3 and a cool-down of 30 s, three timeouts in a row open the breaker. A call 5 s later fails at once without reaching the service. A call 31 s after the breaker opened goes through as the trial, succeeds, and closes the breaker.
Figure 5 · Diagram
stateDiagram-v2 [*] --> Closed Closed --> Closed: success (reset the failure count) Closed --> Open: 3 failures in a row Open --> Open: calls rejected instantly Open --> HalfOpen: cool-down elapsed HalfOpen --> Closed: trial call succeeds HalfOpen --> Open: trial call fails
Figure 6 · Chart
Through a 50-second outage the failing service is called only 5 times, while 21 requests fail fast at the open breaker instead
Retries and breakers handle a service that is slow or down, not one whose API has changed. Contract tests, automated checks that an external API still accepts and returns what your tool expects, catch that change before users do.
In code: CircuitBreaker.call follows the state diagram, and
simulate_outage replays the outage in the figure. View source
Chapter 4
Loops and runaway
The failures so far come from the services an agent calls. A loop is the
agent's own failure: it calls the same tool with the same arguments,
gets the same result and calls it again. search("vpn") three times in a
row is a loop, since nothing new can come back. search("vpn"),
search("vpn error"), search("ERR-4012") is the same tool refining its
query, which is progress. A loop detector flags the first pattern. Pair it
with hard limits on steps and tokens (see Cost and latency), so a
confused agent stops and hands the task to a person.
In code: is_looping reports whether the last few calls were the same
tool with identical arguments. View source
Test yourself
4 questions
Answer each one out loud or on paper before you open it.
Question 1Your agent works in demos and fails half the time in production. Where do you look first?Think it through, then reveal
At the length of real tasks versus demo tasks (compounding error), and at traces for the first failing step: retrieval misses, wrong tools or arguments, tool errors from integrations, context growth. Then fix per-step reliability where it's lowest, add verification after key steps, and put the failing cases into the eval set.
Question 2Why add jitter to retries?Think it through, then reveal
Without it, every client that failed at the same moment retries at the same moment, producing synchronized waves of load that keep an overloaded service down. Random delays spread the retries out.
Question 3When should you not retry?Think it through, then reveal
When the error can't go away on its own: invalid input, permission denied, not found. And never retry a non-idempotent write without an idempotency key, or you'll duplicate its effect.
Question 4What's the difference between a retry and a circuit breaker?Think it through, then reveal
A retry handles a blip for one request. A breaker handles an outage across many requests: it stops sending traffic to a failing dependency, fails fast so callers can fall back, and probes for recovery.
Primary sources
The papers behind this lesson
Researcher's shelf
Further reading
- Martin Fowler, CircuitBreaker: https://martinfowler.com/bliki/CircuitBreaker.html
- AWS Builders' Library, Timeouts, retries, and backoff with jitter: https://aws.amazon.com/builders-library/timeouts-retries-and-backoff-with-jitter/
- Anthropic, Building effective agents: https://www.anthropic.com/engineering/building-effective-agents
About this lesson. This lesson is built from the open-source AI Primer ↗. Its text, figures and numbers are generated from the Primer's source at commit f8b43d1, so the two always agree: the explanation, the code that builds it and the tests that prove it. Its hand-built interactive figures are still to come: the status page shows which lessons have them.