# Spend caps for AI agents: designing budgets that stop a runaway loop before the invoice does

> Source: <https://dev.to/sandscale/spend-caps-for-ai-agents-designing-budgets-that-stop-a-runaway-loop-before-the-invoice-does-3dma>
> Published: 2026-11-26 04:38:40+00:00

**Recommendation:** Set per-task turn and wall-time caps in your orchestrator that escalate to a person, and give every sandbox a lifetime shorter than your detection time so orphans self-terminate.
More in the playbook "Running AI in production": <https://wpnews.pro/playbooks/production-ops>

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).*
