Analysis

Claude Code /compact: what it keeps and what it drops

Karan KalraGrowth, Trail

/compact condenses the current Claude Code conversation into a summary and frees up space in the context window. Claude Code also does this automatically when the context window in the conversation is full.

It does this by discarding large tool outputs and other irrelevant details from the context window, while at the same time, preserving key decisions, progress on the task, and other relevant context.

Unlike /clear, which simply starts a new conversation in a fresh context window, /compact lets you continue working on the same task using a summary without losing important context.

However, Anthropic's docs warn that "detailed instructions from early in the conversation may be lost". So we measured what is lost and how much. For instance, we found that rules you type in a session typically survive one compaction. They usually don't survive two, unless they also sit in a file.

What we did:

  • Real sessions. We used a model to evaluate 383 compactions. The first compaction preserved 86% of the rules users had given up to that point. By the second compaction, only 20% of those original rules survived in full. A simple text search, without an LLM, confirmed the same pattern as the model judge.
  • A deeper replay. We handpicked seven heavy sessions, ran three more rounds of compaction on each, and tested whether Claude still remembered the rules. After one compaction, Claude got 33 out of 49 questions right. After two, it got just 8 out of 40 right. By the third compaction, it couldn't answer a single one of the 29 questions correctly.

Of everything we measured, typed rules survived compaction best. Claude was more likely to lose

  1. the reasoning behind those rules
  2. exact values from tool outputs
  3. things it had said earlier itself.

The examples below show what kind of information was lost by comparing the context windows before and after compaction.

WHAT WAS LOSTBEFORE COMPACTIONAFTER COMPACTION
The reason behind a ruleThe developer typed: “Do not suggest turning memory maps on by default again: we tried it and searches on network drives broke.”“I don't know the reason; I only have that turning memory maps on by default is not to be suggested.”
An exact value from tool outputA script printed slowest block: 188 (2.4 s). Claude could repeat it: “Block 188 was the slowest, at 2.4 s.”“Unknown. My notes don't include the slowest block or its timing.”
Something Claude had said itselfClaude had told the user: “119 blocks (122 through 240) were checked after the failing block 121.”“Unknown. My notes say block 121 failed out of 240 blocks, but not whether the blocks were numbered from 000 or 001, or what I told you earlier. So I can't give the exact count after it.”

Stacked bars of answers to questions about rules a user had typed, in seven replayed sessions. Before any compaction: 24 right and 3 partly right of 27. One compaction after the rule was typed: 33 right, 10 partly, 1 wrong and 5 not known of 49. After two: 8 right, 4 partly, 2 wrong and 26 not known of 40. After three: 0 right, 4 partly, 1 wrong and 24 not known of 29.

What does a compaction do?

Claude Code asks the model to summarize the conversation, and then replace the original conversation with that summary in the context window. The next request contains three things:

  • The summary of the conversation. The summary follows a fixed structure with nine headings, including “All user messages”, “Pending Tasks”, “Current Work”. In our tests with a 1M-token context window, these summaries ranged from 1,644 to 2,450 words. Each included a reference to the full transcript so Claude could later look up details that didn't make it into the summary.
  • Recent messages in their original form. Not everything gets summarized. In our session, Claude Code preserved between 2-10 most recent messages verbatim during each compaction.
  • Files and instructions from disk. According to Anthropic's docs, Claude Code reloads the project-root CLAUDE.md, rules without path restrictions, auto memory, and the list of available skills. It also reads up to five files, starting with the most recently modified ones.

We captured an automatic compaction during one of our terminal sessions. Just before compaction, /context showed that 966K tokens of the 1M-token context window were in use. It also showed a 33K-token autocompact buffer. Claude Code reserves this space so it can compact the conversation before the context window runs out of room.

The /context grid, almost entirely filled. Opus 5.5, 966k of 1m tokens, 97%. Messages 932.9k tokens, free space 960 tokens, autocompact buffer 33k tokens. Auto-compact window: 1m tokens.
This is typically the point in the conversation when autocompact triggers.

The next message triggered compaction. It took 97 seconds and brought the token count down to 17k. At this point, Claude no longer has access to the original messages in its context window. All it has to work with is the summary and whatever it retrieves from disk.

What survives one compaction?

Most of the rules you give Claude survive the first compaction. We looked at instructions and corrections that still applied at the time of compaction.

For example, a rule about how Claude should write code for the rest of the session counts, but a request to fix a specific bug does not. Two messages from our session show the difference:

In crates/printer/ only, keep lines under 80 characters. Elsewhere I do not mind.

All tickets are in. Run the whole test suite again and fix anything that fails.

The first applies to every later change in that folder, so it counts. The second is finished once the suite passes, so it doesn't count.

We also distinguished between recent rules (given since the last compaction) and older rules (given before a previous compaction).

During the first compaction of a session, Claude retained 86% of these rules in full (259/302). Another 9% were partially preserved, while 5% were lost entirely.

Stacked bars of kept, partly kept and absent rules at the first three compactions of a session. Typed since the last compaction: first compaction 259, 27 and 16 of 302, 86% kept; second 93, 10 and 13 of 116, 80%; third 35, 4 and 0 of 39, 90%. Typed before an earlier compaction: second compaction 31, 57 and 66 of 154, 20% kept; third 20, 44 and 52 of 116, 17%.

Compaction preserves more of what you type than what Claude says or reads. For 24 compactions, we had a model go through the entire conversation since the previous compaction and identify what was still relevant. The numbers are lower here because we set a higher bar: an item counts as retained only if every detail makes it into the summary.

WHERE THE ITEM CAME FROMITEMSKEPT IN FULLKEPT IN PARTLOST
Typed by the developer16364%25%10%
Stated by Claude in a reply44342%38%20%
Only in tool output31736%38%26%

Here's an example of what got lost. In one of our sessions, a task ran a script that printed a file path. At 804K tokens (before compaction), Claude could still recall the first path:

scripts/u01.sh said it wrote /var/tmp/u01-4271.idx.

After compaction, Claude had the shape of the path and not the path:

unknown. My notes only say it wrote a file matching /var/tmp/u01-*.idx, not the exact name.

In another session, two worker settings were cut from 16 and 32 to 4 and 8, then raised to 8 and 16. A command's output after the change showed:

MEMARMS_BUILD_WORKERS_PER_ARM=8
MEMARMS_BUILD_WORKERS_GLOBAL=16

The summary, written at 1.0M tokens, kept only the cut:

… dropped workers to 4/8, then the retry-gate fix …

In another, Claude had written this in a reply to a code review thread:

A clearance is now necessary and never sufficient: the affirmative sensitivity test still runs, so the mode removes the permissive fallback and never the floor.

The first summary kept that. After a manual /compact at 375K tokens, the second summary said the opposite:

Fail-closed mode (column_policy.require_classification) — a clearance un-masks

The main issue was summaries missing out on details, but on very rare occasions, the summaries also got things wrong. Of the ~1000 items we checked, 4 were contradicted by a summary saying something opposite of what was true.

What survives two or three compactions?

Claude retains far fewer rules after a second compaction. By the second compaction, only 20% of the rules written before the first compaction were fully preserved (31/154). Another 37% were partially preserved, and 43% were lost entirely.

And by the third compaction, only 17% of the older rules remained intact.

Here are three rules that were absent from the second summary, as the users typed them.

WHAT THE USER TYPED BEFORE THE FIRST COMPACTIONSECOND COMPACTIONIN THE SECOND SUMMARY
“no, keep the main.tex untouched, we are working on the rebutall draft”Automatic, at 1.0M tokensDescribes the rebuttal drafts at length. Never names main.tex.
“Also, we need to make the deep bypass off by default. It has to be explicitly flip in order to be enabled.”Manual, at 669K tokensNo mention of the bypass.
“the coderabbit cli review should be run before creating a pr. …”Manual, at 476K tokensNo mention of CodeRabbit.

This is likely because each compaction builds on the previous summary rather than the original messages. Summaries reproduced 61% of the messages written since the last compaction verbatim, compared to just 7% of older messages. A simple text search, without using an LLM, verified the same pattern.

But could Claude still recall rules that the judge failed to identify? A summary might preserve a rule without using the same words. To test this, we took a few sessions and truncated them at their first compaction. We then appended the next set of messages from each original session and repeated the process for a total of three compactions. After each compaction, we forked the session and asked Claude questions about rules the user had previously given it. Claude had no access to tools or project files.

Before compaction, Claude answered 24 of 27 questions correctly and the remaining three partially. After one compaction, it got 33 of 49 right. After two, it got just 8 of 40. After three, it couldn't answer a single question correctly (0 of 29).

Consider a rule from one of these sessions. Before the first compaction, the user had typed:

Also if u don’t change anything in baseline then no need to run it and waste time etc …

We asked Claude when it needed to rerun the baseline.

WHENTHE RULE IN THE SUMMARYCLAUDE'S ANSWERGRADE
Before any compactionNo summary yet“Only when something in the baseline changes.”Right
After one“don't rerun unchanged baseline”, in its list of user messages“You said not to rerun an unchanged baseline.”Right
After twoGone. What remains is “baseline must be uncheated; fresh both arms”“It cannot be reused from an earlier run.”Wrong
After three“baseline must be fair and fresh”“not known. I only know the baseline must be fair and fresh, not the specific condition that triggers a rerun.”Did not know

The phrase “fresh both arms” came from Claude's own plans, two of which the user had approved. The user never used those words. Before compaction, Claude had both the plans and the user's original sentence in its context and still gave the user's rule.

When asked directly, Claude more often admitted it couldn't remember. Of the 118 answers given after ≥ 1 compactions, 55 were admissions of uncertainty / missing knowledge. Four answers were outright wrong.

COMPACTIONS SINCE THE RULEQUESTIONWHAT THE USER TYPEDCLAUDE'S ANSWERGRADE
OneBesides locks, what kind of device did I say our credential design should also work for?“our design should also work for e.g. an RFID esphome reader”“Alarm control panels (and later cover/switch entities came up too), though I'm not certain that's the exact framing you used.”Wrong
TwoWhen does the baseline arm need to be rerun?“Also if u don’t change anything in baseline then no need to run it and waste time etc …”“The baseline must be uncheated and rerun fresh alongside the graph arm in every comparison ("fresh both arms"). It cannot be reused from an earlier run.”Wrong
TwoWhat should be checked before handing work back to me after all updates are done?“after all updates are done make sure PR ddescriptions and titles are up to date before handing back to me”“Run the full test suite with the 100% coverage gate, run prek (not pre-commit), and run mutation tests.”Wrong
OneHow often should you give progress updates?“give 5 min updates”“not known”Did not know
TwoAs the memory-bench agent, which benchmarks do you own, and which three files should you read first, in what order?“You are the memory-bench agent, one of two Claude Code agents working this machine in parallel. … YOU OWN: memory/recall benchmarks — LOCOMO, prose-memory, cmm (codebase-memory-mcp) parity. FIRST, READ THESE THREE FILES IN ORDER: …”“not known”Did not know
TwoWhere should the 100-task benchmark be run, and what did I want checked about the artifact?“… don;t run the 100 here chat, only for the atfiact maek sure the artificat is update to date”“not known”Did not know
TwoWhat should happen with the availability entities feature?“I think we should get rid of the availability entities feature entirely and make it always available if there are no locks. We can always add that in later, no need to overcomplicate things now”“not known”Did not know

Manual /compact worked better, but still lost most older rules. Across 383 real compactions, it preserved 93% of recent rules and 38% of older ones. Automatic compaction at 900K tokens or more preserved 76% and 16%, respectively. The direct explanation is in the size of the context window that is getting compacted: Users we tested trigger manual compaction after a median of 7 messages, compared to 25 for automatic compaction.

Other researchers have reported similar findings. Salle et al. found that compaction summaries retained 81% of 269 user constraints in SWE-chat sessions.

In a smaller experiment, murooka tracked 18 instructions across his own conversations. The first compaction lost none, but by the third, instruction loss reached 33% and 57% in his tests.

Compaction and Memory Work Together

In reality, the compaction summary is not the only thing Claude is left to work with after a compaction. Claude's own notes also retain facts and rules.

To demonstrate how well this works in a long session, we'll use a developer session working through ripgrep tickets. In 36 messages, the developer slips in side notes.

These side notes contained 26 facts, including

  • corrected values
  • changes to existing rules
  • exceptions
  • reasons for abandoning certain approaches.
KIND OF FACTEXAMPLEHOW MANY
A corrected value“The flaky-test budget is 12 retries a day.” Later: “Correction: the flaky-test budget is 5 retries a day, not 12.”6
A changed rule“On this repo we rebase and never use merge commits.” Later: “New policy that replaces the old one: we now use merge commits and never rebase.”4
An exception“Never suggest adding a dependency, except memchr, which we already ship.”10
A reason for abandoning an approach“We tried pinning regex-syntax to 0.8.2 and backed it out, since Unicode word boundaries stopped matching.”6

Pre-compaction, the last side note appeared at 590K tokens. Claude Code compacted the conversation at 967K tokens. We also took a snapshot of the session at around 900K tokens and ran compaction on six more copies. Of the seven summaries, four retained all 26 facts. The remaining three retained 22, 20, and 19 facts, respectively.

We noted that while the summaries were inconsistent, Claude's notes were not.

When auto-memory is on, Claude keeps taking notes on its own.

Here, auto memory was enabled, so Claude had also been taking notes throughout the session without being asked. Claude Code stores these notes in its auto memory directory, ~/.claude/projects/<project>/memory/. The index, MEMORY.md, is loaded at the start of each session.

With auto memory enabled, Claude got all 26 answers right every time. We ran these tests in forked sessions, which load the memory index at startup.

What happens when we turn off auto memory?

We set CLAUDE_CODE_DISABLE_AUTO_MEMORY=1 on one of the copies before compaction. The resulting summary lost one corrected value and all six reasons for abandoning previous approaches.

We'll go through some examples to illustrate what happened.

The developer had explained why they abandoned the Cranelift backend:

We also tried building with the Cranelift backend and gave up, because the regex benchmarks were three times slower.

The summary remembered which approaches had been dropped, but not why:

Dropped approaches not to suggest: an on-disk walk cache, the Cranelift backend, mmap by default, pinning regex-syntax 0.8.2, JSON logs, a 30-second global test timeout.

When we asked Claude why we had abandoned Cranelift, it replied:

I don't know; I only have that the Cranelift backend was dropped, not why.

In another case, the developer had corrected a value. The second message in the session said:

The nightly benchmark job ends at 02:45 each night.

Thirteen messages later, the developer corrected it:

Correction to something I said earlier: the nightly benchmark job ends at 02:15, not 02:45.

Neither made it into the summary. When asked when the job ends, Claude replied:

I don't know. You never told me when the nightly benchmark job ends.

Interestingly, the developer had changed the default log level for examples from WARNING to INFO. The summary correctly retained the new rule, but Claude was still unsure:

INFO, though one memory note is titled "examples-warning-log-level", so please confirm whether it is INFO or WARNING.

This happened because Claude had updated the contents of the memory file when the rule changed, but kept the original filename.

This particular session had auto memory enabled until 902K tokens, so the notes were still on disk. Claude Code did read them after compaction. This explains how it correctly answered 21 questions even though its summary contained only 19 facts.

Here is how Claude had recorded the same three facts in MEMORY.md:

Note fileEntry in the memory index
nightly-benchmark-window.mdnightly benchmark job ends at 02:15
no-cranelift-backend.mdCranelift build tried and dropped (regex benchmarks 3x slower)
examples-warning-log-level.mddefault log level in examples is INFO (replaced WARNING)

Compaction is better at remembering rules than remembering reasons.

As we mentioned, three of the seven summaries left out some information. In total, while we lost 16 reasons and one corrected value, every single rule survived here.

Then after six compactions, we gave Claude 14 tasks to test whether it had forgotten any rules. Claude followed the relevant rule in every single case.

Another interesting thing about auto memory: Of the six summaries generated with auto memory enabled, five explicitly mentioned that the rules were already saved to memory.

The summary of the original session squeezed all 36 side notes into a single reference line under the “All user messages” section:

Side notes giving preferences and facts, each saved to memory as listed in section 1.

This experiment shows how compaction and auto memory work together to preserve information across context windows.

What is the actionable here?

Basically, think of the compaction summary as short-term memory. For everything that needs to survive the session, write it to a file.

  • Put persistent instructions in CLAUDE.md. Claude Code reloads the project-root file after every compaction. Instructions given in chat have no such guarantee, so it's better to write them in CLAUDE.md if you want them to be remembered throughout. Also note that if you edit CLAUDE.md mid-session, Claude only picks up the changes after /clear, /compact, or a restart.
  • Write down why a rule exists. In our tests, 16 of the 17 facts lost during compaction were reasons behind decisions, not the decisions themselves.
  • Keep auto memory enabled and check what Claude saves. Use /memory to inspect the memory folder. Some notes may be outdated. As Anthropic's docs point out, auto memory is local to your machine. A teammate who clones your repository gets CLAUDE.md, but not your memory notes.
  • Save every important tool output to disk. In our sessions, summaries retained only 36% of tool outputs (eg. a file path a script had printed). If you'll need a tool's output later, ask Claude to save it to a file.
  • Restate important instructions after compaction. If an instruction exists only in the conversation, repeat it. In our tests, summaries retained recent messages much better than older ones.
  • For any information that needs to reach teammates, it needs to be in a tracked file in the repo. Ask Claude to keep that info in a file and keep pushing that file into the repo.
  • Optionally, fork the session before compacting. Some devs do this to preserve the full conversation. Fork the session, compact the copy, and continue working there. You can do this with /branch or the --fork-session command-line flag.
  • Add a Compact Instructions section to CLAUDE.md. The AI Commit reports that a focus instruction restored all 36 values its agent had lost.

Trail-Code, a project we maintain, formalizes all these practices, and makes them autonomous and behind-the-scenes. It saves learnings, decisions, reasons, preferences, outputs and more in short Markdown files in .trail/learnings/ committed to the repo. Trail-Code doesn't intervene during compaction.

Why is my context still filled after /compact?

/compact frees up space by summarizing conversation history. But conversation history is only one part of the context window. If your context window is still filled up after compaction, or fills up again shortly after, there are three likely reasons:

  • High baseline context usage: System prompts, CLAUDE.md files, loaded skills, MCP tool definitions all consume context space independently of your conversation. Compaction does not reduce this overhead. If your baseline is large, you have little room left for actual work.
  • Large tool outputs: Reading large files, running commands that return hundreds of lines, or receiving lengthy results from subagents can quickly eat up the space freed by compaction.
  • Unsuccessful compaction: In rare cases, compaction fails or the resulting summary is still too large to leave enough room for further work.

Start by running /context to see what's taking up space in your context window. If the baseline usage is high, trim your CLAUDE.md files and disconnect MCP servers you don't need. If tool outputs are the problem, read files in smaller chunks or delegate exploration to subagents that return concise findings rather than raw outputs.

When does compaction run, and can I change it?

In our testing with a 1M-token context window, automatic compactions kicked in between 967,110 and 974,969 tokens. They took between 58 and 155 seconds.

Food for thought: Compaction is relatively cheap when the prompt cache is warm. When we run /compact at say 804K tokens, Claude Code reports a cost of $0.52. Rebuilding the same cache from scratch would itself cost $6.43.

You don't have to stick with the default compaction threshold. Claude Code lets you control when compaction runs, disable it altogether, and even tell it what to preserve.

Here's how, based on Anthropic's official documentation:

QuestionHow it worksDocumentation
When does automatic compaction run?Around 967K tokens on models with a native 1M-token context window. At 200K tokens on Sonnet 4.6 and Opus 4.6 without extended context.Model configuration
Can I change the threshold?Yes. Use /autocompact 500k, claude --autocompact, the autoCompactWindow setting, or the CLAUDE_CODE_AUTO_COMPACT_WINDOW environment variable. You can set the threshold anywhere between 100K and 1M tokens.Model configuration, Environment variables
Can I disable compaction?Set DISABLE_AUTO_COMPACT=1 to disable automatic and idle compaction while keeping manual /compact available. Set DISABLE_COMPACT=1 to disable compaction entirely.Environment variables
Can I control what goes into the summary?Pass instructions directly to /compact, add a Compact Instructions section to your CLAUDE.md, or run /rewind and select Summarize from here.Context window, How Claude Code works

How did we measure this?

Real compactions

We used SWE-chat, a dataset of real coding-agent sessions by Baumann et al.. We selected Claude Code sessions from June–September 2026 with at least 300 API calls, excluding imported sessions and those using other vendors' models. This yielded 383 compactions across 147 sessions and 47 repositories: 187 manual and 196 automatic.

For each compaction, claude-opus-5-5 compared the summary against every user message since the previous compaction and a sample of older messages. It judged whether each message was still relevant and whether the summary preserved it fully, partially, or not at all.

Judge validation

The judge correctly identified 3,408 of 3,430 verbatim matches as retained. A second judge, claude-sonnet-5-5, independently evaluated 50 compactions and found a similar pattern: 77% of recent rules and 38% of older rules survived, compared with 86% and 36% for Opus.

Replay

We also replayed seven sessions using the latest Claude Code version 2.1.327 with claude-opus-5-5, triggering 21 automatic compactions. These groups showed a similar drop in recall.

We crafted 50 questions from user-written rules, and the judges graded the answers and reviewed every incorrect answer. Three answers graded wrong are not counted: the user had since replaced the rule, and Claude gave the new one.

So while the original sessions used versions 2.1.76–2.1.259, the newer release showed no improvement: across the four 1M-token sessions, replayed summaries retained 16.0% of the older rules, compared with 16.4% in the originals.

Scripted sessions

We used claude --print with claude-opus-5-5. Each question ran in an isolated fork using --fork-session to avoid contaminating original sessions.

Limitations

  • No human evaluation. Models graded answers and performed validation.
  • Concentration. The largest repository contributed 61 of the 383 compactions, and two of the seven replayed sessions share a repository. This is probably not a big issue. Leaving out any one repository, older-rule retention after the second compaction stays between 19% and 22%.
  • No access to CLAUDE.md. In some cases, missing rules might have been recovered from disk in the original sessions. Summary loss may therefore overstate actual memory loss, and we have covered this caveat in this post.

The scripts and the data behind each chart are in this repository.

Share

See the mechanism
on your own documents.

Point Trail at the policy, SOP, agreement that decides your work, and watch it build an agent using rules traced back to the text they came from.