Token-based rate limiting for LLM APIs in Apache APISIX

Token-based rate limiting in Apache APISIX caps LLM spend per team or customer using the ai-rate-limiting plugin, with counters shared across gateway nodes through Redis.

On this page

How token-based rate limiting caps LLM spend in APISIX

When several teams or customers call paid LLM APIs through one gateway, the provider invoice arrives as a single number with no owner attached. The ai-rate-limiting plugin in Apache APISIX lets you place a ceiling on that consumption before requests reach the provider. It counts tokens rather than requests, so it can limit total tokens, prompt tokens or completion tokens within a time window. You can apply the limit to each LLM instance, so one provider or model has its own budget, or to each consumer, so each team or customer has its own. When a consumer exhausts its quota, the plugin returns HTTP 503 by default, and you can confirm in the plugin documentation which rejection status and fields your version uses before you write client error handling.

Request-based limits such as a fixed number of calls per minute do not reflect LLM cost, because one request can carry a short question or a very long document and produce a very different token count. Token-based limits fit when the goal is to bound spend or capacity per team, product or customer, and when prompts vary widely in size. Request limits still help against bursts and abuse. Most teams combine both, with the request limit protecting the gateway and the token limit protecting the budget. Decide which windows match your chargeback or finance cycle, which consumers need separate budgets, and what should happen when a team reaches its limit mid-month.

Before you rely on these limits, confirm three things with your own tests: how your chosen models report token usage, which window types and reset behavior your APISIX version supports, and how the plugin behaves when the counter store is unavailable. The plugin's budget counts are only as accurate as the usage data returned by each provider, so the gateway enforces a budget but does not guarantee the cost figure on any invoice. Treat the gateway limit as a control you test, not a promise about the final bill.

Choosing what to count: total, prompt or completion tokens

Counting total tokens adds prompt and completion tokens into one budget for each window. It is the simplest limit to explain to a finance team, because a single number tracks how much model capacity a team or customer has used. The trade-off is that one number hides which direction the cost is growing, so a long prompt and a long answer can exhaust the same budget in different ways.

  • Simple to communicate: one allowance per window maps easily to a monthly or daily usage cap.
  • Blind to direction: a team that sends very large prompts and one that requests long answers can hit the same limit with different cost profiles.
  • Good default for broad caps: it works well as an outer ceiling above more granular limits.
  • Consistent across providers: it avoids differences in how each provider splits input and output charges.
  • Dependent on reported usage: enforcement follows the token counts the upstream returns, so confirm how your chosen providers report usage.

Before you pick this option, check whether your provider's invoices weight input and output differently, because a single total may not match your actual spend.

Configuration options and when each fits

This table compares the main ai-rate-limiting configuration keys, the limit type, time window, scope, rejection status and counter backend, and shows when each setting fits a spending cap. Read each row as a decision: pick the value that matches how your teams and customers are billed, then confirm the exact key names and accepted values in the plugin documentation for your APISIX version.

Configuration keyOptions to weighWhen it fitsWhat to confirm
Limit typeTotal tokens, prompt tokens or completion tokensTotal tokens fits a single shared budget. Prompt or completion limits fit cases where input size or output length drives your provider cost differently.Confirm which token types your upstream reports in usage fields and how the plugin counts them.
Time windowFixed window measured in secondsShort windows protect against bursts from a single runaway job. Longer windows match monthly or team budgets but allow larger spikes inside the window.Confirm the supported window unit and how the counter resets at the window boundary.
ScopePer LLM instance or per consumerInstance scope caps total spend on one provider or model. Consumer scope gives each team or customer its own budget, which suits chargeback and tiered plans.Confirm that consumer scope requires an authenticated consumer and which auth plugin identifies it.
Rejected status codeDefault 503 or a configured codeKeep the default if your clients already handle 503 retries. Set a 429 response when client libraries expect standard rate-limit semantics.Confirm the default and whether the response body or headers can be customized.
Counter backendredis, redis-cluster or redis-sentinelA single redis instance fits a lab or one gateway node. Redis Cluster or Sentinel fits several gateway nodes that must share one count and survive a Redis failure.Confirm the connection keys for each mode and the failover behavior of your Redis topology.
Redis topology for HAStandalone, Sentinel with at least three Sentinel instances, or ClusterSentinel fits when you do not need data sharding. Cluster fits larger keyspaces, though it changes how keys are spread across nodes.Confirm the Sentinel quorum and Redis version you will run, and the license terms of your Redis edition.
Fallback when quota runs outReject the request or shift traffic to another instance through ai-proxy-multiRejection fits strict budgets. Shifting traffic fits availability goals, but the second provider then carries spend you may not have planned for.Confirm how the two plugins interact in your APISIX version and where the counter is read in the request flow.

Why shared Redis counters matter across gateway nodes

If each APISIX node keeps its own token counter, a consumer can exceed a budget simply by spreading calls across nodes. Each node counts only the tokens it has seen, so a client with a 100,000-token window that sends traffic evenly to three nodes can consume close to three times the intended allowance before any single node reaches its limit. The ai-rate-limiting plugin avoids this by writing its counters to a shared store through the redis_host settings, or the equivalent cluster and sentinel options, so every node checks the same running total. Before go-live, test this with a single consumer sending load through a load balancer that spreads requests across all gateway nodes, and confirm that the count matches what the upstream providers report.

Redis Sentinel is the usual high availability option when you do not use Redis Cluster. Sentinel monitors the master and promotes a replica automatically when the master fails, and APISIX reconnects to the new master through the sentinel configuration. The Redis Sentinel documentation says a robust deployment needs at least three Sentinel instances, so that a majority can agree on a failover even when one Sentinel is down. Plan the Sentinel quorum, the placement of those instances across failure domains, and the password and TLS settings for both Redis and Sentinel connections. Confirm the exact failover behavior and any counter loss during promotion with your own tests, because a short gap in counters can briefly allow overspend.

The shared store adds latency to every request that the plugin checks, and it adds a service your team must run, patch and monitor. If Redis becomes unreachable, decide in advance whether the gateway fails open or closed for budget checks, since that choice trades cost exposure against availability. Redis licensing also needs review: the Redis licenses page explains that Redis 8.0 and later can be used under AGPLv3, RSALv2 or SSPLv1 at the user's choice, while Redis 7.4.x to 7.8.x are under dual RSALv2 or SSPLv1. Confirm which Redis version and license your organization can accept before you choose the backend.

Which Redis version and license fit your organization

The shared Redis counter is the piece of this design that carries a license decision, not just a technical one. The version you pick determines which license terms apply, and the hosting model determines who operates the service. Work through these choices with your legal and procurement teams before the counter goes into production.

Redis 8 and later licensing

Redis 8.0 and later can be used under a license of your choice: RSALv2, SSPLv1 or GNU AGPLv3. The choice matters for APISIX because the rate limiter writes counters to Redis, and any modified Redis code offered as a network service may carry source-disclosure duties. Ask counsel which option fits your use and record the decision in your architecture file.

Redis 7.4 to 7.8 terms

Redis 7.4.x through 7.8.x remain under the dual RSALv2 or SSPLv1 license, and neither is OSI-approved. If your standard requires OSI-approved open source, these versions may not satisfy it even though the software is widely used. Confirm with your procurement or legal team whether a version in this range is acceptable for the counter that enforces your LLM budgets.

Redis 7.2 and earlier terms

Redis 7.2.x and earlier remain under the BSD-3-Clause license. Redis states that teams moving from 7.2 or earlier with unmodified artifacts see no license impact. This line is the simplest in license terms but will not receive the newer 8.x features, so check whether the security support you need is still available for the version you choose before standardizing on it.

Self-managed or vendor hosting

Redis Open Source is self-managed. Redis Software adds compliance, reliability and resiliency features for self-managed deployments, and Redis Cloud is a managed service on AWS, Google Cloud and Azure. Neither of the commercial options is open source. Decide whether your team will operate Sentinel and failover, or whether a vendor contract is the better fit for your data residency and support requirements.

Windows host limitation

Redis Open Source runs on Linux, macOS and the BSD systems, but on Windows only through Docker. If your APISIX nodes or Redis hosts run on Windows servers, plan a Linux container host for the Redis tier. Confirm the supported container image and version with your Redis build pipeline before you commit to a Windows-based design.

How to set up token quotas per consumer

This sequence takes a gateway from an inventory of LLM instances and consumers to a working per-consumer token budget backed by shared Redis counters, with fallback to other providers when a quota runs out. Each step ends with a check you can run before moving on.

  1. Confirm LLM instances and consumers

    Your team lists every LLM instance the gateway will front, including provider, model, endpoint and the credential each one uses, and marks which instances are approved for which teams or customers. Then you list each consumer that will receive a quota, such as an internal team, a product or an external customer, and the window each budget should cover. Confirm the limit types you need: total, prompt or completion tokens. Done when every consumer maps to a named budget owner and every instance has a documented provider and model name that you can test directly against the provider's API.

  2. Deploy the Redis backend and Sentinel quorum

    Your platform team deploys Redis in infrastructure you control, with replicas and at least three Sentinel instances, since the Redis Sentinel documentation describes a robust deployment as needing that minimum. Confirm the Redis version and license with your legal and procurement teams before deployment, because licensing differs across Redis releases. Done when Sentinel reports a healthy master, a replica promotes correctly after a controlled failover in a test environment, and the gateway hosts can reach the Sentinel and Redis ports.

  3. Define consumers and API keys

    Your team creates one APISIX consumer for each budget owner and attaches the authentication plugin your design uses, such as key-auth or jwt-auth, so the gateway can identify the caller before it counts tokens. Keep consumer names stable, because the per-consumer counter depends on them. Store issued keys in your secrets system rather than in route configuration. Done when a test request with each consumer's key is accepted, and a request without a key is rejected before it reaches any LLM route.

  4. Configure ai-rate-limiting with limit type and window

    Your gateway engineer adds the ai-rate-limiting plugin to the LLM route with the chosen token type, limit value and time window, and sets the key to consumer or instance depending on whether the budget belongs to a caller or to a model. Point the Redis settings at the Sentinel-managed deployment so counters are shared across nodes. Confirm the response status the plugin returns when a quota is exceeded, since the default is 503, and decide whether clients should see that code or a remapped one. Done when a configured test consumer receives successful responses until the limit is reached and then receives the rejection, with the remaining counts visible in the response headers you enabled.

  5. Pair with ai-proxy-multi for traffic shifting

    Your gateway engineer places ai-proxy-multi on the same route so that when one instance's quota is exhausted or it returns errors, traffic moves to an approved alternative. Define priorities and weights for each instance, choose the balancing algorithm, and set max_retries. Each instance needs a unique name in the configuration, which the plugin requires. Confirm that a fallback provider is also covered by the consumer's policy, since shifting traffic to an unapproved model can break your data handling commitments. Done when a test that exhausts the primary instance's budget results in a successful response served by the next instance, and the logs show which instance served it.

  6. Test quota exhaustion across two gateway nodes

    Your QA or security engineer runs two APISIX nodes against the same Redis backend and sends requests for one consumer alternately to each node until the limit is hit. The aim is to show that both nodes count against one shared total rather than each keeping its own tally. Record the request number at which rejection starts on each node and confirm both match the configured limit. Also confirm that a second consumer's budget is unaffected during the test. Done when the rejection point is identical on both nodes and the second consumer continues to receive responses.

  7. Test counter sharing after a Redis failover

    Your platform engineer triggers a controlled Sentinel failover in the test environment while requests continue to flow through both gateway nodes. The check is that counters survive the promotion of a replica and that the consumer's remaining budget stays consistent afterward. Confirm with your Redis and gateway teams how the plugin behaves while the backend is unavailable, and document the fail behavior your organization accepts. Done when the consumer's remaining budget after failover matches the pre-failover count within the behavior you documented, and the failover event is captured in your monitoring.

Shifting traffic to another provider when a quota runs out

A quota rejection and a provider failover are different events, and the gateway treats them differently. When ai-rate-limiting finds that a consumer or LLM instance has used its token allowance for the window, it rejects the request itself. By default that rejection returns HTTP 503, and no upstream provider is contacted. When ai-proxy-multi fails over, it is reacting to a response from an upstream, such as a 429 or 5xx, and it retries another instance under its max_retries setting. A quota rejection is therefore not a signal that a provider is unhealthy, and a failover path will not help a client whose own budget is spent. Teams need to decide which of the two outcomes they want for each consumer before they write any plugin configuration.

Teams usually combine the two plugins in one of two ways. The first places ai-rate-limiting on the consumer or route and uses ai-proxy-multi priorities to send overflow to a second instance, such as a cheaper model or a second account with another provider. The second keeps the budget check on the primary instance and gives a separate, cheaper instance its own route for consumers who have exhausted the main quota. Either design changes who pays for overflow traffic, so finance and the owners of each provider account should agree on the spillover rules first. Clients also need a defined behavior for the 503 case, such as backing off and retrying after the window resets, or switching to a lower-cost route in their own code. Without that contract, a quota rejection will look to the client like an outage.

The exact combination must be validated in a test environment before production. Confirm the plugin ordering, the behavior of priorities when the primary instance rejects a request, whether the rejection status can be changed for a given route, and how shared Redis counters behave during a failover between instances. Run a test that exhausts a quota and checks that overflow reaches the intended instance, that the client receives the status it expects, and that usage is logged against the right consumer. Check the current plugin documentation for your APISIX version, since the behavior of these plugins has changed between releases, and APISIX ai-rate-limiting plugin documentation is the starting point for that check.

Security and operational risks to plan for

Token quotas only hold if the counters behind them are reliable, protected and auditable. The cards below cover the shared Redis dependency, the credentials that guard it, the consumer keys that identify callers, and the logs that prove spend. Each one names a control to put in place before the quota is trusted for budget decisions.

Redis as a shared dependency

Every node in the APISIX cluster writes to the same counters, so a Redis outage affects every LLM route that uses ai-rate-limiting. Decide in advance whether requests fail closed or open when the counter store is unreachable, and document that choice for finance and security. Confirm the failure behavior of your chosen Redis topology with the APISIX plugin docs and test it in staging by stopping the Redis primary.

Redis credentials and network access

The Redis instance stores spend counters that decide whether callers are blocked, so anyone who can write to it can reset a budget. Restrict Redis to the APISIX network segment, require authentication, and keep the password in your secrets store rather than in the plugin configuration committed to Git. Confirm with your Redis deployment team which ACL users APISIX needs and whether TLS is enabled on the connection.

Consumer key handling and rotation

Per-consumer budgets depend on the gateway identifying each team or customer correctly, which usually means an API key issued through a consumer. Set a rotation schedule and a revocation procedure that removes the consumer key without deleting its counter history. Check that a rotated key keeps the same consumer identity, or your budget reporting will split one customer into two.

Quota bypass through unidentified callers

A request that reaches the LLM route without a consumer identity may fall outside the per-consumer limit, or be counted against a shared bucket that blocks everyone. Make authentication mandatory on every LLM route, and test that a request without a key is rejected before it reaches the model. Review routes for any path that proxies to a provider without the limiter attached.

Token usage logs for audit

Finance and auditors will ask how a figure in the spend report was produced, so the token counts behind each decision need to be recorded. Log the consumer, model, prompt and completion token counts, and the response status for every limited request, and set a retention period that matches your finance and compliance policy. Confirm with your logging team where those records land and who can read them.

Counter state during failover

When Sentinel promotes a replica, writes not yet replicated from the old primary can be lost, which may let a consumer exceed its window by the amount of the lost writes. Decide whether that small overrun is acceptable for your budget, or add headroom to the limit. Test a failover in staging and compare the counter values before and after to measure the effect on your own traffic.

What teams ask before running LLM token limits in-house

What drives the implementation effort for LLM token limits in APISIX?

The effort follows three things: how many consumers and budgets you need to enforce, how the shared counters are laid out in Redis, and how many LLM providers sit behind the gateway. A single team with one model provider and one budget is a small configuration task. Per-customer budgets across several providers add consumer design, counter keys, provider-specific token accounting and failover testing. Redis topology matters because the ai-rate-limiting plugin can share counters through redis, redis-cluster or redis-sentinel, and each option brings different operational duties. Ask for a written scope that lists each of these three counts before you compare quotes.

Can one engineer build LLM token limits in APISIX alone?

One experienced engineer can build a narrow version, such as a single gateway with one provider and one Redis instance. The harder parts are rarely the plugin configuration itself. They include deciding how budgets map to consumers, testing what happens when a quota runs out, confirming how each provider reports token usage, and making sure the Redis counters survive a node or failover event. Teams that own the gateway and Redis, and have someone who can review the Lua or configuration changes, usually cover the build internally. Teams without that depth often benefit from a second pair of hands for design review and verification.

What access and inputs does the client need to provide?

The work depends on inputs that only your organization can supply. Typically this includes:

  • Access to the APISIX Admin API or the configuration repository for the gateway
  • A Redis deployment the gateway can reach, with credentials and network rules agreed in advance
  • The consumer list, including teams or customers that need budgets and how they are identified
  • Accounts and API keys for each LLM provider in scope, with usage and billing visibility
  • Test traffic or a non-production environment where budget exhaustion can be exercised safely

Missing any of these usually moves the start date, so confirm them before the scope is signed.

How long does validation take for token limits?

Validation time depends on scope rather than a fixed number. A single-instance limit tested with one consumer is short to verify. Validation grows when you add per-consumer budgets, several time windows, multiple providers with different token reporting, and a failover path that moves traffic when a quota runs out. Testing should cover quota exhaustion, counter behavior across gateway nodes, Redis failover, and how the gateway responds to clients. DeployOpen does not publish durations, so ask for the test cases and their order in the written scope and check that each maps to a scenario you care about.

What recurs after token limits go live?

Several tasks recur after launch. Budgets need tuning as usage patterns change, and teams often adjust time windows and limits in the first months. API keys for consumers and providers rotate on your schedule and need the gateway configuration updated. Upgrades to APISIX and to Redis are periodic work, and Redis licensing and release lines should be checked at each upgrade because they have changed. Someone must also review counter and cost reports so budget breaches are noticed. Ask whether the quote covers these as a retainer, as separate work, or as handover to your team.

Which Redis licensing questions should we check before choosing a topology?

Redis licensing changed across releases, so confirm the version and license before you standardize on it. According to the Redis licenses page, Redis 8.0 and later can be used under a license of your choice among RSALv2, SSPLv1 and AGPLv3. Redis 7.4.x to 7.8.x remain under dual RSALv2 or SSPLv1, which are not OSI-approved, while Redis 7.2.x and earlier remain under BSD-3-Clause. Your legal team should review which license fits your use, especially if you modify Redis and offer it as a network service. The licenses FAQ on the same site explains the obligations in more detail.

How is the cost of token limits quoted?

DeployOpen does not publish a price list. A quote follows from a written scope that names the consumers, providers, Redis topology, test environments, documentation and handover expectations. Most engagements are fixed-scope, so deliverables and timelines are agreed in writing before work starts. Ask the quote to separate one-time build and validation from any recurring retainer for support, upgrades or continuous testing. Compare quotes on the same scope items rather than on headline totals, and confirm what is excluded, such as provider contract changes or Redis hosting.

How DeployOpen scopes and delivers this in your infrastructure

DeployOpen plans, implements and supports this work inside your own infrastructure, and the scope is agreed in writing before any build starts. DeployOpen is a Chainscore Labs company. For an APISIX token budget project, the written scope should name the APISIX version you will run, the deployment mode (traditional, decoupled or standalone), the cloud accounts, data centers or air-gapped networks that host the gateway and its Redis counters, and each LLM provider or self-hosted model in the route set. It should also list which consumers and teams get budgets, which limits apply (total, prompt or completion tokens, per instance or per consumer), what happens when a quota runs out, and what evidence you will receive, such as the ai-rate-limiting configuration, test results and runbooks. Ask for these items to appear as deliverables with acceptance criteria, so the quote reflects the actual work.

Pricing follows from that written scope. DeployOpen does not publish a price list, so a quote depends on the number of routes and consumers, how many providers and fallback paths need testing, whether Redis is new or already operated by your team, and how much of the Redis topology (single node, Sentinel with at least three Sentinel instances, or Redis Cluster) you need to design and verify. The one-time costs are design, configuration, integration with your identity and logging systems, and acceptance testing. Recurring costs depend on whether you need ongoing support, upgrades and continuous testing, which DeployOpen offers as retainers. When you request a quote, ask which items are fixed, which are estimates tied to a named assumption, and what changes trigger a scope revision.

After delivery, the gateway, Redis and budget settings still need an owner. Confirm with your own team who approves changes to quota values, who reviews the 503 responses that appear when a budget is exhausted, and how upgrades to APISIX and Redis will be tested before rollout. Before signing, confirm the licensing position of the Redis version you plan to run, since the Redis licenses page describes different terms for 7.4.x to 7.8.x and for 8.0 and later. A retainer can cover these operational tasks, while the scope document should record who is responsible for each one.

Talk to our team.

Tell us what you're working on, whether it's a deployment, an audit, a security test or a cyber range. You'll speak with an engineer who can help you scope it.

  • 30-minute call: free, with no obligation.
  • NDA on request: we can sign before you share details.
  • Clear next steps: a scope and plan after the call.