A fail-closed AI gateway, live: stopping a runaway agent

July 30, 2026

For two weeks this series described a single thing: a fail-closed AI gateway — one door every model and agent call passes through, that meters the spend, caps it, and defaults to stopping, not spending. Describing controls is easy. So I built the minimal version in the open and pointed it at a deliberately broken agent, to show the one failure the whole series is about: the agent that loops on a broken tool all weekend and runs up a bill nobody’s watching.

Watch it live → · Read the code (MIT) →

What you’re looking at

The demo runs the same broken agent three ways, side by side:

Talk is cheap — so here it is, stopping the burn live.

The money is illustrative and the LLM is mocked, so you can run it as many times as you like without a real bill. The logic is the real thing.

Why the breaker is the piece that matters

Every quick answer to “how do I stop a runaway agent” reaches for a static cap — a bigger recursion limit, a token budget, a turn budget. Each one just delays the same burn: a broken tool hits the higher ceiling too, only later and more expensively. And a flat budget can’t separate working hard from stuck, because a real forty-step task burns tokens the same way a loop does.

The signal isn’t the size of the bill — it’s the repeat. The same call failing the same way, over and over, is the loop announcing itself. Watch for that and you can cut the one run that’s stuck without touching anything else. That’s what the demo does in about three seconds.

The honest scope

Learning in public means being straight about what’s built. The spine — meter, hard spend cap, per-user quota, token-aware rate limit, kill switch — and the per-run breaker are done and running. Auth, response caching, model routing, and the content guardrails are what I’m adding next, alongside these posts. It’s a work in progress, not a finished product — but it runs today, and the code is open for you to read or copy.

Why I built it in the open

A dashboard tells you you’re bleeding; it doesn’t stop it. The whole point of this gateway is to be the switch in the wire, not another chart. Building it in public is also the most honest demo I can give you of what I’d wire into your stack: not a slide about controls, but a working one you can click.


If you’re shipping an AI feature or running agents and you’re not sure where a loop could still run unbounded, that’s exactly the thing I map. Every message comes straight to me — I read and reply to each one myself, usually within a business day, and what readers send shapes what I build next. Send me your setup and I’ll show you where it’s still fail-open — free, within a day.

Free live workshop: cap your AI spend (Oct 8)

A hands-on 90-minute session — wire a fail-closed spend cap + cost-aware rate limiting against a real stack, live, so a leaked key or runaway agent can't run up your bill. Full details & times → Save your seat (and get each new lesson as it lands):

Double opt-in — one email to confirm. The lessons are free; the course is optional. No spam, unsubscribe anytime.