cd /news/ai-agents/spend-caps-for-ai-agents-designing-b… · home › topics › ai-agents › article
[ARTICLE · art-176518] src=dev.to ↗ pub= topic=ai-agents verified=true sentiment=↑ positive

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.

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 playbook: Running AI in production
by read2 min views5 publishedNov 26, 2026

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:

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 covers how to pick it alongside command and request timeouts.

From pricing and the API limits docs:

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 for the cases where you do not want to wait for any budget.

Originally published on the Sandscale blog.

── more in #ai-agents 4 stories · sorted by recency
── more on @sandscale 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/spend-caps-for-ai-ag…] indexed:0 read:2min 2026-11-26 · —