Error Handling and Recovery Policies for LLM Agents

Error Handling and Recovery Policies for LLM Agents

You built an agent. It thinks, it plans, it uses tools. Then it breaks. Not with a clean stack trace, but with a silent hallucination or a half-written file that corrupts your database. Traditional software fails loudly; LLM agents fail quietly and destructively. If you are deploying Large Language Model (LLM) agents in production, standard try-catch blocks won't save you. You need a structured approach to error handling and recovery that accounts for the nondeterministic nature of language models.

Why does this matter? Because agents don't just return text; they execute actions. A wrong API call can delete data. A bad plan can waste hours of compute. The difference between a toy demo and a reliable system is how gracefully it handles failure. Let's look at how to build resilience into your agentic workflows using proven patterns like the SHIELDA framework, schema validation, and checkpointing.

The Three Phases of Agent Failure

Most developers treat agent errors as a single bucket: "it didn't work." But failures happen in distinct phases, each requiring different fixes. Research from the SHIELDA (Structured HandlIng of Exceptions in LLM-Driven Agentic Workflows) framework identifies 36 exception types across three critical operational stages. Understanding where the break happens is half the battle.

  • Reasoning Phase: This is where the agent formulates its logic. Errors here include ambiguous user intent, vague instructions, or logical contradictions. For example, if a user asks to "fix the code," the agent might misinterpret which part needs fixing. These aren't crashes; they're misunderstandings.
  • Planning Phase: Here, the agent structures multi-step workflows. Failures include inefficient paths, circular dependencies, or plans that ignore constraints. An agent might plan to deploy code before testing it, creating a bottleneck that stalls execution.
  • Execution Phase: The agent interacts with external systems-APIs, databases, file systems. Errors here are tangible: API timeouts, invalid JSON responses, permission denied, or state mismatches. This is where traditional software errors live, but amplified by the agent's autonomy.

If you only handle execution errors, you'll miss the subtle reasoning failures that cause most long-term drift in agent behavior. Your recovery policy must be phase-aware. A reasoning error needs clarification; an execution error needs a retry or rollback.

Schema Validation: The First Line of Defense

Never trust raw LLM output. Always validate it against a strict schema before acting. This is the "Validation Over Trust" principle. If your agent outputs JSON for an API call, that JSON must match a predefined structure exactly. Libraries like Pydantic in Python are perfect for this.

When validation fails, don't just throw an error. Route the output to a sanitizer. This could be a secondary LLM call with a specific prompt: "Fix this JSON to match Schema X." Or it could be a deterministic parser that attempts minor repairs, like adding missing quotes. If sanitation fails, trigger a regeneration with a modified prompt template. This multi-template fallback strategy treats prompt variation as a first-order recovery mechanism. Instead of retrying the same broken prompt, you switch to a stricter, more constrained version.

Common Validation Failures and Immediate Fixes
Failure Type Symptom Immediate Fix
Format Error Invalid JSON/XML syntax Regex repair or re-prompt with format examples
Type Mismatch String instead of Integer Casting attempt or re-generation
Missing Field Required key absent Default value injection or clarification query
Hallucinated Value ID does not exist in DB Database lookup verification before action

Checkpointing and State Rewinding

Agents often run multi-step workflows. If step 10 fails, restarting from step 1 is expensive and slow. Worse, side effects from steps 1-9 might already be committed. You need checkpoints. Save the agent's state after every successful step. When a failure occurs, rewind to the last valid checkpoint rather than starting over.

This requires storing side effects in reversible formats. For file operations, use git stashes or temporary files. For API calls, log the request and response so you can verify state changes. Consider this scenario: your agent writes a config file to `/tmp/config.yaml`. After writing, it queries the filesystem to confirm existence. If the file is missing, the agent triggers a re-plan with the error reason `config_file_missing`. This post-execution validation catches silent failures that APIs often hide.

Checkpoints also enable debugging. You can replay the agent's state from any point to see exactly where the logic diverged. Without this, debugging non-deterministic agents feels like chasing ghosts.

A recovery agent shielding against data errors while rewinding a workflow timeline.

Graceful Degradation: The Chain of Responsibility

Not all errors should be handled by the main agent. Use a hierarchical recovery model. Think of it as a chain of responsibility with decreasing complexity:

  1. Primary Agent: Full reasoning and planning capabilities. It tries to fix the issue itself using context.
  2. Recovery Agent: Simpler logic. Uses templates for common failures. For example, if an API times out, it retries with exponential backoff. If a tool returns unexpected data, it formats it using a fixed script.
  3. Emergency Fallback: Rule-based logic or human escalation. If both previous layers fail, stop the workflow and alert a human. Do not let the agent guess blindly.

This structure prevents infinite loops. The primary agent shouldn't keep trying to solve a fundamental ambiguity forever. After two failed attempts, hand off to the recovery agent. If that fails, escalate. This ensures bounded resource usage and predictable behavior.

Handling Memory Corruption in Long-Term Agents

Agents with memory face unique risks. They might store incorrect facts, contradictory information, or outdated preferences. The SHIELDA framework categorizes these as memory-level exceptions. Strategies include:

  • Write Control: Validate new memories before storing them. Don't let hallucinations enter the long-term store.
  • Noise Reduction: Summarize noisy histories. If an agent has logged 50 similar interactions, compress them into a single insight.
  • Temporal Anchoring: Tag memories with timestamps. If a fact becomes obsolete, decay its utility score. Prevent old, contradicted facts from resurfacing.

For ambiguous user intent, proactive clarification is better than guessing. If a user says "make it faster," ask "Do you mean latency or throughput?" Turn-based goal alignment refines the objective iteratively. Subgoal decomposition breaks vague goals into specific, testable tasks.

A hierarchical structure of primary, recovery, and human agents managing errors.

Human-in-the-Loop for High-Stakes Actions

Complete automation is risky in critical domains like finance or infrastructure. Implement human review gates for destructive operations. Before deleting a database or pushing code to production, show the agent's plan to a human. Provide preview diffs, visualizations, and logs. Allow manual overrides.

Capture human corrections. If a user edits the agent's output, feed that back into the system. Update prompts or fine-tune models based on these interventions. Human judgment remains essential for novel failure scenarios that automated rules can't anticipate.

FAQ

What is the SHIELDA framework?

SHIELDA stands for Structured HandlIng of Exceptions in LLM-Driven Agentic Workflows. It is a research-backed framework that categorizes 36 distinct exception types across 12 agent artifacts, focusing on phase-aware handling strategies for reasoning, planning, and execution errors.

Why is schema validation critical for LLM agents?

LLMs produce nondeterministic text. Schema validation ensures that outputs conform to expected structures (like JSON or function parameters) before being used in downstream systems. This prevents type errors and cascade failures caused by malformed data.

How do I handle transient API failures in agents?

Use intelligent retry logic with exponential backoff. Distinguish between transient errors (timeouts, rate limits) worth retrying and permanent errors (bad requests, auth failures) that require immediate escalation or correction. Frameworks like LangChain and AutoGen offer built-in support for this.

What is checkpointing in agentic workflows?

Checkpointing involves saving the agent's state after each successful step. If a later step fails, the agent rewinds to the last valid checkpoint instead of restarting from scratch. This saves time and resources while preserving determinism.

When should I involve a human in agent error recovery?

Involve humans for high-stakes, irreversible actions (like data deletion or financial transactions) or when automated recovery fails after multiple attempts. Human review provides safety and allows the system to learn from novel edge cases.

5 Comments

  • Image placeholder

    Elizabeth Bailey

    November 12, 2026 AT 15:22

    Wait, who actually gave you permission to talk about my workflow like this? I feel like you just walked into my living room and rearranged the furniture without asking. You say "standard try-catch blocks won't save you" but have you considered that maybe we just need better humans? Because honestly, this whole thing feels a bit presumptuous.

  • Image placeholder

    Kendra Edmonds

    November 12, 2026 AT 22:07

    This is all wrong.

    You are overcomplicating things. LLMs are just fancy autocomplete tools. We don't need some new framework called SHIELDA. We just need to write better prompts. If your agent breaks it is because you wrote a bad prompt not because of some complex phase theory. Everyone is trying to sell you a solution for a problem that doesn't exist. Just use regex and be done with it. Simple works best. Stop making up words to sound smart.

  • Image placeholder

    Alice Clark

    November 12, 2026 AT 23:08

    the real issue is control

    they want us to believe agents fail quietly so they can build more layers of oversight that watch us watching the machines its a loop of paranoia disguised as engineering

    who validates the validator? if the schema is wrong does the agent hallucinate the fix or do we blame the json parser for having feelings? its all smoke mirrors and big tech wants you to pay for the mirror cleaner

  • Image placeholder

    Kevin Ching

    November 14, 2026 AT 17:17

    Hey everyone! Great points here. I think the key takeaway is that resilience is totally achievable if we just stay positive and keep iterating. The checkpointing idea is super helpful because it means we never really start from zero again. It's like having a safety net while learning to walk. We can definitely make these systems reliable if we just care enough and put in the effort. Let's keep building!

  • Image placeholder

    adam pohan

    November 14, 2026 AT 17:26

    I find it deeply troubling that we are normalizing the idea that software should be allowed to lie to us without consequence because it is too expensive to debug properly which is essentially what this article suggests by introducing complex recovery policies instead of demanding deterministic behavior from our foundational models in the first place.

    We have a moral obligation to ensure that technology serves humanity reliably and not the other way around where we spend all our energy patching holes in a leaky boat rather than fixing the hull itself which seems to be the prevailing attitude in modern development circles today.

    If an agent deletes data silently then the engineer responsible for deploying that agent has failed in their duty to the user and no amount of schema validation excuses that fundamental lack of accountability in the design process.

Write a comment