Back to Blog
Claude Code Series · Part 3/6 14 min read

4 Memory Systems: Extraction, Session, Living Docs, and Dreams

Your coding assistant literally has a "dream" system. 4 independent memory mechanisms that extract, organize, maintain, and consolidate knowledge — including one that reorganizes memories while you sleep.

G
Gonzalo Monzón
Abstract representation of memory systems and artificial intelligence

In the previous article we analyzed how Claude Code compresses context to maintain the illusion of an infinite conversation. But there's a deeper question: what happens when you close your session and come back tomorrow?

The answer will surprise you. Claude Code doesn't simply "remember" what you told it. It has 4 completely independent memory systems, each with its own trigger, format, and philosophy. One extracts knowledge in the background after every interaction. Another takes periodic notes during the session. A third maintains living documentation that updates itself. And the fourth — the most fascinating — consolidates and reorganizes memories every night, like the human brain during REM sleep.

It's not a metaphor. The system is literally called auto-dream.

🧠 Thesis of this article

Claude Code implements 4 bio-inspired memory systems: automatic background extraction (Auto-Memory), periodic session notes (Session Memory), self-maintained documentation (Magic Docs), and nightly consolidation (Auto-Dream). The analogy with human memory — encoding, working memory, externalization, and sleep — is no accident.

Overview

The 4 Memory Systems

Before diving into each one, here's the complete map. Each system operates independently, with its own trigger, data format, and lifecycle:

🔍

1. Auto-Memory

Background extraction

Trigger: end of each query loop
What it saves: preferences, corrections, project context
Format: YAML frontmatter + markdown

📝

2. Session Memory

Periodic session notes

Trigger: token threshold + tool call count
What it saves: current state, files, errors, worklog
Format: 11-section template

📚

3. Magic Docs

Self-maintained living documentation

Trigger: changes to related code
What it saves: overview, architecture, entry points
Format: files with # MAGIC DOC header

🌙

4. Auto-Dream

Nightly consolidation

Trigger: every 24h or 5 sessions
What it does: merge, deduplicate, organize by topic
Limit: MEMORY.md < 200 lines / 25KB

The biological inspiration is deliberate. Just as the human brain has separate systems for encoding (hippocampus), working memory (prefrontal cortex), externalization (written notes), and consolidation during sleep — Claude Code replicates these 4 pillars with background agents and nightly tasks.

System 1

Auto-Memory — Background Extraction

Auto-Memory is the most fundamental memory system. It activates at the end of each "query loop" — every time Claude Code finishes processing your request — via the handleStopHooks hook. Without you noticing, a parallel agent (fork) analyzes the conversation and extracts any data that should be remembered.

The key engineering trick: the forked agent shares the parent agent's prompt cache. This means it doesn't need to re-read the entire conversation — it already has the context cached. Extraction is cheap in tokens.

Each extracted memory is classified into one of 4 types:

# Format of an extracted memory (YAML frontmatter)
---
name: Prefers TypeScript over JavaScript
description: User always chooses TS and requests strict types
type: user          # user | feedback | project | reference
---

Additional context: uses strict mode,
prefers interfaces over types for public objects.

What IS saved

  • user — style preferences, language, favorite tools
  • feedback — user corrections ("no, not like that, do it this way")
  • project — project structure, stack, conventions
  • reference — URLs, external documentation, APIs

What is NOT saved

  • • Code patterns (re-discoverable by reading files)
  • • Git history (already persisted externally)
  • • Debugging solutions (ephemeral by nature)
  • • Anything already in CLAUDE.md
  • • Temporary task details

💡 Design philosophy: Memories capture knowledge about the user, not technical details that can be re-discovered. If Claude Code can read a file to get the information, it won't memorize it. If it needs to remember that you prefer tabs over spaces — that gets saved.

System 2

Session Memory — Periodic Notes

If Auto-Memory is long-term memory, Session Memory is working memory. Controlled by the feature gate tengu_session_memory, this system takes periodic "notes" about what's happening in the current session.

The trigger isn't time-based — it's based on token consumption and tool call count. When the session crosses a certain activity threshold, the system generates a snapshot of the current state using an 11-section template:

# Session Memory Template (11 sections)

## 1. Current State        — What's being done right now
## 2. Task Spec            — Description of the user's task
## 3. Files                — Relevant files touched/read
## 4. Workflow             — Steps completed and pending
## 5. Errors               — Errors encountered and their status
## 6. Codebase Documentation — Notes about the codebase
## 7. Learnings            — Discoveries from this session
## 8. Key Results          — Important results achieved
## 9. Worklog              — Chronological log of actions
## 10. [Reserved]          — Reserved for future expansion
## 11. [Reserved]          — Reserved for future expansion

Two constants govern the limits: MAX_SECTION_LENGTH = 2000 tokens per section and MAX_TOTAL = 12000 tokens for the entire note. This prevents session notes from becoming a complete conversation dump — they must be concise.

The best part? The template is customizable. You can create your own template at ~/.claude/session-memory/config/template.md to adapt the sections to your workflow. If you work in data science, you could add sections for datasets and metrics. If you work in frontend, sections for components and UI states.

System 3

Magic Docs — Living Documentation

The first two systems store memories about the user and the session. Magic Docs is different: it maintains documentation about the code itself, and it does so automatically.

The pattern is elegant in its simplicity. Any file that starts with the special header becomes a "magic document":

# MAGIC DOC: API Architecture

Overview of the REST API layer...

// This file auto-updates when related code changes.
// A background agent keeps it synchronized.

When you change code that's related to a Magic Doc, a background agent detects the change and updates the document. You don't need to ask for it — it just happens. The agent follows a strict philosophy:

"BE TERSE. High signal only. For OVERVIEWS, ARCHITECTURE, ENTRY POINTS."

No long-winded explanations or tutorials. Magic Docs are navigation maps — they tell you where things are and how they fit together, not how they work line by line. If you need to customize the behavior of the agent maintaining your docs, you can create a custom prompt at ~/.claude/magic-docs/prompt.md.

🎯 Key use case: Imagine a monorepo with 200 files. Instead of having a README.md that goes stale after 3 days, you put a # MAGIC DOC: Service Map at the root. Every time a service is added or modified, the doc updates itself. Documentation that never goes stale.

System 4

Auto-Dream — Nightly Consolidation

And now the most fascinating system. Auto-Dream is the reason this article's title says "dreams" without quotation marks. Claude Code literally has a consolidation process that runs periodically — every 24 hours or every 5 sessions — to reorganize and clean up accumulated memories.

The analogy with human sleep is precise. During REM sleep, the brain:

  • • Consolidates the day's memories into long-term storage
  • • Discards irrelevant information
  • • Reorganizes connections between existing memories
  • • "Compacts" the storage

Auto-Dream does exactly the same thing, in 4 phases:

Phase 1: Orient

Reads the complete memory directory and the MEMORY.md file (the main index). Understands the current structure: what topics exist, how many memories there are, how large the index has grown.

Phase 2: Gather

Scans daily logs and scattered memories generated since the last "dream." Collects all new memories that Auto-Memory and Session Memory deposited.

Phase 3: Consolidate

Merge, deduplication, and organization by topic. If two memories say the same thing in different words, they're fused. If a memory contradicts a more recent one, the old one is discarded. Memories are grouped into coherent themes.

Phase 4: Prune & Index

The final step ensures MEMORY.md doesn't exceed 200 lines / 25KB. If needed, old or low-priority memories are pruned and the index is updated.

In KAIROS mode (the advanced variant), Auto-Dream also uses daily logs as input and produces a "nightly distillation" — a high-level summary of what was learned that day, which gets integrated into long-term memory.

Structure

The Memory Directory

Everything is stored on the filesystem. No databases, no cloud services — markdown files on disk. The structure:

# Filesystem layout
~/.claude/memory/
├── MEMORY.md                  ← Main index (max 200 lines)
├── user-preferences.md        ← "user" type memories
├── project-goals.md           ← "project" type memories
└── logs/
    └── 2026/
        └── 07/
            └── 2026-07-04.md  ← Daily log

# Constants
ENTRYPOINT = 'MEMORY.md'
MAX_LINES  = 200
MAX_BYTES  = 25_600  // 25KB

The MEMORY.md file is the entry point. When Claude Code starts a new session, the first thing it reads is this file to orient itself — just like you'd read yesterday's notes before starting work.

There are two modes of operation: Individual (private memories per user) and TEAMMEM (shared + private memories). In TEAMMEM mode, there's a shared directory for team memories and a private one per developer, allowing project knowledge to be collective without losing personal preferences.

Comparison

Memory vs CLAUDE.md — Don't Confuse Them

A common question: what's the difference between the memory system and CLAUDE.md? They're complementary but very different:

Feature Memory CLAUDE.md
Writer Automatic (background agent) Manual (user edits it)
Content User preferences, project context Project rules, conventions
Persistence Across sessions (can be pruned) Permanent (version controlled)
Update Automatic extraction Manual user edits
Promotion /remember → CLAUDE.md

The /remember command is the bridge: it promotes an automatic memory to a permanent rule in CLAUDE.md. It's like when a provisional note becomes a team policy — it stops being an observation and becomes a permanent instruction.

Application

Patterns You Can Apply Today

Claude Code's 4 memory systems aren't just curious internals. They're design patterns you can apply to any agent or chatbot:

1. Background memory extraction

Don't wait for the user to say "remember this." After every interaction, use a lightweight agent to extract implicit memories — preferences, corrections, context. The key: share the prompt cache to make it cheap.

2. Append-only logs + periodic consolidation

Never edit existing logs. Always append. Then, a periodic process (daily, weekly) reads all logs, consolidates them, and produces a clean summary. This pattern scales infinitely and is crash-safe.

3. Typed memories (user/feedback/project/reference)

Don't put all memories in one bucket. Classify them by type. This allows the consolidation process to treat each type differently — user preferences get merged, references get deduplicated, feedback accumulates.

4. Auto-documentation with header patterns

The Magic Docs pattern is simple and powerful: define a canonical header (# MAGIC DOC:) and make any file with that header self-maintained. It's living documentation with zero friction — placing the header is all you need.

Takeaway

Your Assistant Has Dreams — And That's a Good Thing

The boundary between a chatbot that "remembers" and an agent with real memory is exactly this: 4 independent systems, each optimized for a different type of retention. Auto-Memory extracts what you learn. Session Memory maintains current state. Magic Docs keeps the maps updated. And Auto-Dream consolidates everything while you sleep.

The most revealing aspect is what the system doesn't save. The explicit exclusions — no code patterns, no debugging solutions, nothing already in CLAUDE.md — reveal a precise philosophy: memory is for what can't be re-discovered. Everything else is read in real time.

In the next article in this series, we'll explore Claude Code's hidden features — commands, flags, and capabilities that don't appear in the official documentation but transform the user experience.

📚 Claude Code Anatomy Series: This is article 3 of 6. The previous one covers the 4-level context compression system. The following articles cover hidden features, the tool & permission system, and the dual build (internal vs public).

Building agents with LLMs?

At Cadences we design agents using the same industrial patterns we discovered in Claude Code. If you want to apply these principles to your business, let's talk.

G

Gonzalo Monzón

Founder of CadencesLab. Software engineer, multi-agent systems architect, and perpetual student of how machines think. This series is born from months of reverse engineering the tools we use daily.

Newsletter

Don't miss any story

Subscribe to receive new releases, exclusive chapters, and behind-the-scenes content.

  • Weekly insights & articles
  • Exclusive content & early access
  • No spam, unsubscribe anytime

We respect your privacy. Unsubscribe anytime.