How do you put a hard spending cap on an agent so a runaway task can't burn through credits?
I had an agent get stuck retrying the same failing step overnight, and it quietly racked up far more usage than the task was worth. Nothing broke, but it was a wake-up call.
What I've tried since:
- A max number of loop iterations per task
- A token budget tracked in the loop itself, with the agent told how much is left
- A provider-side usage limit as the last line of defence
The provider limit feels too blunt, and the in-loop budget only works if I remember to wire it in. How do you cap spend per task without killing legitimate long runs?
Comments · 3
Set caps at the gateway level rather than in the agent code. A hard limit on tokens per run and per day, plus a max step count, stops loops cleanly.
Add alerts at 50% and 80% of budget so you're notified before the hard stop. Hard caps prevent disasters but alerts give you time to react.
Also detect repeated failures: if the same step fails three times, halt and ask for a human. Cheap and it catches the overnight loop.