Zolakwika
Pempho lomwe likulephera limabweza HTTP status code ndi body ya JSON yokhala ndi type ya cholakwika ndi message. Tsamba ili limalemba status iliyonse, limaonetsa body ya cholakwika ya mtundu uliwonse wa API, ndipo limanena zolakwika zoyenera kutumizanso.
Ma SDK amatulutsa object ya cholakwika yomwe imanyamula status code ndi ma field a body. Pempho ili limatchula model yomwe kulibe:
import openai
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.shannon-ai.com/v1",
)
try:
client.chat.completions.create(
model="no-such-model",
messages=[{"role": "user", "content": "Hello"}],
)
except openai.APIStatusError as error:
print(error.status_code) # 400
print(error.type) # invalid_request_error
print(error.body["message"]) # unknown model: no-such-model
print(error.response.headers.get("x-request-id")) # the id of this request import OpenAI from "openai";
const client = new OpenAI({
apiKey: "YOUR_API_KEY",
baseURL: "https://api.shannon-ai.com/v1",
});
try {
await client.chat.completions.create({
model: "no-such-model",
messages: [{ role: "user", content: "Hello" }],
});
} catch (error) {
if (!(error instanceof OpenAI.APIError)) throw error;
console.log(error.status); // 400
console.log(error.type); // invalid_request_error
console.log(error.error.message); // unknown model: no-such-model
} curl -i https://api.shannon-ai.com/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "no-such-model",
"messages": [{"role": "user", "content": "Hello"}]
}' Limabweza status 400 ndi body iyi:
{
"error": {
"type": "invalid_request_error",
"message": "unknown model: no-such-model"
}
} Body ya cholakwika
Pali ma body awiri. /v1/messages imayankha mu mtundu wa Anthropic; endpoint ina iliyonse imayankha mu mtundu wa OpenAI. Mu zonse ziwiri, zambiri zili mu error.type ndi error.message, kotero reader imodzi imatumikira ma endpoint onse.
{
"error": {
"type": "authentication_error",
"message": "Invalid API key",
"code": "invalid_api_key",
"param": null
}
} {
"type": "error",
"error": {
"type": "authentication_error",
"message": "Invalid API key"
}
} | Field | Mtundu | Kufotokozera |
|---|---|---|
error.type | string | Mtundu wa cholakwika. Onani Mitundu ya zolakwika pansipa. |
error.message | string | Chiganizo cha munthu. Ma text a mu ma table pansipa ndi okhazikika kupatula magawo awo osintha, owonetsedwa ngati <id>, <n> kapena …. |
error.code | string | null | Imakhalapo pa zolakwika zina za mtundu wa OpenAI: invalid_api_key ndi 401, request_too_large ndi 413, context_length_exceeded pamene zokambirana ndi zazitali kwambiri, apo ayi null kapena kulibe. Sankhani potengera status ndi error.type poyamba ndipo tengani code ngati chowonjezera. |
error.param | null | Imakhalapo pa zolakwika zomwezo ndi code. Nthawi zonse null. |
type | string | Mtundu wa Anthropic wokha. Nthawi zonse error. |
Mitundu ya zolakwika
| Mtundu | Ma status | Kufotokozera |
|---|---|---|
invalid_request_error | 400, 413, 415, 422 | Pempho lokha ndi lolakwika: field, body, model id kapena input yomwe model siilandira. Sinthani pempho musanatumizenso. |
authentication_error | 401 | API key ikusowa, siidziwika kapena yathetsedwa. |
not_found_error | 404 | Njira kulibe. |
rate_limit_error | 429 | Balance siikwana pempho, flood protection yaliletsa, kapena model ili otanganidwa. Message imanena chomwe. |
api_error | 500, 502, 503, 504 | Pempho linali labwino ndipo vuto lili kumbali yathu. Tumizaninso. |
server_error | 500 | Pempho linali labwino ndipo vuto lili kumbali yathu. Tumizaninso. |
Ma status code
| Status | Mtundu | Message | Liti |
|---|---|---|---|
400 | invalid_request_error | Failed to parse the request body as JSON: … | Body si JSON yovomerezeka. |
400 | invalid_request_error | unknown model: <id> | model si id yofalitsidwa. Pa /v1/responses komanso pamene model ikusowa. |
400 | invalid_request_error | No user message provided | Ma tier a Shannon: pempho lilibe text ya user. |
400 | invalid_request_error | <id> does not accept image input | Chithunzi chinatumizidwa ku hosted open-weight model yopanda input ya zithunzi. |
400 | invalid_request_error | audio and video input are not supported by any hosted model | Part ya audio kapena video inatumizidwa ku hosted open-weight model. |
400 | invalid_request_error | <id> does not accept response_format | Structured output inapemphedwa kuchokera ku hosted open-weight model yomwe ilibe. |
400 | invalid_request_error | <id> does not accept response_format json_schema; use json_object | JSON schema inatumizidwa ku hosted open-weight model yomwe imalandira json_object yokha. |
400 | invalid_request_error | unknown reasoning effort '<value>'; expected off, low, medium or high | Reasoning effort si imodzi mwa mitengo yolembedwa. Hosted open-weight model. |
400 | invalid_request_error | count_tokens is available for the hosted open models; unknown model: <id> | Kuwerenga ma token kunapemphedwa pa model yosakhala hosted open-weight model. |
401 | authentication_error | Missing authentication | API key sinatumizidwe. |
401 | authentication_error | Invalid API key | Kiyi siidziwika, idasinthidwa ndi yatsopano, kapena yathetsedwa. |
404 | not_found_error | Not Found | Njira kulibe, mwachitsanzo /v1/embeddings. |
405 | api_error | Method Not Allowed | Njira ilipo koma osati ndi HTTP method iyi. |
413 | invalid_request_error | Failed to buffer the request body: … | Body ya pempho ndi yayikulu kuposa 32 MiB. |
415 | invalid_request_error | Expected request with `Content-Type: application/json` | Header ya Content-Type ikusowa kapena si application/json. |
422 | invalid_request_error | Failed to deserialize the JSON body into the target type: … | Field yofunika ikusowa, monga messages, kapena field ili ndi JSON type yolakwika. |
429 | rate_limit_error | Quota exceeded. Upgrade your plan at shannon-ai.com/plan | Malire a output a pempho ndi akulu kuposa zomwe zatsala pa balance yanu. |
429 | rate_limit_error | Too many requests. Retry in <n>s. | Flood protection: ma pempho opitirira 120 mu mphindi imodzi pa akaunti yanu. <n> ndi chiwerengero cha masekondi mpaka mphindi itatha. |
429 | rate_limit_error | Shannon Coder call quota reached. Upgrade your plan at shannon-ai.com/plan | shannon-coder-1 pa /v1/chat/completions ndi /v1/messages: ma call a Shannon Coder a plan yanu a window yamakono ya maola 4 agwiritsidwa ntchito. |
429 | rate_limit_error | Shannon routes are temporarily busy. Please retry. | Model ili otanganidwa pakali pano. |
500 | server_error | The model backend failed to answer. Please retry. | Model sinapange yankho. |
500 | server_error | An internal error occurred. Please retry. | Cholakwika chamkati pamene pempho likukonzedwa. |
502 | api_error | The model backend failed to answer. Please retry. | Model sinapange yankho. Banja la Shannon 3 ndi hosted open-weight model. |
503 | api_error | Could not verify your quota right now. Please retry. | Balance yanu siinawerengedwe pakali pano. |
503 | api_error | token counting is temporarily unavailable for this model | Kuwerenga ma token sikupezeka pa model iyi pakali pano. |
504 | api_error | upstream timed out | Hosted open-weight model inatenga nthawi yayitali kuposa malire a nthawi a pempho losachita stream. |
Kuyang'ana kumachitika motere: API key, body ya pempho, model id, flood protection, balance. Pempho lokhala ndi kiyi yolakwika ndi body yolakwika limabweza 401. Pempho lokanidwa ndi status ya 4xx siliperekedwa mtengo.
Balance ndi flood protection
Pempho lililonse limatumikiridwa mofanana: palibe ma rate tier ndipo palibe API quota yosiyana. Zinthu ziwiri zikhozabe kuyankha 429, zonse ndi type rate_limit_error. Message imasiyanitsa.
Balance
Balance yanu ndi allowance ya plan ya lero, yomwe imayambiranso pa 00:00 UTC, ndipo pambuyo pake credit yomwe munagula. Chat ndi API zimatenga pa balance imodzi.
Pempho lililonse poyamba limasunga malire ake a output: max_tokens, kapena max_output_tokens pa /v1/responses, 4,096 ngati simutumiza iliyonse. Yankho likatha, kuchuluka komwe kunasungidwa kumasinthidwa ndi zomwe pempho lagwiritsa ntchito. Ngati kuchuluka kwake sikukwana mu balance yotsala, pempho limabweza Quota exceeded model isanayambe.
Ma pempho oyenda nthawi imodzi aliyense amasunga kuchuluka kwake. Choncho ma pempho ambiri nthawi imodzi okhala ndi max_tokens yayikulu amatha kuthetsa balance kwa kanthawi, ngakhale yankho lililonse ndi lalifupi. max_tokens yocheperako imasunga zochepa.
Flood protection
Flood protection imalola ma pempho 120 pa mphindi pa akaunti. Imawerenga akaunti, osati kiyi, pa /v1/chat/completions, /v1/responses ndi /v1/messages pamodzi. Mphindi imayamba ndi pempho lanu loyamba.
Pempho nambala 121 mkati mwa mphindi imeneyo limabweza Too many requests. Retry in <n>s. ndipo silimawononga chilichonse. Ma pempho okanidwa amawerengedwanso, choncho client yomwe imatumizanso popanda kudikira imakanidwabe mpaka mphindi itatha. Dikirani masekondi <n> ochokera mu message.
Ma pempho otumizidwa nthawi imodzi sakanidwa chifukwa chotumizidwa nthawi imodzi. Amadikira pamzere ndipo amayankhidwa motsatira.
GET /v1/models ndi ma endpoint owerengera ma token siiwerengedwa.
Zambiri pa malire ndi balance, ndi komwe mungaone balance ndi usage yanu: Malire ndi balance Keys & usage
Zoyenera kuyesanso
Tumizaninso pempho pokhapokha pempho lomwelo lingapambane. Ma OpenAI ndi Anthropic SDK amayesa kale mayankho a 429 ndi 5xx kawiri paokha; khazikitsani max_retries (maxRetries mu JavaScript) kuti musinthe izi.
| Cholakwika | Kutumizanso? | Motani |
|---|---|---|
400, 404, 405, 413, 415, 422 | Ayi. Konzani pempho poyamba. | Message imatchula field, model kapena input yolakwika. |
401 | Ayi. Gwiritsani kiyi yovomerezeka. | Pangani kapena sinthani kiyi patsamba la Keys & usage. |
429 Too many requests | Inde, mukadikira. | Dikirani chiwerengero cha masekondi chomwe chili mu message, kenako tumizani pempho lomwelo. |
429 Quota exceeded | Osati momwe lilili. | Chepetsani max_tokens, dikirani kuyambiranso kwa tsiku pa 00:00 UTC, kapena onjezani credit. |
429 Shannon Coder call quota reached | Inde, mu window yotsatira. | Ma window amayamba maola 4 aliwonse kuyambira 00:00 UTC. Model ina imayankha nthawi yomweyo. |
429 … temporarily busy | Inde. | Dikirani masekondi ochepa, kenako nthawi yayitali pa kuyesa kulikonse kotsatira. |
500, 502, 503 | Inde. | Dikirani sekondi 1, kenako 2, 4 ndi 8. Imani pambuyo pa kuyesa kochepa ndipo nenani id ya pempho. |
504 | Inde. | Tumizani ndi stream yokhazikitsidwa kukhala true: malire a nthawi amagwira ntchito pa mayankho osachita stream. |
Retry loop yotsatira table iyi:
import re
import time
import openai
from openai import OpenAI
# max_retries=0: this loop does the retrying, the SDK does not add its own.
client = OpenAI(api_key="YOUR_API_KEY", base_url="https://api.shannon-ai.com/v1", max_retries=0)
def create_with_retry(attempts=5, **request):
for attempt in range(attempts):
try:
return client.chat.completions.create(**request)
except openai.RateLimitError as error:
text = str(error)
flood = re.search(r"Retry in (\d+)s", text)
if flood:
time.sleep(int(flood.group(1))) # flood protection: wait out the minute
elif "temporarily busy" in text:
time.sleep(2 ** attempt)
else:
raise # balance: a wait of seconds does not help
except (openai.InternalServerError, openai.APIConnectionError):
time.sleep(2 ** attempt) # 1, 2, 4, 8 seconds
raise RuntimeError("the request failed after all attempts")
response = create_with_retry(
model="shannon-3",
messages=[{"role": "user", "content": "Hello"}],
)
print(response.choices[0].message.content) import OpenAI from "openai";
// maxRetries: 0 — this loop does the retrying, the SDK does not add its own.
const client = new OpenAI({ apiKey: "YOUR_API_KEY", baseURL: "https://api.shannon-ai.com/v1", maxRetries: 0 });
const sleep = (seconds) => new Promise((resolve) => setTimeout(resolve, seconds * 1000));
async function createWithRetry(request, attempts = 5) {
for (let attempt = 0; attempt < attempts; attempt++) {
try {
return await client.chat.completions.create(request);
} catch (error) {
const text = String(error.message);
const flood = text.match(/Retry in (\d+)s/);
if (error.status === 429 && flood) {
await sleep(Number(flood[1])); // flood protection: wait out the minute
} else if (error.status === 429 && text.includes("temporarily busy")) {
await sleep(2 ** attempt);
} else if (error.status >= 500 || error instanceof OpenAI.APIConnectionError) {
await sleep(2 ** attempt); // 1, 2, 4, 8 seconds
} else {
throw error; // wrong request, wrong key or balance
}
}
}
throw new Error("the request failed after all attempts");
}
const response = await createWithRetry({
model: "shannon-3",
messages: [{ role: "user", content: "Hello" }],
});
console.log(response.choices[0].message.content); Zolakwika mkati mwa stream
Stream imakhala ndi status 200 kuyambira byte yake yoyamba. Cholakwika chomwe chachitika pambuyo pake chimafika ngati event mkati mwa stream: chunk yokhala ndi member ya error pa /v1/chat/completions, event ya error pa /v1/messages, event ya response.failed kapena error pa /v1/responses. Ma event omaliza a stream amatsatirabe. Ganizirani kuti yankho lalephera ndipo tumizaninso pempho, monga pa status ya 5xx.
Zokambirana zazitali kuposa context window ya model zimanenedwa motere, ndi type invalid_request_error ndi code context_length_exceeded. Chepetsani zokambirana musanatumizenso.
Ma frame enieni afotokozedwa pamodzi ndi streaming. Kutsitsa nthawi yeniyeni
Ma id a pempho
Yankho lililonse, kuphatikiza zolakwika, lili ndi header ya x-request-id. Ndi mtengo womwe mwatumiza mu header yanu ya x-request-id, kapena zilembo 12 za hexadecimal ngati simunatumize. Ulembeni mu log, ndipo utchuleni mukapempha thandizo pa pempho.