Skip to main content
Module 5: The Fable 5 Era 5 / 8
Advanced S29 Memory Context Personalization

Persistent Memory: How Claude Code Remembers

How Claude Code's file-based persistent memory works — one fact per file, a MEMORY.md index, four memory types, and hygiene rules that keep recall useful.

July 8, 2026 16 min read

What You’ll Learn

Every Claude Code session still begins with a fresh context window. You can carry standing rules across that boundary in CLAUDE.md, but before auto memory, everything learned during a session — the correction you made on Tuesday, the deploy gotcha you discovered last month — evaporated when the session ended. Auto memory changes that with a persistent, file-based memory directory per project: Claude writes notes for itself as it works, and auto memory is on by default in local sessions.

By the end, you’ll understand:

  • Where memories live and the one-fact-one-file format
  • The four memory types: user, feedback, project, reference
  • How MEMORY.md acts as an always-loaded index, and why it must stay short
  • Wiki-style [[links]] between memories
  • The hygiene rules that keep memory useful instead of noisy
  • How recall works (and why recalled memories must be re-verified)
  • How compaction, memory, and the scratchpad divide the work
  • How to turn auto memory off, move it, or audit it

The Problem

An agent without memory forces you to be its memory. Every session, you re-explain that the staging database is the one with the weird name, that you prefer squash merges, that the flaky test was already investigated and it’s the CI runner’s clock. That’s real cost, paid on every session start.

The naive fixes both fail:

  • Reload the chat log. Transcripts are enormous and mostly noise — tool outputs, dead ends, abandoned approaches. Loading them wholesale burns the context window on garbage.
  • Stuff everything into CLAUDE.md. CLAUDE.md is for standing rules — how to build, what conventions to follow. Mixing in ephemeral discoveries (“the auth refactor is half-done”) turns your rules file into a junk drawer that goes stale.

What’s needed is a third thing: curated, durable knowledge that accumulates across sessions, loads cheaply, and can be corrected when it’s wrong.

How It Works

One Memory, One File, One Fact

Each project gets a memory directory:

~/.claude/projects/<project>/memory/
├── MEMORY.md                          ← the index (always loaded)
├── user_prefers-squash-merges.md
├── feedback_use-pnpm-not-npm.md
├── project_auth-refactor-status.md
└── reference_staging-dashboard.md

The <project> path comes from the git repository, so every worktree and subdirectory of one repo shares a single memory directory; outside a git repo, the project root is used. Memory is machine-local: it isn’t shared across machines or cloud environments. The transcript cleanup sweep skips these files, so they stay until you or Claude edit or delete them.

The rule is strict: one memory = one file = one fact. Each file carries YAML frontmatter:

  • name — a short kebab-case slug
  • description — a one-line summary, used for relevance during recall
  • metadata.type — one of user, feedback, project, reference

When Claude writes a memory file with frontmatter, Claude Code also records the write time in a modified field (an ISO 8601 timestamp, 2.1.214+), so you and Claude can both see how fresh a fact is.

Why one fact per file? Because facts change independently. When the auth refactor finishes, you delete that file — not hunt through a monolithic notes document for every stale sentence it contaminated.

The Four Memory Types

TypeWhat it capturesExample
userWho the user is — role, expertise, preferences”Prefers squash merges; senior backend dev”
feedbackCorrections and confirmed approaches, with Why and How to apply”Use pnpm, not npm — npm corrupts the lockfile”
projectOngoing work, goals, constraints not derivable from code or git history”Auth refactor paused pending security review”
referencePointers to external resources — URLs, dashboards, tickets”Staging metrics live at the Grafana dashboard”

The feedback type is the workhorse. Its body should answer two questions: Why is this the right approach, and How to apply it next time. A correction without those is just a rule the agent will misapply in a slightly different situation.

Note what’s not a type: “code structure.” That’s deliberate — see the hygiene rules below.

MEMORY.md: The Always-Loaded Index

Loading every memory file each session would recreate the chat-log problem. So the system uses the same deferred-loading philosophy you saw with skills in Session 5: names upfront, content on demand.

MEMORY.md is the index — one line per memory:

- [Use pnpm, not npm](feedback_use-pnpm-not-npm.md) — npm installs corrupt the lockfile
- [Auth refactor status](project_auth-refactor-status.md) — paused pending security review

Claude Code loads the first 200 lines or 25KB of MEMORY.md, whichever comes first, at the start of every conversation. Anything beyond that isn’t loaded, so when a write leaves the index near or over the limit, Claude Code tells Claude to tighten it. Memory content stays in the individual topic files, which aren’t loaded at startup; Claude reads them on demand with its normal file tools. The agent scans one line per fact and pulls full files only when relevant.

Memories link to each other with [[wiki-style]] links that use the other memory’s name slug:

The lockfile issue first surfaced during the [[auth-refactor-status]] work.

Dangling links are fine — a link to a memory that doesn’t exist yet simply marks something worth writing later. It’s a cheap way to leave a breadcrumb without stopping to write the full fact.

Recall Is Background Context, Not Gospel

When memories are recalled mid-session, they appear inside <system-reminder> blocks — harness-injected notes, the same mechanism used for tool-availability changes and mode changes — and the interface shows a notice such as “Recalled 2 memories” (or “Saved 2 memories” when Claude writes). Two properties matter:

  1. They’re context, not user instructions. A recalled memory doesn’t command; it informs.
  2. They reflect what was true when written. The codebase has moved since. Recalled memories must be re-verified against the current code before acting on them. For a memory more than a day old, Claude Code adds its age and a reminder that memories are point-in-time observations, not live state.

That second point is the difference between memory helping and memory hurting. A six-week-old note saying “the config loader lives in src/utils/” is a lead to check, not a fact to build on.

The Three Knowledge Layers

Memory doesn’t replace CLAUDE.md or skills — it completes a stack. You write CLAUDE.md; Claude writes auto memory; both load at the start of every conversation as context, not enforced configuration:

CLAUDE.md   →  standing rules        ("always use pnpm; run tests before commit")
Memory      →  accumulated experience ("we tried X on 2026-06-12; it broke Y")
Skills      →  procedures             ("how to cut a release, step by step")

Rules, experience, procedures. If you find yourself writing a memory that reads like a rule, move it to CLAUDE.md. If it reads like a step-by-step process, it wants to be a skill.

Compaction, Memory, and the Scratchpad

Persistent memory pairs with how long conversations are handled. As Session 6 showed, Claude Code compacts automatically as the context window fills: it summarizes the conversation so a full window doesn’t end the session. Claude’s own instructions say that some or all of the context may be summarized and carried into the next context window, so it does not need to wrap up early or hand off mid-task. After compaction, Claude Code re-injects the project-root CLAUDE.md and auto memory from disk and re-reads up to five recently modified files: the conversation becomes a summary, but memory comes back whole.

Two supporting details round this out:

  • Scratchpad directory: when you’re signed in with a claude.ai account on the Anthropic API (and artifacts aren’t turned off), Claude Code gives each session a scratchpad directory for temporary files, used instead of /tmp — outside your project and usable without permission prompts. It lasts as long as the session’s transcript. Ephemeral working files go there, not into memory.
  • The division of labor is clean: compaction preserves this session’s thread; memory preserves what should outlive the session; the scratchpad holds what shouldn’t outlive it at all.

Managing Auto Memory

Auto memory is on by default in local sessions. Run /memory to see your CLAUDE.md files, toggle auto memory (saved as autoMemoryEnabled in ~/.claude/settings.json), and open the memory folder; every memory is plain markdown you can read, edit, or delete. To turn it off for one project, set "autoMemoryEnabled": false in that project’s settings; CLAUDE_CODE_DISABLE_AUTO_MEMORY=1 turns it off through the environment, and autoMemoryDirectory stores memory in another directory (an absolute path or one starting with ~/). The main conversation’s auto memory isn’t loaded into subagents, except forks; a subagent keeps its own memory only when its definition sets the memory field.

Hands-On Example

Here’s a realistic feedback memory, written the way the format intends — one fact, a recall-friendly description, and a body with Why and How to apply:

# File: ~/.claude/projects/my-shop-app/memory/feedback_use-pnpm-not-npm.md
---
name: use-pnpm-not-npm
description: Always use pnpm in this repo — npm installs corrupt pnpm-lock.yaml
metadata:
  type: feedback
---

Use pnpm, never npm, in this repo. Confirmed on 2026-07-08 after a broken deploy.

**Why:** The repo is a pnpm workspace. Running `npm install` writes a
package-lock.json and desyncs pnpm-lock.yaml, which broke the
CI install step (see [[ci-pipeline]]).

**How to apply:**
- All installs: `pnpm install`, never `npm install`
- Adding deps: `pnpm add <pkg>` in the relevant workspace package
- If package-lock.json ever appears, delete it and re-run `pnpm install`

And its index line in MEMORY.md:

- [Use pnpm, not npm](feedback_use-pnpm-not-npm.md) — npm installs corrupt pnpm-lock.yaml

Notice the details:

  • Fact first. The body leads with the rule itself, then the Why: and How to apply: lines.
  • Absolute date. “Confirmed on 2026-07-08,” not “confirmed yesterday.” Relative dates rot — “yesterday” is meaningless when the file is read three months later.
  • Dangling link. [[ci-pipeline]] may not exist yet. That’s fine — it marks a memory worth writing.
  • The description earns its keep. It’s the line used for relevance during recall, so it states the fact, not just the topic.

The Hygiene Rules

Memory quality is maintenance, not accumulation. Four rules:

  1. Update, don’t duplicate. If a fact changes, edit the existing file. Two files disagreeing about the same fact is worse than no file.
  2. Delete wrong memories. A memory proven false is negative-value. Remove it.
  3. Don’t save what the repo already records. Code structure, git history, anything in CLAUDE.md — the repo is the source of truth for those, and it’s always current. Memory that shadows it goes stale by definition.
  4. Convert relative dates to absolute. “Last week” becomes “2026-07-01.”

What Changed

Before Persistent MemoryWith Persistent Memory
Every session starts from zeroCurated facts persist per project
You re-explain preferences and gotchasuser and feedback memories carry them
Learned context dies with the sessionOne-fact-one-file survives it
Chat log is the only record (noisy, huge)MEMORY.md index: one line per fact
CLAUDE.md becomes a junk drawerRules, experience, procedures — separate layers
Compaction was the only thing carrying context forwardCompaction carries the thread; memory and CLAUDE.md reload from disk

Key Insight

Memory is curated knowledge, not a chat log.

The design keeps making the same choice at every level: distill, don’t accumulate. One fact per file forces each memory to be a deliberate, editable unit. The index-plus-files split means the always-loaded cost is one line per fact — the same names-upfront, content-on-demand philosophy that makes skills scale. The hygiene rules exist because memory’s failure mode isn’t forgetting — it’s confidently remembering things that are no longer true.

That’s also why recall arrives as background context to re-verify rather than instructions to obey. A good memory system doesn’t make the agent trust its past; it makes the past cheap to check.

Next Session

Session 30 covers Deferred Tools & ToolSearch — how the names-upfront, definitions-on-demand philosophy you’ve now seen twice (skills, memory) got generalized to the entire tool surface, and why that matters most for MCP servers with large tool catalogs.

Official Sources