Skip to content

Commit 4a27029

Browse files
committed
fix: add prerequisites and correct constant key in rate limiting examples
- Add key-auth plugin to Example 1 (per-tier), Example 3 (limit-conn), and the combined example: ${consumer_name} requires an authenticated consumer; without it the rule is silently skipped and APISIX returns 500 - Replace bare 'global' constant key with '${http_host ?? global}' in Example 3 (limit-conn), the ai-rate-limiting example, and the combined example: plain constant strings produce n_resolved=0 and are skipped at runtime due to an APISIX bug (filed at apache/apisix#13180) - Add prerequisite note to the ai-rate-limiting section: the plugin is silently inactive without ai-proxy, which sets picked_ai_instance_name
1 parent 54c4348 commit 4a27029

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

blog/en/blog/2026/04/14/apisix-3.16-dynamic-rate-limiting.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,13 @@ Variable support lets you pull rate limiting parameters directly from the reques
110110

111111
Suppose your authentication middleware injects an `X-Rate-Quota` header based on the user's subscription tier:
112112

113+
> **Prerequisite**: this example uses `${consumer_name}` as the rate limit key, which requires an authentication plugin on the route so that the consumer identity is available at request time. Add `key-auth` (or any other auth plugin) to the route plugins and create a consumer before testing.
114+
113115
```json
114116
{
115117
"uri": "/api/v1/*",
116118
"plugins": {
119+
"key-auth": {},
117120
"limit-count": {
118121
"rules": [
119122
{
@@ -178,10 +181,15 @@ Tenant A calling `/api/v1/users` and Tenant B calling the same endpoint get inde
178181

179182
The `limit-conn` plugin also supports rules and variables, enabling dynamic concurrency control:
180183

184+
> **Prerequisite**: the per-consumer rule uses `${consumer_name}`, which requires an authentication plugin. Add `key-auth` to the route plugins and create a consumer before testing.
185+
>
186+
> **Note**: plain constant strings (e.g. `"global"`) are not supported as `key` values in the `rules` array due to a current APISIX limitation — the rule is silently skipped at runtime. Use a variable expression that always resolves instead, such as `"${http_host ?? global}"`. A fix has been filed at [apache/apisix#13180](https://github.com/apache/apisix/issues/13180).
187+
181188
```json
182189
{
183190
"uri": "/api/v1/inference",
184191
"plugins": {
192+
"key-auth": {},
185193
"limit-conn": {
186194
"default_conn_delay": 0.1,
187195
"rules": [
@@ -193,7 +201,7 @@ The `limit-conn` plugin also supports rules and variables, enabling dynamic conc
193201
{
194202
"conn": 100,
195203
"burst": 20,
196-
"key": "global"
204+
"key": "${http_host ?? global}"
197205
}
198206
],
199207
"rejected_code": 503
@@ -212,6 +220,10 @@ This limits each consumer to 5 concurrent connections while capping the total at
212220

213221
## AI Rate Limiting: Token Budget Management
214222

223+
> **Prerequisite**: `ai-rate-limiting` must be used alongside the `ai-proxy` plugin. Without `ai-proxy`, the plugin is silently inactive — it relies on `ai-proxy` to populate `ctx.picked_ai_instance_name` and `ctx.ai_token_usage` at runtime. The configuration below shows `ai-rate-limiting` in isolation for clarity; in production, add your `ai-proxy` configuration to the same route.
224+
>
225+
> **Note**: the global cap rule uses `"${http_host ?? global}"` instead of a plain `"global"` string. See the note in Example 3 for the reason.
226+
215227
For AI gateway use cases, the `ai-rate-limiting` plugin combines multiple rules with variable support for fine-grained token budget control:
216228

217229
```json
@@ -236,7 +248,7 @@ For AI gateway use cases, the `ai-rate-limiting` plugin combines multiple rules
236248
{
237249
"count": 1000000,
238250
"time_window": 60,
239-
"key": "global",
251+
"key": "${http_host ?? global}",
240252
"header_prefix": "global"
241253
}
242254
],
@@ -264,10 +276,13 @@ As AI API costs scale directly with token usage, this kind of layered budget con
264276

265277
The real power emerges when you combine both features. Here is a complete example for an API platform with tiered pricing:
266278

279+
> **Prerequisite**: add an authentication plugin (e.g. `key-auth`) to the route so that `${consumer_name}` is populated at runtime. The global cap rule uses `"${http_host ?? global}"` instead of a plain `"global"` string — see the note in Example 3 for the reason.
280+
267281
```json
268282
{
269283
"uri": "/api/v1/*",
270284
"plugins": {
285+
"key-auth": {},
271286
"limit-count": {
272287
"rules": [
273288
{
@@ -285,7 +300,7 @@ The real power emerges when you combine both features. Here is a complete exampl
285300
{
286301
"count": 100000,
287302
"time_window": 60,
288-
"key": "global",
303+
"key": "${http_host ?? global}",
289304
"header_prefix": "global"
290305
}
291306
],

0 commit comments

Comments
 (0)