Sub topic 2
What actually fills the context in an agent session
This chapter assumes you have used a coding agent in a terminal or editor, can run basic shell commands and git, and know that a model reads a limited number of tokens at once. The previous chapter covered the window and its token budget. Here you will see what consumes that budget.
The context is rebuilt every turn
A coding agent does not remember your session the way a person does. On every turn, the agent sends the model the full set of material it should work from: instructions, the conversation so far, and every tool result still kept. The model's reply is then added to that set. Anything that stays in the set is paid for again on every later turn.
This is why a session can feel fine for an hour and then get worse. Nothing breaks. The set only grows.
The main sources of context
The table lists the parts of a typical session, roughly in the order they are added.
| Source | What it contains | How it grows |
|---|---|---|
| System prompt | The agent's built-in rules and behavior | Fixed per session |
| Tool definitions | Names, descriptions, and parameters of each tool, including extra tools from connected servers | Fixed, but grows with each tool you add |
| Persistent instructions | Project files the agent loads at start, such as a rules or memory file | Fixed unless you edit them |
| Conversation | Your messages and the agent's replies | Grows steadily |
| File reads | Contents of files the agent opened | Grows in large steps |
| Tool output | Search results, command output, test logs | Grows in large, unpredictable steps |
The first three are the fixed base. You pay for them before you type anything. The last three grow during the session, and the last two usually dominate.
Why file reads and tool output dominate
Your messages are short. A file or a log is not. Reading a 1,500-line source file can add many thousands of tokens in one step. A single unfiltered test run can add more.
Two habits make this worse:
- Reading a whole file to check one function.
- Running a command that prints everything, then reading all of it.
The model has to read these results on every later turn, even after the useful part has been used. A stack trace you fixed an hour ago still takes up space.
A worked example
Suppose you ask the agent to fix a failing test. A realistic sequence looks like this:
- The agent lists the repository tree. A large tree can cost a few thousand tokens.
- It reads three source files in full to understand the area.
- It runs the full test suite. The log is long, and only the last failure matters.
- It edits a file, which echoes the edit back into the context.
- It reruns the tests and receives another long log.
After five steps, almost none of the context is your request or the agent's reasoning. Most of it is raw file text and logs, much of it no longer relevant. Compare this with a targeted version of the same work:
# Locate the failing test, then read only the failure summary
pytest -x -q 2>&1 | tail -n 30The -x flag stops at the first failure, and tail -n 30 keeps only the last 30 lines. The context receives a few dozen lines instead of the whole log.
What you can and cannot control
You can change the growing parts directly. You can ask for narrower reads, cap command output, and keep instructions short. You can change the fixed base only indirectly, by trimming your instruction files and disconnecting tools you do not use.
The next chapters build on this split. Symptoms of a crowded context, measuring usage, and each technique for reducing growth all depend on knowing which source is responsible.
Exercise
A session starts with a 6,000-token fixed base. After the agent reads four large files and runs the full test suite twice, the model begins to ignore an instruction from your rules file. Which parts of the context most likely caused the crowding, and name one change that reduces it.
Show answer
The file reads and the two test logs, which are the large, growing parts of the context. The fixed base is small by comparison. Reasonable changes include asking the agent to read only the relevant line ranges with search plus sed -n, or limiting test output with pytest -x -q 2>&1 | tail -n 30.