# Rate Limit limits and errors

## Limitations

- Memory counts live in one process. Do not use them in production on scaled-out or per-request hosts. On several Node instances, turn on KV so the `kv` driver shares the counts. See [Share counts on Node](https://vitehub.dev/docs/rate-limit/hosts#share-counts-on-node).
- A production build for an unknown host must set a driver or use a direct Rate Limiter.
- The request identity defaults to the client address in the event's Standard Context, and explicit user or tenant identities remain application policy.
- Managed policies must be static, so ViteHub can create the driver resources.
- The package has only atomic consume. Providers cannot all check without consuming.
- A refund gives back one request in the current window. When a window ends between the request and its refund, the refund goes to the new window.

## Cloudflare limits

The [Cloudflare Rate Limiting binding](https://developers.cloudflare.com/workers/runtime-apis/bindings/rate-limit/) accepts a `period` of 10 or 60 seconds and is best-effort. A Rate Limit with another window counts in the default KV store. A Cloudflare build fails when a policy needs more:

| Policy | Code |
| --- | --- |
| `enforcement: 'strict'` with a `10s` or `1m` window | `RATE_LIMIT_R0015` |
| `window` other than `10s` or `1m`, for example `30s` or `1h`, and the default KV store has no shared atomic `increment`, such as Cloudflare KV | `RATE_LIMIT_R0016` |

The dev server counts in memory, but checks the same policies when the build targets Cloudflare: `vitehub({ host: 'cloudflare' })`, `hubRateLimit({ driver: 'cloudflare' })`, or the Cloudflare Nitro preset with no driver set. Then `vite dev` fails to start with the same code, and saving a file with an unsupported policy logs the error. Without a Cloudflare target, the dev server accepts every valid policy.

## Memory limits

A production build that counts in memory fails with `RATE_LIMIT_MEMORY_STRICT_UNSUPPORTED` when a Rate Limit sets `enforcement: 'strict'`, because each server process allows the full limit. `vite dev` and app tests run one process, so they accept strict Rate Limits in memory.

The memory driver keeps at most 100,000 counters in one process. It removes counters whose window has ended before it adds a new one. When every counter is still active, the next new key fails with `RATE_LIMIT_R0008`. Pass `maxEntries` to `memoryRateLimitDriver()` to change the cap. Counters are lost when the process restarts, and two processes count separately.

## Errors

A request over the limit is not an error code. `requireRateLimit()` answers `429`, and an Agent Invocation fails with `RATE_LIMIT_REJECTED`. The codes below are defects in a policy, a call, or a driver. Each one is a Nostics diagnostic with a fix.

### Policies

The build reads the policy of each `checkRateLimit()`, `refundRateLimit()`, and `requireRateLimit()` call. A policy that it cannot read or accept stops `vite dev` and `vite build` with `RATE_LIMIT_R0001`, before a request arrives. `createRateLimiter()` checks its policy when you call it and throws `RATE_LIMIT_R0031` to `RATE_LIMIT_R0036`.

| Code | Cause |
| --- | --- |
| `RATE_LIMIT_R0001` | A policy is not a static object literal, or one of its values is wrong. The message names the file, line, and column. |
| `RATE_LIMIT_R0002` | A source file does not parse while ViteHub collects policies. |
| `RATE_LIMIT_R0003` | Two calls use the same ID with different policies. The message lists both locations. |
| `RATE_LIMIT_R0031`, `RATE_LIMIT_R0032` | `window` is not a string, or it is not greater than zero. A string that is not a duration, such as `10s`, `1m`, `1h`, or `1d`, fails with [`RUNTIME_DURATION_INVALID`](https://vitehub.dev/docs/runtime/diagnostics/runtime_duration_invalid). |
| `RATE_LIMIT_R0033`, `RATE_LIMIT_R0034` | The policy is not an object, or `limit` is not a positive integer. |
| `RATE_LIMIT_R0035`, `RATE_LIMIT_R0036` | `enforcement` is not `'best-effort'` or `'strict'`, or `failure` is not `'allow'` or `'deny'`. |

### Calls

| Code | Cause |
| --- | --- |
| `RATE_LIMIT_R0009` | The Cloudflare Rate Limiting binding of the policy is not on the request. Deploy the generated Wrangler configuration. |
| `RATE_LIMIT_R0010` | `checkRateLimit()`, `refundRateLimit()`, or `requireRateLimit()` has an empty ID. |
| `RATE_LIMIT_R0011`, `RATE_LIMIT_R0012` | The call has an unknown option, or `key` is an empty string. |
| `RATE_LIMIT_R0013` | The request has no client address and the call has no `key`. Pass a user or tenant key. |
| `RATE_LIMIT_R0030`, `RATE_LIMIT_R0038` | A direct Rate Limiter call has an empty key. |

### Drivers

| Code | Cause |
| --- | --- |
| `RATE_LIMIT_R0027` | The policy asks for `enforcement: 'strict'`, and the driver is best-effort. |
| `RATE_LIMIT_R0028` | The driver does not support the window of the policy. The message lists the windows it supports. |
| `RATE_LIMIT_R0029` | `createRateLimiter()` has no driver, or its options cannot be read. |
| `RATE_LIMIT_R0004` to `RATE_LIMIT_R0006` | The Cloudflare driver has no binding, the binding failed, or it returned an invalid result. A failed binding follows the `failure` of the policy. |

## KV limits

The `kv` driver reads the current counter generation and uses one atomic `increment` for each request. Counter keys include the policy ID, window length, client key, and window start. Cloudflare adds the deployment namespace. A reset advances the generation so in-flight refunds cannot credit the new counter. Previous counters expire by TTL.

- Rejected requests also increment the counter, so `used` can be larger than `limit`. The manifest says `rejectedAttempts: "counted"`.
- Each instance computes the window start from its own clock. Keep the server clocks in sync, or requests at the end of a window can count in the next one.
- Each individual KV storage call has a one-second timeout, so an operation that makes several calls can take longer. When a call fails or times out, `consume` follows the `failure` policy (`deny` returns `503`, `allow` lets the request continue), and `peek`, `refund`, and `reset` return `unavailable`. The deadline does not cancel the KV write (including `consume`, `refund`, or `reset`), so `unavailable` does not tell callers whether a write took effect. A KV configuration error, such as a missing `REDIS_URL` for an explicit `redis` store, throws.
- `refund()` adds one to a second key, `<counter key>:refunds`, because KV has no atomic decrement. `consume()` reads that key only when the counter passes the limit, so the `used` and `remaining` of an allowed request do not include refunds. `peek()` includes them. Two refunds at the same time can give back one request more than the window counted.
- `peekRateLimit()` and `resetRateLimit()` read the window from the policies that this process used. In a process that has not used the Rate Limit yet, `peek` returns `unused` and `reset` returns `unsupported`.
- An explicit `driver: 'kv'` on a KV store without a shared atomic increment, such as `fs-lite`, fails the build with `RATE_LIMIT_KV_STORE_UNSUPPORTED`. A runtime that `setRateLimitRuntimeConfig({ driver: 'kv' })` configures fails with `RATE_LIMIT_KV_RUNTIME_STORE_UNSUPPORTED` on such a store, unless it also sets `memoryFallback: true`.
- Without a KV store, the `kv` driver fails with `RATE_LIMIT_R0047`. `kvRateLimitDriver()` without `increment` and `get` fails with `RATE_LIMIT_R0046`.

<!-- vitehub-docs:section-links -->
## In this section

- [Overview](https://vitehub.dev/raw/docs/rate-limit.md)
- [Get started](https://vitehub.dev/raw/docs/rate-limit/get-started.md)
- [Server API](https://vitehub.dev/raw/docs/rate-limit/server-api.md)
- [Agent capability](https://vitehub.dev/raw/docs/rate-limit/agent-capability.md)
- [Hosts](https://vitehub.dev/raw/docs/rate-limit/hosts.md)
- [Limits and errors](https://vitehub.dev/raw/docs/rate-limit/limits-and-errors.md) (this page)

Next: [RATE_LIMIT_MEMORY_PER_PROCESS](https://vitehub.dev/raw/docs/rate-limit/diagnostics/rate_limit_memory_per_process.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
