A free guide by Pierre Sabbagh
Keep Claude Code’s context window clean
How to see how full Claude Code’s context window is, and the commands that free it up: /context, /compact with a focus, /autocompact and /clear. When each one fits.
Needs a computer and a paid Claude plan. You won’t write any code.
It writes the one command to try first, from how you work. Press send when it opens.
Written December 1 from Claude Code’s own pages on the context window and model configuration. The steps are below.
Let AI walk you through it
It reads this guide and works through it with you, one step at a time.
You’ll need Claude Code, which comes with a paid Claude plan. Nothing else to install.
When your browser asks, allow it to open the app. The prompt will be typed in for you, so just press send.
Need the app first? Get the Claude app for Claude Code.
Send this guide to your inbox.
Follow the guide yourself
Here are the commands from my video. Claude Code keeps your whole conversation in what’s called a context window. The longer a session runs, the fuller it gets. Below: how to see how full yours is, and the four commands that free it up. You won’t write any code.
What you need
- A paid Claude plan. Claude Code comes with it.
- Claude Code, not the regular Claude chat.
/context,/compact,/autocompactand/clearare Claude Code commands. If you haven’t set it up yet, start with my guide to setting up Claude Code. - A session that has already run for a while, so there is something to look at.
Why it matters
The context window holds everything Claude knows about your session: your instructions, the files it reads, and its own replies. Claude’s docs say every message and reply stays in it, and earlier turns are kept in full. They also say that as the amount of text grows, accuracy and recall get worse, an effect they call context rot. So a long, crowded session can give worse answers than a fresh one.
Old conversation also costs something: Claude Code’s context window page says it “costs tokens on every message”. On a paid plan that uses up your plan’s usage, not a separate bill.
Step 1: see how full it is
- In Claude Code, with your long session open, type this and press Enter:
/context
- It draws a colored grid of your current context use, with suggestions for anything taking too much room, such as heavy tools or large memory files. Look at how full it is before you change anything.
Step 2: compact it yourself
/compact summarizes the conversation so far and frees up space. Type it on its own, or give it a focus so the summary keeps what you care about:
/compact focus on the auth bug fix
Replace “the auth bug fix” with your own job. Claude’s docs suggest doing this before you start a long new task: the summary keeps what you chose, instead of what the automatic pass guesses is important.
Then type /context again to see the difference.
What comes back after a compact: your CLAUDE.md files and auto memory are loaded again from disk, and Claude Code re-reads up to five of the files Claude read or edited most recently. The rest of the conversation is replaced by the summary.
Step 3: know what auto-compact does
Claude Code also compacts by itself, so a long conversation doesn’t end your session. You don’t have to do anything for that to happen. Two things to know, both from the model configuration page:
- When it happens depends on your model and settings. On current models with a 1 million token window (the Fable models, Sonnet 5 and later, Haiku 5.5, Opus 4.7 and later), the default is to compact near 400,000 tokens, with some cases running later. The page says compaction there runs at about 367K tokens. That 400K default needs Claude Code v2.1.334 or later and is still rolling out. On an older version, or before it reaches you, it uses the full 1 million window, so compaction waits until about 967K tokens. The check below shows which one you have.
- You can see and change the point. Type
/autocompactwith no value to see your session’s window. Give it a size to change it, for example:
/autocompact 500k
Sizes can be a plain token count, or end in k or M. The command saves your choice for the model you are using. Type /autocompact auto to go back to the window Anthropic tunes for your model. Needs Claude Code v2.1.221 or later.
Step 4: start fresh when the job changes
If you are switching to unrelated work, don’t compact. Type this instead:
/clear
It starts a new conversation with empty context. Claude’s docs say old conversation crowds out the files you need next, and that /clear costs nothing. To return to the earlier conversation, type /resume.
Which one to use
- Same job, getting long:
/compact, with a focus line. - A new, unrelated job:
/clear. - You want compaction to happen earlier or later:
/autocompact. - Not sure how full you are:
/context.
Claude’s pages do not give a percentage at which to compact, so I won’t give you one. Check /context now and then, and compact between tasks rather than in the middle of one.
Written on 1 December 2026 from Claude Code’s pages on the context window, commands, costs and model configuration, and Anthropic’s page on context windows. I haven’t run these steps myself while writing this guide. Claude changes fast: if a command has moved, look for the same words in the app.
The prompt
This is what the buttons send. Copy it and paste it into the AI app you use.
Walk me through this guide, one step at a time: https://pierresabbagh.com/resources/keep-claude-code-context-clean Read the whole page first. If your web tool can't open it, try a command like curl. If you still can't open it, tell me so I can paste the text in, and don't guess the steps. Explain each step in plain words, ask before you install or change anything, and stop after each step so I can check it. The guide is written for Claude Code. If you are a different app, tell me how to do each step in your app, and say so if you're not sure it works there.
Send this guide to your inbox.
Next guide
Pick one way to make money with AI
Ten ways to make money with AI by yourself, split into the ones that can pay soon and the ones that take one to three years. Each has the first step to take, and a way to pick one and stay with it for a full year.
At work
Doing this at a company?
With a team, the hard part is the task itself, the people who do it and who checks the result.