Fallback Actions
A fallback is an alternative action the agent takes when the primary approach fails. Good fallbacks achieve the same goal through a different means.
8 min•By Priygop Team•Updated 2026
Fallback Chain Pattern
Fallback Chain Pattern
# Fallback chain: try multiple approaches in order
class FallbackChain:
"""
Execute a series of fallback strategies in order.
Each strategy is tried only if the previous one fails.
"""
def __init__(self, strategies: list):
"""
strategies: list of (name, function) tuples, in priority order
"""
self.strategies = strategies
def execute(self, **kwargs) -> dict:
errors = []
for name, strategy_fn in self.strategies:
print(f" Trying strategy: {name}")
try:
result = strategy_fn(**kwargs)
if isinstance(result, dict) and result.get("status") == "error":
print(f" ✗ {name} failed: {result.get('error', 'unknown')}")
errors.append({"strategy": name, "error": result.get("error")})
continue
print(f" ✓ {name} succeeded")
return {**result, "_strategy_used": name}
except Exception as e:
print(f" ✗ {name} raised: {e}")
errors.append({"strategy": name, "error": str(e)})
continue
# All strategies exhausted
return {
"status": "error",
"error_type": "all_strategies_failed",
"error": "All fallback strategies failed",
"attempts": errors
}
# Define search strategies in fallback order
def live_web_search(query: str) -> dict:
# Simulating a failure
return {"status": "error", "error_type": "network", "error": "Network unavailable"}
def cached_search(query: str) -> dict:
# Simulating a cache miss
return {"status": "error", "error_type": "not_found", "error": "Not in cache"}
def internal_knowledge_base(query: str) -> dict:
# Simulating success from internal KB
return {"status": "success", "source": "internal_kb",
"results": [f"KB result for: {query}"]}
search_chain = FallbackChain([
("live_web_search", live_web_search),
("cached_search", cached_search),
("internal_knowledge_base", internal_knowledge_base),
])
result = search_chain.execute(query="AI agent frameworks")
print(f"\nFinal result: {result['status']} via {result.get('_strategy_used', 'N/A')}")