Fallbacks & Error Handling
When the agent breaks. Retries, corrections, and graceful exits.
1. Agents Are Fragile
Chains are deterministic logic. Agents are probabilistic logic. Probabilities fail.
- Tool Error: API is down.
- Parse Error: Agent output malformed JSON.
- Loop Error: Agent decides to search for "A" forever.
If you don't build Fallbacks, your agent is a prototype, not a product.
2. Validation Errors (The Self-Correction)
We discussed this in Tool Selection.
If delete_user fails, feed the error back.
This converts a Crash into an Observation.
Agent: "Action: Delete(User 5)" System: "Error: User 5 not found." Agent: "Thought: Ah, I made a mistake. I will search for the user first."
This is the cheapest form of resilience.
3. Tool Fallbacks (The Backup Plan)
If SearchGoogle API times out, the agent shouldn't die.
It should automatically switch to SearchBing or SearchDuckDuckGo.
primary_tool = SearchGoogle()
backup_tool = SearchBing()
final_tool = primary_tool.with_fallbacks([backup_tool])
# If primary raises Exception, backup runs instantly.
# The Agent doesn't even know it failed.4. LLM Fallbacks (Model Degradation)
If GPT-4 is overloaded (Rate Limit Error), automatically fallback to GPT-3.5 or Claude. The answer might be dumber, but a dumb answer is better than a 500 Server Error.
5. Summary
Elegance is handling failure without the user noticing. Your agent should be like a cat: It always lands on its feet.
Key Intuition: "The plan A never survives contact with reality."