Skip to content
se.hadi/

Open source · Go · MIT

Governor: a hard spending cap for LLM calls

A lightweight Go gateway between your app and the model provider. It reserves cost before a request goes out, meters every streamed chunk, and cancels the upstream request the moment the cap would be crossed.

Try it against the mock provider, no API keys needed:

go run ./cmd/mockprovider &

go run ./cmd/governor

curl -X POST localhost:8080 -d '{"model":"fake","messages":[{"role":"user","content":"hi"}]}'

The problem

LLM spend is invisible until the invoice arrives. Most tools report cost after the fact, and the ones that enforce limits are heavyweight proxies to run. Governor answers one question in one binary: can this request afford to run, and should this stream keep going?

Architecture: your app sends requests to Governor, which reserves estimated cost against the cap, then forwards to OpenAI, Anthropic or a mock provider and meters each streamed chunk against the cap.Your appGovernor1. reserve cost vs cap2. meter every chunk3. cut off + refundrequeststreamOpenAIAnthropicMock providerbudget store: in-memory (default) or Redis (opt-in)
The original hand sketch: App, Governor gateway with cost and cap, and the providers
Where it started: the first sketch, with the question “cap check without added latency?”

How it works

Proof

Benchmark: reserving budget

StoreTime per reserve
In-memory (default)≈ 25 ns/op
Redis (opt-in)≈ 96 µs/op

An in-process mutex vs a network round trip to a local Redis, on an 11th Gen Intel i7-1165G7. Reproduce with go test ./internal/budget/... -bench=BenchmarkReserve -run=^$.

Concurrency: no double spend

200 goroutines race for a cap that fits exactly 20 reservations, under Go's race detector. Exactly 20 get through, on both the in-memory and the Redis store.

Tests under -race, then the benchmark.
Terminal: go test with -race passing TestReserve_AllowsUnderCap, DeniesOverCap and ConcurrentNoDoubleSpend for memory and Redis, then the benchmark at 25.28 ns/op in memory and 95,577 ns/op on Redis

Mid-stream cutoff

A mock provider streams 30 chunks with a $0.06 cap. Governor forwards 7, then sends budget_exceeded and tears down the upstream connection: the result is Completed: false, CanceledByClient: true, and no connection is left open to the provider.

Chunks 0–6 go through; the 8th would cross the cap.
Terminal: the mock provider streams chunk-0 to chunk-6, then Governor returns finish_reason budget_exceeded, and a check shows no connection left to the mock provider

Honest limits

Compared with LiteLLM, Helicone and Portkey

Links

Chat on WhatsApp

I reply within 24 hours