Have you experienced this: after chatting with Claude Code for a long time, you ask it to recall something discussed earlier, and it says “I’m not sure”?

This isn’t AI getting dumber—it’s the “amnesia” phenomenon after compaction. Today we’re talking about what AI actually remembers after compaction.

The “Label System” When Moving

Imagine you’re moving to a new house, but the new place is only half the size of the old one. What do you do?

You can’t take everything—need to make choices:

  • Throw away: Old newspapers, expired coupons
  • Box up: Things you don’t use often, label them and put in storage
  • Keep handy: Things you use daily, within arm’s reach

Claude Code’s context compaction follows the same logic.

A 200K token context window is like a new house—it can’t fit the entire codebase (old house). So it needs to “move”—“box up and compress” old content, bringing only the most important things.

Lossy Compression: What Gets Discarded, What Gets Kept

Compaction is “lossy”—like compressing a high-resolution photo into a WeChat avatar, details get lost.

What Claude Code Discards:

  1. Complete File Contents

    • You had AI read a 5000-line log file
    • After compaction, those 5000 lines are gone
    • But AI remembers “I read this file”
  2. Detailed Tool Results

    • Grep search returned 1000 matches
    • After compaction, those 1000 specific matches are gone
    • But AI remembers “I searched and found some results”
  3. Intermediate Thinking Process

    • AI previously analyzed three approaches and chose the second
    • After compaction, the details of all three approaches are gone
    • But AI remembers “I analyzed it and chose approach B”
  4. Outdated Information

    • Earlier said “this bug exists in v1.0”
    • Now version is v2.0
    • v1.0 info can be discarded

What Claude Code Keeps:

  1. Existence Memory

    "I read /src/main.rs"
    "I executed git status"
    "I searched for 'user authentication'"
    
  2. Key Conclusions

    "That bug is on line 45 of utils.js"
    "Config file uses YAML format"
    "Project uses React, not Vue"
    
  3. Metadata Labels

    File: /src/config.js
    Read time: 2026-05-23 10:30
    File size: 2KB
    Key content: Database configuration
    Reference ID: file-read-12345
    

This is like boxes after moving: you might not remember every item in each box, but the labels tell you “these are winter clothes,” “these are kitchen supplies.”

Metadata: “Labels” After Compaction

Metadata is the “information summary” left after compaction.

A Typical Post-Compaction State:

File Operation Record:
- Path: /src/auth/login.js
- Operation: FileRead
- Time: 5 minutes ago
- Key functions: validatePassword, generateToken
- Reference ID: read-001
- Status: Compressed (complete content on disk)

Tool Execution Record:
- Tool: GrepTool
- Search: "TODO"
- Result count: 15 matches
- Key finding: 3 high-priority TODOs
- Reference ID: grep-002
- Status: Compressed (detailed results can be re-obtained)

AI sees this and knows:

  • “I read login-related code”
  • “I searched TODOs and found 3 important ones”
  • “If I need detailed content, I can retrieve it via reference ID”

Compaction isn’t deletion—it’s “archiving to storage.” When needed, it can be “retrieved.”

Ways to Reload:

Method 1: Automatic Reread

When AI needs a compacted file, it automatically uses FileReadTool to reread:

AI: I remember reading config.js before, let me check again
Discovers content is compacted
Automatically executes FileReadTool(path: "/src/config.js")
Re-obtains content
Continues task

Users barely perceive this process.

Method 2: Summary Is Enough

Sometimes complete content isn’t needed—summary suffices:

User: Did you find that TODO earlier?
AI: Yes, in utils.js line 45, it's about cache optimization. (No need to reread the complete file)

Method 3: User Reminder

If AI seems to have forgotten key information, you can remind it:

User: Didn't we agree to use Redis for caching?
AI: Oh right, I remember now. Let me confirm the configuration...
(AI reloads relevant context based on your reminder)

State Recovery Strategy

Claude Code intelligently judges whether state recovery is needed:

Automatic Recovery:

  • When AI references a compacted file
  • System automatically uses FileReadTool to reread
  • Newly read content re-enters cache

Lazy Loading:

  • Recover only when needed
  • Avoid unnecessary overhead
  • Similar to “lazy loading” pattern

Smart Judgment:

  • AI itself decides “is this information important?”
  • If important, proactively preserves or recovers
  • If not important, lets it be compacted away

It’s like after moving:

  • Daily-use items on the desk (not compacted)
  • Occasional-use items in cabinets (lightly compacted)
  • Rarely-used items boxed in storage (deeply compacted)
  • Trash thrown away (deleted)

Practical: Coping with AI “Amnesia”

Understanding state retention helps you use Claude Code better:

Scenario 1: AI Says “I’m Not Sure”

This usually happens because relevant information was compacted.

Response: Proactively remind of key information

User: Help me change the authentication logic
AI: I'm not sure which file you mean...
User: The one we discussed earlier, /src/auth/login.js
AI: Oh right, I remember now...

Scenario 2: Put Key Information in CLAUDE.md

Project-level key information goes in CLAUDE.md—it gets priority preservation:

# CLAUDE.md

## Project Key Information
- Tech stack: React + TypeScript + Node.js
- Database: PostgreSQL
- Authentication: JWT
- Important: All APIs require authentication

This information is in the system prompt and won’t be compacted.

Scenario 3: Process Long Conversations in Segments

If a conversation gets very long, you can proactively end the current session and start a new one:

"Let's pause here, start a new session to continue discussing the next module"

The new session has clean context, unaffected by previous compaction.

Scenario 4: Understand It’s Not a Bug

“Amnesia” in long conversations is a normal mechanism, not an AI problem.

It’s like chatting with a friend all day—they can’t remember every detail from the morning either.

Implications for Building AI Agents

If you want to build your own AI Agent, state retention is key:

Design Compaction Strategy:

  • What can be compacted? (Detailed content, intermediate process)
  • What must be preserved? (Key conclusions, user-explicitly-emphasized)
  • How to judge importance? (User marking, AI judgment, usage frequency)

Preserve Metadata:

  • Even when compacting content, preserve “existence”
  • Provide “reference links” to make content recoverable
  • Record time, type, and other key attributes

Support State Recovery:

  • Provide tools to reload content
  • Lazy load to avoid unnecessary overhead
  • Cache recovered content to avoid repeated loading

Summary

The state retention mechanism after compaction lets AI work gracefully within limited memory:

  • Lossy Compression: Preserve key, discard details
  • Metadata Labels: Remember “what was read,” forget “specific content”
  • Reference Recovery: Can reload when needed
  • Smart Judgment: AI itself decides what matters

This is like human memory: you remember “I read an article about Rust yesterday,” but not necessarily every sentence—if needed, you can go back and read again.

Understanding this lets you:

  • Understand AI’s “forgetting” behavior
  • Proactively help AI recall key information
  • Implement similar mechanisms in your own AI Agent