Sub topic 2
Deciding what to retry and what to surface immediately
This chapter assumes you can write Python functions and handle exceptions, you know what HTTP status codes and headers are, and you have already called an LLM API with an SDK or an HTTP client. It builds on the previous chapter's four failure groups: client, server, network, and content errors.
The core question
A retry sends the same request again and hopes for a different result. That only helps when the failure was temporary. If the request itself is wrong, the second attempt fails the same way as the first. It also wastes time and, on a metered API, may cost money.
So every failure falls into one of two groups:
- Retryable: a temporary problem that may clear by itself.
- Permanent: a problem a retry cannot fix. Surface it immediately, which means raise it or report it to the caller without further attempts.
Status codes
| Status | Meaning | Action |
|---|---|---|
| 400 | Malformed request, such as a bad parameter or too many tokens | Surface. Fix the request. |
| 401 | Missing or invalid API key | Surface. Fix the credentials. |
| 403 | Key lacks permission | Surface. |
| 404 | Unknown model or endpoint | Surface. |
| 408 | Server timed out waiting for the request | Retry. |
| 429 | Rate limit hit | Retry, after waiting. |
| 500, 502, 503, 504 | Server or gateway trouble | Retry. |
The next chapter covers 429 in detail. Here, treat it as retryable because the limit resets as time passes.
Some providers also return a 429 for an exhausted quota or billing limit. That is permanent until someone adds credit. Check the error body or code if your provider distinguishes the two.
A classifier
The first code block turns the table into a function.
RETRYABLE_STATUS = {408, 429, 500, 502, 503, 504}
def is_retryable_status(status_code: int) -> bool:
"""Return True for statuses that usually indicate a temporary problem."""
return status_code in RETRYABLE_STATUS
for code in (200, 400, 401, 404, 429, 500, 503):
print(code, is_retryable_status(code))Running it prints True only for 429, 500, and 503. A set makes the policy explicit and easy to change in one place.
Exceptions without a status code
Network failures such as a dropped connection, a DNS error, or a connect timeout produce no HTTP response, so there is no status to check. Treat these as retryable too. With httpx, they are subclasses of httpx.TransportError. SDKs wrap them in their own exception types, for example a connection error or timeout error, so check your SDK's exception list.
Content-level problems are different. A response that arrives with status 200 but is blocked by a safety filter, truncated at the token limit, or fails your JSON parsing is not a transport failure. Retrying the identical prompt may give the same result. You can sometimes retry a malformed output, since sampling is random, but do it with a small separate limit and decide it deliberately.
Common mistakes
- Retrying everything. A bad API key retried five times is five wasted calls and a slower error message.
- Retrying nothing. A single 503 during a brief outage breaks a request that would have worked a second later.
- Hiding the final error. When retries run out, raise the last error. Do not return
Noneand let the failure surface somewhere else.
Exercise
A request returns 401 and another returns 503. Which one should your code retry, and why?
Show answer
Retry the 503. It signals a temporary server problem that may clear. The 401 means the API key is invalid or missing, so a retry will fail the same way. Surface it immediately so the key can be fixed.
Exercise
A call fails because the network connection dropped before any response arrived. There is no status code. Should you retry?
Show answer
Yes. A dropped connection is a transport failure and is usually temporary. Treat it as retryable, with a limit on attempts.