Error Handling and Retries in LangGraph
Explore how to implement error handling and retry mechanisms in LangGraph to build resilient AI assistants. Understand managing flaky tools, applying retry policies, and providing fallback responses that keep workflows running smoothly even when some tools fail. This lesson helps you design systems that recover gracefully from errors and maintain context in complex multi-tool AI applications.
Our assistant can now call tools in sequence or in parallel, weaving answers together like a skilled host managing a lively dinner party. But we have quietly assumed up to this point that every tool always works perfectly.
In the real world, this is rarely the case. APIs fail. Tools throw exceptions. Sometimes the guest you invited to the party forgets their notes or spills soup before answering the question. The host needs to handle these hiccups gracefully and not let the whole evening collapse. This is where error handling and retries come into play.
For example, imagine asking for the weather in Paris and London. The Paris forecaster answers promptly, but the London forecaster trips up. Perhaps their microphone breaks, or they just shrug. The host should not stop the conversation altogether. Instead, they might ask the London forecaster to try again. If the forecaster still cannot answer, the host should at least summarize what Paris said so the guest leaves with something useful.
This is what error handling looks like in an assistant: recover when things go wrong, retry when appropriate, and give partial but graceful answers if some tools fail.
How to implement error handling with tools
To illustrate resilience, we need something that occasionally breaks. Let’s adjust our weather tool so that London’s forecast fails randomly more than half the time.
import randomdef get_weather(city: str) -> str:"""Return a mock weather string for the given city, sometimes failing."""if city.lower() == "london" and random.random() < 0.75:raise Exception("Weather service for London failed!")return f"The weather in {city} is sunny."
We introduce a flaky tool here: for London only, we flip a biased coin at line 5 and raise an exception 75 percent of the time. Paris remains reliable. This asymmetry is intentional; it lets learners see partial success when the graph joins results. The short docstring at line 4 documents the behavior and keeps your tool self-describing.
Because this failure is purely inside a function, nothing about LangGraph’s wiring ...