Failure Scenarios: Detection and Runbooks
Each of the seven production failure scenarios has a characteristic signal in traces and metrics and a concrete runbook; knowing both turns a 3 a.m. page into a ten-minute fix.
You have not marked these as done yet. Read on anyway if they feel familiar; come back to them if something here does not land.
Key points
- Every failure scenario has a distinct signal in traces and metrics; classify before you fix.
- Tool failures need backoff, caps, fallbacks and actionable error text returned to the model.
- Wrong-tool and bad-retrieval problems are eval problems: reproduce on the golden set, fix the pipeline, re-run.
- Loops are almost always caused by an unhelpful tool result; the step limit is the safety net, not the fix.
- Cost and latency problems hide in the top 1% of requests; read those traces first.
Tool failure and wrong tool selection
Scenario 1: a tool fails. Signal: tool span error rate rises for one tool; retries cluster; task success drops while step count rises. Runbook: classify the error (timeout, 429, 5xx, 4xx, malformed). Transient errors get exponential backoff with jitter, capped at three attempts and a total timeout. Persistent errors get a fallback (a secondary API, a cached value, a degraded answer) and the error text is returned to the model as the tool result so it can change plan. Never retry a non-idempotent call without an idempotency key (Idempotency).
Scenario 2: the model chooses the wrong tool or arguments. Signal: wrong-tool rate on the golden set; in production, a spike in argument validation failures or in a tool being called with suspiciously uniform arguments. Runbook: inspect twenty traces where the wrong tool was called. Usually the cause is two tools with overlapping descriptions, a missing example in the description, or a parameter whose semantics the model guessed. Fix the schema and description, add the failing cases to the eval set, and re-run before shipping (Tool Schemas, Golden Datasets). If ambiguity is inherent, add an explicit router step (Router Architecture).
Bad retrieval and provider outage
Scenario 3: retrieval returns bad context. Signal: answer faithfulness drops while retrieval latency is normal; users report confident wrong answers; retrieved chunks have low reranker scores. Runbook: check the query first (is the rewritten query sensible?), then the index (was the corpus re-embedded after the model change? are new documents missing?), then chunking (are answers split across chunk boundaries?), then filtering (is a metadata filter excluding everything?). Fix the earliest broken stage; measure precision@k and faithfulness before and after (RAG Evaluation).
Scenario 4: the provider is unavailable. Signal: model span error rate or latency jumps across all requests simultaneously; 429 and 503 codes; the provider status page. Runbook: the fallback chain should already be engaging automatically (Fallbacks, Caching and Model Routing). Verify it did, verify the fallback model passes your smoke evals, and watch cost, since fallbacks are often more expensive. If no fallback exists, switch to a degraded mode (cached answers, a static message) rather than erroring for every user.
Loops, cost and latency
Scenario 5: the agent loops. Signal: steps-per-task p99 hits the limit; the same (tool, args) pair appears repeatedly in a trace; the model alternates between two tools. Runbook: the step limit should have terminated the run with a clear message. Read the trace: loops are usually a tool returning an error the model does not understand, a tool that returns nothing so the model retries, or a goal the tools cannot achieve. Fix the tool result to be actionable, add loop detection (Budgets, Limits and Termination), and add the case to the eval set.
Scenario 6: execution is expensive. Signal: cost per task drifts up over days; context tokens per step grow linearly with step number; a small number of requests dominate spend. Runbook: find the top 1% of requests by cost and read their traces. Common causes: full tool results appended verbatim, conversation history never compressed, retries multiplying a large prompt, an expensive model used for a trivial sub-task. Apply token budgets and compression (Context Selection & Compression), route sub-tasks to smaller models, and set a hard cost ceiling per request.
Scenario 7: latency is too high. Signal: p95 latency well above p50; waterfall traces show serial spans that do not depend on each other. Runbook: parallelise independent tool calls (Parallel vs Sequential Tool Calls), enable prompt caching for the stable prefix, stream tokens so the user sees progress, and route easy requests to a faster model. Set per-call timeouts so one slow dependency cannot hold the whole request.
A scenario table for the on-call engineer
Keep this in the runbook so the first responder can classify in under a minute.
- Tool errors up, one tool → scenario 1 → check dependency status, confirm retries and fallback engaged.
- Validation failures up, or eval wrong-tool rate up → scenario 2 → diff the tool schemas and prompt since last deploy.
- Faithfulness down, latency normal → scenario 3 → check index freshness and query rewriting.
- All model calls failing or slow → scenario 4 → confirm fallback chain engaged; check provider status.
- Steps p99 at limit → scenario 5 → read a looping trace; find the unhelpful tool result.
- Cost per task up → scenario 6 → top 1% traces by cost; check context growth.
- Latency p95 up, error rate flat → scenario 7 → look for serial spans and cache misses.
When to use — and when not to
- Writing the on-call runbook for an agent before launch.
- Triaging a live incident or a regression in the dashboards.
- Post-mortems: map the incident to a scenario and check whether the mitigation existed.
- Do not apply every runbook at once; classify first, one scenario usually explains the incident.
- Do not fix a wrong-tool issue by adding prompt text “please use tool X”; fix the schema.
- Do not raise the step limit to “fix” a loop; find the unhelpful tool result.
Failure modes
Reference · showHide
- Tool errors are swallowed and the model sees an empty string, so it retries the same call.
- Retrieval regression after re-indexing goes unnoticed because faithfulness is not measured.
- Fallback provider engages but its prompt format differs and quality silently drops.
- Cost alert fires on a daily total, hours after a single runaway request.
- On-call has metrics but no per-scenario mapping and spends the incident guessing.
Check yourself
Answer out loud or on paper first, then compare. These come from the interview questions linked to this lesson.
Your agent sometimes loops indefinitely or runs far longer than expected. What causes this and how do you prevent it?
Show a strong answerHide the answer
Causes I see most: no clear completion criterion (the goal is vague so the model keeps "checking"); tool errors misread as progress (the same failing call retried by the model); oscillation between two states (search → wrong result → refine → same result); unbounded sub-tasks (each step spawns more work); and context growth that makes the model lose track of what it already did. See Budgets, Limits and Termination and The Agent Loop.
Prevention has two layers. Hard limits in code: max iterations sized from the loop-length distribution of successful runs (if p99 is 12 steps, cap at 20, not 100), a token and cost budget, a wall-clock timeout, and a cap on retries per tool. Structural fixes: explicit completion signals (a
finishtool with a required result schema), state tracking that detects repeated identical tool calls and injects "you already tried this", and returning structured errors so the model does not treat failures as partial success.When a limit fires, the run ends with a clear status and a partial result, and the event is counted; a rising cap-hit rate is an alert, not noise.
Full interview question, with follow-ups and red flags →Costs of your agent grew ten times faster than traffic after launch. How do you find the cause and bring it under control?
Show a strong answerHide the answer
Cost growing faster than traffic means cost per task grew, so I attribute cost from traces: tokens and dollars per run broken down by step, model, tool, and task type, then look at the distribution rather than the mean; a fat tail of long runs often accounts for most of the spend. Common drivers: context accumulating across steps (history and tool results never compressed), more steps per task on real inputs than in the demo, retries counted in tokens, and evaluation or judge calls running on every production request. See Logging, Metrics and Alerts and Token Budgets.
Controls in order of impact: per-run token and dollar budgets that stop runaway runs; compression of history and tool outputs so tokens per step stay flat; prompt caching for stable prefixes; routing simple steps (classification, extraction) to a small model and reserving the strong model for judgement steps; caching tool results and retrieval within and across runs; and removing steps that evals show add nothing. See Fallbacks, Caching and Model Routing and Context Selection & Compression.
Every cost change is validated against the golden set so quality does not silently drop.
Full interview question, with follow-ups and red flags →How do you choose the iteration cap?
Show a strong answerHide the answer
From the loop-length distribution of successful runs, with margin above p99, per task type.
Full interview question, with follow-ups and red flags →