AI agent burning through tokens? How to cap the budget before the bill spikes
You write an agent, give it a loop, and go to sleep. You wake up to a $400 API bill because it got stuck trying to fix a typo all night. This happens more than anyone admits — and the fix is a mid-run budget check that catches the burn before it becomes a bill.
Why agents burn tokens
Every iteration of an agent's loop is a model call, and every model call burns tokens. When a run has no stop condition — or is stuck in a loop — it keeps calling the model long after it stopped making progress. Two things compound it:
- Loops. The single biggest cause. A repeated-tool-call loop turns a 3-step task into a 73-step token bonfire. (If that's your symptom, start with loop detection.)
- Growing context. Context accumulates every step, so the 30th model call is far more expensive than the 3rd — the burn accelerates as the run goes on.
And like a loop, runaway burn is silent. The agent looks busy right up until the bill lands.
Catch it mid-run — free, under 150ms
The fix is to check your budget while the run is happening, not after. budget_guard takes your live counters and returns advisory warnings with severity and concrete suggested actions — before you overrun:
budget_guard({
tokens_used: 8200, token_budget: 10000,
cost_usd: 0.42, cost_budget: 0.50,
step: 18, step_budget: 25,
context_pct: 0.86 })
// → { warnings: [{type:"token_burn", severity:"critical",
// suggestions:["trim context", "wrap up", ...] }] }budget_guard checks context-limit approach, token-burn rate, cost-burn rate, and step budget in under 150ms with no LLM — cheap enough to call on every step. When it warns critical, the agent can trim context, wrap up, or hand off before the overrun instead of after.
How to cap the budget properly
- A hard cost cap per session — the backstop nothing crosses ("max $2.00 per run").
budget_guardon each step — the early-warning system that lets the agent react before the cap.- Loop detection alongside it — since a loop is the usual cause,
detect_loopstops the root cause, not just the symptom. - Context trimming — when the guard flags context approaching its limit, summarise or prune so later calls don't balloon.
These guards are free and unlimited by design — the safety checks that protect your bill should never be a cost you hesitate over calling.
Machine-readable guide: https://snapback.sh/llms.txt · Tool discovery: https://snapback.sh/.well-known/mcp.json · MCP endpoint: https://api.snapback.sh/mcp. Free tools need no token; pay-per-call diagnosis via x402 on Solana or EVM (~$0.0032 USDC).