Exception handling & recovery
Real-world AI systems encounter errors constantly: API timeouts, invalid responses, rate limits. The exception handling pattern builds resilience with fallback strategies, exponential backoff retries, and graceful degradation so your agent doesn't crash at the first hiccup.
Fallback chain
Try primary, fall back to secondary, then generate error response.
Instead of crashing when the primary LLM is down, your agent falls back to a simpler model, a cached response, or a helpful error message. The user still gets a reasonable experience even when things go wrong.
class ExceptionHandler:
def __init__(self, max_retries=3):
self.max_retries = max_retries
self.llm = get_llm()
def handle_with_fallback(self, primary_op, fallback_op, *args):
"""Try primary, fall back to secondary."""
try:
return primary_op(*args)
except Exception as e:
try:
return fallback_op(*args)
except Exception as fallback_err:
return self._generate_error_response(
str(e), str(fallback_err)
)
def handle_with_retry(self, operation, *args):
"""Retry with exponential backoff."""
for attempt in range(self.max_retries):
try:
return operation(*args)
except Exception as e:
wait_time = 2 ** attempt # 1s, 2s, 4s
time.sleep(wait_time)
return self._generate_error_response("Max retries exceeded")
def _generate_error_response(self, *errors):
prompt = f"Generate a helpful error message for: {errors}"
return self.llm.generate(prompt).contentFallback chains and exponential backoff retry logic.
Quiz: Quiz
Loading practice…
Ordering exercise: Order the error recovery strategy
Loading practice…
Flashcards: Flashcards
Loading practice…
Exception handling turns fragile prototypes into production-ready systems. Your agents can now recover gracefully from API failures, model errors, and unexpected inputs. Next up: human-in-the-loop, where we add human oversight for the decisions that matter most.