learning-it by example

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

1. In the tool retry function, how is an unknown city handled compared with a timeout?
2. Three layers each allow 4 attempts (3 retries) around a backend that always fails. How many backend calls does one request cause?
3. In one run, a small local model (llama3.2) was handed a raw timeout error from a weather tool, with no retries in the tool. What did it do?
4. In the retry function, how long does it wait between tries?
5. Why should a tool mark a fallback answer from a cache, such as 3 h old?

Practice only: nothing is saved and no score counts towards anything. Each answer comes with the reason.

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

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

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

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

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

Up next in AI agents and MCP

More in AI agents and MCP

All AI agents and MCP videos

Related videos