E3 · Lesson 06
A step is not a unit of cost: four budgets and a stop reason
Module 1's loop has one limit, ten steps. A step is not a unit of anything you pay for. One step can send 600 tokens and the next 6,000, and a model that thinks before it answers spends its thinking as output tokens. Asked to read two files, qwen3:4b wrote 702 completion tokens, of which the two tool calls were about 30 (measured 25 October 2026). The rest was reasoning. On a hosted API that is the expensive kind of token.
Why tokens grow faster than steps
The model keeps no state between calls (Module 1 lesson 5), so every step re-sends the whole history. With a system prompt and tool schemas of S tokens, a task of T, and each step adding a call of C and a result of R, step t sends
so a run of n steps sends
That is linear per step and quadratic per run. With S = 600, T = 40, C = 30 and R = 400 (a short file per step), the study gives:
| step | prompt at this step | prompt so far |
|---|---|---|
| 1 | 640 | 640 |
| 2 | 1,070 | 1,710 |
| 5 | 2,360 | 7,500 |
| 10 | 4,510 | 25,750 |
At ten steps, 19,350 of the 25,750 prompt tokens are history the model has already seen. Prompt caching makes re-sent prefixes cheaper on servers that support it (E1 lesson 4; Ollama's usage shows cached_tokens), but they still count against the context window and the token limit. A file-reading task hits a token limit long before a step limit, so a step limit alone is not a budget.
Four limits, one stop reason
loop/budget.py checks four limits before every model call, in a fixed order, and names the first one reached:
| limit | counts | protects against |
|---|---|---|
steps |
model calls | the loop that never stops |
tokens |
prompt + completion, summed over the run | the context that grows every step |
cost |
tokens at a price per million | the bill: zero on a laptop, so set a price to make it visible |
seconds |
wall clock since the run started | the user who is waiting; a slow server, between calls |
b = Budget(max_steps=10, max_tokens=60_000, max_cost=0.05, max_seconds=120,
price_in=1.0, price_out=5.0) # prices per million tokens, in your currency
Two design choices are worth arguing about.
At the limit is out. With max_steps=3, three charged calls leave no room for a fourth. The check comes before the call, so the run never overshoots its step limit. It can overshoot its token limit by one call's worth, because a call's size is known only after it returns. Bounding that overshoot needs a per-call cap (max_tokens on the request), which servers support and the lab does not set.
The run reports why it stopped. Result.stop is "answer", a limit's name, "error" or "schema". "It returned a string" is not an outcome. "[stopped: step limit reached]" in Module 1 is a string that looks like an answer, and a caller that forwards it to a user has shipped a failure as a reply.
What to do at the limit
A hard stop returns nothing useful. The alternative is to grant one last call with no tools, asking for the best answer from what has been gathered. That costs at most one more call and often saves a run that was one step short. It also guarantees an answer, which is not always what you want: an agent that ran out of budget halfway through a refund should say so, not summarise. This loop stops hard and leaves the choice to the caller, who can see stop == "tokens" and decide. Problem 11 asks for the cost bound of each policy.
Measured on the twenty tasks
On the twenty tasks the median run used 4,582 tokens with Module 1's loop and 4,194 with E3's, and no run of either reached ten steps (the build). They send the same prompts, so similar totals are expected. Most of the completion tokens were thinking. The budget did not bind on these tasks. Its job is the tail you have not met yet: the run that loops, and that Module 1 would report as an answer.
Check yourself
- A run's budget is 20,000 tokens with S + T = 800 and C + R = 1{,}000 per step. After how many steps does the token limit stop it, if completion tokens are ignored?
- Why is a cost budget worth setting on a laptop, where every token is free?
- A server is down and every call times out after 30 s, with three retries per call. With
max_seconds=120, which of the four limits stops the run, and after how many charged steps?
Answers
- After 7 steps, at 26,600 tokens. The prompt total after n steps is 800n + 1000\,n(n-1)/2: 800, 2,600, 5,400, 9,200, 14,000, 19,800 for n = 1, \dots, 6, and 26,600 for n = 7. The check before step 7 sees 19,800, below 20,000, so step 7 is sent and the run ends at 26,600. The overshoot is the one-call-worth this lesson describes.
- Because the same loop will run on a priced API, and a budget you never set is a budget you never tested. With a price set, the laptop run reports what it would have cost, so a design that is ten times more expensive shows up before it is deployed.
- None of them. A failed call is never charged, so the step, token and cost limits never move, and the time limit is checked only between model calls. After four attempts (three retries) and about 122 s (120 s of timeouts, and three backoff waits of at most 3.5 s in all),
call_with_retriesre-raises the timeout and the run stops withstop == "error"and zero charged steps, already past its 120 s. Problem 11 asks how to let the time budget cut a retry sequence short.