Lesson 1 of 6 · 3 min read
The Context Window as a Budget
In this lesson
TL;DR: The window is not storage, it is a budget the harness spends every turn. Account for each part (stable prefix, conversation, tool results, notes), cap what grows, reserve room for the model's reply, and act at thresholds (clear old tool results first, compact later) well before the limit. Quality drops long before the window is full, and every token you keep is paid for again on each later turn.
After this lesson, you should be able to break a context into its parts and say how each behaves over a run, set thresholds for clearing and compaction, reserve output space, and explain why "just use a bigger window" does not fix a long-run agent.
Before you read
Commit to an answer now. The lesson that follows explains it, and having guessed first, even wrongly, makes the explanation easier to remember.
1A coding agent at turn 30 has 140,000 tokens in context: 3,000 of system prompt, 14,000 of tool definitions, 30,000 of conversation and 93,000 of file reads and test logs. Which part would you cut first, and why?
What is in the window, and how each part behaves
Every request an agent harness sends is assembled from parts with very different behaviour:
| Part | Typical size | Behaviour over a run | Notes |
|---|---|---|---|
| System prompt | 1k to 5k | Fixed | Should never change mid-run |
| Tool definitions | 5k to 40k | Fixed, unless tools are loaded on demand | Front of the prompt, cached |
| Conversation | Small at first | Grows every turn | User messages, model replies, tool calls |
| Tool results | Usually the largest | Grows fastest | File reads, search results, logs |
| Notes and memory | Small | Grows slowly | Plans, progress, facts carried forward |
| Reserved output | 2k to 16k | Fixed | Room for the reply and its tool calls |
Two parts sit still and two grow, and the growth is dominated by tool results. That is the first lesson of context budgeting: the window fills with what tools returned, not with what anyone said.
Watch a run spend its budget
- System prompt3k
- Tool definitions14k
- Conversation800
- Tool results0
- Progress notes0
Turn 1
A small task statement on top of a stable, cached prefix.
Why the budget is tighter than the window
Two forces make the usable budget much smaller than the advertised window.
Quality falls before the window fills. Models use information less reliably as inputs grow and as relevant facts get buried among irrelevant ones (see Context Rot). The practical ceiling for good performance is often well below the maximum, and it varies by model and task, so it is something you measure on your own evals rather than look up.
Every token is paid for again. The model rereads the whole context each turn. A 20,000-token result read at turn 10 of a 40-turn run is read 30 more times. Caching makes repeated prefix tokens cheaper, but they still take the model's attention and still count toward the limit.
Running the budget
A harness that budgets the window does four things on every turn:
class WindowBudget:
def __init__(self, window, reserve_out, clear_at=0.5, compact_at=0.75):
self.window, self.reserve = window, reserve_out
self.clear_at, self.compact_at = clear_at, compact_at
def action(self, parts):
used = sum(parts.values())
share = (used + self.reserve) / self.window
if share >= self.compact_at:
return "compact_history"
if share >= self.clear_at and parts.get("tool_results", 0) > 0:
return "clear_old_results"
return "none"
b = WindowBudget(window=200_000, reserve_out=8_000)
turn_12 = {"system": 3_000, "tools": 14_000,
"conversation": 9_000, "tool_results": 41_000}
turn_30 = {"system": 3_000, "tools": 14_000,
"conversation": 30_000, "tool_results": 93_000}
print(b.action(turn_12), b.action(turn_30)) # => none clear_old_results
- Count each part. Use the provider's token counter or the usage it reports; a rough four characters per token is fine for planning and wrong for enforcement.
- Cap what grows at the source. Tools return trimmed, paginated results (see Tool Contracts), so no single result can take a fifth of the window.
- Reserve output space. The reply and its tool calls need room; a request that leaves none gets truncated output.
- Act at thresholds, cheapest first. Clear tool results the agent has already used, then move durable facts into notes, and only much later compact the conversation itself. Each step is lossier than the last.
A bigger window is not the fix
When a long-running agent degrades, upgrading to a model with a larger window is the most common suggestion and the least effective. The extra room fills with the same low-value results, quality still falls as the context grows, and the cost per turn rises. A bigger window helps tasks that need a lot of material at once, such as reading a long contract; it does not help a run that is accumulating noise.
Say it in 30 seconds
"I treat the window as a per-turn budget. The system prompt and tools are a stable, cached prefix; the conversation and especially tool results grow every turn and dominate. I cap results at the tool, reserve room for output, count every part, and act at thresholds well below the limit: clear used tool results first, move key facts to notes, and compact the conversation rarely. Quality drops before the window is full and every kept token is reread each turn, so a bigger window is not a fix for a noisy run."
Check yourself
Answer aloud or on paper before revealing. Producing the explanation unaided is exactly what a panel listens for.
1Why set the clearing threshold at 50% of the window rather than near 100%?
2A teammate proposes doubling the context window to fix an agent that loses track after 50 turns. What do you ask first?
3Which parts of the context should never change during a run, and why?