DocumentazioneLimiti ed errori
Gestisci
Limiti ed errori
Cosa rifiuta l’API, perché, e cosa deve fare il tuo codice con ogni risposta.
In questa pagina
I limiti
| Limite | Valore | Oltre il limite |
|---|---|---|
| Richieste | 300 al minuto per chiave | 429, con l’attesa in retry-after |
| Richieste simultanee | Secondo il tuo piano | 429, nuovo tentativo dopo 5 secondi |
| Token al giorno, alla settimana o al mese | Secondo il tuo piano | 429, senza retry-after |
| Dimensione di una richiesta | 50 MB | Rifiutata |
Gli errori
| Codice | Causa | Cosa fare |
|---|---|---|
400 | Richiesta malformata o modello sconosciuto. Il messaggio indica il campo in questione. | Correggere la richiesta |
401 | Chiave mancante, sconosciuta, revocata o scaduta | Verificare la chiave, senza riprovare in loop |
403 | Modello o funzione non inclusi nel tuo piano | Cambiare modello o piano |
429 | Troppe richieste, o tetto di token raggiunto | Attendere retry-after se presente, altrimenti fermarsi |
502 | Il modello non ha risposto correttamente | Riprovare un po’ più tardi |
503 | Servizio temporaneamente non disponibile | Riprovare un po’ più tardi |
Il corpo di un errore contiene sempre un messaggio leggibile:
{
"error": {
"message": "model \"max\" is not part of this plan",
"type": "router_error"
}
}Riprovare correttamente
Riprova solo quando l’API ti dice quanto attendere. Un tetto di token raggiunto non si risolve insistendo.
import time
from openai import OpenAI, RateLimitError
client = OpenAI(
base_url="https://api.learnya.ai/v1",
api_key=os.environ["LEARNYA_API_KEY"],
max_retries=0,
)
def complete(**request):
for attempt in range(5):
try:
return client.chat.completions.create(**request)
except RateLimitError as error:
# The gateway says how long to wait.
# A spent token budget says nothing: stop there.
wait = error.response.headers.get("retry-after")
if wait is None:
raise
time.sleep(int(wait) + attempt)
raise RuntimeError("still limited after five attempts")import OpenAI, { RateLimitError } from "openai"
const client = new OpenAI({
baseURL: "https://api.learnya.ai/v1",
apiKey: process.env.LEARNYA_API_KEY,
maxRetries: 0,
})
async function complete(request) {
for (let attempt = 0; attempt < 5; attempt++) {
try {
return await client.chat.completions.create(request)
} catch (error) {
// The gateway says how long to wait.
// A spent token budget says nothing: stop there.
const wait =
error instanceof RateLimitError
? error.headers?.get("retry-after")
: null
if (!wait) throw error
await new Promise((done) =>
setTimeout(done, (Number(wait) + attempt) * 1000),
)
}
}
throw new Error("still limited after five attempts")
}import OpenAI, { RateLimitError } from "openai"
import type {
ChatCompletion,
ChatCompletionCreateParamsNonStreaming,
} from "openai/resources"
const client = new OpenAI({
baseURL: "https://api.learnya.ai/v1",
apiKey: process.env.LEARNYA_API_KEY,
maxRetries: 0,
})
async function complete(
request: ChatCompletionCreateParamsNonStreaming,
): Promise<ChatCompletion> {
for (let attempt = 0; attempt < 5; attempt++) {
try {
return await client.chat.completions.create(request)
} catch (error) {
// The gateway says how long to wait.
// A spent token budget says nothing: stop there.
const wait =
error instanceof RateLimitError
? error.headers?.get("retry-after")
: null
if (!wait) throw error
await new Promise((done) =>
setTimeout(done, (Number(wait) + attempt) * 1000),
)
}
}
throw new Error("still limited after five attempts")
}