( Engineering Notes · 2026 · 4 min read )
Every LLM Call Needs a Budget: Metering, Rate Limits and Admission Control
Why every team shipping LLM features needs a governance layer, and how to build metering, budgets, rate limits and cost reconciliation that never get in the way.
The first LLM feature in a product is easy to run: one team, one API key, one bill. Then a second team ships an agent, customers start running workflows on their own, and a runaway loop burns a week's budget overnight. At that point you need a governance layer, and every developer building on LLMs eventually has to build or adopt one. This post covers why, and how to build one that is accurate and never gets in the way.
Why every LLM call needs governance
A good governance layer answers three questions for every call: who is calling, are they allowed to, and what did it cost?
How to build it well
1. Put identity on every call
Propagate tenant, organisation, agent and environment identity as metadata from the caller, through your LLM gateway, into the usage record. Anything that arrives without identity goes into an explicit "unattributed" bucket. Never guess an owner: billing the wrong customer is worse than admitting you do not know.
2. Admit at run start, and decide your failure policy explicitly
Check budgets and limits when a run starts, not halfway through it. Then decide what happens when the metering service itself is down. For most products the right answer is to fail open and alert, so an outage in billing never stops customers' work. Fail closed only on limits you have confirmed are hard.
async def admit(run: Run) -> Decision:
try:
return await metering.check(run.org, run.agent, run.model, timeout=0.5)
except MeteringUnavailable:
log.warning("metering unavailable, failing open", run_id=run.id)
alerts.increment("admission.fail_open")
return Decision.admit(reason="fail_open")3. Separate soft limits from hard limits
Soft limits notify; hard limits deny. Support both per organisation and per agent, and enforce RPM and TPM in sliding windows. Give operators a console to change limits without a deploy.
4. Never drop a usage event
Usage reporting must survive network blips and service outages. Write each event to a durable outbox with an idempotency key (for example run id plus call index), deliver at least once, and deduplicate on the receiving side. Bound the outbox and alert on its depth so a long outage is visible.
5. Watch your HTTP client under concurrency
A classic bug in async Python: creating a new HTTP client per call, or sharing one client across event loops. Under load this exhausts sockets (thousands in TIME_WAIT), latency climbs, and usage events silently fail to send. Keep one pooled, keep-alive client per event loop.
import asyncio
import httpx
_clients: dict[int, httpx.AsyncClient] = {}
def http_client() -> httpx.AsyncClient:
loop_id = id(asyncio.get_running_loop())
if loop_id not in _clients:
_clients[loop_id] = httpx.AsyncClient(
limits=httpx.Limits(max_connections=100, max_keepalive_connections=20),
timeout=httpx.Timeout(10.0),
)
return _clients[loop_id]In my own measurements, this one change cut median latency by 3.3x and socket churn by 62% at 250 concurrent LLM calls.
6. Reconcile against the real bill
Your meter is a model of the bill, not the bill. Reconcile it daily against the provider's billing data or invocation logs, priced at exactly the same rates, including cache-read and cache-write tokens. The gap is a signal: it reveals traffic that bypasses your gateway, models you price wrongly, and callers you have not identified.
7. Treat model routing as policy
Express "which models and regions may this tenant's data reach" as data, not code. Enforce it twice: in the calling service, and again as a backstop in the gateway that rejects any non-compliant request. Defence in depth matters here because one forgotten code path is enough to break a residency promise.