Error codes: diagnosis and retry strategy
The API returns OpenAI-format errors. The HTTP status tells who should act: the client (4xx except 429), the rate limit (429) or the service (5xx). Full table, useful headers and when to retry.
Error response format
One format, whatever the endpoint.
{
"error": {
"message": "Credits exhausted. Top up your account on /portal.",
"type": "insufficient_quota",
"code": "insufficient_credits",
"param": null
}
}
code is stable and meant for client code; message
is readable but may change. The x-ef-request-id header lets
support find the request.
Code table
| Code | HTTP | Meaning | Action |
|---|---|---|---|
| missing_api_key / invalid_api_key | 401 | Missing or unknown key | Check the Authorization header |
| key_disabled | 403 | Revoked key | Create a new key |
| insufficient_credits | 402 | Balance exhausted | Top up before retrying |
| model_not_found | 404 | Model not in the served catalogue | List models with /v1/models |
| illegal_content | 400 | Prohibited content (legal lock) | Do not retry |
| rate_limit_exceeded | 429 | RPM or TPM exceeded | Wait for Retry-After, then retry |
| upstream_unavailable | 503 | Backend unavailable or starting | Retry with backoff |
| upstream_stream_error | 200 (SSE) | Stream interrupted on the backend | Detect the error event and restart |
| bad_upstream_response | 502 | Unreadable backend response | Retry once, then contact support |
Retry or fix?
Simple rule: 4xx = your code, 429/5xx = timing.
- 400, 401, 402, 403, 404: retrying will not help. Fix the request, the key or top up the account.
- 429: RPM/TPM limit reached. Read
Retry-Afterand thex-ratelimit-remaining-requests/x-ratelimit-remaining-tokensheaders. - 503: backend starting or unavailable.
Retry-Aftergives the minimum delay; exponential backoff avoids adding load. - 502: invalid backend response, often transient. One
retry is enough; if it repeats, open a ticket with the
x-ef-request-id.
Retry strategy
The only retry pattern that does not double the bill.
Requests rejected before generation (401, 402, 404, 429) cost nothing. A blind retry of a generation that already started can double the cost: only retry on statuses where no generation began.
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 stops after four attempts and returns the last response: a persistent 429 is an alarm signal (quota, sizing), not something to loop on forever.
Useful headers
| Header | Content |
|---|---|
| x-ef-request-id | Request identifier, quote it to support |
| x-ratelimit-remaining-requests | Requests left in the current window |
| x-ratelimit-remaining-tokens | Tokens left in the current window |
| Retry-After | Minimum delay in seconds (429, 503) |
| x-ef-requested-model / x-ef-served-by | Requested model and actual fallback model |
| x-ef-cost-micro / x-ef-balance-micro | Cost and balance after the request (not streaming) |
Quick diagnosis
- 401: does the key start with
sk-ef-? Is it sent asAuthorization: Bearer? - 402: is the balance positive? The portal and
/v1/megive the exact amount. - 404: is the model listed by
/v1/models? - 429: which limit header is at zero? Reduce concurrency or wait for the next window.
- 503: is the model starting cold? The status page and console logs show the state.
FAQ
Is a failed request billed?
A request rejected before generation (401, 402, 404, 429) is not billed. An interrupted stream is billed for the tokens actually produced.
Why a 200 status on a stream error?
HTTP headers are sent with the first token. An error after that point
is delivered inside the SSE flow as an error event, which is
standard SSE behaviour.
Need help with an error?
Open a ticket with the request's x-ef-request-id.