Day 5
Agent Failure Modes
Identify, debug, and prevent common agent failures.
Agents fail in ways traditional software does not. They hallucinate tool arguments, get stuck in loops, ignore instructions, and produce plausible-sounding wrong answers. Understanding these failure modes is essential for building reliable agents and for debugging interview problems.
Today’s Lesson
Read
Agent Loop
Bounded ReAct and reflection loops that stop cleanly.
Practice
Apply what you learned by solving these coding problems.
Review
Test yourself with these interview-style questions.
1Describe two detection techniques for identifying when an agent is stuck in a loop. For each, explain what signal it captures, how you would implement the detection, and how you would respond when the loop is confirmed.+
First, exact-action repetition: compute a signature of (action_name, sorted arguments) for each step. If the same signature appears 3+ times within a sliding window of recent steps, the agent is looping on the same action. Response: inject an observation warning the LLM to try a different approach. Second, semantic-thought similarity: compute Jaccard similarity between consecutive thought texts. If thoughts are highly similar for 3+ consecutive steps, the agent is in a reasoning rut. Response: same warning. If the loop persists for 3 more steps after the warning, force a Final Answer with whatever partial result exists.
Read full answer
I combine two complementary detection signals to catch loops without false positives. The first signal is exact action repetition: compute a signature of (action_name, sorted(args.items())) for each step and track consecutive repetitions within a sliding window. If the same signature appears 3+ times, that indicates an action-level loop — the agent keeps calling the same tool with the same arguments. This is common when a tool returns insufficient information and the agent tries the exact same call expecting a different result. The second signal is semantic thought similarity: split consecutive thought texts into word sets and compute Jaccard similarity (intersection over union). If similarity exceeds a threshold (typically 0.7-0.8) for 3+ consecutive steps, the agent is in a reasoning rut — thinking the same thing without progress. I combine these with OR logic because they catch different failure modes. Recovery follows a two-stage escalation: first, inject a gentle observation like "You have called the same tool 3 times. Consider a different approach or produce a Final Answer." If the pattern continues for 3 more steps, escalate to a forced Final Answer with whatever partial result exists. This prevents infinite token burn while preserving any work done before the loop started.
2Review this trace: Step 1: search_web("latest Python version") -> "Python 3.13". Step 2: search_web("latest Python version release date") -> "October 2024". Step 3: search_web("Python 3.13 new features") -> "JIT compiler, improved errors". Step 4: search_web("latest Python version"). The agent is stuck. Identify the failure mode and propose three mitigations ordered by invasiveness.+
The agent is in a confirmation loop — it keeps searching for progressively narrower confirmations instead of synthesising an answer. This is stuck-reasoning failure from lack of confidence. Least invasive: add a system prompt instruction to stop gathering info after finding the direct answer. More invasive: after 3 tools calls on the same topic, force a synthesise step. Most invasive: semantic deduplication — compute embedding similarity between consecutive queries; if >0.85 for 3 steps, force Final Answer.
Read full answer
This is stuck-reasoning where the agent lacks a stopping criterion. It found the answer in step 1 but continues seeking confirmations. The root cause is the system prompt did not specify when to stop gathering and produce a Final Answer. Three mitigations ordered by invasiveness: first, a prompt-level fix — "Once you have found information directly answering the question, produce a Final Answer. Do not search for additional confirmations." This costs nothing. Second, a programmatic guard — after 3 tool calls on the same topic, override the LLM and inject "You have sufficient information. Produce a Final Answer." Third, semantic deduplication — use an embedding model to compute similarity between consecutive queries. If similarity exceeds 0.85 for 3 steps, force termination. Start with the prompt fix and escalate if the problem persists.
3Design a "failure-aware agent loop" that classifies failures and adapts. Cover four categories: tool hallucination, context loss, stuck reasoning, and instruction ignoring. For each, specify detection method and adaptive response.+
Tool hallucination: detect when action name is not in the registry — inject observation listing available tools. Context loss: detect when LLM asks for info already in history (via embedding similarity) — inject reminder with relevant past result. Stuck reasoning: detect repeated actions — warn, then force synthesise. Instruction ignoring: detect structural output violations — re-prompt with stricter examples. The loop escalates responses per failure category: from gentle hint to firm override after repeated occurrences.
Read full answer
The failure-aware loop has detection, classification, response selection, and adaptation layers. Tool hallucination detection checks action name against the registry; if absent, inject observation listing all tools with brief descriptions. Context loss detection uses embedding similarity between the current query and all past observations — if a match exists above threshold, inject a reminder with the relevant past result. Stuck reasoning uses the sliding-window repetition detector from my earlier answer, escalating from warning to forced-synthesise. Instruction ignoring is detected by structural validation against expected output format — on failure, re-prompt with few-shot example. The adaptation layer maintains a counter per failure category per session. If the same failure occurs twice, the response escalates one level — from gentle re-prompt to firmer instruction to forced override. The failure log is included in the final session summary for offline analysis. This ensures the loop is not endlessly forgiving of the same mistake.
Back to 30-Day Agentic AI Interview Prep Path