Skip to main content

Token budgets per Consumer with ai-rate-limiting

A budget of tokens per minute keyed by Consumer on an LLM Route: the headers that say what is left, the 429 when it is spent, and what a streaming answer does.

By Akif F. Güven AI Gateway

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-api with key-auth and ai-proxy-multi from 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_assistant and partner_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.

The ai-rate-limiting form with A budget for each key selected: one rule with Count 300, Key $consumer_name, Time window 60, and the Header prefix column; under it Limit strategy total_tokens One rule, one Consumer at a time. The key is the Consumer the authentication Plugin set, so the budget follows the caller, not the address.

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.

The Behavior section of the ai-rate-limiting form: Policy local, Rejected code 429, Rejected message holding a JSON object, Show limit quota header on, Allow degradation off with its note The refusal is yours to word. Degradation is the policy for a counter that cannot be reached.

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.

Traffic Explorer filtered to the path /ai/chat: 200 records for assistant-api · partner_portal answered by llm-onprem:8080, a 429 record for assistant-api · support_assistant answered by gateway in 0 ms, and the support Consumer's 200 records before it The sixth call never reached a model. The Consumer column says whose budget it was.

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

The Requests card of the assistant-api Analytics tab: 85 requests in the last 24 hours, 2xx 80 at 94 %, 4xx 5 at 6 %, 3xx and 5xx 0 Five 4xx on the card for nine 4xx records in the same 24 hours: the 429s of this article, the 401 of the previous one and four 429s from the single limit the Route had before. Analytics leaves the first sample of each new series out and, on this Prometheus, gives estimates, as Connect Prometheus explains; Traffic Explorer is the complete list. A share that grows is a budget to raise, or a caller to talk to.

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.

The Change section of the third audit record of the assistant-api Route: the anthropic-haiku instance with its x-api-key as four dots on both sides, and in the ai-rate-limiting block the rule with key $consumer_name, count 300 and time_window 60, header_prefix changed from X-AI-Budget to consumer, and the rejected_msg and rejected_code 429 unchanged One changed line, the prefix. The message, the code and the masked keys around it are the same on both sides.

What to do next

Written by
Akif F. Güven
Engineer, G8
Was this helpful?
Next step

See G8 in a demo

We’ll show you the console with APIs and AI traffic like yours, and answer the questions these articles leave open.

  1. 01Demo
  2. 02Proof of conceptOn Docker for a quick start, or on the infrastructure you plan to run G8 on.
  3. 03LicenseWe prepare a quote for your edition and nodes, and you receive a signed license file.