glimfly
ai updated

What is /compact in Claude Code? (and why it can lose your context)

In one sentence:/compact is Claude Code rewriting your conversation into a shorter summary when the context window is filling up, and summaries always drop detail.

What it actually is

Compacting is Claude Code rewriting your conversation into a shorter summary so it still fits inside the model’s context window, the fixed amount of text Claude can hold as “working memory” for a session. Picture a notebook that’s nearly full: instead of stopping, someone tears out the early pages and replaces them with a one-page summary of what happened. The gist survives (what you asked for, what got built, the key decisions), but exact wording, small caveats, and the dead ends you tried and abandoned mostly don’t make the cut.

You can trigger it yourself with /compact, optionally with instructions like /compact focus on the auth bug fix. Claude Code also triggers it on its own (that’s “auto-compact”) as you get close to the context limit. Either way you’ll see a “Conversation compacted” message and the token count drop.

Why your AI just did this

Claude Code manages context automatically as a session fills up. First it clears out older tool outputs (the full text of a grep run, a long npm test log, a file it read five edits ago) since those matter less than the conversation itself. If that’s not enough, it summarizes what’s left. The goal is to keep the session going instead of hitting a hard wall where Claude can’t take in anything new.

This tends to show up on long tasks: a refactor spanning a dozen files, a debugging session with several failed attempts, or any session where large files got read over and over. It isn’t a malfunction. It’s the mechanism that stops a full context window from ending your session outright.

When you’ll run into it

  • The “Conversation compacted” notice appearing mid-task, without you asking for it.
  • You running /compact deliberately before a big new task, to control what gets kept instead of leaving it to auto-compact’s judgment.
  • Claude re-asking something you already answered, or drifting back to a pattern you corrected 50 messages ago, because that correction only lived in conversation history, not in a file.
  • An error instead of a summary: when a single file or tool output is so large that context fills right back up after each compaction pass, Claude Code stops trying after a few attempts and surfaces an error instead of looping forever.
  • Checking /context mid-session and seeing exactly what’s using space (CLAUDE.md, auto memory, loaded skills, tool outputs) before a compact even happens.

What to check

  • Before a long task, run /compact focus on <what matters> instead of letting auto-compact guess what to keep.
  • Move anything Claude must always remember (coding conventions, “never touch this file,” API contracts) into your project-root CLAUDE.md. It reloads from disk after every compact; a nested CLAUDE.md or plain conversation doesn’t.
  • Run /context on long sessions to see what’s using space before you get an unexpected summary.
  • After a compact, re-check that safety-relevant instructions (don’t touch production data, don’t commit secrets, ask before deploying) are still being honored. Don’t assume a summary kept every caveat.
  • For new, unrelated work, use /clear to start fresh instead of letting a bloated session run until it forces a compact on its own terms.

Say it like a dev

Instead of: “Ugh, it forgot what I told it.” Say: “Before we start the migration, run /compact focus on the database schema changes so that doesn’t get lost.”

Instead of: “Just remember, don’t touch payments.ts.” Say: “Add a rule to CLAUDE.md, never edit payments.ts without asking, so it survives the next compact.”

Instead of: “Never compact!” Say: “Run /context first so I can see what’s filling up, then let’s /compact with instructions instead of waiting for auto-compact.”

People actually ask

“What is your Claude Code compacting strategy?”

Don't wait for auto-compact to guess what matters. Run `/compact focus on <the thing you're working on>` before starting a big new task, and put anything you need Claude to always remember (conventions, off-limits files, API contracts) in your project-root CLAUDE.md so it reloads after every compact instead of depending on conversation history.

“What is compacting technically and why does it ruin the session?”

Technically, Claude Code first drops older tool outputs (old file reads, command logs), then has the model summarize the remaining conversation into a shorter version once you're close to the context limit. It can feel like it "ruins" the session because a summary is lossy by nature: a specific instruction, correction, or edge case you mentioned once can get smoothed over or dropped, and Claude has no way to recover it unless it was written to a file.

“Should I "never compact"?”

Avoiding it entirely isn't possible. Auto-compact fires on its own once you're near the context limit, regardless of what you want. The more reliable fix is making the things you care about survive: put persistent rules in project-root CLAUDE.md, run `/compact` with a focus instead of letting it happen automatically, or start a fresh session with `/clear` for unrelated work instead of running one session indefinitely.

Related terms

Go deeper

Checked against

Glim can explain your sessions, live.

The app watches your coding agent and narrates it in plain words. Waitlist is open.

Join the waitlist