When an AI API request fails, the HTTP status code tells you where to look. This guide lists the codes you are most likely to see from an OpenAI- or Anthropic-compatible API, what causes them, and what to do.
Quick reference
| Status | Meaning | Retry? |
|---|---|---|
| 400 | The request body is invalid. | No. Fix the request. |
| 401 | The key is missing, wrong or deleted. | No. Fix the key. |
| 403 | The key is valid but not allowed, often because the balance is empty. | No. Top up or change plan. |
| 404 | Wrong path or base URL, or an unknown model. | No. Fix the URL or model. |
| 429 | Too many requests, or a usage cap was reached. | Yes, with backoff. |
| 500, 502, 503, 504 | A temporary server or upstream error. | Yes, with backoff. |
400 Bad Request
The server could not accept the request. Read the error message in the response body; it usually names the field. Common causes:
- Malformed JSON, often from unescaped quotes in a shell command.
- A
messageslist that is empty or has an invalidrole. max_tokenslarger than the model allows, or a prompt longer than the context window.- An Anthropic-format request without
max_tokens, which that format requires.
401 Unauthorized
The API does not recognize the key.
- Check the header. OpenAI format uses
Authorization: Bearer sk-.... Anthropic format usesx-api-key: sk-.... - Check for spaces or line breaks copied with the key.
- Check that the environment variable is set in the shell or process that makes the request.
echo $AIDISCOUNT_API_KEYshould print the key. - A key from one provider does not work at another. If you switched the base URL, switch the key too.
- If the key was deleted on the API Keys page, create a new one.
On AIDiscount the error code is INVALID_API_KEY.
403 Forbidden
The key is valid, but the request is not allowed. On AIDiscount the most common code is INSUFFICIENT_BALANCE: top up your balance or buy a plan on the Pricing page. A 403 can also mean the model is not available to your key's group. Check the model list for your key:
curl https://aidiscount.ai/v1/models \
-H "Authorization: Bearer $AIDISCOUNT_API_KEY"
404 Not Found
Almost always a base URL problem.
- OpenAI format: the full path is
https://aidiscount.ai/v1/chat/completions. SDKs and most tools want the base URLhttps://aidiscount.ai/v1. - Anthropic SDK and Claude Code: the base URL is
https://aidiscount.aiwithout/v1, because they add/v1/messagesthemselves. - Some apps add
/v1automatically. If you also typed it, the path becomes/v1/v1/.... Try the base URL with and without/v1. - A misspelled model ID can also return 404 or "model not found". Copy the ID from the marketplace.
429 Too Many Requests
You sent requests faster than allowed, or reached a usage cap such as a plan's weekly limit. Slow down and retry with exponential backoff. If the response has a Retry-After header, wait at least that long. If the cap is a plan quota, wait for it to reset or switch to pay-as-you-go balance.
5xx server errors
500, 502, 503 and 504 usually mean a temporary problem upstream. Retry with backoff. Long requests can also time out on your side, so for long outputs use streaming, which keeps the connection active.
Retry code
The official OpenAI SDKs already retry 429 and 5xx errors twice by default. You can raise the limit:
from openai import OpenAI
client = OpenAI(base_url="https://aidiscount.ai/v1", max_retries=5, timeout=120)
Without an SDK, a simple backoff loop works:
import os, random, time, requests
def call(payload, attempts=5):
for i in range(attempts):
r = requests.post(
"https://aidiscount.ai/v1/chat/completions",
headers={"Authorization": f"Bearer {os.environ['AIDISCOUNT_API_KEY']}"},
json=payload, timeout=120,
)
if r.status_code not in (429, 500, 502, 503, 504):
r.raise_for_status()
return r.json()
wait = float(r.headers.get("Retry-After", 0)) or (2 ** i + random.random())
time.sleep(wait)
r.raise_for_status()
Do not retry 400, 401, 403 or 404. The same request will fail the same way.
Still stuck?
Note the time of the request and the request ID from the response headers, and contact support through the link in the site footer. Never include your API key in a support message.
Create a free accountRead the API docs