On this page
How to Set a Spend Limit for a Coding Agent
Limit at three levels: per run, per day, and per account. A limit that is checked after the fact is a report, not a control.
Quick answer
- Set a per-run ceiling and enforce it inside the loop, before the next model call, not in a nightly report.
- Add a daily cap so a runaway loop cannot spend a month’s budget in an hour.
- Set a provider-side account limit as the last line of defence.
- Make the limit visible to the agent: when it is close, the agent should wrap up rather than be cut off mid-step.
- Track cost per completed task, not cost per token, because a cheap run that produces nothing is expensive.
Where should the limit be enforced?
Inside the loop, before each model call, using the usage returned by the previous call. If the accumulated cost exceeds the run budget, stop and return a partial result. Checking after the run is a report; checking before the call is a control.
Then add the outer layers: a daily cap at the queue or worker, and a hard cap in the provider account. Layers matter because bugs are possible in any single enforcement point.
run_budget = min(task.budget, remaining_daily_budget)
before each model call:
if spent + estimated_next > run_budget: stop("budget_exhausted")
if daily_spent >= daily_cap: pause_queue()
provider account: hard monthly limit set in the consoleHow do you choose the number?
Start from a measured task. Run the workflow ten times, record the cost distribution, and set the budget above the observed maximum but below the point where a runaway loop becomes painful. A budget that is too tight produces partial work; too loose and it never triggers.
Differentiate by task class. A review of a small diff and a repository-wide refactor should not share a ceiling. The budget belongs to the task definition, alongside its time limit and tool list.
What should happen when the limit is hit?
Stop cleanly, persist the run state, and report what was completed and what remains. A hard kill that loses the checkpoint wastes everything spent, and the next attempt starts from zero.
Warn before the limit if the harness can estimate remaining work. An agent that knows it has a small budget left will summarise and stop; one that is cut off mid-edit leaves a broken tree.
How do you keep costs visible?
Attribute cost to a task, a run, and a team, not just to an account. The useful question is which task class is expensive relative to its value, and an account total cannot answer it.
Review the top ten runs by cost each week. The distribution is usually skewed, and the outliers explain most of the bill. That review is also how you find the loop that has not failed yet.
Where this bites vibecoders
The first setup has no limit: the agent retries until it succeeds, and a loop can spend more in an afternoon than the project’s entire monthly budget. Put the per-run cap in the loop before adding another tool.
Where AI coding assistants get this wrong
- Enforcing the budget only in a daily report.
- Using one budget for every task class.
- Killing the process without persisting state.
- Assuming a subscription plan makes cost unbounded in practice.
- Tracking cost per token instead of per completed task.
- Never reviewing the most expensive runs.
Checklist
- Set a per-run budget from measured tasks.
- Check it before each model call.
- Add a daily cap at the worker.
- Set a provider-side account limit.
- Persist state when the budget is exhausted.
- Review the top runs by cost weekly.
FAQ
Does a monthly subscription plan need a limit?
Yes, of a different kind: a usage limit. Plans throttle or require upgrades when usage is high, so an expensive loop still costs you time and may block other work. A per-run budget also stops a loop that is producing nothing.
How accurate is a token estimate?
Rough, because output length is unknown in advance. Use the previous call’s usage as the baseline and allow a margin. The goal is to stop a runaway, not to predict the cost to the cent.
What is a reasonable per-task budget?
Whatever your measured maximum is, plus a margin. For a small review it may be a fraction of a dollar; for a long refactor it may be several. The number matters less than having one and revisiting it from data.
Should the agent see the budget?
Yes. Telling it the budget and the remaining amount changes its behaviour: it prioritises, summarises, and stops instead of exploring. That is a cheaper way to stay under a limit than cutting the connection.
Related topics
- What AI Coding Actually Costs Per Month
- How to Set a Token Budget Per Task
- Why Did My AI Coding Bill Spike?
- What Is Rate Limiting for AI Coding Tools?
- How to Deploy an AI Agent to Production
- How to Run a Coding Agent in CI