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| Source | Typical size | Controllable? |
|---|---|---|
| System prompt and tool definitions | Fixed, several thousand tokens | No |
| Instruction files (CLAUDE.md) | Grows with your project | Yes - keep them short |
| File reads | Largest and fastest-growing | Yes - read ranges, not whole files |
| Command output | Spiky; a test run can be huge | Yes - pipe through head or grep |
| Conversation history | Grows every turn | Yes - /clear and /compact |
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.
/clearwhen the next task shares no files with the current one;/compactwhen 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,tailor 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 3Prompt 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
| Command | Effect | Use when |
|---|---|---|
claude -c | Continues the latest session in this directory | You closed the terminal mid-task |
claude -r | Interactive picker over past sessions | You want to find that debugging session from yesterday |
claude --fork-session -c | Resumes without overwriting the original history | You want to try a different approach from the same starting point |
/clear | Empties the conversation, keeps the process | Starting 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?
Why does the agent forget something I just told it?
Related
Scoped instructions: memory, rules and path-specific context Cost, model choice and troubleshooting
Last refreshed 2026-09-18.