API Engineering

API Rate Limiting: How HTTP 429, Headers, and Token Buckets Work

Understand API rate limiting algorithms: token bucket, leaky bucket, and sliding window. How to parse X-RateLimit headers and handle HTTP 429 backoff.

By Cometflow API Engineering Team • Updated 2026-10-06

Rate limiting protects API backends from noisy tenants, credential stuffing, and accidental infinite loops. When a client exceeds its quota, the server responds with HTTP 429 Too Many Requests alongside machine-readable reset headers.

1. Standard Rate Limit Response Headers

HTTP/1.1 429 Too Many Requests
RateLimit-Limit: 100
RateLimit-Remaining: 0
RateLimit-Reset: 45
Retry-After: 45
Content-Type: application/json

{
  "error": "rate_limit_exceeded",
  "message": "Quota of 100 requests per minute exceeded. Retry after 45 seconds."
}

2. Client-Side Exponential Backoff with Jitter (TypeScript)

export async function fetchWithBackoff(url: string, maxRetries = 4): Promise {
  for (let attempt = 0; attempt <= maxRetries; attempt++) {
    const res = await fetch(url);
    if (res.status !== 429 || attempt === maxRetries) return res;

    const retryHeader = res.headers.get('Retry-After');
    const baseDelayMs = retryHeader ? Number(retryHeader) * 1000 : Math.pow(2, attempt) * 1000;
    const jitterMs = Math.random() * 500;
    await new Promise(resolve => setTimeout(resolve, baseDelayMs + jitterMs));
  }
  throw new Error('Unreachable');
}

Frequently Asked Questions (FAQ)

Why is random jitter important during retries?

Without random jitter, 1,000 rate-limited clients that failed at the exact same second will all wake up and retry simultaneously at T+2s, T+4s, and T+8s, causing a synchronized thundering herd spike.