Recommended resources
The freely available resources this lesson will direct you to.
- Agentic Rag: Building a coding agent (no frameworks) 🦄 ep #28www.youtube.com
- Master ALL 20 Agentic AI Design Patterns [Complete Course]www.youtube.com
- Architecting the Future: Practical Patterns for Agentic AI Applicationswww.refactored.pro
- Building Multi-Agent AI Systems: Architecture Patterns and Best ...dev.to
Introduction
In our last lesson, we focused on prevention—using constraint-based prompting and architectural guardrails to stop an agent from acting outside its defined scope. We established the boundaries of the agent's "playing field."
However, even within that field, things can go wrong. An agent might encounter a situation it wasn't explicitly trained for, a tool it relies on might fail, or it might generate a response that is syntactically correct but logically flawed. A robust agentic system doesn't just prevent failure; it anticipates and manages it gracefully.
This lesson directly addresses that challenge. Our learning outcome is to design fallback and escalation behaviors for when an agent encounters ambiguity or edge cases. We will shift our focus from "How do we stop the agent from doing the wrong thing?" to "What should the agent do when it doesn't know the right thing to do, or when its attempt fails?"
We'll explore a structured, multi-layered approach to failure handling, encompassing:
- A taxonomy of common agent failures.
- Automated fallback and self-correction patterns.
- Structured escalation paths, including agent-to-agent reviews and human-in-the-loop (HITL) interventions.
A Taxonomy of Agent Failures
Before designing solutions, it's critical to understand the types of problems an agent can encounter. Architecturally, we can group them into four main categories:
-
Execution Errors: These are failures in the environment or tools the agent uses. They are typically unrelated to the LLM's reasoning.
- Transient Errors: Temporary issues like a network timeout, a rate limit on an API call, or a database deadlock.
- Permanent Errors: An API endpoint is deprecated, a required file is permanently deleted, or a tool call is made with fundamentally invalid parameters that will never succeed.
-
Reasoning Errors: These failures originate from the LLM itself.
- Malformed Outputs: The agent produces an output that isn't syntactically valid (e.g., broken JSON) and breaks the downstream system.
- Hallucinated Actions: The agent attempts to use a tool that doesn't exist or with parameters it invented.
- Looping Behavior: The agent gets stuck in a repetitive cycle of actions without making progress, often due to ambiguous instructions or an inability to change its strategy.
-
Ambiguity and Edge Cases: The system is working, but the agent encounters a situation that requires clarification or a special procedure.
- Under-specified Inputs: A user's request is valid but too vague for the agent to proceed confidently (e.g., "Summarize the document" without specifying length or focus).
- Hitting Designed Limits: The agent encounters a predefined boundary, such as the maximum number of iterations or the character limit on a tool's output. The system needs a defined behavior for what to do next.
-
High-Stakes Decisions: The agent proposes a valid action that carries significant risk or consequence (e.g., deleting a database, authorizing a large payment, sending an email to a client list). While not a "failure," this requires a verification step before execution.
Pattern 1: Automated Fallbacks and Self-Correction
The first line of defense is to design automated responses to handle common, recoverable failures without human intervention. This maintains the system's autonomy and efficiency.
Master ALL 20 Agentic AI Design Patterns [Complete Course]
The video 'Master ALL 20 Agentic AI Design Patterns' by Mark Kashef provides an excellent overview of the 'Exception Handling and Recovery' pattern. It introduces a clear distinction between temporary and permanent errors and outlines corresponding strategies.
Watch the segment on 'Exception Handling and Recovery' from 31:16 to 34:26. Pay close attention to: The classification of errors into 'permanent' and 'temporary'. The proposed automated responses for each type (e.g., retries with backoff vs. activating a 'plan B'). The examples of backup options, such as using a simpler method or default answers.
As the video explains, these automated patterns are your first architectural tool for resilience. Let's break down the key techniques.
Retry with Exponential Backoff
For transient execution errors like network timeouts, the simplest fallback is to try again. A naive retry loop can overwhelm a struggling service, so the standard practice is exponential backoff: wait 1 second, then 2, then 4, and so on, up to a maximum number of retries. This gives the external service time to recover. Architecturally, this is often implemented within a "circuit breaker" pattern that can temporarily stop all calls to a consistently failing service.
The Self-Correction Loop
For reasoning errors, such as a malformed output or a hallucinated tool call, simply retrying is often ineffective. A more sophisticated pattern is the self-correction loop, which gives the agent a chance to fix its own mistake.
Agentic Rag: Building a coding agent (no frameworks) 🦄 ep #28
The video 'Agentic Rag: Building a coding agent' offers a practical look at how to implement self-correction. The presenter explains how their system handles invalid tool calls from the model.
Please watch these two key segments: 22:44 - 23:35: The presenter describes how their system detects when the model hallucinates a tool and feeds an error back to the model, telling it to try again. 1:01:09 - 1:04:27: This is a crucial section. Focus on how the orchestrator catches a parsing failure, constructs a specific, helpful error message, and re-invokes the model with that message. Note the critical design choice: this temporary error-handling state is not persisted in the main context window, preventing it from confusing the agent in later turns.
The self-correction pattern is powerful. From an architectural standpoint, it works like this:
- Orchestrator detects error: A non-LLM component validates the agent's output (e.g., parsing JSON, checking tool name against a list of valid tools).
- Error context is constructed: On failure, the orchestrator generates a new, temporary prompt message like:
Invalid response. You must respond with one of the following tools: [tool_A, tool_B]. - Model is re-invoked: The agent is called again with the original context plus the new error message.
- State is managed: This corrective "mini-loop" is ephemeral. Once the agent produces a valid response, the error messages are discarded, keeping the main conversation history clean.
This technique treats the LLM's mistakes as fixable bugs rather than terminal failures.
Pattern 2: Structured Escalation
When an automated fallback is insufficient or the stakes are too high, the system must escalate. A well-designed escalation path is not a panic button but a structured, multi-tiered process.

Architecting the Future: Practical Patterns for Agentic AI Applications
The article 'Architecting the Future: Practical Patterns for Agentic AI Applications' proposes a formal, tiered structure for verification that goes beyond simple human oversight.
Please read the following two sections: 'Verification Layers: Humans and Reviewer Agents': This section introduces a powerful three-level escalation model. Pay attention to the concept of a 'Reviewer Agent' as the first line of defense. 'Failure Isolation and Fallback Strategies': This reinforces the concepts of circuit breakers and defining fallback behaviors, and introduces the idea of requesting human guidance as an explicit fallback path.
The article introduces a clear escalation hierarchy:
- Level 1: Reviewer Agents: Before involving a human, one agent's work can be passed to another specialized agent for review. For example, a "Developer Agent" submits its code to a "Security Agent" that tries to find vulnerabilities. This automates a significant portion of quality control.
- Level 2: Deterministic Policies: These are the hard guardrails we discussed in the previous lesson, which use formal logic or code to prevent certain actions.
- Level 3: Human-in-the-Loop (HITL): This is the final and most critical escalation point, reserved for high-stakes decisions, persistent ambiguity, or repeated failures of automated systems.
Architecting Human-in-the-Loop
Implementing HITL effectively requires more than just sending an email. The agent's workflow must be designed to pause, wait for external input, and then resume. As a computer scientist, you'll recognize that the most robust way to manage this is with a state machine.
Building Multi-Agent AI Systems: Architecture Patterns and Best Practices
The 'Building Multi-Agent AI Systems' article explains how state machine architectures, like those used by frameworks such as LangGraph, are the key to building reliable agentic workflows with human approval gates.
Please read the sections 'State Machines: The Secret to Reliable Agents' and the paragraph on human-in-the-loop within it. Focus on how modeling the workflow as a graph with explicit states and transitions allows you to insert a 'human approval' node that pauses execution until external validation is received.
By defining the workflow as a directed graph, you can create a specific node (a state) that represents AWAITING_HUMAN_APPROVAL. The system can transition to this state, pause execution indefinitely, and only resume once an external signal (from a human via a UI) is received. This makes the escalation process predictable, observable, and testable—a stark contrast to an unstructured agent loop.
A Decision Framework for Failure Handling
When designing an agent, you can use the following mental model to decide which pattern to apply:
-
Identify Potential Failure Points: For each step in your agent's workflow (e.g., calling a tool, generating a response), ask: "How can this go wrong?" Use the taxonomy of failures to guide you.
-
Apply Automated Fallbacks First:
- For a transient execution error (e.g., API timeout): Implement retries with exponential backoff.
- For a model reasoning error (e.g., malformed JSON): Implement a self-correction loop that provides specific feedback.
- For a permanent execution error (e.g., API deprecated): Design a fallback to an alternative path (e.g., use a different tool, return a default value).
-
Define Escalation Triggers:
- For a high-stakes decision: Always trigger an escalation.
- For persistent ambiguity or repeated automated failures (e.g., 3 retries failed): Trigger an escalation.
- For an edge case not covered by fallbacks: Trigger an escalation.
-
Design the Escalation Path:
- Can the check be automated by another agent? If so, use a Reviewer Agent as the first level of escalation.
- If not, or if the Reviewer Agent also fails, escalate to a human-in-the-loop approval gate, architected as a state in a state machine.
Conclusion
Robustness in agentic systems is not an emergent property; it is an explicitly designed feature. By anticipating failures and creating clear, structured responses, you can build systems that are not only powerful but also reliable and trustworthy.
Key Takeaways:
- Design for Failure: Assume that tools will fail, and models will make mistakes. Build your architecture to handle this from day one.
- Use a Tiered Approach: Start with automated fallbacks for simple, recoverable issues and reserve escalation for complex or high-risk scenarios.
- Automated Fallbacks are Key to Autonomy: Patterns like retries with backoff and self-correction loops allow the agent to resolve many issues without human intervention.
- State Machines Enable Reliable Escalation: Modeling your workflow as a state machine provides the architectural foundation for adding robust human-in-the-loop approval gates that are predictable and testable.
Preview of the Next Lesson
We've seen how self-correction loops are needed when an agent produces malformed output. A primary way to minimize these errors is to force the agent's output into a specific, guaranteed format from the outset. In our next lesson, we will do a deep dive into how to apply structured output formatting (e.g., JSON schemas) to enforce consistent agent responses, ensuring they are always machine-readable and ready for downstream processing.