Production

Handle errors and retries

Use the HTTP status and OpenAI-compatible error object to decide whether to fix the request, refresh state, or retry later.

Error guide

StatusMeaningAction
400Invalid request or model IDFix the payload; fetch /v1/models for the exact model ID.
401Missing or invalid API keyCheck the Bearer token and replace a revoked or exposed key.
402 / 429Balance, quota, or rate limit reachedReview dashboard balance or limits. Respect Retry-After when it is present.
502 / 503Transient upstream or service availability issueRetry a safe request with bounded exponential backoff; contact support if it persists.

Bounded retries

Retry only transient responses, keep attempts finite, and add jitter so concurrent clients do not retry in lockstep. Prefer the server-provided Retry-After value for a 429 response.

typescript
async function requestWithRetry(makeRequest: () => Promise<Response>) {
  for (let attempt = 0; attempt < 3; attempt += 1) {
    const response = await makeRequest();
    if (response.ok || ![429, 502, 503].includes(response.status)) return response;

    const retryAfter = Number(response.headers.get("retry-after"));
    const delay = Number.isFinite(retryAfter) && retryAfter > 0
      ? retryAfter * 1_000
      : 500 * 2 ** attempt + Math.random() * 250;
    await new Promise((resolve) => setTimeout(resolve, delay));
  }
  throw new Error("Gonka24 request failed after retries");
}

Do not retry

Do not automatically retry 400 or 401 responses; they require a request or credential fix. Also do not replay a completed model response if your application may execute a payment, write, or other external side effect from its output.