DokumentationLimits und Fehler
Betreiben
Limits und Fehler
Was die API ablehnt, warum, und wie Ihr Code mit jeder Antwort umgehen soll.
Auf dieser Seite
Die Limits
| Limit | Wert | Bei Überschreitung |
|---|---|---|
| Anfragen | 300 pro Minute und Schlüssel | 429, mit der Wartezeit in retry-after |
| Gleichzeitige Anfragen | Je nach Plan | 429, neuer Versuch nach 5 Sekunden |
| Tokens pro Tag, Woche oder Monat | Je nach Plan | 429, ohne retry-after |
| Grösse einer Anfrage | 50 MB | Abgelehnt |
Die Fehler
| Code | Ursache | Was tun |
|---|---|---|
400 | Fehlerhafte Anfrage oder unbekanntes Modell. Die Meldung nennt das betroffene Feld. | Anfrage korrigieren |
401 | Schlüssel fehlt, ist unbekannt, widerrufen oder abgelaufen | Schlüssel prüfen, nicht endlos wiederholen |
403 | Modell oder Funktion nicht in Ihrem Plan enthalten | Modell oder Plan wechseln |
429 | Zu viele Anfragen oder Token-Limit erreicht | Die Zeit in retry-after abwarten, falls vorhanden, sonst aufhören |
502 | Das Modell hat nicht korrekt geantwortet | Etwas später erneut versuchen |
503 | Dienst vorübergehend nicht verfügbar | Etwas später erneut versuchen |
Der Body eines Fehlers enthält immer eine lesbare Meldung:
{
"error": {
"message": "model \"max\" is not part of this plan",
"type": "router_error"
}
}Richtig wiederholen
Versuchen Sie es nur erneut, wenn die API Ihnen sagt, wie lange Sie warten sollen. Ein erreichtes Token-Limit lässt sich durch Wiederholen nicht lösen.
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")
}