Skip to content
agentgateway has joined the Agentic AI FoundationLearn more

For the complete documentation index, see llms.txt. Markdown versions of all docs pages are available by appending .md to any docs URL.

Budget and spend limits

Page as Markdown

Cap LLM spend per API key in dollars or tokens, with per-key budgets or with rate limiting.

Cap how much each API key can spend on LLM traffic, in US dollars or in tokens.

About budget and spend limits

Agentgateway has two ways to cap LLM usage. They solve different problems, and you can use both at the same time.

Per-key budgetsRate limit token budgets
Where you configure itEach entry in apiKey.keysA localRateLimit or remoteRateLimit policy
UnitUS dollars or tokensTokens only, so a spend cap is an estimate that drifts when prices or the model mix change
ScopeOne API keyGateway-wide for a local limit, or any descriptor you choose for a remote limit
WindowA fixed window that is aligned to the Unix epochA token bucket that refills at an interval
Where the count livesA database that agentgateway managesMemory for a local limit, or a rate limit server for a remote limit
Action when the limit is reachedReject the request, or record the overage and allow itReject the request
Extra componentsA databaseA rate limit server, for per-user limits
ModesStandalone onlyStandalone and Kubernetes
When to choose itYou want a true dollar cap, because a budget charges the realized cost of each requestYou want to smooth burst traffic, because a token bucket refills continuously and a budget window does not, or you run in Kubernetes mode

Note

Per-key budgets work in standalone mode only. The Kubernetes API does not carry the budgets or allowedModels fields, so a Kubernetes deployment uses rate limit token budgets.

How per-key budgets work

A budget belongs to one entry in apiKey.keys. Each budget has a name, a limit, a window, and an action to take when the key goes over the limit.

Every budget needs a database. Agentgateway keeps the running count for each budget in the database that you set in config.database, and refuses to start when a key has a budget but that section is missing. To set up the database, see Configuration storage.

Agentgateway charges usage to a budget after the LLM response returns, because the provider reports the token counts and the cost only in the response. This ordering has two consequences:

  • The request that crosses the limit still completes. Agentgateway rejects the next request. A budget is therefore a cap on what a key can start, not a hard ceiling on what it spends.
  • When the provider does not report the unit that the budget needs, agentgateway does not charge the request at all. Agentgateway logs the request, but cannot charge or reject the request after the fact.

Agentgateway holds the running count in memory and writes it to the database every five seconds, which keeps the database off the request path. The database also makes the count survive a restart.

How budget windows work

A window is aligned to the Unix epoch, not to the first request that uses the key. A 1h window follows UTC clock hours, a 24h window starts at midnight UTC, and a 30d window uses consecutive 30-day periods rather than calendar months. Every key with the same window length therefore resets at the same moment.

Guides

Was this page helpful?
Agentgateway assistant

Ask me anything about agentgateway configuration, features, or usage.

Note: AI-generated content might contain errors; please verify and test all returned information.

Tip: one topic per conversation gives the best results. Use the + button in the chat header to start a new conversation.

Switching topics? Starting a new conversation improves accuracy.
↑↓ navigate select esc dismiss

What could be improved?

Your feedback helps us improve assistant answers and identify docs gaps we should fix.

Need more help? Join us on Discord: https://discord.gg/y9efgEmppm

Want to use your own agent? Add the Solo MCP server to query our docs directly. Get started here: https://search.solo.io/.