June 12, 2026 · Yunus Emre Vurgun
Error Handling Patterns for AI Agents Calling Public APIs
Agents that hit external APIs die in three common ways: tight retry loops, no backoff, and no plan for partial failure. None of these are hard to fix. All of them matter.
The minimum viable pattern
- Wrap the call. Catch every exception, log a request id, return a structured error.
- Retry on 429 and 5xx only, with exponential backoff and jitter.
- Cap retries at 3 within a single task step. Move on or surface a clear failure.
- On persistent failure, fall back to a cached response if one is available and fresh enough.
What not to do
- Retry on 4xx other than 429. The server told you no; believe it.
- Retry forever. Most API outages last under five minutes.
- Silence the error. The next agent in the chain needs to know.
A tiny, reusable helper
def call_with_retry(url, max_tries=3):\n for i in range(max_tries):\n r = http.get(url, timeout=10)\n if r.status_code == 200: return r\n if r.status_code in (429, 500, 502, 503, 504):\n sleep(2 ** i + random() * 0.5); continue\n return r\n return r\nThat is the whole pattern. Everything else is policy.