Error handling
Handle API errors, retries, and rate limits safely.
EUrouter uses standard HTTP status codes. Read the response body for the error message and keep the request ID from the response headers when contacting support.
| Status | Meaning | Recommended action |
|---|---|---|
400 | Invalid request | Fix the request; do not retry unchanged. |
401 | Missing or invalid API key | Check the Authorization header and key. |
402 | Insufficient credits | Add credits or change your billing setup. |
404 | Resource or model not found | Check the endpoint and live model catalog. |
408 | Request timed out | Retry with backoff if the operation is safe to replay. |
429 | Rate limit reached | Wait, honor Retry-After when present, then retry. |
5xx | Temporary service or provider error | Retry with exponential backoff and jitter. |
Retry example
async function requestWithRetry(url, options, maxAttempts = 4) {
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
const response = await fetch(url, options);
if (response.ok) return response;
const retryable = response.status === 408 || response.status === 429 || response.status >= 500;
if (!retryable || attempt === maxAttempts) {
const body = await response.text();
throw new Error(`EUrouter error ${response.status}: ${body}`);
}
const retryAfter = Number(response.headers.get("retry-after"));
const delay = Number.isFinite(retryAfter) ? retryAfter * 1_000 : 500 * 2 ** (attempt - 1) + Math.random() * 250;
await new Promise((resolve) => setTimeout(resolve, delay));
}
}Only retry operations that are safe to replay. For streaming requests, retry only before the first response token arrives.