What is the X-RateLimit-Remaining header?
X-RateLimit-Remaining reports how many requests (or other quota units) the client has left in the current rate limit window. It is one of the de facto trio with X-RateLimit-Limit and X-RateLimit-Reset. It has never been standardized and is not in the IANA HTTP Field Name Registry.
The IETF draft draft-ietf-httpapi-ratelimit-headers first standardized it as RateLimit-Remaining and now carries the same number as the r parameter of RateLimit, tied to a named policy in RateLimit-Policy.
API Usage & Best Practices
- It is the field clients actually pace on. A well-behaved client watches
X-RateLimit-Remainingfall and slows down before it reaches zero, instead of discovering the limit through a429 Too Many Requests. Send it on successful responses, not only on rejections, or it cannot do that. - Make it true across your fleet. If each gateway node counts separately, the value is each node’s share, and a client that reads
40can still be rejected by the next node it reaches. Tyk has exactly this behavior with its default distributed limiter. Count in a shared store, or document that the number is approximate. - Report the tightest limit. With several limits in force (per second and per day, say), a single integer can only describe one. Report the one closest to exhaustion; better, emit
RateLimitandRateLimit-Policy, which report each limit by name. - A positive value is a hint, not a promise. The server may still reject a request with quota left, under load or because a different limit applies. Clients must handle a 429 regardless of what the header said.
- Clients: zero means wait for the reset, not retry now. When it reaches
0, wait until the time given byX-RateLimit-Reset(in whatever format that API uses) or byRetry-Afterif present. If both are present, followRetry-After, as the IETF draft requires for its own fields. - Clients: check the response is fresh. A value from a cached response is stale. The IETF draft tells clients to ignore
RateLimiton cached responses, and the same reasoning applies to theX-form. - New APIs: use
RateLimitinstead.RateLimit: "default";r=42;t=37carries the remaining count and the window it applies to in one field.
Examples
A client with 42 requests left in the current window:
HTTP/1.1 200 OKX-RateLimit-Limit: 100X-RateLimit-Remaining: 42X-RateLimit-Reset: 37The client has run out. Retry-After tells it exactly how long to wait:
HTTP/1.1 429 Too Many RequestsRetry-After: 37X-RateLimit-Limit: 100X-RateLimit-Remaining: 0X-RateLimit-Reset: 37The current IETF equivalent of the first response:
HTTP/1.1 200 OKRateLimit-Policy: "default";q=100;w=60RateLimit: "default";r=42;t=37