What you will have at the end
On the Route from A Route to language models, a budget of 300 tokens per minute for each Consumer, counted on the total tokens the provider reports. A Consumer that spends it is answered 429 with a body you wrote, while another Consumer’s budget is untouched. Every answer carries three headers: the limit, what was left when the call arrived, and when the window turns. The refusals are records in Traffic Explorer with gateway as the Upstream node and a share on the Route’s Analytics. And one thing measured rather than guessed: a streaming answer that starts within the budget is never cut, the budget goes negative, and the next request is the one refused.
Prerequisites
- The Route
assistant-apiwithkey-authandai-proxy-multifrom the previous article, its primary on-premises model answering a fixed 65 tokens per call, and its fallback a cloud model. - Two Consumers with keys,
support_assistantandpartner_portal. - The fallback instance is a real cloud model on a real account, called with the lab’s own key. The key is masked in every screenshot and in the audit log, and no part of it is printed here.
- Traffic logging turned on under Traffic Explorer › Settings, and a role that may manage Routes. The screenshots below were taken as
nora.becker.
Which tokens, for whom
Open the Route, Plugins, Add plugin, ai-rate-limiting. Under Basics, the first choice is what the budget is attached to. One budget of tokens is a count and a window for the Route, or for each AI instance the proxy picks, or both: a row per instance under Instances, named as the instance is named in ai-proxy-multi, caps that instance alone. A budget for each key is a list of rules, each keyed by a request variable: $consumer_name, so every Consumer gets a window of its own; $remote_addr would give one per client address. The note under the list says what a rule whose key resolves to nothing does: it is skipped, and with no rule left the request is answered 500.
The article adds one rule: Count 300, Key $consumer_name, Time window 60 seconds, and a Header prefix, consumer, which names the headers below.

Limit strategy says which count the budget is measured on: total_tokens, the default, prompt_tokens, completion_tokens, or an expression of your own. Total is the right one for a cost cap, prompt tokens for a cap on what callers send.
Under Behavior, Policy local counts in each gateway node’s memory; the Redis policies share the count across nodes. Rejected code 429. Rejected message is the body of the refusal; the article writes a JSON object. Show limit quota header stays on. Allow degradation is the switch to read twice: when the limit cannot be counted, off answers 500 and on lets every request through unlimited.

Select Add plugin. It takes its place after key-auth and ai-proxy-multi on the request path, which is why it knows the Consumer and the token counts.
What a client sees
Six calls as support_assistant, one after another, each answered by the on-premises model with 65 tokens:
call 1 HTTP/1.1 200 OK usage total 65
call 2 HTTP/1.1 200 OK usage total 65
call 3 HTTP/1.1 200 OK usage total 65
call 4 HTTP/1.1 200 OK usage total 65
call 5 HTTP/1.1 200 OK usage total 65
call 6 HTTP/1.1 429 Too Many Requests
{"error_msg": "{\"error\":\"token budget spent, try again next minute\"}"}
### partner_portal, its own budget, in the same minute
HTTP/1.1 200 OK usage total 65
Five calls fit, 325 tokens against 300: the fifth was allowed because 40 tokens were still left when it arrived, and the budget is charged after the answer, when the provider’s count is known. The sixth found nothing left. The partner’s call in the same minute passed, on its own counter, whose window had turned since the streams of the next section spent it a minute earlier. The refusal body is the message from the form, which the engine wraps in its own error_msg field; a client parses that outer object first.
The three headers on every answer, named by the prefix, for a second run of the same six calls in a fresh window:
call 1 200 X-AI-consumer-RateLimit-Limit: 300 Remaining: 300 Reset: 60
call 2 200 X-AI-consumer-RateLimit-Limit: 300 Remaining: 235 Reset: 59.42
call 3 200 X-AI-consumer-RateLimit-Limit: 300 Remaining: 170 Reset: 47.65
call 4 200 X-AI-consumer-RateLimit-Limit: 300 Remaining: 105 Reset: 31.68
call 5 200 X-AI-consumer-RateLimit-Limit: 300 Remaining: 40 Reset: 25.93
call 6 429 X-AI-consumer-RateLimit-Limit: 300 Remaining: 0 Reset: 19.33
Remaining is what was left when the call arrived, before its own 65 tokens were charged, which is why the first call reads 300 and the fifth still reads 40. Reset is the seconds until the window turns.
The prefix is inserted into the engine’s own header names, so write a word, not a header name; X-AI-Budget as a prefix produced X-AI-X-AI-Budget-RateLimit-Limit in the first run of this article.
A streaming answer that crosses the budget
The fallback model streams. With the primary switched off so the fallback answers, and a fresh budget of 300 for partner_portal, three streaming requests:
stream 1 HTTP/1.1 200 OK text/event-stream 5 chunks … usage total 169 … data: [DONE]
stream 2 HTTP/1.1 200 OK text/event-stream 5 chunks … usage total 170 … data: [DONE]
stream 3 HTTP/1.1 429 Too Many Requests
The second stream started with 131 tokens left and used 170: it was not cut, it ran to [DONE], and the budget went to minus 39. The third request was refused. A budget is a gate at the start of a request, not a meter on the stream, so size it with one answer’s worth of headroom.
The streams were answered by the fallback, and the rule keyed by Consumer counted its tokens. That differs from the single limit of the fallback article, where the tokens the fallback produced were not counted against the budget: a rule per Consumer counts whichever instance answered, a single limit did not.
What Traffic Explorer and Analytics show
The refusal is a record with gateway in the Upstream node column and 0 ms, between the Consumer’s answered calls; the partner’s calls carry the backend’s address.

The Route’s Analytics tab counts the refusals as 4xx against the 2xx of the same window:

What the audit log keeps
The Plugin took three saves, and each is a Route update with the ai-rate-limiting block as the difference. The first replaced the earlier single limit with the rule, its key, the count and the window. The second added the rejected message and a first header prefix, X-AI-Budget, the one that produced the doubled header names above. The third, below, changed the prefix to consumer. The provider keys in the neighbouring ai-proxy-multi block stay masked on both sides of every one.

What to do next
- Shape what the models are asked: Prompt rules at the gateway.
- Give the budget a second dimension, per instance, when the fallback is the expensive one: the Instances rows under One budget of tokens, not configured here.
- Answer repeated prompts without spending the budget: Answer repeated prompts from a cache.
- Token budgets are on AI Gateway and in the AI Gateway datasheet.