Engineering 22 min read

When Your AI Agent Compacts, Keep the Full Record and Tell It to Search

MMNMNOTE
context compactionclaude codecoding agentssession transcriptagent memorygrepcompaction summary

When an AI coding agent compacts its context, the summary keeps far less than it seems. In one small study, the post-compaction context held 6% of exact answers — paths, numbers, error lines, commands. Keep the full session transcript as a file, and add one standing line telling the agent to search it before answering about earlier work.

The study is a preprint by Adarsh Srivastava of CtrlB Explore Inc., posted to arXiv on 29 November 2026.1 It used real compaction points from four engineers' coding sessions, and its author's company builds one of the recall tools it tested. Both facts limit how far its numbers travel — read every figure below as one study's direction, not a rate.

Keeping the record is the easy half. Most agent tools already write one — Claude Code saves every session as a plaintext transcript on disk.2 The hard half is that an agent holding a summary rarely thinks to look. "Retaining a transcript is necessary but not sufficient," the paper concludes.1

So the fix has two parts. Keep the file. Then tell the agent it exists.

What does an AI agent lose when it compacts?

Mostly the exact strings. At real compaction points, the summary text alone held 1% of exact answers — 2 of 174. Adding the recent messages kept verbatim and the re-attached files raised that to 6%. Gist fared better: the context still stated the answer to 40% of questions about decisions, results and state.

The paper splits its questions in two. Exact questions ask for a path, a number, an error line or a command. Gist questions ask about decisions, results, state and reasons. The headline sentence is about the whole context the agent sees after compaction: it "held 6% of exact answers and 40% of gist answers."1

The summary itself carried less. "The summary text contained 2 of the 174 exact golds (1%)," the results section reports; the rest of the 6% came from messages kept word for word and files read back in.1 The two numbers measure different things, and the gap between them is the point — almost nothing exact survives the summarizing step itself.

Not everything dies. "The retained context preserved current state (60% survival) more often than commands and error lines (0%), numbers (4%)" and the other details that led to it.1

A summary records where the work stands. It drops how the work got there.

That loss has a measurable cost. Agents given only the post-compaction context "scored 22–35% on Detail recall," and "Larger models still failed to recover most missing details (Sol 35%, Luna 29%)."1 Sol and Luna are the paper's short names for its two main Codex models, gpt-6.1-sol and gpt-6-luna; this post keeps the paper's labels.

How compaction decides what to keep

It doesn't decide with your future question in mind. Compaction asks a model to summarize the conversation so far, then replaces the older turns with that summary and keeps a few recent messages verbatim. The summary is written before anyone knows what will be asked later, so exact strings fare worst.

Earendil's engineering post on the Pi agent puts the mechanism plainly: "In practice, though, implementations of compaction use an LLM request to summarize the conversation history."3 Pi keeps a tail of recent messages untouched — "Pi's current default of 20 thousand tokens comes out to roughly 5 to 20 turns" — and stores the summary "as plain text in the session."3 That post drew 211 points and 91 comments on Hacker News.4

Claude Code describes its own /compact command in one line: "Free up context by summarizing the conversation so far. Optionally pass focus instructions for the summary."5 Its documentation lists what comes back afterward. The system prompt and output style "Both still apply." Project-root CLAUDE.md and unscoped rules are "Re-injected from disk." Up to five of the files Claude read or edited are re-read, most recently modified first.6

Everything else is the summary.

The conversation itself — the error you pasted at turn 40, the flag you tried and abandoned, the reason the column type changed — exists only as the model chose to describe it.

Pi's post names the ideal: "The ideal outcome of a good summarization for a coding agent is like a handoff briefing from one shift to the next."3 A good briefing still leaves out whatever the next shift did not know it would need.

Why not just use a bigger context window?

Because a long window is not an index. In the study, Haiku given everything since the previous compaction — a median of 186k tokens — scored 56% on 50 questions, against 90% with search. In 10 of 29 exact questions the answer predated the window entirely; inside it, search still found more.

The paper's own wording: "On 50 questions, Haiku with a median of 186k tokens of context scored 56%, against 90% with search. In 10 of the 29 exact questions the answer predated the window; where it was in the window, full context found 12 of 19 and search 17 of 19."1

Two separate failures sit in that sentence. A window, however long, starts somewhere, and a session that has compacted once has already lost what came before. And a fact inside the window is not the same as a fact the model will find. The paper's phrase for it is "a long context is not a reliable index."1

That second failure is not new — Nelson Liu and co-authors showed in 2023 that model "performance can degrade significantly when changing the position of relevant information," and that it "significantly degrades when models must access relevant information in the middle of long contexts, even for explicitly long-context models."7

More room delays compaction. It does not make the past searchable.

Why the agent does not search, even when it could

Because the summary looks complete. In the study, Claude Code's summaries named the transcript every time, yet Haiku and Sonnet answered 60–62% of gist questions without opening it. Without a standing rule, summary-equipped agents made no search on 48–89% of gist questions. The agent cannot see what the summary dropped.

The paper gives two reasons in a section titled "Why agents holding a summary skip the record."1 The first: "The agent does not know what the summary dropped." On exact questions the gap is visible — the summary plainly lacks the string — and Haiku and Sonnet searched the transcript for over 90% of them, scoring 91–95%.1

Gist is different. "On gist questions the summary offers a plausible answer," and the agents took it, scoring 51% and 58% on gist overall.1

An exact question announces its own gap. A gist question hides it.

The second reason is simpler. "On Codex, nothing points to the record. None of the 32 compaction entries in our Codex sessions names the session file," so a stock Codex agent has only the summary.1

The claim that Claude Code's summary names the transcript is the paper's observation — it was not reproduced for this post.

Step one: keep the full record, and know how long it lasts

Most agent tools already write the full session to disk; the job is to make sure it is still there when you need it. Claude Code saves each session as a plaintext JSONL transcript and deletes files older than cleanupPeriodDays — 30 days by default. Find your tool's path, then decide how long to keep it.

Where each tool writes its transcript — Claude Code, Codex CLI, Cursor, Gemini CLI and others — is mapped in Your AI Tool Already Saved Your Session to Disk. That post covers finding and grepping the files yourself. This one is about getting the agent to do it.

For Claude Code, the transcript lives at projects/<project>/<session>.jsonl under ~/.claude, and holds the conversation's messages, tool calls and tool results.2 On retention, the documentation is exact: "The default is 30 days and the minimum is 1."2 To keep transcripts longer, set the value in ~/.claude/settings.json:

{
  "cleanupPeriodDays": 90
}

Before raising it, read the privacy line on the same page. "These files are plaintext. Anything that passes through a tool is written to a transcript on disk: file contents, command output, pasted text."2 The documentation adds that transcripts "are not encrypted at rest," and suggests lowering cleanupPeriodDays to reduce exposure.2

A longer record keeps your secrets longer too.

Raise the number only on a machine you trust — or leave the default and copy out the few sessions you will want back, after checking them for keys and tokens.

Add one short rule to the agent's standing instructions: the summary leaves details out, here is where the full record lives, search it before answering about earlier work. In the study, it cut runs with no search to 6% or less in five of six setups, and raised Detail recall by 5 to 47 points.

This is the rule as the paper prints it. The bracket is the author's placeholder for where the record lives:1

Your earlier turns in this session are not all in your context: the compaction summary leaves details out. [Where the record is.] Before answering anything about earlier work in this session, search it. Do not say you cannot find something until you have searched.

The file version, from the paper's appendix, fills the bracket with a path:1

Your earlier turns in this session are not all in your context: the compaction summary leaves details out. The full record of this session up to the compaction is the file {path}. Before answering anything about earlier work in this session, search that file. Do not say you cannot find something until you have searched.

The last sentence does real work. It closes an easy exit — a confident "I can't find that" drawn from the summary alone.

In the study, the rule was appended to the system prompt: through --append-system-prompt in Claude Code, and as developer instructions in Codex.1 A one-off session looks like this:

claude --append-system-prompt "Your earlier turns in this session are not all in your context: the compaction summary leaves details out. The compaction summary names the transcript file of this session. Before answering anything about earlier work in this session, search that file. Do not say you cannot find something until you have searched."

That second sentence is our adaptation — not the paper's text. It leans on the paper's observation that Claude Code's summary names the transcript, so the rule does not need a session path that changes every time.

For a standing home, project-root CLAUDE.md is one option — Claude Code's documentation says it is "Re-injected from disk" after compaction, so a rule placed there should survive every compaction.6 This placement is our inference from the documentation. The paper did not test it.

What the rule changed, setup by setup

The rule mattered most where nothing pointed the agent to its record. On Codex with a recall tool, one model went from 34% to 81% correct. On Claude Code with the plain transcript file, it added 8 points for Haiku and 5 for Sonnet, where skipping was rarer to begin with.

The paper's Table 6 isolates the rule on its Detail recall questions, exact and gist together.1 The Claude Code rows compare the default harness without the rule against grep over the same transcript, in the same shell, with the rule. The Codex rows run the same setup with and without it.

ModelHarnessRecordRuns with no searchCorrectGain (95% CI)
HaikuClaude Codetranscript file32% → 6%73% → 81%+8 (0, +16)
SonnetClaude Codetranscript file33% → 18%78% → 83%+5 (−2, +13)
LunaCodextranscript file60% → 1%55% → 77%+22 (+12, +32)
LunaCodexrecall tool90% → 0%34% → 81%+47 (+37, +57)
SolCodextranscript file34% → 0%89% → 94%+5 (0, +10)
SolCodexrecall tool56% → 0%72% → 94%+22 (+14, +31)

Read the Claude Code rows honestly — Sonnet's interval crosses zero, and Haiku's touches it. "Sonnet with the rule still skipped search on 24% of gist questions."1

The big gains sit where skipping was common: a Codex agent whose summary names no file, or an agent handed a tool it rarely called. On Claude Code with the transcript file, the rule is cheap insurance — not a multiplier.

It costs one paragraph of instructions.

Plain files and grep, or a purpose-built store?

Plain files are the honest default. With the rule, transcript files searched by grep scored 77–94% across four models, against 81–94% for the store the author's company builds. Grep was lower on gist questions in three of the four models, and at 100 times the record, the grep calls that completed took minutes.

The paper's abstract puts both arms side by side: "With the full record in CtrlB Recall and a one-line instruction to search it, four models in two families scored 81–94%. Plain transcript files searched with grep scored 77–94%, lower on gist questions in three of the four models."1

Read the conflict before reading the gap. The paper states it in its own section: "Competing interest. The author works at CtrlB Explore Inc., which builds CtrlB Recall. Behind the same recall tool, a plain-file grep backend returned identical rows on the replayed calls."1

That control matters. Retrieval matched row for row, "so the recall gains come from keeping the record and from the tool, not from a different match rule."1 The tool still earned something — on Claude, "gist accuracy was 64–69% when the agent grepped the raw JSONL transcript, against 78–82% with the tool’s rows."1 The paper's explanation is format — raw transcript lines are long JSON with escaped text, while the tool returns individual blocks.

Speed is the other gap. In a synthetic scale test at 100 times the record, "the grep calls that completed took minutes."8

For one person's sessions on one machine, plain files are enough to start — the record is already there, it is already greppable, and nothing has to be installed. This post names no store as the fix.

What the study does not show

It does not show that compaction breaks agents, and its numbers are not rates. It is one preprint, posted 29 November 2026, from four engineers at one company over six weeks, graded by model judges with no human audit. It measured recall of earlier work, not whether long tasks got finished.

The limitations section opens bluntly: "Four engineers, one company, six weeks. All compaction points are in Claude Code sessions, so Codex models answer questions about Claude Code sessions."1 The corpus "holds proprietary code and is not released," so nobody can rerun it.1

There is more. Answers were graded by model judges with "no human audit." Each question ran once. And the question wording "follows the record, which may help search arms" — the arms this post recommends.1 No independent replication existed as of 2 December 2026.

Compaction itself has a better record on a different measure. Peter Belcak and eleven co-authors found that "frontier language models sacrifice little to no task accuracy when maintaining short contexts."9 Srivastava's paper cites that result in its first sentence, then asks a narrower question: what the short context loses about the agent's own past.

The two do not conflict — compaction can keep an agent on task and still leave it unable to say which flag it tried yesterday. Srivastava's own scope line is "Recall and plans, not completed long tasks."1

Treat the numbers as one study's direction. The rule is cheap enough to try on that basis.

A handoff note is for you; the record is for the agent

Keep both. A hand-written handoff note carries the state of play between sessions, for you or for a fresh agent. The full transcript holds what any summary left out, for the agent to search inside a session. One is a briefing. The other is the evidence the briefing was written from.

The case for the note is made in Write the AI a State-of-Play Before You Close the Tab. That post is explicit about its limits: a handoff note "is not a substitute for the tool's own transcript." Nothing here changes its advice. It adds the step after: make the agent read the transcript too.

The same principle applies to notes generally — let a model summarize, but keep the original. The Summary Is a View, the Note Is the Source covers it for documents. For a record a person can audit, rather than one the agent searches, see The Only Record of What Your Agent Did Is the One You Keep.

A summary — automatic or hand-written — is a claim about the record. It is never the record.

Frequently Asked Questions

These are the questions people type after an agent loses the thread mid-session. Each answer separates what the study found, what the tool's documentation says, and where this post's advice goes beyond either — the line between the three matters here.

What does Claude Code lose when it compacts?

Mostly exact details. In one study of real sessions, the post-compaction context held 6% of exact answers — paths, numbers, error lines, commands — and 40% of gist answers. The summary text alone held 1%. Current state survived more often than the commands and errors that led to it. Project-root CLAUDE.md is re-injected from disk.

Why did my agent forget what we did before compaction?

Because the summary was written before anyone knew what you would ask, and the agent cannot see what it dropped. On gist questions the summary offers a plausible answer, so agents often answer from it without searching. In the study, summary-equipped agents made no search on 48–89% of gist questions without a standing rule.

How do I make Claude Code remember after /compact?

Keep the transcript and tell Claude Code to search it. The transcript is a plaintext JSONL file under ~/.claude/projects/. Add a standing rule saying the summary leaves details out and earlier work must be searched before answering. The study appended the rule to the system prompt; putting it in project-root CLAUDE.md is an untested inference.

Will a bigger context window fix it?

Not reliably. In the study, Haiku given a median of 186k tokens of history scored 56% on 50 questions, against 90% with search. In 10 of 29 exact questions the answer predated the window. Where it was inside, search still found more — the paper calls a long context "not a reliable index."

Does my agent's transcript stick around?

Not forever — Claude Code deletes transcripts older than cleanupPeriodDays; the documented default is 30 days and the minimum is 1. You can raise it in ~/.claude/settings.json. The files are plaintext and not encrypted at rest, and they include file contents and command output, so a longer setting keeps secrets on disk longer.

Can I stop Claude Code from compacting mid-task?

You can change when it happens — Claude Code's /autocompact command sets how full the context window gets before it compacts automatically. A long session will still compact eventually, and a larger window only delays it. Keeping the record and the search rule matters either way.

Is a compaction summary the same as a handoff note?

No, though both are summaries. A compaction summary is written by the model to free up context inside a session. A handoff note is one you write between sessions. Neither replaces the full transcript; a handoff note is for orientation, and the transcript is what the agent should search for details.


The rule can still fail — one model skipped search on a quarter of gist questions even with it in place. A summary is a note about the work. The transcript is the work.


MNMNOTE keeps notes as open Markdown, stored locally in your browser — a record you can read, search and keep. mnmnote.com

Footnotes

  1. Adarsh Srivastava (CtrlB Explore Inc.), "You Don't Need Long Context, You Need the Record," arXiv:2611.39334v1 [cs.AI], 29 November 2026, HTML version. https://arxiv.org/html/2611.39334v1, accessed 2026-12-02. ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10 ↩11 ↩12 ↩13 ↩14 ↩15 ↩16 ↩17 ↩18 ↩19 ↩20 ↩21 ↩22 ↩23 ↩24 ↩25

  2. Anthropic, Claude Code documentation, the .claude directory reference (application data, retention and plaintext storage). https://code.claude.com/docs/en/claude-directory, accessed 2026-12-02. ↩ ↩2 ↩3 ↩4 ↩5

  3. Earendil Engineering, "How Compaction Works in Pi," 13 August 2026. https://earendil.com/posts/compaction-in-pi/, accessed 2026-12-02. ↩ ↩2 ↩3

  4. Hacker News, "How Compaction Works in Pi," item 49289654 (211 points, 91 comments as of 2026-12-02). https://news.ycombinator.com/item?id=49289654, accessed 2026-12-02. ↩

  5. Anthropic, Claude Code documentation, "Commands" (/compact). https://code.claude.com/docs/en/commands, accessed 2026-12-02. ↩

  6. Anthropic, Claude Code documentation, context window page, section "What survives compaction." https://code.claude.com/docs/en/context-window, accessed 2026-12-02. ↩ ↩2

  7. Nelson F. Liu et al., "Lost in the Middle: How Language Models Use Long Contexts," Transactions of the Association for Computational Linguistics, vol. 12, 2024; arXiv:2307.03172 (first posted 2023). https://arxiv.org/abs/2307.03172, accessed 2026-12-02. ↩

  8. Adarsh Srivastava, "You Don't Need Long Context, You Need the Record," arXiv abstract page. https://arxiv.org/abs/2611.39334, accessed 2026-12-02. ↩

  9. Peter Belcak et al., "You don't need long context," arXiv:2611.05306, 3 November 2026. https://arxiv.org/abs/2611.05306, accessed 2026-12-02. ↩