Claude Code compact commands
Use Claude Code compact commands, then keep instructions and memory in the right place.
Published here , updated · 6 sources · Resume reading
Claude Code /compact summarizes the current chat so you can keep working in the same session without wiping decisions. /clear starts empty context for a new task. Rules that must survive either command belong in CLAUDE.md, not in chat history alone. [1][2]
Related reading: Claude Code Mode on Bedrock, Foundry, Agent Platform.
When to compact versus clear
The commands doc separates mid-task controls from between-session ones. During work, /context shows what fills the window and /compact summarizes history to free space. Between tasks, /clear resets the conversation while project memory files still load next time. [1]
| Goal | Command | What changes |
|---|---|---|
| Same task, less history noise | /compact optional focus text | Older turns become a summary; session continues [1] |
| New task, same repo | /clear | Empty chat; CLAUDE.md still applies [1][2] |
| See what is eating tokens | /context or /context all | Grid of usage plus suggestions [1] |
Optional focus after /compact tells the summarizer what to preserve, per the commands reference. [1]
A concrete branch-debug pattern: you spent twenty turns finding that npm test fails on one assertion in auth.test.ts. Context is nearly full. Run /compact keep the npm test command, the failing assertion, and the file path. The next turn still knows the repro. /clear at that moment throws away the paid investigation and makes you re-paste the same failing output. [1]
Compact and context in the terminal
Documented slash commands:
/compact
/compact keep the npm test command and the failing assertion
/context
/clearRun /context before a large paste. The commands doc says the view warns when you exceed the window and points at commands that free space. [1] /context all adds which memory files loaded, which helps when a nested CLAUDE.md you forgot about is eating budget. [1][2]
If /context shows tool results and old diffs dominating the grid, compact with a focus line that names the decision you still need. If the grid shows mostly system and memory files, compaction will not invent room; trim CLAUDE.md or move path-scoped rules into nested files instead. [1][2]
Why compaction can save a decision
ALGO ARCADE models a 16-token pipe (a teaching size, not a live Claude window). The same chat twice: without compaction the oldest message drops and the model cannot answer; with compaction old turns become one summary that still holds the name.
curl -fsSL -o context_window.py https://raw.githubusercontent.com/kamilikamil/algo-arcade/main/world2/context_window.py
python3 context_window.pyCaptured output:
== plain: a pipe of 16 tokens
[####............] 4/16 ADD "my name is sam" (4 tokens)
...
[###############.] 15/16 DROP "my name is sam" (4 tokens)
[###############.] 15/16 ANSWER ?
== compact: a pipe of 16 tokens
...
[##########......] 10/16 COMPACT "summary: my name is sam" (6 tokens)
[##########......] 10/16 ANSWER SAM
Round 1 lost the name: it was the oldest message. Round 2 compacted the old messages and kept it.That mirrors why /compact exists: replace evicted detail with a shorter summary instead of silently losing it. Real Claude Code uses model-written summaries, not this script. [1][6]
Compaction trades verbatim turns for a shorter summary; files and CLAUDE.md hold what must not drift.
Put non-negotiable rules in CLAUDE.md
Claude Code memory docs say each session starts fresh while CLAUDE.md and auto memory carry knowledge across sessions. Project instructions live in ./CLAUDE.md or ./.claude/CLAUDE.md, and Claude can read AGENTS.md where the repo uses that file instead. Target under 200 lines per file so instructions stay followable. [2]
Example project snippet:
Run npm test before changing a dependency.
Never commit secrets; stop if .env would be staged.Run /init once if the repo has no instruction file yet; the memory guide documents that flow. [2]
Nested directories can carry their own instruction files that load when you work in that path. Keep each file focused: root for repo-wide gates, nested for package-local commands. Stuffing every preference into one global file is how /context starts looking full before you paste a single log. [2]
ChatGPT and Claude app parallels
ChatGPT Custom Instructions apply across chats; Memory can reuse preferences from older conversations. [3][4] Claude app Projects hold project chats, knowledge, and instructions in one workspace. [5]
Those features resemble CLAUDE.md plus project files more than they resemble /compact. When you outgrow a long ChatGPT thread, copying a short summary into a new chat is a manual version of compaction. Claude Code documents the built-in /compact and /clear pair instead. [1]
Suggested paste prompt when you manually move chats (workflow tip, not a vendor command):
Summarize this chat in five lines. Keep every decision, file path, and command we agreed on.Closing the terminal without /clear is not the same as compacting. A new session still reloads CLAUDE.md and auto memory, but the previous chat's summary is gone unless you wrote decisions into a file first. [1][2] Keep a NOTES.md in the branch for the failing command and assertion when you know you will restart the agent overnight.
Auto memory versus things you write
Auto memory holds notes Claude writes from corrections; CLAUDE.md holds instructions you author. The memory doc compares them: you edit CLAUDE files directly, while auto memory loads from MEMORY.md with size limits when enabled. [2]
Use auto memory for preferences that emerge over time. Use CLAUDE.md for commands that must never drift, such as test gates or secret-handling rules. After a productive compact, if a decision must survive the next /clear, promote it into CLAUDE.md or a ticket file in the repo before you reset. [2]
Mistakes with compact and context
Compacting then treating the summary as a verbatim transcript. Summaries shorten text. Exact wording that still matters belongs in a file Claude can read again or in CLAUDE.md. [1][2]
Using /clear while the feature branch debug is unfinished. /clear wipes conversational context. Prefer /compact with a focus phrase when the same branch is still active. [1]
Skipping /context before a huge paste. Check the grid first; free space with compact or by trimming memory files before dumping multi-thousand-line logs. [1]
Stuffing everything into one global CLAUDE.md. Nested rules and path-scoped files load on demand; the memory guide recommends keeping each file focused. [2]
Expecting compaction to reload skills you removed. Compaction summarizes conversation; installed skills and memory files follow their own load rules. [1][2]
Treating ALGO ARCADE output as token-accurate. The script uses a 4-character-per-token estimate for teaching. Real models use their own tokenizers. [6]
Compare with an agent tool allow list and a 429 retry, or browse the Cost and context topic.
When the feature branch is still active, /compact keep the repro steps beats /clear and losing the thread you already paid for in tokens.
Treat compact as a checkpoint, not a brain wipe. Before you compact a long debug thread, paste the failing command and the last error into project notes so the summary cannot drop the one fact you still need.
Send this to the friend who ran /clear on a branch debug session instead of /compact keep.

Keep this guide
Filed under Cost and context, AI agents
Questions about this guide
What do Claude Code compact commands do?
It compacts the conversation so the session can continue with a shorter context.
What does Claude Code context show?
It visualizes current context use and can show the memory files Claude Code loaded.
Sources
- Claude Code commandscode.claude.com
- Claude Code memorycode.claude.com
- ChatGPT custom instructionshelp.openai.com
- ChatGPT memoryhelp.openai.com
- Claude app projectssupport.anthropic.com
- ALGO ARCADE context-window levelgithub.com