/compact - Free Up Claude Code's Memory Mid-Session
When Claude Code gets slow or forgetful, /compact summarizes the conversation and frees up context space so you can keep working.
What is it
Claude Code has a "context window." Think of it as short-term memory. Everything you say, every file Claude reads, every command it runs fills up that memory. When it gets full, Claude starts forgetting things from earlier in the conversation, and responses slow down.
/compact is a built-in command that tells Claude to summarize everything important from the conversation so far and throw away the raw history. Same information, way less space. It's like compressing a folder full of files into a zip.
Claude Code context window docs
What problems it solves
You're a couple hours into a complex refactor. Claude was sharp at the start, but now it's repeating itself, forgetting decisions you already made, or taking forever to respond. The context window filled up with file reads and command outputs from earlier steps. You don't want to start a new session because you'd lose all that shared context about what you're doing.
Or you asked Claude to read a dozen files to understand a codebase, and now there's barely any room left for it to actually do the work. All that research is sitting in memory taking up space, even though Claude already extracted what it needs.
How to use it
Just type it:
/compactClaude summarizes the conversation and clears the raw history. You'll see a "Conversation compacted" message and can keep working like nothing happened.
The real power is the focus argument. Tell Claude what to prioritize in the summary:
/compact focus on the auth refactor and the test failures we foundWithout a focus, Claude decides what's important. With a focus, you guarantee the stuff you care about survives the compression.
What about auto-compact?
Claude Code also compacts automatically when context gets full. It clears older tool outputs first (file reads, command results), then summarizes the conversation if needed. Your messages and key code snippets are preserved; detailed instructions from early in the session might not be.
That's why running /compact yourself with a focus is better than waiting for auto-compact. You control what survives.
Keep instructions alive across compacts
If there are rules or decisions Claude should never forget during a session, don't rely on the conversation history. Put them in your project's CLAUDE.md file instead. That file gets reloaded after every compact.
You can also add a "Compact Instructions" section to CLAUDE.md to tell Claude specifically what to preserve when compacting.
Pro tips
Run /context any time to see exactly what's eating up space. MCP tool definitions, file reads, and long command outputs are usually the biggest consumers. This helps you decide when to compact and what to focus on.
If you're about to start a new phase of work (done researching, ready to implement), that's the ideal time for a /compact. Compress the research, keep the conclusions, and start building with a clean context.