A client that gets a 401 and a client that gets a 429 often share the same reflex: retry. In both cases that is the wrong answer. The API returns OpenAI-format errors — {"error": {"message", "type", "code", "param"}} — and the HTTP status says who should act: your code (4xx), the rate limit (429) or the service (5xx). The code field is stable and meant for your logic; the message is readable but may change. The x-ef-request-id header identifies the request, quote it if you open a ticket.
401: the key is not recognised
Two neighbouring codes share this status. missing_api_key when the Authorization header is missing or malformed; invalid_api_key when the token does not start with sk-ef- or matches no stored key. Diagnosis comes down to two questions: is the key sent as Authorization: Bearer sk-ef-..., and is it the one of the account you think? A revoked key does not return 401 but 403 key_disabled: it existed and is now disabled. In all three cases, retrying changes nothing; fix the header or issue a new key.
402: the balance does not cover the request
insufficient_credits, of type insufficient_quota, means generation is refused before any backend call. Two situations trigger it: a zero balance, and an estimated reservation above the remaining balance — typically a high max_tokens on a long context. The fix is the same: check the exact balance on /v1/me or in the client portal, then top up with a pack from the pricing page. Lowering max_tokens is sometimes enough to get through without a top-up. Note that account and catalogue endpoints remain reachable with a dry key; only inference is blocked. A request refused before generation is not billed.
429: the rate limit
rate_limit_exceeded, of type rate_limit_error, means the one-minute window is saturated, in requests (RPM) or in tokens (TPM), for that key. The response carries what you need to decide: Retry-After gives the minimum delay in seconds, while x-ratelimit-remaining-requests and x-ratelimit-remaining-tokens show what is left. Retrying immediately only adds load. Reduce concurrency, wait for the next window, and treat a 429 that keeps coming back as a sizing signal, not an obstacle to work around.
Retrying without doubling the bill
Requests refused before generation — 401, 402, 404, 429 — cost nothing. A generation that already started, however, can be billed twice if you retry blindly. The rule: only retry on statuses where no token was produced.
import random, time
import httpx
RETRY_STATUS = {429, 502, 503}
def post_with_retry(url, payload, headers, attempts=4):
for attempt in range(attempts):
resp = httpx.post(url, json=payload, headers=headers, timeout=120)
if resp.status_code not in RETRY_STATUS:
return resp
retry_after = resp.headers.get("Retry-After")
delay = int(retry_after) if retry_after and retry_after.isdigit() \
else min(60, 2 ** attempt) + random.random()
time.sleep(delay)
return resp
This pattern stops after four attempts and returns the last response. It prefers Retry-After when the header is present, otherwise a capped exponential backoff, with some jitter so several workers do not restart together. A 503 upstream_unavailable follows the same logic: the backend is starting or recovering, and the header gives the minimum delay. A 502 bad_upstream_response deserves a single retry, then a ticket with the request identifier.
Streaming has its own case: HTTP headers leave with the first token. An error that happens afterwards therefore keeps a 200 status and arrives in the SSE stream as an error event, with the upstream_stream_error code. Your client must inspect events, not just the status, and count only the tokens actually produced.
In short: 401, check the key; 402, top up or lower max_tokens; 429, read Retry-After and wait. The full table of codes and headers is in the documentation, common questions about keys and billing in the FAQ and served models in the catalogue.