Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Handling Failures: Fallback Strategies for Sub-agents and Tools

Introduction

In our last lesson, we built the first line of defense for your agentic system by implementing robust retry logic with exponential backoff. This strategy is essential for handling the transient, temporary failures that are common in distributed systems. You now have a mechanism to automatically recover from network hiccups or temporary service unavailability, making your agent significantly more resilient.

However, what happens when a failure isn't transient? What if a tool's API is down for an extended period, a sub-agent is consistently unable to perform its task, or the LLM generates invalid inputs that even retries can't fix? This lesson moves to the second layer of resilience: designing and implementing fallback strategies. We will explore how to make your system degrade gracefully, maintaining partial functionality and service continuity rather than failing completely. This is a critical step in transforming your architecture into a truly production-grade service.

The Nature of Agentic Failure

Before designing fallbacks, it's crucial to understand that failures in agentic systems are different from those in traditional software. A standard microservice might time out or crash, which are binary, easily detectable states. An agent, however, can fail in more subtle, "cognitive" ways.

Agent Failure & Recovery: Key Redundancy Patterns - Auxiliobits

This article from Auxiliobits, "Agent Failure & Recovery: Key Redundancy Patterns," provides an excellent conceptual framework for this topic. It differentiates between predictable infrastructure failures and the more complex failures of autonomous agents.

Please read the first two sections, "The Nature of Agentic Failure" and "Why Traditional Redundancy Doesn’t Suffice". Focus on the idea that agents can "diverge" or fail due to misinterpretation, not just mechanical errors. This context is key to understanding why our fallback strategies need to be more sophisticated than simple server failover.

As the article highlights, agentic failures can stem from misinterpretation, hallucination, or flawed reasoning. Our fallback strategies must account for both these cognitive failures and more conventional technical failures, such as a tool's API being offline.

Strategies for Tool Failure

When an agent's tool fails, we have several options for recovery. The Vercel AI SDK provides built-in mechanisms that facilitate these strategies.

Detection: Identifying Tool Errors

First, you need to detect the failure. The AI SDK offers clear ways to identify when a tool's execute function throws an error.

AI SDK Core: Tool Calling

The official Vercel AI SDK documentation on "Tool Calling" explains how tool-related errors are surfaced.

Please review the section on "Handling Errors". Pay close attention to two key aspects: try/catch blocks: How errors like NoSuchToolError and InvalidToolInputError are thrown directly by generateText. tool-error parts: How execution errors from your tool's code are added to the steps result object. This is a crucial mechanism for our fallback strategies.

With this knowledge, we can design fallbacks that react to these specific error types.

Strategy 1: Let the Model Self-Correct

The simplest fallback is to inform the model that its tool call failed and let it decide what to do next. The Vercel AI SDK handles this automatically in multi-step calls. When your tool's execute function throws an error, the SDK catches it and adds a tool-error part to the conversation history. On the next generation step, the model sees its previous tool call, the inputs it provided, and the resulting error.

A well-instructed model can often self-correct by:

  • Calling the same tool with corrected parameters.
  • Choosing an alternative tool to accomplish the goal.
  • Informing the user that it cannot complete the specific request.

This is the default behavior and often a surprisingly effective first line of defense.

Strategy 2: Programmatic Tool Repair

Sometimes, you may want more direct control over fixing a failed tool call, especially for common InvalidToolInputError cases. The AI SDK provides an experimental experimental_repairToolCall function for this purpose. This function allows you to intercept a failed tool call and attempt to fix the inputs before retrying.

AI SDK Core: Tool Calling

This section of the AI SDK documentation details the advanced repair feature.

Read the section on "Tool Call Repair" and study the code example for the structured output repair strategy. This shows how you can use a secondary LLM call, perhaps to a more powerful model, specifically to fix malformed JSON or invalid arguments.

This pattern is a powerful way to add a "corrective layer" to your agent's tool usage, improving reliability without cluttering the main agent logic.

Strategy 3: The Circuit Breaker Pattern

If a tool fails repeatedly, continuing to call it is wasteful and can delay the overall task. This is where the Circuit Breaker pattern becomes invaluable. As a seasoned developer, you're likely familiar with this from distributed systems. We can apply the same logic here.

A state diagram of the Circuit Breaker pattern. The system transitions from a `Closed` state (operations are allowed) to an `Open` state upon failure. After a timeout `T`, it enters a `Half-Open` state to test for recovery `ρ`. Success returns it to `Closed`, while another failure `θ/W` keeps it `Open`.
  1. Closed: The tool is assumed to be healthy. Calls are executed normally.
  2. Open: After a certain number of consecutive failures, the circuit "trips" and becomes Open. For a set duration, all calls to this tool are immediately rejected without attempting to execute them. This prevents the system from wasting time and resources on a known-failing component. Instead, the system can immediately invoke a fallback.
  3. Half-Open: After a timeout, the circuit moves to Half-Open. It allows a single "probe" request to go through. If it succeeds, the circuit returns to Closed. If it fails, it trips back to Open, restarting the timeout.

You can implement this pattern by maintaining the state of each tool (e.g., in-memory with Redis or a similar cache) and checking its state before attempting to execute a tool call.

Strategies for Agent Failure and System Durability

Handling a failing sub-agent or a complex, multi-step workflow requires a more robust approach to state management and execution. This is where workflow orchestration frameworks become powerful.

Vercel's Workflow DevKit is designed specifically for building durable and reliable agentic systems. It allows you to define your agent's logic as a workflow composed of discrete steps.

Building durable Agents with Workflow DevKit & AI SDK - Peter Wielander, Vercel

This talk, "Building durable Agents with Workflow DevKit & AI SDK," introduces a powerful pattern for creating resilient agentic workflows.

Please watch the following segments to understand how this framework helps implement fallback strategies: The Problem: The presenter outlines the challenges of building reliable, long-running agents in a serverless environment, motivating the need for a workflow pattern. Defining Steps: See how useStep is used to mark a function (like a tool call) as an individual, resilient unit of work. The framework automatically handles caching, retries, and isolated execution for these steps. Timeouts and Retries: This part directly addresses how the framework automatically retries steps that fail or time out, which links back to our previous lesson but shows a framework-level implementation. Custom Error Logic: This is the key part for fallbacks. The presenter explains that you can catch an error within a step and implement custom logic, such as signaling a human or "trying another avenue," instead of just retrying.

Using a workflow engine like this provides a structured way to handle failures. If a step (e.g., a sub-agent's task) fails permanently after all retries, the catch block in your workflow code is the perfect place to trigger a fallback:

  • Hierarchical Escalation: The workflow can return an error to the parent agent (the supervisor), which can then decide to delegate the task to a different sub-agent.
  • Human in the Loop: The workflow can pause and wait for human intervention. The awaitWebhook feature shown in the same video (LINK at 00:37:23) is a perfect example of this, where the agent suspends execution until a human provides input via a unique URL.
  • Graceful Degradation: The workflow could proceed with a partial result or use a default value, logging that the primary task could not be completed.

A Model for Graceful Degradation

We can visualize these fallback strategies as a state transition model for our services. A component isn't just "up" or "down"; it can be in a Degraded state where it relies on fallbacks to provide continued, if limited, service.

A state transition diagram showing how a service moves from a `Healthy` state to `Degraded` and `Failed`. In a `Healthy` state, there's a high probability of success (0.9). If it fails, it can transition to a `Degraded` state, which has a lower probability of success (0.7) but still functions. Further failures lead to a `Failed` state. Each state can invoke a different `Fallback` mechanism.

This diagram illustrates the core idea:

  • Healthy: The primary agent/tool is working as expected.
  • Degraded: The primary has failed. The system switches to a Fallback—this could be a simpler model, a cheaper API, a cached response, or a different agent. The service continues, but perhaps with lower quality or capability.
  • Failed: The fallback mechanism has also failed. The system can now invoke a final Fallback, which might be to log the error comprehensively and request human intervention.

Your orchestrator agent, which we will build in a later module, is the ideal place to manage these state transitions, routing tasks to fallback agents or tools based on the health status of the primary components.

Conclusion

You have now moved beyond simple retries and designed a comprehensive, multi-layered approach to resilience. By treating failures not as endpoints but as signals for adaptation, you can build an agentic system that is robust, reliable, and capable of degrading gracefully under pressure.

Key Takeaways:

  • Agentic failures are complex, involving both technical and "cognitive" errors that require sophisticated handling.
  • For tool failures, you can leverage the AI SDK to let the model self-correct, programmatically repair inputs, or use a Circuit Breaker to isolate failing components.
  • For agent and workflow failures, using a durability framework like Vercel's Workflow DevKit allows you to define resilient steps with built-in retries and custom fallback logic within catch blocks.
  • Key fallback patterns include hierarchical escalation (reporting up to a supervisor), switching to simpler backup agents/tools, and initiating a human-in-the-loop process.
  • Graceful degradation is the goal: maintain service continuity with reduced functionality rather than failing completely.

In our next lesson, we will address another critical aspect of resilience: implementing timeout handling for long-running agent tasks. This will protect your system from tasks that get stuck or run indefinitely, ensuring that your agentic infrastructure remains responsive and doesn't exhaust its resources.

Can't find a good explanation? Sign up and we'll make it for you

Sign up