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
vaild vaule:
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
vaild vaule:
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
If 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.
Whenrulesis set, the headers use a prefix instead. Seerules.header_prefixfor details.limit_strategy
vaild vaule:
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
vaild vaule:
any non-empty string (must be a valid Lua arithmetic expression)
Lua arithmetic expression for dynamic token cost calculation using raw usage response fields. Available field names depend on the API format and response mode. This field is required and validated when
limit_strategyisexpression.Missing variables evaluate to
0. A final cost of0is not added to the rate-limit counter.The expression can use arithmetic operators and the Lua
mathlibrary. The functionsabs,ceil,floor,max, andminare also available without themath.prefix.Native Anthropic Messages expressions can combine
input_tokens,cache_read_input_tokens,cache_creation_input_tokens, andoutput_tokens. OpenAI Chat Completions expressions can useprompt_tokensandcompletion_tokens. OpenAI Responses expressions can useinput_tokensandoutput_tokens.Numeric fields nested inside the usage response can be referenced by joining their keys with
__. For example,input_tokens_details__cached_tokensreferencescached_tokensinsideinput_tokens_details. Top-level fields keep their original names.Support for joined names was introduced in API7 Enterprise 3.10.8. Joined names are not available in API7 Enterprise 3.9.x or APISIX 3.19.0, where they evaluate to
0.Available in API7 Enterprise from version 3.9.8 and APISIX from version 3.17.0.
instances
LLM instance rate limiting configurations.
name
Name of the LLM service instance.
limit
vaild vaule:
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
vaild vaule:
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
vaild vaule:
between 200 and 599 inclusive
The HTTP status code returned when a request exceeding the quota is rejected.
rejected_msg
vaild vaule:
any non-empty string
The response body returned when a request exceeding the quota is rejected.
policy
vaild vaule:
local,redis,redis-cluster, orredis-sentinelThe policy for rate-limiting counters. API7 Gateway requires this field; configure
localwhen the counters should not use Redis. APISIX defaults tolocalwhen 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
The address of the Redis node. Required when
policyisredis.redis_port
vaild vaule:
greater than or equal to 1
The port of the Redis node when
policyisredis.redis_username
The username for Redis data-node authentication when
policyisredis. Support for this field withredis-sentinelwas introduced in API7 Enterprise 3.10.5 and APISIX 3.18.0. If Redis uses onlyrequirepass, configureredis_passwordwithout a username.redis_password
Password for Redis data-node authentication when
policyisredisorredis-cluster. Support for this field withredis-sentinelwas introduced in 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
vaild vaule:
greater than or equal to 0
The database number in Redis when
policyisredisorredis-sentinel.redis_ssl
If true, use SSL to connect to Redis when
policyisredis.redis_ssl_verify
If true, verify the server SSL certificate when
policyisredis.redis_server_name
TLS server name used for SNI and, when certificate verification is enabled, hostname verification. Applies when
policyisredisandredis_sslistrue. Defaults toredis_host. If the selected value is an IP address, no server name is sent. To verify a DNS-named certificate when connecting by IP, set this field to the certificate's DNS name and enableredis_ssl_verify.Introduced in API7 Enterprise 3.9.21 and 3.10.8, and in APISIX 3.19.0. This field applies to the single-node Redis connector, not Redis Cluster or Sentinel.
redis_timeout
vaild vaule:
greater than or equal to 1
The Redis timeout value in milliseconds when
policyisredisorredis-cluster.redis_cluster_nodes
List of Redis cluster nodes with at least one address. Required when
policyisredis-cluster.redis_cluster_name
The name of the Redis cluster. Required when
policyisredis-cluster.redis_cluster_ssl
If true, use SSL to connect to Redis cluster when
policyisredis-cluster.redis_cluster_ssl_verify
If true, verify the server SSL certificate when
policyisredis-cluster.redis_sentinels
An array of Redis Sentinel nodes (host and port). Required when
policyisredis-sentinel.host
vaild vaule:
at least 2 characters
Hostname or IP address of the Redis Sentinel node.
port
vaild vaule:
between 1 and 65535 inclusive
Port of the Redis Sentinel node.
redis_master_name
The name of the Redis master group that Sentinels are monitoring. Required when
policyisredis-sentinel.redis_role
vaild vaule:
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
vaild vaule:
greater than or equal to 1
Timeout in milliseconds for establishing a connection to a Redis node. Configurable when
policyisredis-sentinel.redis_read_timeout
vaild vaule:
greater than or equal to 1
Timeout in milliseconds for reading data from a Redis node. Configurable when
policyisredis-sentinel.redis_keepalive_timeout
vaild vaule:
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
vaild vaule:
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
Username used to authenticate with the Redis Sentinel instance. Configurable when
policyisredis-sentinel.sentinel_password
Password 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
If 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
An 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
vaild vaule:
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
vaild vaule:
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
A variable expression that resolves to the key used to count requests for this rule. Prefix each variable with
$; both$variableand${variable}forms are accepted. For example, use${http_custom_a}or${http_custom_a}_${http_custom_b}. See built-in variables for available variables.A rule applies when at least one variable reference resolves to a request value or its
??default. A missing variable without a default contributes an empty string but does not count as resolved, so a partial expression can still apply when another variable resolves. An expression with no resolved variables, including a value with no variable references, is skipped.If
rulesis configured and no rule applies to a request, the gateway returns HTTP 500 by default. Settingallow_degradationtotruelets the request continue without rate limiting.In an ADC configuration, escape a gateway expression with a simple name, such as
${http_user}, as${http_user}. ADC treats an unescaped${NAME}containing only word characters as load-time environment interpolation; dotted gateway variable names pass through unchanged.header_prefix
Prefix 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.