#25 AI agents and MCP 5.7 min
Tool Errors: Retries, Fallbacks and Made-Up Answers
My AI tool failed, and the model made up the answer. Here is how to retry and fall back inside the tool.
Run the four cases
Copy the code folder first. Run python demo.py. This needs no model. Check that the first case recovers on the third service call after two waits, the always down case returns the cached Leeds answer, the York case says nothing cached, and the Atlantis case makes 1 call with no waits. Write one sentence on why Atlantis is not retried.
A hint
Open service.py and see which kind of error an unknown city raises.
Fail three times instead of two
In demo.py change the first case from Weather(failures=2) to Weather(failures=3), but leave attempts=3 in the lookup call. Predict the answer and the number of service calls, then run it. When we ran it, the result was the cached Leeds answer, marked cached, 3 h old, after 3 service calls. The printed label still says fails twice, because it is fixed text.
A hint
With 3 attempts the last try also fails, so the retry function gives up and the tool falls back to the cache.
Remove the randomness
In demo.py change rng=random.Random(1).random to rng=lambda: 1.0 so the wait is always the full ceiling. Predict the two waits for the first case, then run python demo.py. We saw waits of [0.5, 1.0].
A hint
The ceiling starts at base, which is 0.5, and doubles after each failed attempt.
Change the layers
In layers.py the layer function uses attempts=4. Predict the number of backend calls if you change it to attempts=2, and then attempts=3. Run python layers.py after each change. We saw 8 and 27. The printed text still says three retries each, because it is fixed.
A hint
Attempts multiply across the three layers, so the number is attempts multiplied by itself three times.
Add a cache entry
In tools.py add a cached answer for bath to CACHE, marked as cached. In demo.py change the York city in the no cache case to Bath. Predict what it prints, then run python demo.py. When we did this, the always down case returned the cached Bath answer instead of the service down message. The printed label still says no cache, because it is fixed text.
A hint
The cache keys are lower case city names.
These tasks are for you to try on your own machine. Nothing is checked or saved.
What this does not show
One small local model, one question and one tool, one run; another model may retry or say it does not know. The weather service and its data are made up and fail on purpose. No circuit breaker, process-wide retry budget or time limit is built. Only calls that are safe to repeat should be retried.
