AI 智能體支出上限:如何設計預算,在失控循環造成帳單前停止執行
Spend caps for AI agents: designing budgets that stop a runaway loop before the invoice does
Sandscale 提出為 AI 智能體設置分層支出上限,以便在重試循環、過度扇出或孤立沙箱擴大支出前停止執行或通知負責人。文章建議在任務、智能體或整合服務、專案及組織四層設限,任務層可設定最大回合數、執行時間及運算量,觸限後升級交由人員處理。
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".
Where agent spend runs away
- Retry loops. The model's fix fails, it tries again, forever. Each attempt runs commands and calls the model.
- Fan-out. A planner splits work into subtasks and starts a sandbox for each. A bad plan can mean hundreds.
- Orphans. Your worker crashes or is redeployed after creating a sandbox. Nothing stops it.
- Oversized boxes. Every run gets the build-server size because one run once needed it. See sizing a sandbox.
- Model tokens. Often the largest line, and outside the sandbox provider's view entirely.
Budgets at four levels
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;
}
What should happen when a cap is hit
Decide this before the first cap trips, per level:
- Per task: stop the run, keep its sandbox paused or its record intact, and notify the owner with what it was doing. Do not silently drop it.
- Per agent or project: refuse new runs, let running ones finish their current step, and alert.
- Organization: refuse new work. Whether to stop running boxes is a business decision; stopping them loses work, letting them run spends money.
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.
Lifetimes are caps too
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.
What Sandscale provides
From pricing and the API limits docs:
- Spend caps per task, agent, project and organization on Pro, Team and Enterprise.
- A choice when included hours run out on paid plans: turn on usage beyond the plan at $0.05 per awake vCPU-hour, buy a 500-hour pack for $10, or keep it off so nothing past your plan is charged and new boxes wait for next month. Running boxes are never stopped.
-
Free has no usage beyond its 100 included hours: new boxes wait until the allowance renews, and the API answers
402 free_monthly_limitwith when it resets. -
Plan quotas on boxes at once and vCPU per box are refused before anything starts, with
quota_exceeded. - Awake-time billing. Paused time and idle time between tool calls do not count against awake vCPU-hours, so a waiting agent does not burn its budget while it waits.
What Sandscale cannot see is your model spend. Put token budgets in your orchestrator or your model gateway, at the same levels as above.
A checklist
- Every task has a turn limit and a wall-time limit, and escalates to a person when it hits them.
- Every sandbox has a lifetime.
- Fan-out has a maximum per task.
- Each project or agent has a budget with an alert below the hard limit.
- The organization has a ceiling, and you know whether running work stops at it.
- The agent cannot change any of these.
- You have rehearsed what a cap trip looks like, including who gets notified.
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.
來源:DEV Community · dev.to