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 read basic shell commands, and know that a language model has a limited input size measured in tokens. The previous chapter defined the context window as the fixed budget of tokens the model can see at once. This chapter looks at what spends that budget.
The working model
An agent session is not a chat where only your messages count. On every turn, the agent sends the model the whole working context: everything the model needs to decide the next step. The model keeps no memory between calls, so anything it should "remember" must be present in that input again.
The context has six main sources:
- System prompt. Fixed instructions from the agent's maker, such as how to behave and how to call tools.
- Tool definitions. The names, descriptions, and parameter schemas of every tool the agent can call. Each connected tool server adds more.
- Persistent project instructions. Files the agent loads at startup, such as a project rules file or a steering file.
- Conversation history. Your messages and the agent's replies, including its reasoning text.
- Tool calls and results. Every command the agent ran, every file it read, and every search result that came back.
- Attachments and pasted content. Logs, stack traces, and documents you add by hand.
Sources 1 to 3 form the fixed overhead. They are present from the first turn. Sources 4 to 6 grow as the session goes on, and source 5 usually grows fastest.
The two arrows leading back from the model show the feedback loop. Each step the model takes adds its own output and the result of any tool it called. The next turn then starts with a larger context than the last.
Fixed overhead
Fixed overhead is the cost you pay before you type anything. It matters for two reasons. It reduces the budget available for real work, and it repeats on every call.
Tool definitions are the part people overlook. A tool is described to the model in text, usually with a JSON schema for its parameters. A single tool may cost a few hundred tokens. An agent with a dozen built-in tools and several external tool servers can carry thousands of tokens of definitions that are never used in a given session.
You can reduce this overhead without changing your task:
- Disable tool servers you do not need for the current job.
- Prefer a small set of general tools to many narrow ones.
- Keep project instructions short. Chapter 6 covers this in detail.
Fixed overhead is also the most stable part of the context. Chapter 13 explains why that stability can make calls cheaper and faster.
Conversation history
Conversation history holds what you and the agent said. Your own messages are usually short. The agent's replies are longer, since they include explanations, plans, and sometimes visible reasoning.
History has one property that matters later: it is cumulative and ordered. The agent cannot drop turn 12 while keeping turn 40 unless something edits the history. Without compaction (Chapter 9), every earlier turn stays in the context, whether or not it still helps.
Tool calls and results
Tool results are the main reason a coding session fills up. Look at how big typical results are:
| Action | Typical result | Why it is large |
|---|---|---|
| Read a whole source file | 500 to 5,000 lines | The agent gets every line, including code it does not need |
| Search the repository with no limit | Hundreds of matching lines | Common names match in many places |
| List a directory recursively | Thousands of paths | Build output and dependencies are included |
| Run a full test suite | Thousands of log lines | Passing tests print as much as failing ones |
| Install dependencies | Long progress output | Package managers print every step |
| Print a diff of a large change | Every changed line | Generated or reformatted files inflate it |
Two properties make tool results costly. First, the result stays in the history after the agent has used it. A file read at turn 5 is still in the context at turn 80. Second, the same content is often added repeatedly. If the agent reads a file, edits it, and reads it again to check, the file now appears in the context twice, in two slightly different versions.
Measuring the size of a result
Before you decide how to limit output, it helps to see how large it is. Tokens depend on the tokenizer, but a rough rule for English text and code is about four characters per token. This script gives a quick estimate of how many tokens a command's output would add:
import subprocess
import sys
def estimate_tokens(text):
# Rough rule: about 4 characters per token. Real counts vary by tokenizer.
return len(text) // 4
def main():
if len(sys.argv) < 2:
print("usage: python estimate.py <command> [args...]")
sys.exit(1)
result = subprocess.run(sys.argv[1:], capture_output=True, text=True)
output = result.stdout + result.stderr
lines = output.count("\n")
print(f"lines: {lines}")
print(f"characters: {len(output)}")
print(f"estimated tokens: {estimate_tokens(output)}")
if __name__ == "__main__":
main()Run it against a command you often give the agent:
python estimate.py git log
python estimate.py ls -RA git log on a mature repository can come to tens of thousands of tokens. Seeing that number makes the cost concrete. The estimate is approximate, so use it to compare commands, not to budget exact counts.
Pasted content and attachments
When you paste a stack trace or a document, you decide what enters the context, which makes this the easiest source to control. Paste the part that carries the information. For a stack trace, that means the error message and the frames inside your own code, not a 400-line trace through framework internals. For a log, include the lines around the failure, not the whole file.
An example session budget
This table shows how a context might fill over one afternoon on a mid-sized task. The numbers are illustrative and use a 200,000 token window.
| Source | Tokens | Share of window |
|---|---|---|
| System prompt | 4,000 | 2% |
| Tool definitions | 12,000 | 6% |
| Project instructions | 2,000 | 1% |
| Conversation history | 30,000 | 15% |
| File reads | 60,000 | 30% |
| Search and test output | 40,000 | 20% |
| Pasted content | 6,000 | 3% |
| **Used** | **154,000** | **77%** |
| **Free** | **46,000** | **23%** |
The fixed overhead is 9% of the window. The tool results (file reads plus search and test output) take half of it. In a typical coding session, this is where savings are largest, so Chapters 7 and 8 focus on them.
Common errors
- Assuming only your messages count. The words you type are a small share. Judge usage by the total, not by how much you wrote.
- Letting the agent read whole files by default. A file read costs the same whether the agent needs one function or all of it. Ask for a range or a search first.
- Running the full test suite after every edit. Run the single test that covers your change, and run the full suite once at the end.
- Leaving unused tool servers connected. Their definitions cost tokens on every turn.
- Pasting entire logs. Cut to the relevant section before you paste.
- Assuming a deleted file or reverted change leaves the context. The earlier read and the earlier diff stay in the history until compaction or a new session removes them.
Where this leads
Knowing the sources tells you where to act. Fixed overhead is reduced once, at setup. Pasted content is controlled by you. Tool results are controlled by how you ask the agent to read and run things. History is controlled by compaction and by starting new sessions. The next chapters take these in turn, starting with the symptoms that tell you the context is too crowded.
Exercise
A session uses a 200,000 token window. The system prompt, tool definitions, and project instructions together take 20,000 tokens. The agent has read 12 files at about 4,000 tokens each, and run three test suites that printed about 8,000 tokens each. Conversation history is 25,000 tokens. Roughly what share of the window is used, and which source is the largest?
Show answer
File reads are 12 x 4,000 = 48,000 tokens. Test output is 3 x 8,000 = 24,000 tokens. The total is 20,000 + 48,000 + 24,000 + 25,000 = 117,000 tokens, which is about 58% of the window. File reads are the largest single source at 48,000 tokens. Tool results together (72,000 tokens) are larger than the fixed overhead and the conversation history combined (45,000 tokens).
Exercise
The agent read src/config.py at turn 5, edited it at turn 9, and read it again at turn 10 to check the edit. How many copies of the file's content are in the context at turn 11, and what does this suggest about how to verify an edit?
Show answer
There are two copies, an old version from turn 5 and a new version from turn 10, plus the edit itself from turn 9. Nothing removes the old copy. To verify an edit more cheaply, ask the agent to print only the changed lines or the diff, or to run the relevant test, instead of reading the whole file again.