DEV Community

rene
rene

Posted on

An autonomous agent burned $40 of API credits overnight — the 3-line guard that stopped it

An autonomous agent burned $40 of API credits overnight — the 3-line guard that stopped it

Autonomous agents are great until one gets stuck in a retry loop at 2am.

Last month an agent I run burned $40 of API credits in a single night because a single while loop retried a failed call forever. No platform warning, no rate limit kicked in — just a growing bill and a surprise the next morning.

The fix that stopped it, in three lines:

def call_llm(prompt, budget):
    if budget.spent > budget.ceiling:
        raise BudgetExceeded("hard stop")
    return client.chat(prompt)
Enter fullscreen mode Exit fullscreen mode

That's a hard cost ceiling checked before every call, plus a kill switch that halts the entire run when it trips.

The full pattern (now in production)

  1. Coupled kill switch. The spend tracker and the executor share one flag. When the budget hits the ceiling, everything stops — not just the one call.
  2. Rate limits on every irreversible action. An agent that can email, tweet, or buy needs a per-hour cap, or it will spam.
  3. Record actual spend after every call — not an estimate. You can't fix a bill you never measured.

I keep a 5-field cost log — timestamp, model, tokens, cost, run-id — so I can see which run is leaking, not just that the total went up. That's the difference between "the bill is too high" and "run #7 is retrying itself into a hole."

Why this beats watching a dashboard

Most teams find out about agent cost from an invoice weeks later. A guard that lives in the loop stops the leak in the moment, and a kill switch that's coupled to the spend means a runaway can't outrun its own budget.

If you want the checklist I use before shipping any autonomous agent, it's free: the shipping checklist.

And if you'd rather have the whole thing already wired up — the cost logger, the coupled kill switch, and the budget guard as a ready-to-run module — I packaged it as the LLM Cost Tracker.

Top comments (0)