Sub topic 3
The context window: the model's entire working memory
This chapter assumes you know basic Python, JSON, and how a model call is made as a stateless request. It also assumes you know that a conversation is sent as a list of role-tagged messages. If those are unfamiliar, review the two previous chapters first.
What the context window is
The context window is the maximum amount of text, measured in tokens, that the model can read in one call. A token is a chunk of text, often a short word or part of a word. The window covers everything in the request and the model's reply:
- The system prompt and project instructions.
- Tool definitions.
- Every earlier user, assistant, and tool message.
- The reply the model is about to write.
The model has no other memory. If a fact is not in the window, the model cannot use it. A file the agent read twenty steps ago is only "remembered" if its text is still in the messages the harness sends.
Why a coding agent fills it quickly
A chat uses a few short messages per turn. An agent adds a tool result at every step, and those results can be large: a whole source file, a test log, or search output. The harness resends the full history on every call, so the history only grows until something removes text from it.
The numbers are illustrative. Real growth depends on how much each tool returns.
Estimating the budget
You can check usage before each call. A rough rule for English text and code is about 4 characters per token. A real count needs the model's own tokenizer, because different models split text differently.
def rough_tokens(messages):
# Crude estimate of about 4 characters per token; real counts need the model's tokenizer.
return sum(len(str(m.get("content", ""))) for m in messages) // 4
def needs_compaction(messages, window=200_000, threshold=0.8):
return rough_tokens(messages) > window * thresholdThe window size of 200,000 is an example value. Check the limit for the model you use. The threshold of 0.8 leaves room for the next tool result and the model's reply. If you wait until the window is 100% full, the next call fails or the reply gets cut off.
Note that this estimate counts only content. It ignores tool definitions and the tool_calls field, so it under-counts. Add those if you need a tighter number.
What happens when it is full
Providers handle overflow differently. Some return an error. Some truncate the oldest messages. Some reject only the request that exceeds the limit. In all cases, the harness should act first, because silent loss of early messages can remove the original task.
Even below the limit, a crowded window can hurt. Relevant details buried among thousands of lines of logs are easier to miss, and every token costs money and time on each call.
What the harness can do
The harness controls what goes into the window. The common options are:
| Strategy | What it does | Risk |
|---|---|---|
| Truncate tool output | Cut each result to a fixed length | May cut the useful part |
| Search instead of read | Return matching lines, not whole files | Needs good search queries |
| Summarize old history | Replace early messages with a short summary | Summary may drop details |
| Delegate to a sub-agent | Do noisy work elsewhere, return only the result | Adds complexity |
The next chapters cover these in detail. For now, the key point is that context is a budget the harness manages.
Common errors
- Assuming the model remembers earlier calls. It only sees what the harness sends.
- Estimating tokens by word count. Code and non-English text often use more tokens per character.
- Ignoring the reply budget. The model's output also uses the window.
- Returning whole files or full logs when a few lines would do.
Exercise
An agent's history is 170,000 estimated tokens, the window is 200,000, and the compaction threshold is 0.8. Does needs_compaction return True? What would you do next?
Show answer
Yes. The threshold is 160,000 tokens (200,000 × 0.8), and 170,000 is above it. Before the next model call, the harness should reduce the history, for example by summarizing older messages while keeping the original task and recent steps, or by truncating large old tool results.