Learn/LangChain Mastery/Part 8 · 7. Tools & Agents
Lesson 58 of 68
SyllabusLangChain Mastery · Part 8

Fallbacks & Error Handling

When the agent breaks. Retries, corrections, and graceful exits.

Course outline & contents
LangChain Mastery
Designing Tool SchemasFunction Calling in LangChainTool Selection & ValidationReAct AgentsPlan-and-Solve AgentsFallbacks & Error HandlingLangGraph: Stateful AgentsHuman-in-the-Loop

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.

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.

If SearchGoogle API times out, the agent shouldn't die. It should automatically switch to SearchBing or SearchDuckDuckGo.

tool_fallback.py
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.

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.

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."

Track your progressSign in and your progress follows you across devices.
Sign in to track progress Next lesson
← Previous lessonPlan-and-Solve AgentsNext lesson →LangGraph: Stateful Agents