Skip to main content
504Retryable

Google Gemini 504 Deadline Exceeded

The service couldn't finish within the deadline — usually a very large prompt or context exceeding the processing window.

Most likely causes

  1. 1.Huge multimodal input (long video/audio) in a single call
  2. 2.Very large context with non-streaming response

Fix checklist

  • Set a larger client timeout and stream the response
  • Chunk large media; use the Files API rather than inline data
  • Reduce per-call output size

Retry guidance

Retry once with streaming and a longer deadline; identical calls will deadline again.

// Retry 504 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 !== 504) 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 504: still failing after backoff — check https://status.cloud.google.com");
}

Provider status page: status.cloud.google.com