Sub topic 4
Why an agent loop costs more than one request
This chapter assumes you can read basic Python and know that models bill per token, with output tokens usually priced higher than input tokens. You also need to know what a context window is. It explains why a coding agent's bill grows faster than its number of turns.
One request versus a loop
A single request costs its input tokens times the input rate, plus its output tokens times the output rate. An agent does not make one request. It runs a loop. The model replies with a tool call, the harness runs the tool, and the result is appended to the conversation. Then the whole conversation is sent again.
The API is stateless, so the model only knows what you send it on each call. Every turn resends the system prompt, the tool definitions, all earlier messages, and all earlier tool results.
Counting the billed input
The function below totals the input tokens billed across a loop with no caching.
def loop_input_tokens(system_tokens, step_tokens, turns):
"""Total input tokens billed across an agent loop with no caching."""
total = 0
context = system_tokens
for _ in range(turns):
total += context # the whole context is sent on every turn
context += step_tokens # each turn adds a tool call and its result
return total
print(loop_input_tokens(5_000, 2_000, 20)) # 480000The system prompt and tools take 5,000 tokens. Each turn adds 2,000 tokens of tool call and result. After 20 turns the context is only 45,000 tokens, yet 480,000 input tokens were billed. That is more than ten times the size of the final context.
The growth is quadratic
Let S be the starting context, d the tokens added per turn, and n the number of turns. The billed input is:
total = n * S + d * n * (n - 1) / 2The first term grows linearly with turns. The second grows with the square of the turns. Doubling the session from 20 to 40 turns in the example above gives 1,760,000 billed tokens. That is about 3.7 times as many, not twice as many.
| Turns | Final context | Billed input tokens |
|---|---|---|
| 1 | 7,000 | 5,000 |
| 10 | 25,000 | 140,000 |
| 20 | 45,000 | 480,000 |
| 40 | 85,000 | 1,760,000 |
At a placeholder input price of $3 per million tokens, the 20-turn session costs $1.44 in input alone. Reading the final 45,000-token context once would cost $0.135.
What this means in practice
Output tokens are billed once, because the model writes them once. They become input on every later turn, though. A long reply on turn 3 is paid for again on turns 4 through 20.
Three drivers decide the total:
- Turns. Each extra turn costs more than the one before it.
- Tokens added per turn. A large file read or a long test log raises the size of every later request.
- Starting context. The system prompt and tool definitions are resent every turn, so trimming them pays off n times.
Where the intuition goes wrong
A common error is to budget by the final context size. The final context is only what the last request carries. The bill is the sum of every request along the way.
Another error is to treat one big tool result as a one-time cost. If a 10,000-token file read happens at turn 2 of a 20-turn session, it is billed 18 times, about 180,000 input tokens in total.
The next chapters cover the main levers: caching a stable prefix so repeated tokens cost less, keeping tool output small, and isolating work in subagents.
Exercise
Using loopinputtokens, an agent has a 4,000-token system prompt, adds 1,500 tokens per turn, and runs 10 turns. How many input tokens are billed? What would they cost at $3 per million input tokens?
Show answer
The total is 10 4,000 + 1,500 (10 9 / 2) = 40,000 + 67,500 = 107,500 input tokens. At $3 per million this costs 107,500 3 / 1,000,000 = $0.3225. The final context is only 4,000 + 10 * 1,500 = 19,000 tokens, so the billed total is about 5.7 times larger.