By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Providers rate-limit. Networks blip. A model occasionally returns a 500. None of that is a quality problem — until your test suite turns a transient 429 into a red build and the team stops trusting it. Robust failure handling is what makes an AI suite usable.
| Status | Meaning | Retry? | Why |
|---|---|---|---|
| 429 | Rate limited | Yes, with backoff | Transient by definition |
| 500 / 502 / 503 / 504 | Provider or gateway error | Yes, with backoff | Usually transient |
| 408 | Request timeout | Yes | Often a slow response, not a bad one |
| 400 | Bad request | No | Your prompt or payload is malformed — retrying will fail again |
| 401 / 403 | Auth or permission | No | Config problem; fail immediately and loudly |
| 404 | Model not found | No | Wrong model name or region |
| Content policy | Refused to answer | No | That is a result, not an error — record it |
Retrying immediately is how you turn one 429 into ten. Wait longer each time, and add randomness so a fleet of test runners does not retry in lockstep.
import random, time
RETRYABLE = {429, 500, 502, 503, 504, 408}
def backoff_delay(attempt: int, base: float = 0.5, cap: float = 8.0) -> float:
"""attempt is 0-based. 0.5, 1.0, 2.0, 4.0, 8.0 ... never above cap."""
return min(cap, base * (2 ** attempt))
def sleep_with_jitter(delay: float) -> None:
time.sleep(delay * random.uniform(0.5, 1.0))def call_with_retries(fn, *, max_attempts: int = 4):
last_error = None
for attempt in range(max_attempts):
try:
return fn()
except Exception as exc:
status = getattr(exc, "status_code", None)
retryable = status in RETRYABLE or status is None
last_error = exc
if not retryable or attempt == max_attempts - 1:
raise
sleep_with_jitter(backoff_delay(attempt))
raise RuntimeError("unreachable") from last_errorSet an explicit timeout on every call, shorter than your test's own timeout, so you get a useful error instead of a hung suite. A good default for an eval call is 30 seconds; for a latency test, 10.
client = OpenAI(api_key=key, timeout=30.0, max_retries=0) # we retry ourselvesSetting the SDK's own max_retries=0 is deliberate: two retry layers multiply and produce baffling 60-second stalls. Own the policy in one place.
At the end of a run, a suite should report how many cases failed and which kind of failure it was — not dump forty tracebacks and exit. Distinguish:
Conflating those three is the fastest way to make a team ignore AI tests. Nothing erodes trust like a red build that turns out to be a rate limit.
Key takeaways
Retry 429/5xx/timeouts with exponential backoff and jitter; never retry 4xx.
Never retry a quality failure — that hides the bug you are paid to find.
Set explicit timeouts and disable the SDK's own retries so you own the policy.
Classify failures as quality, transport or harness; never mix them in one count.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.