Skip to main content

HTTP 520 Web Server Returned an Unknown Error

Cloudflare-specific: the origin returned an empty, malformed or unexpected response.

5xx · Server error✓ retryable with backoff

In AI APIs specifically

Several AI providers sit behind Cloudflare; 520s indicate origin-side trouble that Cloudflare can't classify.

Fix checklist

  • Retry with backoff.
  • Check the provider status page.
  • Capture the CF-Ray header for support escalation.

Retry handler (TypeScript)

async function fetchWithRetry(url: string, init: RequestInit, maxRetries = 5) {
  for (let attempt = 0; ; attempt++) {
    const res = await fetch(url, init);
    // 520 is retryable — back off and try again.
    if (res.status !== 520 || 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: official documentation