Skip to main content

HTTP 502 Bad Gateway

A gateway or proxy received an invalid response from the upstream server.

5xx · Server error✓ retryable with backoff

In AI APIs specifically

Often transient infrastructure churn at the provider's edge (deploys, scaling events). With self-hosted gateways/proxies in front of AI APIs, check your own proxy first.

Fix checklist

  • Retry with backoff — usually transient.
  • If you run a proxy (LiteLLM, nginx), check its upstream timeout settings.
  • Check the provider status page for edge incidents.

Retry handler (TypeScript)

async function fetchWithRetry(url: string, init: RequestInit, maxRetries = 5) {
  for (let attempt = 0; ; attempt++) {
    const res = await fetch(url, init);
    // 502 is retryable — back off and try again.
    if (res.status !== 502 || attempt >= maxRetries) return res;
    const retryAfter = Number(res.headers.get("retry-after"));
    const delay = Number.isFinite(retryAfter) && retryAfter > 0
      ? retryAfter * 1000
      : Math.min(60_000, 1000 * 2 ** attempt) * (0.5 + Math.random()); // expo backoff + jitter
    await new Promise((r) => setTimeout(r, delay));
  }
}

Spec: RFC reference