Sub topic 1
How LLM API Calls Fail: Client, Server, Network, and Content Errors
Prerequisites
This chapter assumes basic Python, HTTP status codes, exceptions, and experience calling an LLM through an SDK or HTTP client.
Four Failure Categories
An LLM request can fail before it reaches the provider, while the provider handles it, while the response travels back, or after the response arrives.
| Category | Examples | Usual response |
|---|---|---|
| Client | Invalid JSON, bad API key, unknown model, HTTP 400 or 401 | Fix the request or configuration |
| Server | HTTP 500, 502, or 503 | Retry after a delay |
| Network | DNS failure, connection reset, timeout | Retry when the operation is safe |
| Content | Refusal, malformed output, safety-filter result | Validate, repair, or use a fallback |
A client error usually means repeating the same request will fail again. A server or temporary network error may succeed later. Content errors are successful HTTP requests, so HTTP retry logic alone cannot fix them.
Deciding What to Retry
Retry only temporary failures. A retry policy commonly includes HTTP 408 (timeout), 429 (rate limit), and selected 5xx responses. It also catches transport exceptions such as connection timeouts.
RETRYABLE_STATUS = {408, 429, 500, 502, 503, 504}
def is_retryable_status(status_code: int) -> bool:
return status_code in RETRYABLE_STATUS
for code in (200, 400, 401, 404, 429, 500, 503):
print(code, is_retryable_status(code))A retry is safest when the request is idempotent, meaning repeating it has the same intended effect. Text generation is often logically idempotent, but a request that triggers a payment, sends an email, or writes a record may duplicate that side effect. Use an idempotency key when the provider supports one.
Rate Limits and HTTP 429
Providers limit request-per-minute (RPM) and token-per-minute (TPM) usage. RPM counts requests. TPM counts input and output tokens. A request can stay below RPM while exceeding TPM.
HTTP 429 means the provider is temporarily refusing the request, often because one of these limits was exceeded. A client can reduce concurrency, queue work, and honor the Retry-After response header. Exponential backoff with jitter prevents many workers from retrying at exactly the same time.
Timeouts and Overall Deadlines
Set a per-request timeout so one connection cannot wait forever. Also set an overall deadline around the complete retry operation. The deadline must include all attempts, backoff delays, and response processing. If the deadline expires, surface a clear error or use a fallback.
Exercise
A request returns HTTP 401 twice. Should the client retry it?
Show answer
No. HTTP 401 usually indicates a missing or invalid credential. Fix authentication before trying again.