Rate Limit Request and Token Counting
AISIX can enforce limits on caller API keys, models, and matching policies at the same time. Each layer counts the traffic it owns: caller layers count downstream requests, while direct models can count separate routing attempts.
Token limits behave differently because providers report usage after a response completes. Fixed-window boundaries, estimated usage, retries, failover, and refunds all determine when capacity is consumed or returned.
Follow One Request Through the Limits
Suppose a caller sends one request to model group G. The group tries model A on OpenAI, retries A once, and then succeeds through model B on Anthropic:
The single downstream request produces these counts:
| Limit | Count |
|---|---|
| Caller API key | 1 |
| Model group G | 1 |
| Direct model A | 2 |
| Direct model B | 1 |
This distinction is the foundation for the model, retry, and conditional-policy rules below.
Rate Limit Fields and Windows
Caller API keys and model aliases use the same rate_limit fields. Each field is optional. When a field is omitted, AISIX does not enforce that dimension.
| Field | Limit | Window |
|---|---|---|
rps | Requests per second | 1 second |
rpm | Requests per minute | 60 seconds |
rph | Requests per hour | 3,600 seconds |
rpd | Requests per day | 86,400 seconds |
tpm | Tokens per minute | 60 seconds |
tpd | Tokens per day | 86,400 seconds |
concurrency | In-flight requests | Not windowed |
With the default window_type: fixed, each window aligns to the UTC clock rather than to a caller's first request. Counts reset every second for rps, at the top of the minute for rpm and tpm, at the top of the hour for rph, and at 00:00 UTC for rpd and tpd. The daily reset is 08:00 in UTC+8.
Because the complete count resets at once, bursts on both sides of a boundary can admit up to twice the limit within one window's length. For example, with rpm: 100, AISIX admits 100 requests at 10:00:50 and another 100 at 10:01:05.
Sliding Window Behavior
A sliding window records usage in fixed sub-buckets and checks each request against the sub-buckets that cover one complete window:
| Fields | Window | Sub-Bucket Step |
|---|---|---|
rpm, tpm | 60 seconds | 10 seconds |
rph | 3,600 seconds | 5 minutes |
rpd, tpd | 86,400 seconds | 1 hour |
No interval of one window's length admits more requests than a request limit. Token limits check only recorded usage, so requests in flight can still cause a token overrun.
Because usage is grouped into fixed sub-buckets, it can remain counted for up to one step beyond the window length. For example, an rpm request recorded at 10:00:03 stops counting at 10:01:10.
The difference appears at a boundary. With rph: 10, suppose a caller sends 10 requests between 10:55 and 10:59, then 10 more starting at 11:00:
- With
fixed, the hourly count resets at 11:00, so all 20 requests are admitted within about ten minutes. - With
sliding, the 11:00 calculation still includes the requests from 10:55, so the second burst is rejected until those requests leave the window.
Rate-limit headers expose the sliding window's current state:
- On rejection,
Retry-AfterandX-RateLimit-Resetreport the whole-second wait until a request can be admitted, rounded up with a minimum of1. - For
tpmandtpd, the wait ends when recorded usage drops below the limit because the rejected request's token usage is not yet known. - When several limits are configured, the headers describe the limit that rejected the request.
- Successful Chat Completions responses use
x-ratelimit-reset-requestsandx-ratelimit-reset-tokensfor the time until the current count leaves the window.
AISIX checks and records the three limit dimensions at different points while processing a request:
| Dimension | When AISIX Checks It | When AISIX Records Usage |
|---|---|---|
| Request count | Before the provider call | During the pre-provider checks. When a later layer rejects the request, the counts the earlier layers took are returned; see How Requests Are Counted |
| Tokens | Before the provider call, using only tokens already recorded in the current window. Nothing is reserved for the request | After the response completes, using the usage the upstream reports |
| Concurrency | Before the provider call | Held until the response or stream completes, then released |
Token limits include prompt and completion tokens. For Anthropic traffic, they also include prompt cache creation and cache read tokens, which Anthropic reports separately from input tokens. OpenAI cached tokens are already included in prompt tokens, so AISIX does not count them twice.
Because the provider reports usage after generating a response, AISIX records a request's tokens only once it completes. Admission checks the tokens recorded so far, so the requests in flight are not counted against the limit. Usage can therefore exceed the limit, as described in Token Limit Overruns.
Token counters advance only when the upstream reports usage or the gateway estimates it. The following calls neither report nor estimate token usage, so they add nothing to tpm or tpd:
whisper-1transcriptions and translations- Text-to-speech requests
dall-e-2anddall-e-3image generations and edits- Video requests
Models that report token usage, such as gpt-4o-transcribe and gpt-image-1, are counted normally. Audio duration is recorded for cost calculation only. A call that adds no tokens is still rejected when an applicable token window is already exhausted.
Token limits attached directly to caller API keys or models support minute and day windows only. These resources have no per-second or per-hour token field.
Classic rate-limit policies also enforce token caps only for minute and day windows. For AISIX Enterprise and Cloud, the Admin API rejects a token cap paired with a second or hour window. The AISIX OSS resources schema accepts those combinations, but the gateway does not enforce the token cap.
Estimated Usage and Token Limits
By default, estimated tokens count toward token limits like reported tokens. In AISIX Enterprise and Cloud, open Settings → Policies and find the Estimated usage card to change this behavior. Enable Keep estimated requests out of budgets and token rate limits to exclude them.
The same setting excludes estimated usage from budgets. See Leave Estimated Usage Out of Budgets for permissions and activation timing.
While the setting is on, a request whose usage the gateway estimated, even partly, adds no tokens to any token window: tpm, tpd, and a classic policy's max_tokens. Request-count limits and concurrency still count it, and the admission check before the provider call is unchanged. The usage event still records the estimated tokens. A request whose usage the upstream reported counts as before.
A stream the caller closes before the upstream reports usage is estimated too, so it adds no tokens either. Turn the setting on only when every caller is trusted.
Gateways earlier than 1.11.0 do not recognize the setting and keep counting estimated tokens toward token limits. Upgrade the gateways before relying on it.
Token Limit Overruns
When recorded usage reaches a token limit, AISIX rejects new requests, but every request it has already admitted still completes and is counted. Under concurrency, the overrun is therefore not limited to the one request that crossed the limit. It is roughly the number of requests in flight when the limit is reached, multiplied by the largest token usage of one request.
For example, with 10 requests in flight that each use at most 40 tokens, usage can exceed a tpm limit by about 400 tokens.
The number of requests in flight depends on the counter backend:
- With Redis, count the in-flight requests across all gateway instances that share the backend.
- With the memory backend, each instance counts only its own traffic, so multiply the overrun by the number of instances. The limit itself also applies per instance, as described in Rate Limit Counter Storage.
A concurrency limit caps requests in flight, reducing the overrun; excess requests receive HTTP 429 instead of waiting in a queue.
AISIX does not enforce a strict token cap. Use this figure to size a limit, not as a guarantee.
How Model Limits Apply
If a request matches both caller and model limits, AISIX counts it against the request-count and concurrency limits of both buckets. Exhausting either bucket rejects the request. Tokens are not reserved in either bucket; see Token Limit Overruns.
Model limits also apply when a direct model serves as the target of a routing model or semantic router.
A routing model skips a target that is over its own limit and continues with the remaining targets. The request returns 429 when every target is over its limit. A semantic router enforces the selected target's limit and returns 429 when that target has no capacity. How Requests Are Counted explains how retries and failover affect each layer.
Inject-mode passthrough routes can also reserve model limits. AISIX does this when the request body names a model that uses the same provider as the route's provider key, using either its alias or provider-native name.
The matched model shares request-count and concurrency buckets with modeled routes. Routes using forward_client never perform this model lookup. Caller-level request-count and concurrency limits apply to every route.
An applicable tpm or tpd window that is already exhausted still rejects a passthrough request. Passthrough usage does not advance those token counters, however, even when AISIX detects a recognized envelope and records token usage on the request's usage event.
How Requests Are Counted
A limit counts the requests that arrive at what it limits. Retries and failovers that a model performs on its own are that model's internal behavior and add nothing to the count.
- Caller limits count once per request the caller sends. These are the caller API key's own limit, the classic policies scoped to an API key, team, member, or each member in a team, and MCP server limits.
- A model group or semantic router counts once per request addressed to it. Its retries and failovers are internal. When a semantic router picks a model group as its target, that group also counts once for the request.
- A direct model counts once per request it receives:
- A direct call counts once, regardless of internal retries.
- An attempt from a model group counts separately, including a retry to the same target.
- When a target is over its own limit, the group skips it and tries another target.
- Direct and routed requests to the same model share its bucket.
- A conditional policy follows the request layer its conditions and buckets resolve to:
- It counts once per request when the caller or addressed group settles the match. Examples include
member in {U} AND model in {G},model in {G, A}, andmodel in {G} OR provider == openai. - It counts once per direct-model attempt when the selected target settles the match. Examples include
provider == openai,model == G AND NOT provider == X, andmodel ~= M. - A
group_bycontainingmodel,provider, orprovider_keyalso counts once per attempt because its buckets belong to the direct target, not the group.
- It counts once per request when the caller or addressed group settles the match. Examples include
These limits refer to AISIX model entries, by ID or alias, never to a provider-side model name. A classic model policy counts like the model it names. Token limits are charged once per request, with the usage of the attempt that served it.
For the same A, A, B request shown above, conditional policies count as follows:
| Limit | Counted |
|---|---|
Policy member in {U} AND model in {G}, group_by: [member] | 1, in that member's bucket |
Policy model == G, group_by: [model] | 2 in A's bucket, 1 in B's |
Policy model == G AND provider == openai | 2, for A's two OpenAI attempts; B uses Anthropic |
If the same key calls A directly and A is retried twice before it succeeds, the request counts 1 against A's own limit.
Typical setups:
- To protect a provider deployment's quota, put the limit on the direct model that serves it.
- To cap each member's use of a group regardless of failover, match
model in {G}in a policy withgroup_by: [member]. - To give each target of a group its own cap from one policy, match
model == Gwithgroup_by: [model].
A request refused before any upstream attempt consumes no quota at any layer. When a later layer refuses a request, AISIX returns the counts already taken by earlier layers.
The same rule applies when every target in a model group is over its limit. If no attempt reaches an upstream, the resulting 429 does not charge the caller key, the group, or request-level policies.
An attempt sent upstream always counts, including one that returns a 5xx or upstream 429. Its request also keeps the counts already taken when a later target is refused by its own limit. AISIX also keeps a count when its window rolls over before the refund or the refund cannot reach Redis.
The request logs show the same distinction. In the Dashboard, open Logs. A retried or failed-over request appears as one row per attempt, which lines up with the target model counters. Select Last attempt per request to see one row per downstream request instead, as described in Show the Last Attempt of Each Request.
Troubleshooting
| Symptom | What to Check |
|---|---|
| A target model runs out of capacity sooner than the downstream request count suggests. | A direct model counts every attempt dispatched to it, including retries and failovers, while the caller key and routing model count each downstream request once. Review How Requests Are Counted. |
| A request succeeds and the next request is token-limited, or recorded usage exceeds the configured limit. | Token usage is recorded after completion, so every request already in flight can finish and add usage. Review Token Limit Overruns. |
Next Steps
Return to API Key and Model Rate Limits to configure a direct limit. Use Rate Limit Policies for conditional matching and independent buckets.