Skip to main content
Botmartz AI Solution PVT LTD.
AGENTS

Error Handling and Agent Recovery: Graceful Degradation, Retries, and Fallback Strategies

Agents fail: tools time out, LLMs hallucinate, APIs are down. Implement graceful degradation, retry logic, and fallback strategies to keep agents robust.

SS
Soham Sharma
AI Engineer, Botmartz · July 17, 2026 · 1 min read
Read Time
1 min
Failure Modes
5
Code Snippets
3
Runnable Notebook
1
Error Handling and Agent Recovery: Graceful Degradation, Retries, and Fallback Strategies

Production agents must handle failures: tool timeouts, API errors, LLM inconsistencies. Implement retries with exponential backoff, fallback tools, and graceful degradation to maintain service quality.

Retry with Exponential Backoff

Code Snippet
import time
from functools import wraps

def retry_with_backoff(max_retries=3, base_delay=1):
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            for attempt in range(max_retries):
                try:
                    return func(*args, **kwargs)
                except Exception as e:
                    if attempt == max_retries - 1:
                        raise
                    delay = base_delay * (2 ** attempt)
                    print(f"Attempt {attempt+1} failed. Retrying in {delay}s...")
                    time.sleep(delay)
        return wrapper
    return decorator

@retry_with_backoff(max_retries=3)
def call_external_api():
    # API call that might fail
    pass

Fallback Tools

Code Snippet
# Primary tool
search_tool = StructuredTool.from_function(func=search_api)

# Fallback: cached results
fallback_search = StructuredTool.from_function(func=search_cache)

def robust_search(query):
    try:
        return search_tool.invoke({"query": query})
    except Exception as e:
        print(f"Search API failed: {e}. Falling back to cache.")
        return fallback_search.invoke({"query": query})

Graceful Degradation

Code Snippet
# Try detailed information
try:
    result = get_detailed_weather(city)
except:
    # Fallback to basic info
    result = get_basic_weather(city)
    return result + " (Note: detailed forecast unavailable)"

Conclusion

Error handling separates fragile demos from production systems. Retries, fallbacks, and graceful degradation keep agents running when components fail. Understanding failure modes and mitigation strategies is essential. Next: deploying agents at scale with proper monitoring and observability.

Closing Takeaways

✓
Measure retrieval precision and recall in isolation before touching the model.
✓
Chunk along document structure, not arbitrary character counts.
✓
Combine vector and keyword search — hybrid retrieval beats either alone.
✓
Treat evaluation as continuous infrastructure, not a launch-week report.
Try It Yourself
A runnable Google Colab notebook with the eval harness and hybrid search code from this post.
#Agents#Error Handling#Resilience#Production
0 views
SS
Soham Sharma
AI Engineer at Botmartz, building enterprise RAG and agent systems in production. Contributing to open-source libraries.

Discussion (0)

No approved comments yet. Be the first to share your thoughts!

Leave a Comment

Your email address will not be published. Required fields are marked *

More Engineering Insights
AI AGENTS
9 min read
AI AGENTS
11 min read
OPEN SOURCE
6 min read
AI ENGINEERING
8 min read
CASE STUDIES
14 min read