Guides
Spend caps and budgets for AI coding agents
memtrunk limits spend per API key. Each key can carry a monthly or daily USD cap and a monthly token cap, checked before a request is forwarded. A key at a cap gets an HTTP 429 instead of a model response. Cost is estimated from configured prices, and alerts are an administrator setting, off by default.
AI coding agents can run long sessions without anyone watching the meter. Giving each tool its own key, with its own limits, turns a shared bill into something you can bound and attribute. This guide explains what the limits are, what happens at the edge, and where they do not apply. For the client setup that creates this traffic, see Use Claude Code with an LLM gateway.
The limits you can set on a key
Open API keys in the console and create or edit a key. The form offers these controls:
| Control | What it does |
|---|---|
| Monthly limit (USD) | Caps the key's estimated spend for the month. |
| Budget window | Monthly or Daily. Choosing Daily turns on the daily limit. |
| Daily limit (USD) | Caps estimated spend for the day. It applies only when the budget window is Daily. |
| Monthly token limit | Caps total tokens for the month, in any budget window. |
| Expires | Date after which the key stops working. |
An explicit limit of zero blocks every request. It is never treated as unlimited.
What happens when a cap is reached
The check runs before the request is forwarded, so a capped request never reaches a provider. Limits are checked in this order, and the first one that has been reached decides the answer: daily USD, monthly USD, monthly tokens.
The response is HTTP 429 with a JSON body in the Anthropic error shape. This is a daily breach, with illustrative values for the two reset fields:
{
"type": "error",
"error": {
"type": "rate_limit_error",
"message": "Daily spend limit reached",
"retry_after": 3600,
"resets_at": "<next UTC midnight, as a timestamp>"
}
}The message is "Daily spend limit reached", "Monthly spend limit reached" or "Monthly token limit reached". The three cases differ in one detail:
- A daily breach also includes
retry_after(seconds until the next UTC midnight) andresets_atin the body, and sends aRetry-Afterheader. - A monthly USD or monthly token breach is bare: no hint and no header.
The comparison uses spend recorded so far, before forwarding. A request that starts under a cap can therefore finish over it; the next request is the one that is refused.
Defaults and ceilings
These settings apply on top of the key's own limits. An administrator manages them under Settings, then Budgets & limits:
| Setting | Default |
|---|---|
| Default monthly cap (USD) | 100 |
| Pool-wide monthly ceiling (USD) | 500 |
| Allow callers to request unlimited keys | Allowed |
How they combine depends on the key's source.
Keys whose Source is "Shared pool". The monthly limit is the smaller of the key's own limit (or the default cap, if the key has none) and an effective ceiling. The ceiling comes from the first of these that is set: a per-user ceiling for the key's owner, the owning team's monthly limit, then the pool-wide ceiling. A shared-pool key is never unlimited. Setting a limit above the pool-wide or per-user ceiling is rejected; a team limit lowers the effective limit at request time without rejecting the key.
Keys whose Source is "Own provider key". The limit is the key's own. Left blank, it is unlimited if the instance allows unlimited keys, which it does by default, and otherwise falls back to the default cap. Team and pool ceilings do not clamp these keys.
One trap deserves attention. On a shared-pool key, leaving the monthly limit blank does not mean unlimited, even though the form's placeholder reads "Unlimited". The default cap applies, 100 USD unless your administrator changed it. Enter a number explicitly, and read the effective limit in the key's details panel.
Alerts
Alerts are instance settings, switched off by default, and they are configured by an administrator in Budgets & limits. If you want them, ask whoever runs your instance. They are not something an individual user opts into. There are two kinds:
- Budget alerts. One fires when a key reaches a soft threshold, 80 percent by default, and another when it reaches 100 percent of its monthly USD limit. Each threshold fires once per key per month. They cover the monthly USD limit only, not daily or token limits, and keys with no limit are skipped.
- Spend-spike alerts. For each enabled key, memtrunk compares today's spend with the median of the key's trailing active days. By default an alert fires when today is at least 3 times that median and at least 10 USD, using a 7-day window and requiring at least 3 days with spend.
Budget alerts reach the key's owner and administrators as in-app notifications. An administrator can also set a Slack-compatible webhook URL, and email is available when the server has SMTP configured; email is a server setting, not a console field. Spike events are recorded either way and are sent through the webhook or email when those are configured.
Burn rate and projections
The API keys list shows, per key: Monthly limit, This month, Budget, Projected and Est. exhaustion. They are estimates, computed by straight-line extrapolation:
- Burn rate is spend so far divided by the number of days elapsed.
- Projected month-end spend is spend so far divided by the fraction of the month that has passed.
- Estimated exhaustion is now plus the remaining budget divided by the burn rate. If the key is already at its limit, it reads as now. A key with no limit has none.
- Budget health is one of unlimited, ok, approaching (at or past the soft threshold) or over.
Early in the month a single busy day swings the projection a long way. Treat it as a prompt to look, not a forecast. Profile shows your own month-to-date spend against your limit.
How cost is calculated
Cost is estimated from configured prices. memtrunk multiplies token counts by a per-model price table in USD per million tokens, with separate prices for input, output, cache reads and cache writes. Administrators can override prices on the Pricing tab of the Usage page, and overrides win over the defaults. The figures are not invoices, and traffic served from a subscription is priced at API-equivalent rates. A cap in USD is a cap on this estimate, so if a price is out of date the cap is too.
What caps do not cover
- Requests authenticated with the per-user token URL. Caps belong to API keys. A request that authenticates through the token URL from Profile, Proxy connection, does not run the per-key check. Use API keys for any client where you need a limit.
- Local mode. An instance running without authentication has no per-key gate.
- Request-rate limits. Keys have no requests-per-minute or tokens-per-minute limit. The controls are USD and monthly tokens.
- Daily and token alerts. Alerts cover the monthly USD limit only.
- Memory over MCP. A key at its cap can still call the memory MCP server, because that route does not run the spend check. See memory for Claude Code and Codex CLI.
A setup that works
- Create one key per tool or person, so spend has a clear owner.
- Set the monthly limit explicitly on every key, including shared-pool keys.
- Add a daily window and limit for keys that run unattended, since a daily breach tells the client when to retry. The daily window resets at UTC midnight, not at your local midnight.
- Ask your administrator to turn on budget alerts, and set the webhook if you use Slack.
- Check the Projected and Est. exhaustion columns weekly, and use Test connection in the key's details panel when a key behaves unexpectedly.
For the wider picture of what memtrunk records and controls, see the features page and security.