Written with Claude and checked against the tests it cites. Garmr itself is built the same way, and says so.

Say you give a developer a budget of one dollar a day for LLM calls. They have ninety-nine cents spent. Then their agent sends two hundred requests in the same instant.

How much do they spend?

If the answer is "about a dollar", the cap holds. If the answer is "about ten dollars", you have a number in a dashboard, not a cap. This post is about why the second answer is the easy one to build, what the first one costs, and a test you can run to see both.

Why the easy design overshoots

The obvious way to enforce a budget is two steps:

  1. Before sending a request, check that spend is under the cap.
  2. After the response, add what it cost.

Each step is correct. Together they leave a gap, because the cost of a request is only known when it finishes. Every request that arrives during that gap sees the same "under the cap" and goes through. With one request at a time you overshoot by at most one request. With two hundred at once, all two hundred pass the check.

Agents make this the normal case, not the edge. A coding agent fans out tool calls; a batch job starts fifty workers; a retry storm sends everything again.

Hold first, then settle

The fix is the one a bank uses for a card payment: put a hold on the money before the purchase, and settle the real amount afterwards.

In Garmr this is the hard cap, and it is the default. Before a request is sent:

  1. Estimate the most it could cost. Input is the body's size in bytes divided by three; output is the request's full max_tokens; both priced at the dearest rate that could apply.
  2. In one database transaction, lock the spend counters this request touches, check that the estimate fits under every cap, add the estimate to each counter, and write a reserve entry in the ledger. If it does not fit, roll back and refuse with a 403 that tells the client not to retry.
  3. Send the request.
  4. When it finishes, write a settle entry and move each counter by the real cost minus the hold.

There are nine counters for a request: the member, their team and the organisation, each by day, week and month. They are always locked in the same order, so two requests cannot deadlock.

Because the hold is taken inside the lock, the two hundred requests no longer see the same number. The first takes its hold; the second sees spend with that hold included; and so on until one does not fit.

The test

Garmr's repository has a test that does exactly the scenario above: a member capped at $1.00, inside a team capped at $2.00, and 200 requests sent at once through the real router and a real Postgres to a fake provider. It runs under both enforcement modes, with the provider answering four ways.

Under the hard cap:

The provider's answer Admitted Spent, against the $1.00 cap
Usage equal to the estimate 16 of 200 0.986×
No usage in the response 16 of 200 0.986× (the hold is what counts)
A stream cut short 16 of 200 0.986× (the hold is what counts)
Input 20% larger than estimated 16 of 200 1.042×

Under the soft cap, which checks without holding, the same 200 requests spend 10 to 11 times the cap. Everything in flight completes. That is what it is designed to do, and it is the check-then-charge result.

The test also asserts the bookkeeping: every admitted request has exactly one settle entry, and the ledger adds up to the counters.

What an audit found

A test written by the people who wrote the code checks what they thought of. So Garmr also has an audit with its own referee: a stub provider that writes down the true cost of every answer, in exact fractions, before sending it, and a script that compares that file with what the gateway recorded. It runs four workloads (heavy prompt caching, reasoning tokens, input over the estimate, and streams that end without their usage), in both API formats, under both caps.

In its first runs it found a real bug. An Anthropic stream whose last event carried no usage was settled as if it were complete, at the input and one output token. It was not flagged, so the spend counter read about half of what was really spent, and a hard cap enforces the counter: the burst could really cost up to 1.12 times the cap.

The fix treats such a stream as uncertain and counts the larger of its hold and what it saw. The audit was then rerun twice at a public commit, with the raw results committed beside the report:

  • Hard cap, 200 at once: at most 0.999 times the cap as counted (a burst whose usage never arrived, counted at its holds) and 0.78 times in true cost, in every workload and both runs. No request was counted below its true cost.
  • Soft cap, 200 at once: 3.6 to 26 times the cap as counted, 3.6 to 21 times in true cost.
  • Steady load: where the provider reports usage, all 12,000 requests in a run were recorded at exactly their true cost, cached and reasoning tokens included.

It also reports a criterion it missed. Under the soft cap, two bursts let fewer requests through in one run than in the other, because some settlements landed while the burst was still being admitted. So a soft cap's overshoot depends on timing and is not a fixed number. Nothing about the hard cap or accuracy moved between runs.

The honest limit: the referee follows Garmr's own documented reading of each provider's billing rules. It shows the gateway does what its documentation says, not that the documentation matches a provider's invoice. Checking against real bills is on the roadmap. The full report, method and raw files: docs/audit.md.

Where the 4% comes from

A hold bounds what is admitted. It cannot bound a request that turns out to cost more than its estimate.

In the last row, the provider reports 20% more input tokens than the estimate assumed. Input is 29% of the estimate in this test, so each request costs about 6% more than its hold, and spend ends at 1.042× the cap. Garmr's stated bound for the hard cap is 5% over, and the test fails if it is exceeded.

The honest caveat: the over-run scales with input's share of the estimate. A request that is nearly all input, with an estimate that is badly low, can go further. Dividing bytes by three undercounts dense text such as Chinese, Japanese or base64. Settlement always charges the real usage, so spend is never undercounted; it is only the size of the overshoot that can widen.

What you do when you cannot tell

Rows two and three matter as much as row four. Sometimes a gateway does not learn what a request cost: the client disconnects mid-stream, or the response carries no usage block.

Counting those as zero is the tempting choice, and it turns every dropped connection into free spend. Garmr counts the larger of the hold and whatever part it did see. The cap fails closed.

What this costs

Holding first is not free, and a write-up that skipped this part would be selling something.

  • A database write before every request. The hold is durable, so it survives a crash. The hard cap makes six round trips to Postgres per request and the soft cap two, so what it adds is mostly those round trips times the distance to the database: keep Postgres in the gateway's zone. Garmr's latency benchmark is being rerun at a public commit this week, with its raw results committed; until then this post quotes no latency figure. When it lands it will be in docs/benchmarks.md.
  • Requests are refused that would have fitted. Each request holds its full max_tokens. That is why only 16 of 200 were admitted above: sixteen worst-case holds fill a dollar. Near a cap, a large request can be refused even though its real cost would have fitted. Setting max_tokens to what you need matters more behind a hard cap.
  • One organisation's requests take turns. They all lock the organisation's counters, which works out to about 320 admissions a second at a one-millisecond database round trip (Garmr's operations guide). More gateway instances do not raise that. Garmr's roadmap has a fix; today, a very large organisation should use the soft cap.

If a few milliseconds matter more to you than an exact limit, the soft cap is the right choice, and Garmr lets you pick per organisation. The point is to choose knowing which one you have.

Run it yourself

You need Rust and Docker.

git clone https://github.com/garmrhound/garmr cd garmr docker compose up -d --wait postgres cargo test --test overshoot cap:: -- --nocapture 

Each run prints a line like:

[overshoot] strict, input 120%: admitted 16/200 (0 busy answers retried), held 0.9860 USD, spent 1.0420 USD = 1.042x the cap 

strict and loose are the configuration's names for the hard and soft cap. The audit is one more command, needing Python 3.11 or later and psql, and takes a few minutes:

scripts/audit/run.sh 

Whatever gateway you use, the same experiment is worth an afternoon: set a small cap, send a few hundred requests at once, and read the provider's bill.

Garmr is an open-source LLM gateway in one small container, written in Rust, that enforces dollar and token budgets per member and per team. It is built with Claude Code, with tests written first. The code, and the list of what it does not do yet, are at github.com/garmrhound/garmr.