How do I stop an LLM agent from running up costs?
Short answer
Put hard limits around every run, not just around the model. Cap the number of steps, set max output tokens on every call, give each run a dollar budget that is checked before each model call, stop when the same tool call repeats, and add a daily per-tenant kill switch. Cost grows faster than steps because each step resends the growing history, so doubling the steps roughly quadruples the input tokens.
#The checklist
- Step cap: a maximum number of model calls per run, enforced in the loop itself.
- Output cap: set
max_tokens(or the provider's equivalent) on every call so one reply cannot run long. - Run budget: read the run's spend so far from your usage ledger and refuse the next call when it would pass the budget.
- Loop detection: if the same tool is called with the same arguments twice in a row, stop or change strategy.
- Wall-clock timeout: a run that takes ten times longer than normal is a bug, not a slow job.
- Tenant kill switch: a daily spend ceiling per account that blocks new runs and alerts you.
- Alert on the tail: watch the most expensive runs, not the average.
#Why cost grows faster than steps
If each step adds about the same number of tokens and the full history is sent every time, input tokens across n steps add up to a triangle, roughly n times n plus one over two times the tokens per step. Fifty steps of 1,000 added tokens each is 1,275,000 input tokens, and a hundred steps is 5,050,000, about four times as many. This is arithmetic, not a benchmark, and caching or summarising history lowers it.
The full write-up, with a small budget guard in Python, is in Runaway LLM Agent Costs: Budgets, Step Limits and Kill Switches.
Sources
- Dinesh Solanki, Runaway LLM Agent Costs: Budgets, Step Limits and Kill SwitchesChecklist expanded, with the token arithmetic and a budget guard sketch.
Read the full write-up
Have a question that is not answered here?
Ask me directly