AI Rate Limiting Configuration
Parameters
See plugin common configurations for configuration options available to all plugins.
In API7 Enterprise (from 3.8.17) and in APISIX (from 3.16.0), you should configure one of the following parameter sets, but not both:
rules- Any combination of
limitandtime_windowand/orinstances
-
limit—integer | string· optionalValid values: greater than 0
The maximum number of tokens allowed to consume within a given time interval.
In API7 Enterprise (from 3.8.17) and in APISIX (from 3.16.0), this parameter also supports the string data type and allows the use of built-in variables prefixed with a dollar sign (
$). In earlier APISIX versions, only the integer type is supported. -
time_window—integer | string· optionalValid values: greater than 0
The time interval corresponding to the rate limiting
limitin seconds.In API7 Enterprise (from 3.8.17) and in APISIX (from 3.16.0), this parameter also supports the string data type and allows the use of built-in variables prefixed with a dollar sign (
$). In earlier APISIX versions, only the integer type is supported. -
show_limit_quota_header—boolean· optional · default:trueIf true, includes the rate limiting response headers. Specifically, when using
limit/time_windoworinstances, the headers include the instance name as a suffix:X-AI-RateLimit-Limit-{name}shows the total quota.X-AI-RateLimit-Remaining-{name}shows the remaining quota.X-AI-RateLimit-Reset-{name}shows the number of seconds until the counter resets.
When
rulesis set, the headers use a prefix instead. Seerules.header_prefixfor details. -
limit_strategy—string· optional · default:total_tokensValid values:
total_tokens,prompt_tokens,completion_tokens, orexpressionType of token to apply rate limiting.
total_tokens,prompt_tokens, andcompletion_tokensvalues are returned in each model response, wheretotal_tokensis the sum ofprompt_tokensandcompletion_tokens.When set to
expression, rate limiting cost is calculated using a custom Lua arithmetic expression defined incost_expr. Available in API7 Enterprise from version 3.9.8 and APISIX from version 3.17.0. -
cost_expr—string· optionalValid values: any non-empty string (must be a valid Lua arithmetic expression)
Lua arithmetic expression for dynamic token cost calculation. Variables are injected from the LLM provider's raw usage response fields (e.g.,
input_tokens,output_tokens,cache_creation_input_tokens). Missing variables default to0. Only math functions (abs,ceil,floor,max,min) and arithmetic operators are allowed. Expression syntax is validated whenlimit_strategyisexpression, where this field is required.Example:
input_tokens + cache_creation_input_tokenscomputes cost from Anthropic Claude's cache-aware token usage.Available in API7 Enterprise from version 3.9.8 and APISIX from version 3.17.0.
-
instances—array[object]· optionalLLM instance rate limiting configurations.
-
name—string· requiredName of the LLM service instance.
-
limit—integer | string· requiredValid values: greater than 0
The maximum number of tokens allowed to consume within a given time interval.
In API7 Enterprise (from 3.8.17) and in APISIX (from 3.16.0), this parameter also supports the string data type and allows the use of built-in variables prefixed with a dollar sign (
$). In earlier APISIX versions, only the integer type is supported. -
time_window—integer | string· requiredValid values: greater than 0
The time interval corresponding to the rate limiting
limitin seconds.In API7 Enterprise (from 3.8.17) and in APISIX (from 3.16.0), this parameter also supports the string data type and allows the use of built-in variables prefixed with a dollar sign (
$). In earlier APISIX versions, only the integer type is supported.
-
-
rejected_code—integer· optional · default:503Valid values: between 200 and 599 inclusive
The HTTP status code returned when a request exceeding the quota is rejected.
-
rejected_msg—string· optionalValid values: any non-empty string
The response body returned when a request exceeding the quota is rejected.
-
policy—string· optional · default:localValid values:
local,redis,redis-cluster, orredis-sentinelThe policy for rate limiting counters. API7 Gateway requires this field; use
localfor configurations that do not use Redis. APISIX useslocalwhen the field is omitted.Redis-backed policies are available in API7 Enterprise from version 3.8.19 and in APISIX from version 3.18.0.
Set to
localto store the counter in memory locally.Set to
redisto store the counter on a Redis instance.Set to
redis-clusterto store the counter in a Redis cluster.Set to
redis-sentinelto store the counter on the Redis primary node managed by Redis Sentinel, which ensures high availability by automatically promoting a replica to primary in case of failure. Redis Sentinel provides high availability for Redis when not using Redis Cluster. -
redis_host—string· optionalThe address of the Redis node. Required when
policyisredis. -
redis_port—integer· optional · default:6379Valid values: greater than or equal to 1
The port of the Redis node when
policyisredis. -
redis_username—string· optionalThe username for Redis if Redis ACL is used. If you use the legacy authentication method
requirepass, configure onlyredis_password. Used whenpolicyisredis, and withredis-sentinelin API7 Enterprise 3.10.5 and APISIX 3.18.0. -
redis_password—string· optionalPassword of the Redis node when
policyisredisorredis-cluster, and withredis-sentinelin API7 Enterprise 3.10.5 and APISIX 3.18.0.In API7 Gateway 3.10.2 or later in the 3.10 release series, and 3.9.16 or later in the 3.9 release series, the value is encrypted with AES256 before being saved to the database.
In APISIX 3.18.0 or later, the value is encrypted with AES before being stored in etcd.
-
redis_database—integer· optional · default:0Valid values: greater than or equal to 0
The database number in Redis when
policyisredisorredis-sentinel. -
redis_ssl—boolean· optional · default:falseIf true, use SSL to connect to Redis when
policyisredis. -
redis_ssl_verify—boolean· optional · default:falseIf true, verify the server SSL certificate when
policyisredis. -
redis_timeout—integer· optional · default:1000Valid values: greater than or equal to 1
The Redis timeout value in milliseconds when
policyisredisorredis-cluster. -
redis_cluster_nodes—array[string]· optionalList of Redis cluster nodes with at least one address. Required when
policyisredis-cluster. -
redis_cluster_name—string· optionalThe name of the Redis cluster. Required when
policyisredis-cluster. -
redis_cluster_ssl—boolean· optional · default:falseIf true, use SSL to connect to Redis cluster when
policyisredis-cluster. -
redis_cluster_ssl_verify—boolean· optional · default:falseIf true, verify the server SSL certificate when
policyisredis-cluster. -
redis_sentinels—array[object]· optionalAn array of Redis Sentinel nodes (host and port). Required when
policyisredis-sentinel. -
redis_master_name—string· optionalThe name of the Redis master group that Sentinels are monitoring. Required when
policyisredis-sentinel. -
redis_role—string· optional · default:masterValid values:
masterorslaveThe Redis node role to connect to. Configurable when
policyisredis-sentinel. Set tomasterto connect to the current Redis master, and set toslaveto connect to a Redis replica. -
redis_connect_timeout—integer· optional · default:1000Valid values: greater than or equal to 1
Timeout in milliseconds for establishing a connection to a Redis node. Configurable when
policyisredis-sentinel. -
redis_read_timeout—integer· optional · default:1000Valid values: greater than or equal to 1
Timeout in milliseconds for reading data from a Redis node. Configurable when
policyisredis-sentinel. -
redis_keepalive_timeout—integer· optional · default:10000forredisorredis-cluster;60000forredis-sentinelValid values:
redisandredis-cluster: greater than or equal to 1000;redis-sentinel: greater than or equal to 1Time in milliseconds that an idle Redis connection is kept alive in the connection pool before being closed. Used by all Redis-backed policies in APISIX 3.18.0. In API7 Enterprise, it is used by
redis-sentinel;redisandredis-clustersupport it from version 3.9.16 on the 3.9 release series and version 3.10.3 on the 3.10 release series. -
redis_keepalive_pool—integer· optional · default:100Valid values: greater than or equal to 1
Maximum number of idle Redis connections in the keepalive pool. Used when
policyisredisorredis-cluster. Available in API7 Enterprise from version 3.9.16 on the 3.9 release series and version 3.10.3 on the 3.10 release series, and in APISIX from version 3.18.0. -
sentinel_username—string· optionalUsername used to authenticate with the Redis Sentinel instance. Configurable when
policyisredis-sentinel. -
sentinel_password—string· optionalPassword used to authenticate with the Redis Sentinel instance. Configurable when
policyisredis-sentinel.In API7 Gateway 3.10.2 or later in the 3.10 release series, and 3.9.16 or later in the 3.9 release series, the value is encrypted with AES256 before being saved to the database.
In APISIX 3.18.0 or later, the value is encrypted with AES before being stored in etcd.
-
allow_degradation—boolean· optional · default:falseIf true, allow the gateway to continue handling requests without the plugin when the plugin or its dependencies become unavailable.
Available in API7 Enterprise from version 3.8.19 and in APISIX from version 3.18.0.
-
rules—array[object]· optionalAn array of rate-limiting rules that are applied sequentially.
Available in API7 Enterprise from 3.8.17 and in APISIX from 3.16.0.
-
count—integer | string· requiredValid values: greater than 0
The maximum number of tokens allowed to consume within a given time interval.
This parameter also supports the string data type and allows the use of built-in variables prefixed with a dollar sign (
$). -
time_window—integer | string· requiredValid values: greater than 0
The time interval corresponding to the rate limiting
countin seconds.This parameter also supports the string data type and allows the use of built-in variables prefixed with a dollar sign (
$). -
key—string· requiredThe key to count requests by. If the configured key does not exist, the rule will not be executed.
The
keyis interpreted as a variable. The variable does not need to be prefixed by a dollar sign ($). See built-in variables for available variables. -
header_prefix—string· optionalPrefix for all rate limiting response headers. Available in API7 Enterprise from version 3.8.19 and in APISIX from version 3.17.0.
When configured, the prefix is inserted after
X-AI-in the header name. For example, withheader_prefixset totest, the headers becomeX-AI-Test-RateLimit-Limit,X-AI-Test-RateLimit-Remaining, andX-AI-Test-RateLimit-Reset.When not configured, the index of the rule in the rules array is used as the prefix. For example, headers for the first rule will be
X-AI-1-RateLimit-Limit,X-AI-1-RateLimit-Remaining, andX-AI-1-RateLimit-Reset.
-