Enforcing hard spending limits in an AI gateway

How an AI gateway makes a spending limit a hard ceiling for concurrent agents: reserve each call's worst-case cost before dispatch, fail closed without a bound, and settle interrupted calls once.

October 20264 min readAgent systemsPlatformsVerificationSession gateway and broker

A single iron-grey stroke thins across warm chalk from the upper left, stopping short of a small scraped plane of pale rust.
On this page
  1. The reservation invariant
  2. Bounding worst-case request cost
  3. Settling interrupted calls once
  4. Keeping reservations out of spend
  5. What the ceiling covers

When an autonomous agent running a dozen concurrent model calls crosses its daily budget, every call already in flight still finishes and lands on the bill. Counting spend as calls settle and blocking subsequent requests works reasonably well for human-paced traffic, where one extra turn barely registers and coordination stays cheap. Against unattended agent loops running overnight, however, the in-flight overshoot can account for most of the bill. Based on their documentation in November 2026, both Cloudflare and Vercel accept that trade-off: Cloudflare documents its AI Gateway spend limits as eventually consistent with possible concurrency overshoot, and Vercel documents its AI Gateway budgets as soft caps that exclude bring-your-own-key spend.

Enforcing a spending limit as a ceiling rather than an alert requires admission control by reservation, the authorize-then-capture pattern of card payments: reserve each request’s worst-case cost before dispatch, then settle to actual usage afterwards. Hard enforcement under concurrency is where those gateways leave a gap, so I built that reservation model into Ferrite’s AI gateway, a Rust service on my application platform through which hosted apps call OpenAI, Anthropic, Google, and Amazon Bedrock models. The gateway meters calls in microcredits against account credit or a customer-supplied provider key, paired with a dashboard for setting budgets and reading the account statement.

The reservation invariant

Budgets apply simultaneously across UTC day and calendar month windows and across three scopes: the account, an individual gateway key, and a customer-supplied provider key. For every applicable window and scope, settled cost plus outstanding reservations plus the incoming request’s upper bound must not exceed the limit.

To keep concurrent requests from racing past a shared cap, the gateway stores the window counters, child-scope totals, and active reservation references in a single bounded per-account admission record, checking every applicable budget and installing the new reservation in one compare-and-swap before dispatch. A customer-supplied provider key retains its logical identity across secret rotation, so rotating credentials cannot reset accumulated spend. When any scope lacks headroom for the incoming bound, the gateway rejects the request with a non-retryable HTTP 429 naming the binding scope, giving callers an explicit stop signal instead of a transient error to retry.

Bounding worst-case request cost

Pre-dispatch reservation holds only when the reserved amount is a defensible upper bound, and a byte-count heuristic is not one. Admission computes each request’s upper bound from a versioned price schedule paired with explicit caps on output tokens, extended thinking, tool calls, and media. Any hard-limited request with an unknown price or an unbounded billable feature fails closed at admission instead of dispatching on a guess.

Pricing contracts can still drift if a provider changes billing behavior or exceeds a documented limit. When actual cost exceeds the reserved bound, the gateway records the real charge, freezes further admission, and never clips cost to make the cap appear intact, which would only hide the overrun until the provider invoice arrives.

Settling interrupted calls once

Holding reservations before dispatch protects the ceiling only if interrupted streams and process crashes resolve every open hold deterministically and once. When a call breaks or times out, the gateway resolves the hold under one policy: settled from provider usage when it arrived, retained as usage-unknown when the provider may have performed work, and refunded only when the call provably never left.

Crash recovery introduces another race, because a retry, a crash sweep, and a restarted worker can all pick up the same open operation concurrently. Before writing to the billing ledger, the gateway installs a settle or refund intent onto the operation record via compare-and-swap. Once recorded, that intent is immutable, so every recovery path replays the same decision instead of re-evaluating live state.

In an end-to-end check, an agent turn ran two model calls at 13 microcredits each for 26 microcredits total; replaying the turn kept the same charges, and the next request stopped at the daily cap with a 429 and no provider call.

Keeping reservations out of spend

Admission reservations introduce a second kind of money: held headroom that is neither settled spend nor available credit. Treating returned reservations as ordinary ledger transactions corrupts customer reporting. The dashboard overview once disagreed with the statement by 1,521 microcredits for the same balance because returned reservations were counted as both a charge and funding. The overview now displays only the balance, leaving the statement as the single authority for financial history, and missing usage telemetry renders as unavailable, never zero, so an unreported call does not pass for free inference.

What the ceiling covers

The hard ceiling applies strictly to gateway-metered spend on routes with certified pricing and capability bounds; unpriced modes stay off hard-capped paths.

To verify the state transitions behind reservation, settlement, refund intent, and budget accounting, I modeled the gateway’s control state in Lean 4, a theorem prover. The suite proves 256 theorems over that control state and catches 136 deliberately broken implementations. Those proofs qualify the control-state model, not the compiled Rust gateway, which is checked against it with generated traces and integration tests.

Chris Chabot, October 2026