Sub topic 5
Where the context grows: tool output, file reads and history
This chapter assumes you know what tokens and a context window are, and that you have read the earlier chapters on token pricing and the cost of an agent loop. You should be able to read short Python and JSON.
Three sources of growth
In an agent loop, the context is the system prompt, the tool definitions, the conversation so far, and every tool result. The model only adds a short message or tool call on each turn. Most of the growth comes from what the tools send back.
| Source | Typical size | Why it grows |
|---|---|---|
| System prompt and tool definitions | Fixed, often a few thousand tokens | Set once, but resent on every turn |
| Tool output | Hundreds to tens of thousands of tokens per call | Test logs, search results, directory listings |
| File reads | Size of the file | A whole file is read to look at a few lines |
| History | Sum of all earlier turns | Nothing is removed unless you remove it |
Each item stays in the context after it is used. A 10,000-token test log read on turn 3 is billed again on every later turn.
Estimating growth
The loop function from the previous chapter assumed a fixed step size. Real steps vary, so it is more useful to pass in the size of each step.
def context_by_turn(system_tokens, step_sizes):
"""Return the input tokens sent on each turn."""
sizes = []
context = system_tokens
for step in step_sizes:
sizes.append(context)
context += step
return sizes
# One big test log (12,000 tokens) among small steps.
steps = [800, 3_000, 12_000, 600, 600, 600]
sizes = context_by_turn(5_000, steps)
print(sizes) # [5000, 5800, 8800, 20800, 21400, 22000]
print(sum(sizes)) # 83800The 12,000-token log is added on turn 3. It is then resent on turns 4, 5 and 6, so it is billed four times in total, and 36,000 of the 83,800 input tokens come from that one result. A single large result costs more the earlier it appears.
Common causes
- Reading whole files. A 2,000-line file can be 25,000 tokens or more. The agent often needs one function.
- Unfiltered command output. Running a full test suite or
git logwith no limit returns far more than the agent needs. - Repeated reads. The agent reads the same file again after editing it, so both copies sit in the context.
- Broad searches. A search that matches a common word returns hundreds of lines.
- Verbose formats. Pretty-printed JSON and long stack traces use many tokens for little information.
Measuring it
You can find the large results by counting the size of each message. Exact counts need the provider's tokenizer or the usage data in the API response. For a quick estimate, divide characters by about 4 for English text and code.
def rough_tokens(text):
return len(text) // 4
messages = [
{"role": "tool", "name": "read_file", "content": "x" * 100_000},
{"role": "tool", "name": "run_tests", "content": "y" * 8_000},
{"role": "tool", "name": "grep", "content": "z" * 2_000},
]
for m in sorted(messages, key=lambda m: -len(m["content"])):
print(m["name"], rough_tokens(m["content"]))
# read_file 25000
# run_tests 2000
# grep 500Sorting by size shows which tool is responsible. The next chapters cover prompt caching and the ways to keep context small.
Exercise
A loop starts with a 4,000-token system prompt and runs 5 turns. The steps add 1,000, 10,000, 1,000, 1,000 and 1,000 tokens. How many input tokens are billed in total with no caching, and how many of them come from the 10,000-token result?
Show answer
The input sizes on the five turns are 4,000, 5,000, 15,000, 16,000 and 17,000, which total 57,000. The 10,000-token result is added after turn 2, so it is sent on turns 3, 4 and 5. That is 30,000 of the 57,000 input tokens.