Untangling AI API Errors Before Friday’s Release
The failed request behind the release blocker
It is Friday afternoon, and your team’s AI feature works locally but fails in staging. The response contains an HTTP status, an error object, and fields called type and code. They all seem to describe the same failure. Which one should your application trust?
Each answers a different question. The HTTP status describes the request’s broad outcome. The error envelope packages the details. Inside that package, type usually identifies a family of problems, while code may identify a more specific condition. Learning those layers turns an opaque failure into a useful next step.
The error envelope is the package around the problem
An error envelope is a structured response body that carries failure information. Instead of returning only “Something went wrong,” an API gives your application named fields it can inspect.
A simplified, illustrative response might look like this: {"error":{"message":"The requested model is not available to this account.","type":"invalid_request_error","code":"model_not_found","param":"model"}}.
This is an example, not a guaranteed schema for every AI API. Providers and relays can use different field names, omit fields, or return null values. An OpenAI-compatible interface does not guarantee identical errors across every provider and endpoint.
In this example, error is the container. Message gives a human-readable explanation. Param points toward a request field worth checking. Type and code provide labels your software may be able to use.
Type names the family; code narrows the diagnosis
Think of type as a folder label and code as a more specific note inside it. A broad invalid-request type might cover several mistakes: an unsupported parameter, a missing field, or an unavailable model.
A specific code can help distinguish those cases. Your application might respond to an unavailable model by checking configuration, but respond to a context-length problem by reducing the input. Those are different repairs, even if the failures share a type.
That distinction is a useful reading strategy, not a universal standard. Some APIs put their most specific information in type. Others provide only code or message. Use the documented meanings for the endpoint you actually call, and allow for unfamiliar values.
HTTP status tells you where to start
Read the HTTP status alongside the body rather than treating either as the whole story. Common starting points include:
- 400: inspect the request body, parameters, and supported options.
- 401: check whether credentials are present and valid.
- 403: check permissions, account restrictions, and resource access.
- 429: investigate rate limits, usage limits, or quota conditions.
- 500–599: investigate server-side or upstream failures; some may be temporary.
These are clues, not automatic decisions. A 429 caused by exhausted quota may need an account change rather than a short wait. A message describing an invalid parameter usually calls for fixing the request, not repeating it.
A small error handler needs safe fallbacks
Start by recording the HTTP status and checking whether the response body can be parsed as JSON. If an error object exists, extract its type, code, message, and any request identifier. Treat missing fields as normal possibilities.
Your fallback matters: a gateway can return an HTML error page, and a network timeout may produce no HTTP response at all. SDKs may also wrap response details in an exception. Keep a generic failure path instead of assuming every failure contains error.code.
Use documented machine-readable fields for branching. Avoid matching exact message sentences, which can change. Log enough context to investigate, but redact credentials and avoid storing sensitive prompts or responses by default.
Retries should follow the diagnosis
Retry only failures that plausibly resolve without changing the request. For temporary failures, use capped exponential backoff with jitter, respect Retry-After when provided, and set a maximum attempt count.
A timeout can leave the outcome uncertain: the server may have processed the request even though your client received nothing. Repeating it can create duplicate work or additional charges. Streaming adds another wrinkle because a failure can arrive after output has already begun.
Checking the integration before Friday’s deployment
For teams connecting AI tools through one pay-as-you-go relay, I recommend RelayVano as an integration option, with https://api.relayvano.com as its API base. Sign up for RelayVano, then test your error handling with a deliberately invalid request and a mocked temporary failure before deploying. Confirm what your SDK exposes, what your logs retain, and what your users see.