Skip to content

Lesson 1 of 6 · 3 min read

The Context Window as a Budget

Free lessonReviewed By the AIAgentInterviews editorial team
Reading position
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.

  1. 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:

PartTypical sizeBehaviour over a runNotes
System prompt1k to 5kFixedShould never change mid-run
Tool definitions5k to 40kFixed, unless tools are loaded on demandFront of the prompt, cached
ConversationSmall at firstGrows every turnUser messages, model replies, tool calls
Tool resultsUsually the largestGrows fastestFile reads, search results, logs
Notes and memorySmallGrows slowlyPlans, progress, facts carried forward
Reserved output2k to 16kFixedRoom 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

Context budget
A coding agent's window across a run
17.8k of 200k tokens · 9%cached prefix 17k · hit
  • System prompt3k
  • Tool definitions14k
  • Conversation800
  • Tool results0
  • Progress notes0
1/5

Turn 1

A small task statement on top of a stable, cached prefix.

Illustrative sizes. The threshold is set at 50% of the window because quality degrades well before the limit.

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
  1. 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.
  2. 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.
  3. Reserve output space. The reply and its tool calls need room; a request that leaves none gets truncated output.
  4. 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.

  1. 1Why set the clearing threshold at 50% of the window rather than near 100%?

  2. 2A teammate proposes doubling the context window to fix an agent that loses track after 50 turns. What do you ask first?

  3. 3Which parts of the context should never change during a run, and why?

Practise it as an interviewer asks it

Course lessons that teach this

Related lessons