{"slug": "spend-caps-for-ai-agents-designing-budgets-that-stop-a-runaway-loop-before-the", "title": "Spend caps for AI agents: designing budgets that stop a runaway loop before the invoice does", "summary": "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.", "body_md": "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\".\n\nA 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.\n\n| Level | Catches | Typical form | \n|---|---|---|\n| Per task (one agent run) | Retry loops, one oversized run | Max turns, max wall time, max compute for this run | \n| Per agent or per integration | A misbehaving agent or a bad deploy of one | Daily compute and token budget | \n| Per project or team | One team's experiment eating the month | Monthly budget with an alert threshold | \n| Organization | Everything else | Hard monthly ceiling | \n\nThe 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:\n\n``` js\nconst MAX_TURNS = 10;\nconst MAX_WALL_MS = 30 * 60 * 1000;\nconst started = Date.now();\n\nfor (let turn = 1; ; turn++) {\n  if (turn > MAX_TURNS || Date.now() - started > MAX_WALL_MS) {\n    await escalate(task, { reason: 'budget', turn }); // a person looks at it\n    break;\n  }\n  const done = await runOneTurn(task);\n  if (done) break;\n}\n```\n\nDecide this before the first cap trips, per level:\n\nTwo 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.\n\nEvery 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.\n\nFrom [pricing](https://sandscale.dev/pricing) and [the API limits docs](https://sandscale.dev/docs/api/limits):\n\n`402 free_monthly_limit` with when it resets.`quota_exceeded`.\nWhat Sandscale cannot see is your model spend. Put token budgets in your orchestrator or your model gateway, at the same levels as above.\n\nPair 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.\n\n*Originally published on the [Sandscale blog](https://sandscale.dev/blog/spend-caps-for-ai-agents).*", "url": "https://wpnews.pro/news/spend-caps-for-ai-agents-designing-budgets-that-stop-a-runaway-loop-before-the", "canonical_source": "https://dev.to/sandscale/spend-caps-for-ai-agents-designing-budgets-that-stop-a-runaway-loop-before-the-invoice-does-3dma", "published_at": "2026-11-26 04:38:40+00:00", "updated_at": "2026-11-26 04:43:24.048677+00:00", "lang": "en", "topics": ["ai-agents", "ai-infrastructure", "mlops"], "entities": ["Sandscale"], "also_reported_by": [], "takeaway": "Enforce per-task budgets in the orchestrator (e.g. 10 turns or 30 minutes) that escalate to a human, and set sandbox lifetimes shorter than a person's detection time so orphaned boxes stop themselves.", "recommendation": {"advice": "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.", "playbook": "production-ops", "playbook_url": "https://wpnews.pro/playbooks/production-ops"}, "alternates": {"html": "https://wpnews.pro/news/spend-caps-for-ai-agents-designing-budgets-that-stop-a-runaway-loop-before-the", "markdown": "https://wpnews.pro/news/spend-caps-for-ai-agents-designing-budgets-that-stop-a-runaway-loop-before-the.md", "text": "https://wpnews.pro/news/spend-caps-for-ai-agents-designing-budgets-that-stop-a-runaway-loop-before-the.txt", "jsonld": "https://wpnews.pro/news/spend-caps-for-ai-agents-designing-budgets-that-stop-a-runaway-loop-before-the.jsonld"}}