---
title: "Keep Claude Code's context window clean"
description: "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."
url: https://pierresabbagh.com/resources/keep-claude-code-context-clean
published: 2026-12-01
author: "Pierre Sabbagh"
---

# Keep Claude Code's context window clean

## Start with this prompt

Paste it into Claude.

```text
At my computer later, I'll use Claude Code's commands to keep a long session's context window clean: /context to see how full it is, /compact to summarize, /clear to start fresh. Get me ready now. Ask me what I use Claude Code for and whether my sessions usually stay on one job or drift to different ones. Then write the one command I should try first and the exact line to type, including a /compact focus line if it fits. Never make up a command or a setting. No setup steps. End with: Set it up at your computer: https://pierresabbagh.com/resources/keep-claude-code-context-clean
```

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`, `/autocompact` and `/clear` are Claude Code commands. If you haven't set it up yet, start with [my guide to setting up Claude Code](https://pierresabbagh.com/resources/set-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

1. In Claude Code, with your long session open, type this and press Enter:

```text
/context
```

2. 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:

```text
/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 `/autocompact` with no value to see your session's window. Give it a size to change it, for example:

```text
/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:

```text
/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.*
