LEARN · PHASE 01 · LESSON 8

The Context Window — the only budget that matters

You can look at any session and say what is in the window, what is about to be

Type
Learn
Prerequisites
Lesson 7, Tokens — why text becomes currency
Time
~35 minutes

Learning Objectives

  • List what occupies a session's window before the user types a word
  • Read /context and name the biggest category in a real session
  • Choose between letting a session continue, compacting it, and clearing it
  • Move a rule out of conversation history and into a file that always loads

The Problem

An agent was mid-way through a four-file refactor. It stopped, restarted its approach twice, and eventually proposed a change that reverted an edit it had made an hour earlier — an edit it had made itself, and reported as done.

Nothing crashed. No error appeared. The context window had filled, the conversation was summarized, and the summary did not carry the detail that the agent had already established: which of the four files it had changed and how. The agent behaved correctly given what it could still see. That is what makes this failure so quiet.

Everything else in this course hangs off this one fact. Parallel agents work because each has its own window. Compaction exists because windows fill. Long sessions are expensive because windows re-send. You cannot reason about any of that until "what is in the window, and what happens when it's full" is concrete.


The Concept

The context window is the entire body of text the model sees on the next turn. Not the chat. Everything: your instructions, the agent's instructions, the tool list, files it has read, command output, every word of both sides of the conversation.

It is a fixed budget. It fills. It is never enlarged by being clever — and when it fills, something is discarded, because there is no other option.

flowchart TD
    subgraph Startup["Loaded before you type"]
        A[System instructions] --> Z[Window]
        B[CLAUDE.md] --> Z
        C[Memory files] --> Z
        D[Tool definitions] --> Z
    end
    subgraph Work["Accumulates as you work"]
        E[File reads] --> Z
        F[Command output] --> Z
        G[Both sides of the conversation] --> Z
    end
    Z --> H{Full?}
    H -->|No| I[Next turn]
    I --> G
    H -->|Yes| J[Compact: summarize older history]
    J --> Z
Mermaid source — renders as a diagram where mermaid.js is available

What fills it, in order of arrival

Before you type anything:

WhatRoughly
System instructions for the harnessA few thousand tokens. Never shown to you.
CLAUDE.md and memory filesWhatever you wrote. Free to add — but not free.
Tool definitionsEvery tool the agent can call, with its schema.
Environment detailsWorking directory, platform, shell, git state.

As work proceeds:

  • every file read, at full length
  • every command's entire output
  • every message, both directions
  • notes the agent takes to itself about itself

The single most expensive thing that can happen is a single large read. One pasted log can outweigh an hour of conversation.

What happens when it fills

It does not fail. It compacts: the harness summarizes older history to make room, and the conversation continues from the summary.

What survives depends on how the content got there. This is the part that matters and almost nobody knows:

ContentAfter compaction
System instructionsStill there
Project-root CLAUDE.md, unscoped rulesRe-injected from disk
Memory filesRe-injected from disk
Recently read or edited filesA handful re-read, most recently modified first
Skill bodies you invokedRe-injected, capped, oldest dropped first
Rules scoped to a pathSummarized away — they load again on demand next time
The middle of the conversationGone. Replaced by a summary.

The last row is where the refactor died. The first rows are why the agent still knows the project's conventions. So:

  • Anything that must survive, write it to a file. Files get re-injected. Conversation history does not.
  • Anything you want a summary to preserve, say so. /compact focus on the auth bug fix steers what survives. The automatic pass has to guess.

The four moves

CommandWhat it doesWhen
/contextDraws the window as a grid, by categoryWhenever you want to know where the tokens went
/compact [focus]Summarizes history, keeps the conversationSame task, window filling
/clear [name]New conversation, empty windowSwitching to unrelated work
/resumeReopen a cleared conversationYou cleared and now want it back

/context is the one to make a habit. It turns context from a vibe into a number you can act on.

The reframe: the window is a fixed budget that fills and then summarizes

itself, so anything that must survive compaction belongs in a file that gets

re-read — never in the conversation that is about to be summarized away.

Window sizes, compaction thresholds, and command behaviour change as Claude Code ships. Check the context window page and /context in your own session rather than trusting a figure quoted here.


Build It

1. Look at a window before it matters

Start a session in this repo and run, as your first action:

/context

Before you have typed a real request. Everything on that grid is what the agent carries for free — the cost of you being here.

✅ You pass when you can name the three largest categories on the grid and none of them are the conversation.

2. Watch it fill

Ask for a real investigation:

Find every place this repo documents a lesson format. Report paths only.

Run /context again. Compare. The conversation is now a real category.

✅ You pass when you can point at which category grew and by roughly how much.

3. Find the expensive single item

Look for the largest category that is not the conversation — usually file reads or command output. Ask for one deliberately huge read and watch it:

Print the whole of scripts/validate-lessons.mjs with line numbers.

Then /context. One request, and a large permanent addition.

✅ You pass when you have a before-and-after number showing what one read cost.

4. Steer a compaction

/compact focus on the lesson-format findings, drop the file listings

Now check what the conversation still contains. The focus instruction decided what survived. Without it, the automatic pass guesses.

✅ You pass when you can quote something you asked to keep and confirm it is still there after the compaction.

5. Watch a rule get summarized away

Put a scoped rule in the conversation and compact, versus putting the same content in CLAUDE.md and compact. The first is at the mercy of the summary. The second is re-read from disk.

Keep it simple — the file moves:

/compact

then confirm the project instructions are still present in the session afterwards.

✅ You pass when you can say which of the two survived and why, naming the mechanism rather than the feeling.

6. Clear between unrelated tasks

You have finished one task and are starting a completely different one. The old context is not stale to the model — it is still there, still being paid for, still crowding out.

/clear phase-one-done

Then /context. Compare to the previous grid.

✅ You pass when the new window is visibly emptier and you named the task you switched away from.


Use It

CommandWhen you reach for it
/contextAny time you want to know where the tokens went
/compactSame task, window filling, history matters
/compact <focus>Before a long stretch — you know what must survive
/clear <name>Switching to unrelated work
/resumeAfter a clear, when you want the old thread back
Put rules in CLAUDE.mdAny rule that must survive compaction
Ask for narrow outputPreventing the fill in the first place

Exercises

  1. Record /context three times in one session: at startup, after a broad investigation, and after /compact. Keep all three grids. ✅ You pass when you can say which category grew most and whether compaction shrank the conversation category specifically.
  1. Write one instruction you want to survive an hour of work. Put it in CLAUDE.md in a scratch project. Compact. Confirm it survived. Then say what would have happened had you only said it in chat. ✅ You pass when the rule is present after compaction and you can describe the mechanism from memory.
  1. Hit /context twice in a real session and find the largest thing you did not need in the window. Remove it with a narrower ask next time. ✅ You pass when you have the before-and-after grids and a one-line note on what you would ask instead.

Key Terms

TermWhat people sayWhat it actually means
context window"the chat history"Every token the model sees next turn: instructions, tool definitions, files, command output, and the conversation. One budget for all of it.
context"what it remembers"The whole re-sent body. It is re-transmitted, not recalled from a store.
compaction"it forgot"Automatic or manual summarization of older history to make room. Loses detail by design.
survives compaction"it's still in context"Depends on how it got there. Files and root instructions are re-read; the middle of the conversation is not.
/clear"new chat"Empty window, new conversation. /resume reopens a cleared one.
window size"how much it can hold"A per-model, sometimes per-configuration number. Move it and you break this lesson — check /context and the docs.

What this lesson skipped

Prompt caching. Reading the same content twice can cost far less the second time, which changes how you reason about repeated reads. It is real and it shows up in /usage. It needs a cost frame to make sense, and Phase 03 is where models and their trade-offs land.

Isolated windows. The reason parallel agents work is that each one has its own window. That is Phase 06, and it depends on this lesson rather than the other way round.

Skills and path-scoped rules as context-management tools. Both appear in the compaction table above and both get their own phases, because each needs grounding this phase cannot give.

How to raise the ceiling. Some models offer a much larger window. Irrelevant until Phase 03, where choosing a model is the actual skill — a bigger window costs more and is usually the wrong answer.

Fine-grained compaction control. Tuning exactly when automatic compaction fires. Available in settings; premature. Get the instincts first.

Check yourself

3 questions. Answer before you reveal — recognising the right option is not the same as being able to produce it.

  1. Halfway through a long session you need to make sure one rule holds for the next two hours of work. Where does it belong?

    1. In the conversation, restated every few turns so the agent keeps seeing it
    2. In CLAUDE.md at the project root
    3. At the top of the file the agent will edit next
    4. In the first message of the session, since context is stored oldest-first
    Show answer

    B. In CLAUDE.md at the project root

    Compaction summarizes older history, so a rule that lives only in conversation is exactly what gets summarized away — and restating it just refills the window you are trying to protect. CLAUDE.md is re-injected from disk after compaction, which is the whole reason it is the durable place for a rule. Option C is seductive but wrong: a file's contents only enter context when it is read, and only that reading gets summarized away. Option D inverts the mechanism — nothing in the window is priority-ordered.

  2. You finished a refactor and are starting an unrelated feature in the same repo. /context shows the window more than half full. What is the right move?

    1. Keep going — the window has room and clearing loses the refactor's reasoning
    2. Run /compact with a focus note, so the old work is summarized rather than dropped
    3. Run /clear with a name, so the new feature starts in an empty window
    4. Ask the agent to ignore the earlier part of the conversation
    Show answer

    C. Run /clear with a name, so the new feature starts in an empty window

    Unrelated work does not benefit from the previous task's history — it only competes for window space and is re-paid on every turn. /clear starts the new task empty and /resume can bring the old thread back if you need it. Option B is for continuing the same task, where the history still has value. Option D asks for something the harness decides, not the model — compaction is not something a request can turn off.

  3. The window is at roughly 80% and the agent still needs to read four more files. What is the most useful thing to do before asking for those reads?

    1. Run /context, find the largest non-conversation category, and see whether it is something you actually needed
    2. Ask the agent to work faster so it generates fewer output tokens
    3. Switch to a model with a smaller context window to leave headroom
    4. Nothing — the harness compacts automatically, so there is no decision to make
    Show answer

    A. Run /context, find the largest non-conversation category, and see whether it is something you actually needed

    /context shows the window broken down by category, so you can find out what is actually large before deciding what to do about it — that is the decision worth making. Option A's wrong turn is assuming the conversation is the problem; often it is one large file read or one command output. Option C inverts the goal: a smaller window makes the squeeze worse. Option D is half true — automatic compaction is real, but it decides what to drop for you, which is exactly the loss you are trying to avoid.