AI agent cost comes from three places: the context you resend, the retries you never see, and the tasks that never converge. An agent with no budget can spend a month of tokens in an afternoon. Not because it is malicious, but because a retry loop, a tool that returns too much, or a task that never converges will happily run until something stops it.
The controls are not complicated. They are just usually missing.
The five controls
1. A per-task token budget
Every task gets a ceiling before it starts. Classification gets a small one. A research task gets a larger one. The agent cannot exceed it, and the router records when it hits the ceiling.
The budget is not a suggestion in the prompt. It is a check in the runtime. A prompt that says "be concise" is not a control.
2. A per-session ceiling
A session can contain many tasks. The session ceiling catches the case where each task is small and the total is not.
When the ceiling is reached, the session stops and a person decides whether to continue. That decision is the point.
3. Two deadlines
A soft deadline flags a slow call while it finishes. A hard deadline abandons it and records a failure. One timeout cannot express both.
On the local tier I use 10 seconds soft and 20 seconds hard. The numbers should come from your latency profile, not from a template.
4. A retry cap and loop detection
Three retries is usually enough. After that, the task escalates.
Loop detection catches the agent that calls the same tool with the same arguments three times. That pattern is almost always a failed assumption, not a hard problem.
5. A monthly alarm and a kill switch
An alarm at 80 percent of the expected monthly spend gives a person time to act. A kill switch stops the system.
A kill switch you have never pulled is a hope. Pull it in staging, time the recovery, and write the procedure down.
What the controls look like together
| Control | Scope | Action when hit |
|---|---|---|
| Task budget | One task | Stop, return partial, log |
| Session ceiling | One session | Pause for a person |
| Soft deadline | One call | Warn, keep waiting |
| Hard deadline | One call | Abandon, fall back |
| Retry cap | One task | Escalate |
| Loop detector | One task | Stop, flag the pattern |
| Monthly alarm | Whole system | Notify the owner |
| Kill switch | Whole system | Stop all execution |
Eight controls. Most deployments have the monthly invoice and nothing else.
The failure story
An agent is asked to reconcile a report. A tool returns a partial result because a date filter is wrong. The agent retries with a slightly different query. The tool returns the same partial result. The agent tries again.
Three retries later, the task is still not done. If there is no retry cap, the loop continues until the session ceiling or the month ends. If there is no ceiling, it continues until someone notices the bill.
The fix is not a better model. It is a retry cap and a loop detector.
Cost per successful task
The only metric that matters is cost per successful task, not cost per token.
A cheap model that fails 20 percent of the time costs more than an expensive model that succeeds, because a person redoes the work. A routing change that halves the token price and doubles the failure rate is a loss.
Measure three numbers:
- Cost per task, including retries.
- Success rate, measured on the evaluation set.
- Cost per successful task, which is the first divided by the second.
That third number is the one to put in front of a budget owner.
Where cost control fails
The budget is in the prompt. The model can ignore it. Put it in the runtime.
The ceiling is per call, not per session. A hundred small calls add up to the same bill.
The alarm goes to a shared inbox. Nobody owns it, so nobody acts.
The kill switch has never been tested. The first pull reveals that it stops the dashboard and not the agent.
Retries are not logged. The largest avoidable line is invisible.
How to start
- Log every call with task, model, tokens, and outcome.
- Set a task budget from the 90th percentile of current usage.
- Add the retry cap and the loop detector.
- Set a monthly alarm at 80 percent of the expected spend.
- Test the kill switch in staging.
- Review cost per successful task monthly.
That is a week of work for a system that already exists, and it prevents the failure that ends AI programs: a bill nobody expected.
Sources and what they change
One security reference names the failure directly.
- OWASP LLM10:2025, Unbounded Consumption. The risk is not only a malicious user. An agent in a retry loop consumes without anyone deciding to spend. A task budget and a loop detector are the control.
- OpenAI Batch API. A 50% discount for work that can wait 24 hours. Moving background jobs off the interactive path is a cost control before it is an optimization.
Related
- AI token cost: the lines the controls are protecting.
- Model routing: the router that enforces the tier.
- AI agent security: the permission side of the same system.
- Reduce AI token costs: the structural changes first.
Questions owners ask
Start with a generous ceiling, measure for two weeks, then set the budget at the 90th percentile of successful tasks.
Stop and return a partial result with a clear reason. An agent that can request more budget will learn to request it constantly.
Three for most tasks. The fourth retry rarely succeeds and always costs.
Hash the tool name and the arguments. Three identical calls in a row is a loop.
The person who owns the workflow and can stop it. Not a shared inbox, and not the vendor.

