An LLM API bills you per token, as fast as it can generate them. An agent that gets stuck in a loop — a bad stop condition, a retry storm, a compromised key — doesn't slow down when the bill climbs. It bills tokens until something stops it. This is how you make that something a number you chose.
If you read one line: the only spend cap that saves money is one enforced before the request reaches the provider. A billing alert emails you after the money is gone; a gateway cap rejects the over-budget call for free. Set the gateway cap.
The one rule: reject before you pay#
There are two places a "spend limit" can live, and only one of them protects your wallet.
- At the gateway (LiteLLM, OpenRouter, a cloud AI gateway): the proxy checks accumulated spend on every request and rejects the call if it would exceed the budget. The rejected call never reaches the model, so it costs nothing. This is a real cap.
- At the provider's billing console (Anthropic, OpenAI): you get an email or a soft alert once spend crosses a threshold. The money is already gone. This is a smoke detector, not a circuit breaker.
Use the console alert as a backstop. Make the gateway cap your primary control. Here's how in the two gateways most builders actually run.
LiteLLM: budget on a virtual key#
If you already route through a LiteLLM proxy, a spend cap is a field on the key. Generate a key with a budget and a reset window:
curl -s http://localhost:4000/key/generate \
-H "Authorization: Bearer $LITELLM_MASTER_KEY" \
-H "Content-Type: application/json" \
-d '{
"key_alias": "research-agent",
"max_budget": 25,
"budget_duration": "30d",
"models": ["claude-opus-5", "gpt-5.6"]
}'
max_budget is the ceiling in dollars; budget_duration is the rotation window (24h, 30d, and so on). LiteLLM tracks spend per key and blocks any request that would push the key over budget, then resets at the end of the window. Two refinements worth knowing:
- Per-model caps. Add
model_max_budgetto cap an expensive model separately — e.g. let a key spend freely on a cheap model but throttle its Opus usage. - Multiple windows. Use
budget_limitsto run more than one cap at once — a$5/dayand a$25/monthon the same key, each resetting on its own clock.
Give every agent its own key. A per-agent budget means a misbehaving agent burns its allowance and stops, instead of draining a shared pool that also feeds your production traffic.
OpenRouter: a per-key limit with zero infra#
No proxy to run? OpenRouter puts the same control at its edge. Every key can carry a credit limit and a limit_reset of daily, weekly, or monthly — on any plan, free or paid. Create a capped key through the management API, then confirm it:
# read back the cap and what's left on the current key
curl -s https://openrouter.ai/api/v1/key \
-H "Authorization: Bearer $OPENROUTER_API_KEY"
# → { "data": { "limit": 5, "limit_remaining": 3.71, "limit_reset": "daily", ... } }
Once a key hits its limit, OpenRouter rejects further requests before they reach the provider, so they incur no upstream cost, and the allowance resets at midnight UTC on the schedule you chose. A $5 daily limit means a compromised key or a runaway loop caps the damage at five dollars, and you're back to normal the next day.
The caveat nobody prints#
Here is the sentence missing from most tutorials: the budget check runs per request. When many calls fire at the same instant — exactly what a parallel agent does — each can pass the check before any of them has recorded its spend, so the total can slightly overshoot the cap before it finally trips.
A gateway spend cap is a strong brake, not a hard financial wall. Set it a little under your true ceiling, keep a provider-level budget as a backstop, and never treat a single per-request cap as a guarantee.
For most builders the overshoot is cents, not dollars, and the cap still turns an unbounded disaster into a bounded annoyance. Just size it with that slack in mind. If you need a limit an agent physically cannot spend past — durable across restarts and safe under concurrency — build the accounting yourself with an atomic ledger, as we walk through in a hard spend cap that survives restarts; and remember that a gateway's own accounting is only as trustworthy as its bookkeeping.
Where this connects#
This is the cap Claude Code's v2.1.225 spend-limit warning surfaces: when a session trips your gateway budget, the message now names the cap and its reset time instead of failing cryptically. But the warning is downstream of the work here — it can only report a cap you already set.
The takeaway#
Decide the number before the loop does. Set a per-key max_budget in LiteLLM or a per-key limit in OpenRouter so an over-budget request is rejected for free, give each agent its own capped key, size the cap slightly under your real ceiling to absorb burst overshoot, and keep a provider billing alert as the backstop. Do that and the worst case stops being a surprise invoice — it becomes a line item you chose.



