Sub topic 1
The Context Window and Its Token Budget
Prerequisites
This chapter assumes you have used a coding agent in a terminal or editor, can run basic command-line tools, and know that a token is a small chunk of text (a word, part of a word, or a symbol) that a language model reads and writes. You do not need to know how models work internally.
What the context window is
A language model has no memory between calls. Each time the agent asks the model for its next step, it sends everything the model needs to see: instructions, earlier messages, file contents, and tool results. That bundle is the context. The context window is the maximum number of tokens the model can accept in one call, counting both the input you send and the output it generates.
The window is a budget. Every token you spend on one thing is unavailable for another. When the budget runs out, the agent must drop or shorten something, or the call fails.
How tokens are counted
Tokens are not characters or words. English prose averages roughly four characters per token. Code and logs often cost more per character, because identifiers, punctuation, and indentation split into many small tokens. Treat any rule of thumb as an estimate. Your agent's own counter is the reliable source.
| Content | Rough cost |
|---|---|
| One short chat message | 20 to 100 tokens |
| A 300-line source file | 3,000 to 5,000 tokens |
| A failing test log with stack traces | 5,000 to 30,000 tokens |
These figures are illustrative. Real numbers depend on the language, the formatting, and the tokenizer.
Why the window grows during a session
The conversation is re-sent on every turn. Early content stays in the window while new content is added on top. A session that reads files, runs tests, and edits code therefore grows steadily, and nothing leaves unless you or the agent removes it.
Input and output share the budget
The window covers input and output together. If the limit is 200,000 tokens and the input already uses 195,000, the model has room for only about 5,000 tokens of reply. Agents usually reserve some space for output, so the usable input is smaller than the headline number.
A worked budget
Suppose a window of 200,000 tokens. A plausible early-session breakdown looks like this:
- System prompt and tool definitions: 10,000 tokens.
- Project instructions file: 2,000 tokens.
- Five source files read in full: 20,000 tokens.
- One full test run pasted into the conversation: 15,000 tokens.
- Conversation so far: 8,000 tokens.
That totals 55,000 tokens, or 27.5% of the window, before any real work is done. Two more test runs and a dozen more file reads would pass 50% quickly. Later chapters in this tutorial show how to avoid each of these costs.
Common mistakes
- Assuming the window is a long-term memory. It is a working area. When it resets, everything not saved to a file is gone.
- Ignoring fixed costs. The system prompt and tool definitions are present from the first turn.
- Reading whole files "just in case". Each read stays in the context for the rest of the session.
- Treating the limit as the target. Problems start before the window is full.
Exercise
A window holds 200,000 tokens. The input is currently 180,000 tokens, and you ask the agent to write a long module that needs about 30,000 tokens of output. What goes wrong, and why?
Show answer
The input and output share the same window, so only 20,000 tokens remain for the reply. The 30,000-token output cannot fit. The agent would truncate the reply, fail the call, or have to compact the context first.
Exercise
Why does a session grow in size even when you only send short messages?
Show answer
The whole conversation is re-sent on every turn, and each file read and tool result is appended to it. Earlier content stays in the window, so the total only goes up unless something is removed or compacted.