Spend caps for AI agents: designing budgets that stop a runaway loop before the invoice does A Sandscale engineer outlined a tiered spend-cap design for AI agents, placing budgets at the per-task, per-agent, per-project and organization levels so runaway retry loops and orphaned sandboxes are stopped before they hit the invoice. The approach pairs orchestrator-level limits on turns and wall time with loud, specific refusal messages and caps that agents cannot raise themselves, plus sandbox lifetimes short enough to self-terminate orphaned boxes. A person who writes a bad loop notices when the laptop fan spins up. An agent does not notice anything. It will retry a failing test forty times, start a sandbox per file in a large repository, or leave a box running because the process that owned it crashed. None of that is malicious, and all of it shows up on a bill. Spend caps are how you turn "we found out at the end of the month" into "the run stopped and told someone". A single organization-wide cap is a fire alarm: useful, but by the time it trips, everything stops for everyone. Put budgets at the level where the problem happens. | Level | Catches | Typical form | |---|---|---| | Per task one agent run | Retry loops, one oversized run | Max turns, max wall time, max compute for this run | | Per agent or per integration | A misbehaving agent or a bad deploy of one | Daily compute and token budget | | Per project or team | One team's experiment eating the month | Monthly budget with an alert threshold | | Organization | Everything else | Hard monthly ceiling | The per-task budget does most of the work. A task that has used ten turns or thirty minutes without passing its tests should stop and hand over to a person. That limit lives in your orchestrator, and it is the cheapest control you will ever write: js const MAX TURNS = 10; const MAX WALL MS = 30 60 1000; const started = Date.now ; for let turn = 1; ; turn++ { if turn MAX TURNS || Date.now - started MAX WALL MS { await escalate task, { reason: 'budget', turn } ; // a person looks at it break; } const done = await runOneTurn task ; if done break; } Decide this before the first cap trips, per level: Two rules make caps trustworthy. First, a refusal must be loud and specific: "project payments hit its daily compute cap" is actionable; a generic error is not. Second, the agent must not be able to raise its own cap. Caps are set by people, through a path the agent cannot call. Every sandbox should have a lifetime shorter than the time it takes a person to notice a problem. That single setting handles orphans: if your process dies, the box stops itself. On Sandscale, timeoutSeconds on create sets it server default one hour . Our post on three timeouts for agent sandboxes https://sandscale.dev/blog/timeouts-for-agent-sandboxes covers how to pick it alongside command and request timeouts. From pricing https://sandscale.dev/pricing and the API limits docs https://sandscale.dev/docs/api/limits : 402 free monthly limit with when it resets. quota exceeded . What Sandscale cannot see is your model spend. Put token budgets in your orchestrator or your model gateway, at the same levels as above. Pair this with a kill switch https://sandscale.dev/blog/kill-switch-for-ai-agents for the cases where you do not want to wait for any budget. Originally published on the Sandscale blog https://sandscale.dev/blog/spend-caps-for-ai-agents .