DocumentationLimits and errors
Operate
Limits and errors
What the API rejects, why, and what your code should do with each response.
On this page
Limits
| Limit | Value | When exceeded |
|---|---|---|
| Requests | 300 per minute per key | 429, with the wait time in retry-after |
| Concurrent requests | Depends on your plan | 429, retry after 5 seconds |
| Tokens per day, week or month | Depends on your plan | 429, without retry-after |
| Request size | 50 MB | Rejected |
Errors
| Code | Cause | What to do |
|---|---|---|
400 | Malformed request or unknown model. The message names the field at fault. | Fix the request |
401 | Key missing, unknown, revoked or expired | Check the key, without retrying in a loop |
403 | Model or feature not included in your plan | Change model or plan |
429 | Too many requests, or token cap reached | Wait for retry-after if present, otherwise stop |
502 | The model did not respond correctly | Retry a little later |
503 | Service temporarily unavailable | Retry a little later |
An error body always carries a human-readable message:
{
"error": {
"message": "model \"max\" is not part of this plan",
"type": "router_error"
}
}Retry cleanly
Retry only when the API tells you how long to wait. Retrying will not lift a token cap.
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")
}