Errors come back in the format of the endpoint you called, so your SDK raises its usual
exceptions. Every error has our own code, which is the same on both formats, and a request id
you can quote to support.
Error messages never name the company that hosted the request, and never pass on a raw message from it.
Where to look
| Situation | Where the error is |
|---|---|
| Request refused before it starts | HTTP status and JSON body |
| Stream fails after it has started | An error event inside the stream; the HTTP status is already 200. See Streaming |
| One request in a batch fails | error on that item in GET / |
| A whole batch is refused | HTTP status and JSON body; the message names the bad item |
| Anything | GET / shows status and error for a request that reached a model |
Error shapes
OpenAI format
Used by /v1/chat/completions, /v1/responses, /v1/models, /v1/generation,
/v1/batches and every other path outside /v1/messages.
{
"error": {
"type": "insufficient_balance",
"code": "insufficient_balance",
"message": "Account balance is zero or negative. Please top up to continue.",
"request_id": "4f1c..."
}
}
type and code both hold our code. There is no param field.
Anthropic format
Used by /v1/messages and /v1/messages/count_tokens.
{
"type": "error",
"error": {
"type": "billing_error",
"message": "Account balance is zero or negative. Please top up to continue.",
"code": "insufficient_balance"
},
"request_id": "4f1c..."
}
error.type is Anthropic’s own type, so Anthropic SDKs and Claude Code react as they would to
Anthropic. error.code is our code.
Headers on every error
| Header | When |
|---|---|
x-yunshu-request-id |
Always. Quote it when you contact support |
Retry-After |
On rate_ (1 second), no_ (5 seconds) and overloaded (1 second) |
Error codes
The table lists every code a /v1/ request can return. “Retry” means the same request may
succeed later without changes.
| HTTP | code |
Anthropic error.type |
Meaning | Retry? | What to do |
|---|---|---|---|---|---|
| 400 | invalid_ |
invalid_ |
The body is not valid: not JSON, a missing or wrong field, empty messages, an unknown model suffix, previous_ on /, a bad batch item |
No | Read message; it names the field. Fix the request |
| 400 | context_ |
invalid_ |
The prompt is longer than the model’s context window | No | Shorten the prompt, use a model with a larger window, or add one as a fallback |
| 401 | authentication_ |
authentication_ |
No key, or the key is wrong, revoked or deleted | No | Check the header and the key. See Authentication |
| 402 | insufficient_ |
billing_ |
Balance is empty, or too low to hold this request’s maximum price | No | Top up, switch on automatic top-up, or lower max_ |
| 402 | budget_ |
billing_ |
The key’s monthly budget is spent, or this request could pass it | No | Wait for the next UTC month, or use another key. See Budgets and limits |
| 403 | offer_ |
permission_ |
The key’s allow-list does not include this model | No | Use an allowed model or another key |
| 403 | account_ |
permission_ |
Your organisation is suspended | No | Contact support |
| 403 | permission_ |
permission_ |
Not allowed for this organisation, for example a batch with zero retention on | No | Read message |
| 404 | offer_ |
not_ |
The model ID is unknown, unpublished, or a combo that does not exist | No | Check the ID against GET /. Check the spelling and the case |
| 404 | not_ |
not_ |
The path, batch or request id does not exist in your organisation | No | Check the URL and the base URL. A / path means / is in the base URL twice. See Base URLs |
| 405 | method_ |
api_ |
Wrong HTTP method for this path | No | For example, POST to /, not GET |
| 413 | request_ |
request_ |
The body is over 2 MiB | No | Send less: fewer or smaller messages, images by URL |
| 429 | rate_ |
rate_ |
Over the key’s or the organisation’s requests per minute, or over 10 batch submissions per minute | Yes | Wait for Retry-After, then back off |
| 500 | internal_ |
api_ |
An unexpected error on our side | Yes, a few times | Retry with backoff. If it continues, contact support with the request id |
| 500 | configuration_ |
api_ |
A setup problem on our side | No | Contact support with the request id |
| 502 | upstream_ |
api_ |
The model could not answer: it was unreachable or returned an error | Yes | Retry with backoff, or add a fallback |
| 503 | no_ |
api_ |
No capacity for this model right now | Yes | Wait for Retry-After (5 s), or add a fallback |
| 503 | quote_ |
api_ |
This model’s pricing is being renewed, so it cannot be priced | Yes, later | Use a fallback, or retry later |
| 503 | overloaded |
overloaded_ |
We are shedding load | Yes | Wait for Retry-After, then back off |
| 504 | upstream_ |
timeout_ |
The model took too long to answer | Yes | Retry, lower max_, or use streaming for long answers |
Requests that end in any error are not charged.
Codes inside streams and batches
- Streams. A failure after the first event carries one of the 5xx codes above, usually
upstream_unavailable, in the stream’s own error event. On the Anthropic format itserror.typeisapi_error; on Responses it isresponse.failedwitherror.code. - Batch items can also end with
cancelled(the batch was cancelled before the item ran) orexpired(the completion window closed first), or with the code of the error that failed it.
Anthropic types that differ from Anthropic’s own API
- Anthropic returns
529foroverloaded_error. We return503. - Anthropic’s
billing_erroris402on both. 413 request_too_largekeeps Anthropic’s type name.
How the SDKs see these errors
Both SDKs choose the exception class from the HTTP status.
| HTTP | OpenAI SDK (Python / TypeScript) | Anthropic SDK (Python / TypeScript) |
|---|---|---|
| 400 | BadRequestError |
BadRequestError |
| 401 | AuthenticationError |
AuthenticationError |
| 402 | APIStatusError (status 402) |
APIStatusError (status 402) |
| 403 | PermissionDeniedError |
PermissionDeniedError |
| 404 | NotFoundError |
NotFoundError |
| 413 | APIStatusError (status 413) |
— |
| 429 | RateLimitError |
RateLimitError |
| 5xx | InternalServerError |
InternalServerError (OverloadedError is for 529 only) |
Both SDKs retry 429, 5xx and connection errors twice by default, reading Retry-After. Set
max_retries to change this.
Branch on our code, not on the message text. Messages may change and are translated in the
console.
import openai
from openai import OpenAI
client = OpenAI(base_url="https://api.yunshurouter.com/v1", api_key="tq-...")
try:
client.chat.completions.create(model="deepseek-v4-pro", messages=[{"role": "user", "content": "Hi"}])
except openai.APIStatusError as err:
body = err.response.json().get("error", {})
code = body.get("code")
request_id = err.response.headers.get("x-yunshu-request-id")
if code == "insufficient_balance":
... # ask for a top-up
elif code == "context_length_exceeded":
... # trim the conversation
else:
raise
import anthropic
client = anthropic.Anthropic(base_url="https://api.yunshurouter.com", api_key="tq-...")
try:
client.messages.create(model="deepseek-v4-pro", max_tokens=256, messages=[{"role": "user", "content": "Hi"}])
except anthropic.APIStatusError as err:
code = err.body.get("error", {}).get("code") if isinstance(err.body, dict) else None
print(err.status_code, code, err.response.headers.get("x-yunshu-request-id"))
import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.yunshurouter.com/v1", apiKey: "tq-..." });
try {
await client.chat.completions.create({ model: "deepseek-v4-pro", messages: [{ role: "user", content: "Hi" }] });
} catch (err) {
if (err instanceof OpenAI.APIError) {
const code = (err.error as { code?: string } | undefined)?.code;
console.log(err.status, code, err.headers?.["x-yunshu-request-id"]);
} else {
throw err;
}
}
Retry guidance
- Retry:
429,500 internal_error,502,503,504. Use exponential backoff with jitter, and honourRetry-After. - Do not retry unchanged:
400,401,402,403,404,405,413. The same request will fail the same way. - Retrying a failed request is safe for billing: a request that ended in an error was not charged.
- For model failures (
502,503,504), a fallback chain is often better than a retry: the next model is tried at once, within the same request.
Common setup errors
| What you see | Usual cause |
|---|---|
404 not_ on every request |
/ is missing from, or doubled in, the base URL. See Base URLs |
401 authentication_ with a key you just created |
Extra characters copied with the key, or the key is in the wrong variable (for example ANTHROPIC_ when the tool reads ANTHROPIC_) |
404 offer_ |
A model ID from another provider’s API (for example gpt-4o). Use an ID from GET / |
402 insufficient_ on a new account |
No top-up yet. There is no free credit |
400 invalid_ mentioning previous_ |
A Responses client using stored state. Send the whole conversation with store: false |
Contacting support
Send the x-yunshu-request-id value, the time of the request (with time zone) and the endpoint.