DEV Community

Barrboss
Barrboss

Posted on

My agent burned $147 in ~40 minutes… so I wrote a small circuit breaker

Last week one of my small AI scripts went off the rails.

It got stuck retrying a step while streaming responses and kept hitting the API again and again.

By the time I noticed the usage jumped about $147 in less than an hour.

So I wrote a small Python “circuit breaker” that stops requests if a rolling token or cost limit is exceeded. It can even cut a streaming response mid-generation.

Nothing fancy. Just a single Python file that runs locally.

If this might help someone, I shared it here:

https://gist.github.com/alexdirochian-star/730122004e289bbc6f6b42d4f431656f

How are you guys preventing runaway token usage in agents?

Top comments (0)