The context window
The context window is Claude Code's working memory — everything it can "see" at once: your prompts, the files it has read, command output, and its own previous responses. It's finite, and managing it well is the difference between sharp answers and sluggish, confused ones.
Why it matters
Every file Claude reads and every command it runs adds to the context window. Two things happen as it fills:
- Performance degrades. The more crowded the context, the harder it is for Claude to focus on what's relevant. Accuracy drops and responses slow down — long before the window is technically "full."
- Cost rises. Larger context means more tokens processed on every turn.
The takeaway: a lean context isn't just cheaper, it makes Claude better. Treat context as a resource you actively manage.
See what's in it: /context
Run this any time to inspect current usage:
/contextIt shows how much of the window is consumed and by what. Use it when responses start feeling off — bloated context is often the cause.
/compact vs /clear
Both free up space, but they do different things:
/clearwipes the conversation entirely. Start here between unrelated tasks — there's no reason to carry the last task's files into the next one./compactsummarizes the conversation so far, preserving the gist while dropping the bulk. Use it mid-task, when you still need continuity but the context has grown heavy.
/clear # done with this task, moving to something new
/compact # same task continues, but context is getting fullNOTE
The prompt-cache nuance. Claude Code caches your context for about 5 minutes. Inside that warm window, /compact is cheap because most of the conversation is still cached. Outside it (after a long pause), the cache is cold either way — so if you no longer need the history, /clear is the cheaper choice. Rule of thumb: compact to continue a task, clear to abandon one.
Auto-compaction
A full window doesn't end the session: as the conversation approaches the limit, Claude Code compacts automatically — the same summarization as /compact, run for you. Project-root CLAUDE.md and auto memory are re-injected from disk afterward, but path-scoped rules and nested CLAUDE.md files are summarized away until a matching file is read again.
How full the window gets before that pass runs is configurable — the auto-compact window — via /autocompact, the --autocompact flag, or CLAUDE_CODE_AUTO_COMPACT_WINDOW. See Tune auto-compaction.
Protect context with subagents
When a task requires reading many files — a broad investigation, a wide search — that reading can flood your main context. A subagent does the work in its own context window and returns only a summary:
use a subagent to find every place we call the payments API and summarize themYour main context stays clean; you get the answer without the clutter. This is one of the most effective ways to take on big investigations without degrading the session.
A simple routine
/clearbetween unrelated tasks./compactwhen a long task fills up but you need to keep going./contextto check before things feel slow.- Subagents for anything that reads a lot of files.
For the full picture on keeping sessions efficient, see Optimize cost & context.
Source: Manage cost & context