Topic 1 of 5 · 4 min read
Step, token and time budgets per agent run
Goal: Set run budgets for an agent from observed run distributions and say what happens at each cap.
Give every agent run a hard budget in steps, tokens and wall-clock time, enforced by your code and not by the prompt, and decide in advance what the agent does when it hits one.
Check yourself · 1 of 3
Where should an agent's step limit be enforced?
Why it matters
A chat call costs one model request. An agent loops: it calls the model, runs a tool, reads the result and calls the model again with a longer context. Each pass costs more than the last, and nothing in the model ends the loop if it keeps believing the next step will help. One confused run can spend more than a thousand normal ones. A budget turns an open-ended cost into a bounded one.
How to set them
- Measure first. Log steps, tokens and seconds per finished run for a week. Look at the median and the 95th percentile, not the average.
- Set the cap just above the healthy tail. If 95 percent of successful runs finish within 14 steps, a cap of 20 stops loops without cutting real work. A cap at twice the median kills good runs.
- Stack the levels. A per-run cap stops loops. A per-user cap stops one account from draining the bill. A per-day cap stops a bug that starts many runs at once.
- Enforce in the loop, outside the model. Count before each model call, not after.
- Decide the exit. At a cap the agent should not simply die. Pick one: return the best partial answer with a clear note, save a checkpoint and hand the run to a person, or stop and mark it failed.
const BUDGET = { maxSteps: 20, maxTokens: 150_000, maxSeconds: 120 };
for (let step = 0; ; step++) {
const over = checkBudget(run, BUDGET); // steps, tokens, elapsed
if (over) {
await saveCheckpoint(run); // keep the progress
return handOff(run, { reason: over }); // a person or a later run continues
}
await runOneStep(run);
if (run.done) return run.result;
}
Before and after
Before: "The prompt says stop after 10 tool calls." The model miscounts, or ignores it when the tool output looks promising. After: the loop counts steps itself and refuses to call the model on step 21, whatever the prompt says.
What goes wrong
- A budget in the prompt only. The model is not a counter and can be talked out of limits by tool output.
- One cap for everything. A long research task and a password reset do not need the same limits. Set budgets per task type.
- Silent cutoffs. If a capped run returns a half answer with no note, users trust it. Say what was cut.
- No checkpoint. Work done before the cap is thrown away, so the retry pays for it again.
Examples
Choosing a cap from data
A support agent logged 4,000 finished runs. Median 6 steps, 95th percentile 13, and the successful runs never needed more than 17. The team set maxSteps to 20 and a time limit of 90 seconds. In the first week 11 runs hit the cap. Every one of them was a loop on the same failing search tool, which is exactly what the cap was for.
Cap behaviour per task
Refund request: at the cap, stop and queue for a human with the checkpoint attached, because a half-done refund is worse than none. Research summary: at the cap, return what was found so far with the line "Stopped at the step limit, 3 of 5 sources read."
Key points
- Enforce budgets in the loop, before each model call, never only in the prompt.
- Set caps from the percentile of successful runs, a little above the healthy tail.
- Stack three levels: per run, per user and per day.
- Decide the cap behaviour in advance: partial answer, checkpoint and hand-off, or clean failure.
- Save a checkpoint at the cap so the work is not paid for twice.
Common mistakes
- Putting the limit only in the system prompt and trusting the model to count.
- Setting the cap at the average run, which cuts off normal long runs and trains people to ignore failures.
- Using one global budget for every task type.
- Hitting the cap and discarding all progress, so the retry repeats the same spend.
Check yourself · 2 of 3
An agent's successful runs show a median of 6 steps and a 95th percentile of 13. Which step cap is the most sensible start?
Check yourself · 3 of 3
A run hits its token cap after 40 minutes of useful work on a refund. What is the best behaviour?
Practice items, not counted towards your mastery.
Start here
- Building effective agents (Anthropic) (opens in a new tab)ArticleFree
Explains why agents need stopping conditions, checkpoints and human review, and when to keep a design simple.
- How we built our multi-agent research system (Anthropic) (opens in a new tab)ArticleFree
Production lessons on tracing agent runs, resuming from errors and the cost of long agent runs.
- Semantic conventions for GenAI agent and framework spans (opens in a new tab)StandardFree
The standard span names and attributes for recording agent invocations and tool calls.
More for this topic
- Persistence (LangGraph docs) (opens in a new tab)DocsFree
How checkpoints save agent state at each step so a run can resume, replay or wait for a human.