Sub topic 1
How LLM API calls fail: client, server, network, and content errors
Prerequisites
This chapter assumes you know basic Python, including functions and exceptions. You should also know how HTTP status codes and headers work. You should have called an LLM API at least once, through an SDK or an HTTP client.
Why classify failures first
Every later technique in this tutorial depends on one question: what kind of failure just happened? A retry helps with a brief server problem. The same retry does nothing for a malformed request, and it can make a rate-limit problem worse. Sorting failures into categories lets you choose the right response.
The four categories
LLM API failures fall into four groups. The group usually tells you whether trying again could help.
| Category | Typical signal | Example cause | Retry helps? |
|---|---|---|---|
| Client | 400, 401, 403, 404, 422 | Bad JSON, wrong API key, unknown model name | No, fix the request |
| Rate limit | 429 | Too many requests or tokens per minute | Yes, after waiting |
| Server | 500, 502, 503, 504, 529 | Provider outage or overload | Often yes |
| Network | Timeout, connection reset, DNS failure | Dropped connection, slow response | Often yes |
A fifth case cuts across these groups: content errors. The HTTP call succeeds, but the result is not usable.
Client errors
A client error means the provider understood your request and rejected it. The 4xx status codes cover this group. Common causes are an invalid API key (401), a key without access to a model (403), a model name that does not exist (404), a request that exceeds the context window (400), and a body that fails validation (422). The request is the problem, so sending it again gives the same answer. Fix the request or the configuration.
Rate-limit errors
Status 429 is technically a 4xx code, but it behaves differently. The request was valid. The provider is telling you that you sent too much, too fast. Providers usually limit requests per minute and tokens per minute. Waiting and trying again works. A later chapter covers these limits in detail.
Server errors
A 5xx status means the provider failed while handling a valid request. Examples are 500 (internal error), 502 and 504 (gateway problems), and 503 (overloaded or unavailable). Some providers add their own codes, such as 529 for overload. These are usually temporary.
Network errors
Network errors happen before you get a status code. The connection may fail to open, drop mid-request, or exceed your timeout. In httpx these appear as exceptions such as httpx.ConnectError and httpx.ReadTimeout, which share the base class httpx.TransportError. A timeout leaves you unsure whether the provider processed the request. That uncertainty matters for retry safety later.
Content errors
Some failures return HTTP 200 with a response you cannot use. Examples include:
- A
finish_reasonoflength, meaning the output was cut off at the token limit. - A refusal or a content-filter result instead of an answer.
- Output that was meant to be JSON but fails to parse.
Retrying the identical request may or may not produce a different result. Treat these as a separate path with their own checks.
A first classifier
This function maps an exception or response to a category. It is a starting point for the decision logic built in the next chapters.
import httpx
def classify_failure(error=None, status_code=None) -> str:
"""Return 'network', 'rate_limit', 'server', or 'client'."""
if isinstance(error, httpx.TransportError):
return "network"
if status_code == 429:
return "rate_limit"
if status_code is not None and status_code >= 500:
return "server"
return "client"
print(classify_failure(error=httpx.ConnectError("refused")))
print(classify_failure(status_code=429))
print(classify_failure(status_code=503))
print(classify_failure(status_code=401))Exercise
A call to an LLM API returns HTTP 200, but the response has finish_reason "length" and the JSON in the text is cut off mid-object. Which category is this, and would an identical retry reliably fix it?
Show answer
It is a content error, because the HTTP call succeeded but the output is unusable. An identical retry will not reliably fix it, since the same limit applies again. Raise the max-token setting, shorten the prompt or expected output, or ask for a smaller response.
Exercise
Your code gets a 401 response. Which category is it, and what should the code do?
Show answer
It is a client error, caused by a missing or invalid API key. The code should not retry. It should surface the error right away so the key or configuration can be fixed.