Skip to main content
499Retryable

Google Gemini 499 Cancelled

The client cancelled the request before the server finished — Google surfaces your own disconnect as 499/CANCELLED in logs.

Most likely causes

  1. 1.Client timeout shorter than generation time
  2. 2.User navigated away / AbortController fired mid-stream

Fix checklist

  • Raise client timeouts for long generations
  • Stream so partial output arrives before any timeout
  • Treat as client-side: check your abort logic before blaming the API

Retry guidance

Retry is fine if the cancellation was unintended; fix the timeout that caused it first.

// Retry 499 with exponential backoff + full jitter.
async function callWithBackoff(payload: unknown, maxAttempts = 5) {
  for (let attempt = 0; attempt < maxAttempts; attempt++) {
    const res = await fetch("https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro:generateContent", {
      method: "POST",
      headers: {
        "content-type": "application/json",
        "x-goog-api-key": process.env.GEMINI_API_KEY!,
      },
      body: JSON.stringify(payload),
    });
    if (res.status !== 499) return res;
    // Honor Retry-After when present; otherwise exponential backoff, capped at 32s.
    const retryAfter = Number(res.headers.get("retry-after"));
    const base = Number.isFinite(retryAfter) && retryAfter > 0
      ? retryAfter * 1000
      : Math.min(1000 * 2 ** attempt, 32_000);
    await new Promise((r) => setTimeout(r, base * (0.5 + Math.random() * 0.5)));
  }
  throw new Error("Google Gemini 499: still failing after backoff — check https://status.cloud.google.com");
}

Provider status page: status.cloud.google.com