Managing the context window and session hygiene

What loads automatically, why a long session gets worse rather than just slower, and when to compact, clear or start fresh.

What fills the window

The context window is the model's working memory for one session. Everything competes for it: the system prompt, your instruction files, the full text of every file that has been read, the output of every command, and both halves of every exchange. Once the window is full, older turns are summarised or dropped, and the agent starts forgetting things it read twenty minutes ago.

# inside a session
/context        # token usage broken down by source
/cost           # what this session has spent so far
/clear          # start over with an empty conversation
/compact        # summarise the conversation so far and continue

# from the shell
claude -c                       # continue the most recent session
claude -r                       # pick a past session to resume
claude --fork-session -c        # resume, but branch instead of overwriting
SourceTypical sizeControllable?
System prompt and tool definitionsFixed, several thousand tokensNo
Instruction files (CLAUDE.md)Grows with your projectYes - keep them short
File readsLargest and fastest-growingYes - read ranges, not whole files
Command outputSpiky; a test run can be hugeYes - pipe through head or grep
Conversation historyGrows every turnYes - /clear and /compact
💡
A degraded session rarely announces itself. The first symptom is usually the agent re-reading a file it already read, or asking a question you answered earlier - that is the signal to compact or clear, not to explain yourself again.

Session hygiene that keeps quality up

  • One task per session. Finishing a feature and then asking for an unrelated refactor in the same window pays for both contexts at once.
  • /clear when the next task shares no files with the current one; /compact when it does and you want the decisions kept.
  • Point the agent at the file and area rather than the directory: the whole repo in context is mostly noise.
  • Ask for head, tail or a filtered version of noisy command output instead of the raw dump.
  • Start fresh after two failed attempts at the same fix - a poisoned session keeps re-reading its own wrong conclusion.
# a cheap way to cap how much output enters the window
npm test 2>&1 | tail -40

# check what a file costs before reading it
wc -l src/server/*.js

# non-interactive, with a hard ceiling on turns
claude -p "list the exported functions in src/api" --max-turns 3

Prompt caching is automatic on the server side: a stable prefix (system prompt, instruction files) is cheaper to re-send than a changing one. That is one more reason not to shuffle your instruction files mid-session - editing them invalidates the cache and can cost more than it saves.

Resuming, branching and long-running work

CommandEffectUse when
claude -cContinues the latest session in this directoryYou closed the terminal mid-task
claude -rInteractive picker over past sessionsYou want to find that debugging session from yesterday
claude --fork-session -cResumes without overwriting the original historyYou want to try a different approach from the same starting point
/clearEmpties the conversation, keeps the processStarting an unrelated task
# a long refactor split into three resumable sessions
claude -p "rename the User model to Account across src/models and update imports" --max-turns 20
claude -c -p "now update the tests to match the rename"
claude -c -p "summarise what changed and which files still reference the old name"

Sending work back with -c keeps the renamed-file list in context, so the follow-up does not have to rediscover it. If a resumed session starts behaving oddly, that is the accumulated history talking: fork instead of continuing, or clear and restate the current state.

FAQ

Should I use /compact or /clear?
Compact keeps a summary of the decisions so far, which is what you want mid-task when the files are still relevant. Clear throws the history away, which is what you want when the next task is unrelated. Compacting repeatedly in one session eventually loses detail either way - at that point start a new session with a written summary.
Why does the agent forget something I just told it?
Almost always context pressure. The instruction was dropped when older turns were summarised. Move anything durable into a CLAUDE.md file instead of typing it into chat, and clear between unrelated tasks.

Scoped instructions: memory, rules and path-specific context Cost, model choice and troubleshooting

Last refreshed 2026-09-18.