
In short: when a request fails, the OpenAI SDK reads the message from error.message and raises an exception with the HTTP status. In Python it is an openai.APIStatusError, with status_code; in Node.js, an OpenAI.APIError, with status. The stable code, such as insufficient_credits, is in the response body, inside error.
In Python
import os
import openai
from openai import OpenAI
client = OpenAI(base_url="https://api.interweb.co.mz/v1", api_key=os.environ["INTERWEB_API_KEY"])
try:
completion = client.chat.completions.create(
model="deepseek-ai/deepseek-v4-flash",
messages=[{"role": "user", "content": "Hello"}],
max_tokens=200,
)
print(completion.choices[0].message.content)
except openai.APIStatusError as e:
error = e.response.json()["error"]
print(e.status_code, error["code"], error["message"], error["correlation_id"])
except openai.APIConnectionError:
print("Could not reach the API")
e.response is the whole HTTP response: from it you take the code, the message and the correlation_id. APIConnectionError happens when there was no response at all, for example on a network failure.
In Node.js
import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.interweb.co.mz/v1", apiKey: process.env.INTERWEB_API_KEY });
try {
const completion = await client.chat.completions.create({
model: "deepseek-ai/deepseek-v4-flash",
messages: [{ role: "user", content: "Hello" }],
max_tokens: 200,
});
console.log(completion.choices[0].message.content);
} catch (err) {
if (err instanceof OpenAI.APIError) {
console.log(err.status, err.message);
} else {
throw err;
}
}
The correlation_id also comes in the X-Correlation-ID header of the response, which the error keeps in err.headers.
Which class goes with each status
The SDKs have one class for each status. You can catch just one, for example openai.RateLimitError, instead of all of them:
| Status | SDK class | Our codes |
| 400 | BadRequestError | invalid_request |
| 401 | AuthenticationError | unauthorized |
| 402 | No class of its own: APIStatusError (Python) or APIError (Node.js) | insufficient_credits |
| 403 | PermissionDeniedError | insufficient_scope, ip_not_allowed, account_* |
| 404 | NotFoundError | model_not_found, not_found |
| 422 | UnprocessableEntityError | validation_failed |
| 429 | RateLimitError | rate_limited, plan_limit_reached |
| 500 or more | InternalServerError | internal_error, upstream_error, disabled |
Automatic retries
The SDKs retry some errors on their own, twice by default and with a short wait: connection failures, 408, 409, 429 and 500 or more. You change it with max_retries in Python and maxRetries in Node.js; 0 turns it off. A 402, 403, 404 or 422 is not retried: fix the request first. And not everything that is retried goes through: a 429 plan_limit_reached or a 409 agent_paused stays the same until something changes.
| In your program, decide by the code and not by the message: the message comes in the language of the Accept-Language header and can change. Keep the correlation_id in your program's log, to give it to support if you need to. The list of codes is in the AI API errors. |
|
Want to test your error handling without spending? Use a sandbox_ key and see the models in All in One AI. See All in One AI |
RECOMMENDED PRODUCT Web hosting with cPanel Domain and SSL included, daily backups and the panel you already know. from $5.36/mo (3-year plan, with coupon) See plans |











