Stopping runaway agent spend before the bill: pre-call guardrails and three gaps found the hard way

MaverikSh · reddit · 2026-11-05

The author (who builds the Cognocient AI LLM gateway) shares engineering practices for agent cost guardrails: most cost tools report spend after the provider billed you, while a runaway loop can burn hundreds of dollars in minutes — so checks must run before a call goes out.

Core techniques:

Three gaps in v1: clock-minute counting let spend straddle two minutes and pass a $30/min limit (fixed with 1-second buckets over a sliding 60s window plus a 10s burst window); the breaker didn't latch, so a blocked loop just waited and resumed (now refuses all calls for 5 minutes with Retry-After); a key-wide trip stopped every agent instead of the looping one (added optional per-run $/min limits).

Trade-off: it all sits in the request path (one base URL change, some latency); in-flight calls still complete and get billed. If you only need after-the-fact reports, a read-only billing integration is simpler.

Related event: Developers share guardrails to stop runaway agent costs(2 posts)→

Original post →

More from coding & agent

coding & agent channel →