Topic 3 of 5 · 4 min read
Step, token, time and cost budgets
Goal: Set step, token and cost limits for an agent from a sample run's numbers and define its behaviour when a limit is hit.
A budget is a number your code enforces on every run: how many turns, how many tokens, how many seconds and how many dollars the agent may use before it must stop or hand back. Set budgets from measured runs, then decide in advance what happens when one runs out.
Check yourself · 1 of 3
Why can an agent's cost grow faster than its number of turns?
Why it matters
An agent's cost is not fixed. Each turn re-sends the whole history, so a run of 20 turns costs far more than twenty single calls. One stuck run can cost as much as a hundred good ones. A budget turns that tail risk into a bounded one.
The four budgets
- Steps. Maximum loop turns. Cheap to enforce and the first line of defence.
- Tokens. Total input plus output tokens across the run, or the size of the history. Providers report usage on each response, so add it up.
- Time. A wall-clock deadline for the run, and a timeout on each tool call. A hung tool otherwise stalls everything.
- Cost. Tokens multiplied by the model's price from your provider's pricing page. Track per run and per user per day.
Setting the numbers
Run the agent on 10 to 20 realistic tasks and log turns, tokens, seconds and cost for each. Take the worst normal run and add headroom, for example 1.5 times. If your slowest good run takes 14 turns, a cap of 20 is sensible. A cap of 100 only delays the bad news. Revisit the numbers when you change the model, the tools or the prompt.
When a budget runs out
Decide the behaviour, do not let it default to an exception:
- Warn first. At around 80 percent, add a message such as "You have 3 turns left; finish or report progress."
- Stop with a result. Return what was done, what is missing and the stop reason
budget_exceeded. - Hand off. Save state so a person or a later run can continue.
const budget = { turns: 20, tokens: 150_000, ms: 120_000, usd: 0.50 };
if (used.turns >= budget.turns * 0.8) messages.push(nudge(budget.turns - used.turns));
if (used.tokens >= budget.tokens || used.usd >= budget.usd || Date.now() > deadline) {
return stop("budget_exceeded", partialReport(messages));
}
Before and after
Weak: only a 100-turn cap, no cost or time limit. A run that pulls large web pages every turn hits the context limit on turn 12 and fails with an API error, or costs far more than expected before turn 100.
Fixed: 20 turns, 150k tokens, 2 minutes and a spend cap, with a nudge near the end and a partial report.
What goes wrong
- Budgets guessed, not measured. Too low and good runs fail; too high and they protect nothing.
- A cap with no partial result. The user waits, then gets an error and loses the work.
- Only a per-run limit. A thousand cheap runs still add up, so add a daily cap per user or app.
Examples
Numbers from a sample
run turns tokens seconds usd
1 8 41,000 22 0.09
2 11 63,000 35 0.14
3 14 92,000 51 0.21
4 9 48,000 28 0.11
The worst good run used 14 turns and 92k tokens. Caps of 20 turns and 140k tokens give headroom of about 1.5 times. A cap of 200 turns would not have caught anything.
The budget message
At 80 percent of the turn cap the loop adds a user-role message: You have 4 turns left. If the task is not finished, call submit_result with what is done and what remains. The agent then ends with a usable partial result instead of being cut off mid-edit.
Key points
- Budget four things: steps, tokens, wall-clock time and cost.
- Cost grows faster than turn count because each turn re-sends the history.
- Set caps from the worst normal run in a sample, plus headroom, and revisit them when the model or tools change.
- Define what happens at the limit: warn near it, then return a partial report with a stop reason.
- Add per-user or per-day spend limits in addition to per-run limits.
Common mistakes
- Setting one very high turn cap and calling it a budget.
- Counting only output tokens and ignoring the history re-sent as input.
- Adding a timeout to the run but not to each tool call.
- Letting the budget error surface as an exception with no partial result.
Check yourself · 2 of 3
Your best five runs used 9, 11, 12, 13 and 14 turns. Which turn cap is the most sensible?
Check yourself · 3 of 3
A run hits its token budget with the task half finished. What is the best behaviour?
Practice items, not counted towards your mastery.
Start here
- ReAct: Synergizing Reasoning and Acting in Language Models (opens in a new tab)PaperFree
The paper that named the pattern of interleaving reasoning steps with actions and observations.
- Building effective agents (Anthropic) (opens in a new tab)ArticleFree
Describes agents as models using tools in a loop, and why to add stopping conditions and checkpoints.
- LLM Powered Autonomous Agents (Lilian Weng) (opens in a new tab)ArticleFree
A long survey of planning, memory and tool use, including ReAct-style loops and their failure modes.
More for this topic
- Effective context engineering for AI agents (Anthropic) (opens in a new tab)ArticleFree
Compaction, tool-result clearing and note-taking for agents whose history outgrows the context window.