An AI agent has no natural spending limit. AI agent cost control means deciding, in code, how much a single run may consume before something stops it, because the agent will not stop on its own and the invoice arrives a month later. Two different things drive the number up: a loop you wrote, and somebody deliberately exploiting the fact that your endpoint costs you money per call. The defences overlap, and neither of them lives in the prompt.
A per-call price tells you very little about a per-run cost
A chatbot answers once. An agent works until it decides it is finished, and every step on the way is another call.
Anthropic's engineering guidance on building effective agents states it plainly: "The autonomous nature of agents means higher costs, and the potential for compounding errors." Both halves matter for the bill. A wrong turn at step three does not end the run, it produces more steps, each paid for. The same guidance recommends "stopping conditions (such as a maximum number of iterations) to maintain control", which is the cheapest line of defence anyone will offer you.
The practical consequence is that a token price is not a budget. One user request can become five calls or fifty, and the difference is decided at runtime by a model. Until you have measured the distribution of steps per run on real traffic, you do not know what your service costs, and an average will hide the tail that actually hurts.
Denial of wallet is a named attack, not a billing accident
If your agent is reachable by the public, the cost ceiling will be tested by someone who is not a customer.
OWASP's entry on unbounded consumption catalogues how. Denial of Wallet is the explicit one: running high volumes of operations to exploit cost-per-use pricing and create financial strain on the provider. The entry lists others that cost you money without looking like an attack:
- Variable-Length Input Flood, overloading the system with inputs of varying lengths, which "can deplete resources and potentially render the system unresponsive".
- Continuous Input Overflow, repeatedly sending inputs past the context window so the system burns computation on them.
- Resource-Intensive Queries, demanding requests that drain resources and prolong processing.
Note what these have in common. None requires a breach, a stolen credential or a clever prompt. They are ordinary requests, sent in volume, priced per unit. A free trial, an unauthenticated demo endpoint or a public support widget is enough.
The same page also covers extraction, where the volume is aimed at replicating your model's behaviour rather than at your bank balance. For most small teams the wallet is the realistic risk first.
Put the limits where the agent cannot talk its way past them
A spending instruction in a system prompt is a preference. The limits that hold are the ones outside the model.
OWASP's mitigations for unbounded consumption name them directly: Rate Limiting, Resource Allocation Management, Timeouts and Throttling, Limit Queued Actions and Scale Robustly, Graceful Degradation, and Comprehensive Logging, Monitoring and Anomaly Detection.
Translated into things to build this week:
- A hard iteration cap per run, enforced by the loop rather than requested of the model. When it trips, the run stops and a human is told.
- A per-run token or currency budget, checked between steps, that aborts rather than truncates. Truncating produces a confident wrong answer at full price.
- Rate limits per authenticated user, not per IP address, on anything that reaches a model. Unauthenticated access to a paid endpoint is a bill waiting to be written.
- A queue depth limit, so a burst waits or is refused instead of fanning out into parallel paid work.
- Graceful degradation, meaning a cheaper model or a plain refusal when the budget is spent, decided in advance.
This is the same principle as the approval boundary: enforcement belongs in the system that executes, not in the judgement of the thing being constrained. We have written about that in designing the approval step.
Measure cost per completed task, not cost per token
Token prices are the input. The number that tells you whether the product works is what one finished job costs, including the runs that failed and were retried.
Three measurements worth having before you optimise anything:
- Cost per completed task, with failed runs charged to the successes. Every finished job is also paying for the attempts that were abandoned, and a figure that counts only successful runs understates what the work costs.
- The tail of the distribution. The ninety-ninth percentile run is the one that empties the budget, and it will not resemble the median.
- Cost per run before and after a change, on the same inputs. A prompt edit that improves accuracy and triples the step count may or may not be worth it, and you cannot tell without both numbers.
That last one pairs with an evaluation set. If you already run cases before and after each change to check behaviour, record the cost of each run while you are there. Building that set from your own failure cases is a separate job, and it is where this measurement belongs once it exists.
The short version
Set an iteration cap and a per-run budget in the loop. Put rate limits behind authentication on anything that costs money per call. Decide now what happens when the budget runs out, because the alternative is discovering it during an incident. Then measure cost per completed task, and watch the tail rather than the average.
None of this is expensive to build, and all of it is awkward to retrofit during the month when the bill has already arrived.
If you are putting an agent in front of the public and want the limits designed before launch rather than after, tell us about the project.
