DEV Community

Ali Baizhanov
Ali Baizhanov

Posted on • Originally published at mengram.io

Claude Code Forgets Everything After Compaction. Here's the Fix That Survives It

The problem: compaction is amnesia by design

When a Claude Code session approaches its context limit, auto-compact summarizes the conversation and throws away the original. It has to — context windows are finite. But what survives is a summary written under token pressure, and what dies is exactly the stuff you needed: the decision you made an hour ago, the constraint you stated once, the approach you already rejected twice.

This isn't a niche complaint. On Anthropic's own issue tracker: enhanced /compact with restorable summaries (114 upvotes), no way to review context after compaction (79), auto-compact erases chat history without warning (35), and — the quiet killer — Claude forgets CLAUDE.md guidance after compaction (28). Hundreds of developers voting on the same wound.

Why CLAUDE.md doesn't save you

The standard advice is "put important context in CLAUDE.md." It helps — until it doesn't. CLAUDE.md is static: it holds what you remembered to write down last week, not the decision from forty minutes ago that compaction just ate. And per the issue above, even CLAUDE.md guidance itself loses force after heavy compaction as the summary crowds it out.

What actually survives: memory outside the context window

The durable fix is structural: keep the important state outside the thing that gets compacted, and re-inject it on every fresh start. Claude Code has the exact machinery for this — the SessionStart hook fires not just on startup, but also on /clear, resume, and after compaction.

That's how the Mengram plugin makes context survive compaction:

  • During the session, a Stop hook captures each turn in the background — facts, decisions, and workflows get extracted into persistent memory (API keys and tokens are redacted client-side).
  • After compaction (or /clear, or a new session, or a different machine), the SessionStart hook reloads your cognitive profile — who you are, what you're building, what you decided — as fresh context. The summary can be lossy; the memory isn't.
  • On every prompt, relevant past context is recalled and injected, so "how did we deploy this again?" gets answered from memory instead of re-derived.

Setup (60 seconds)

# 1. Free API key: https://mengram.io — save it once
mkdir -p ~/.mengram && echo '{"api_key": "om-your-key"}' > ~/.mengram/config.json

# 2. Install the plugin
claude plugin marketplace add alibaizhanov/mengram
claude plugin install mengram@mengram

# 3. Optional: feed in your existing session history (secrets redacted locally)
pip install mengram-ai && mengram import claude-code
Enter fullscreen mode Exit fullscreen mode

Test it: tell Claude something about your project, run /compact (or /clear), and ask again. The context comes back — not from the summary, but from memory.

What this doesn't fix

Honesty section: no external memory restores the full pre-compact transcript — that's gone, and tools claiming otherwise are re-summarizing too. What persistent memory changes is which things survive: instead of whatever the compactor kept under pressure, you keep structured facts, decisions, and workflows extracted while they were fresh. For the transcript itself, vote on #17428 (https://github.com/anthropics/claude-code/issues/17428) — file-backed summaries would compose beautifully with external memory.


I'm the founder of Mengram (https://mengram.io) — a memory layer for AI agents (semantic facts + episodic events + procedural workflows that evolve from failures). Free tier, self-hostable, Apache 2.0. Questions welcome in the comments.

Top comments (1)

Collapse
 
alexshev profile image
Alex Shev

Compaction makes the invisible contract visible. If decisions, constraints, and next steps are not written somewhere durable, the next agent turn is forced to reconstruct intent from a compressed story.