Claude Code Memory: How CLAUDE.md and Auto Memory Work
Claude Code memory is two things: CLAUDE.md files that you write, and auto memory that Claude writes for itself. Every session starts with a fresh context window. CLAUDE.md files carry your instructions into it: build commands, conventions, "always do X" rules. Auto memory carries what Claude has learned from working with you, such as corrections you gave it and preferences you confirmed. Both are plain Markdown files on disk that you can open, edit and delete.
Everything below is checked against Anthropic's Claude Code memory documentation. Claude Code changes quickly, so treat that page as the final word if anything here has moved.
What memory is in Claude Code
The docs split memory into two kinds:
| CLAUDE.md files | Auto memory | |
|---|---|---|
| Who writes it | You | Claude |
| What it holds | Instructions and rules | Learnings and patterns |
| Scope | Project, user or organization | One repository, shared across its worktrees |
| When it loads | Every session | Sessions with auto memory on: the start of MEMORY.md |
| What to put there | Coding standards, workflows, layout | Your preferences, corrections, context Claude needs |
One point matters more than any other: memory is context, not configuration. The docs say CLAUDE.md content is delivered as a user message after the system prompt, and that "there's no guarantee of strict compliance, especially for vague or conflicting instructions." If something must happen every time, such as a check before every commit, the docs point you to a hook, which runs as a shell command whatever Claude decides. To block an action outright, use a PreToolUse hook or a permissions.deny rule.
Claude Code can also read an AGENTS.md file as your project instructions. By default it does so only when there is no CLAUDE.md, .claude/CLAUDE.md or CLAUDE.local.md in your working directory or above it.
Every memory location and its scope
| Scope | Path | Loads | Shared with |
|---|---|---|---|
| Managed policy | macOS: /Library/Application Support/ClaudeCode/CLAUDE.mdLinux and WSL: /etc/claude-code/CLAUDE.mdWindows: C:\Program Files\ClaudeCode\CLAUDE.md | At launch, first | Every user on the machine |
| User | ~/.claude/CLAUDE.md | At launch | Only you, in every project |
| User rules | ~/.claude/rules/*.md | At launch, before project rules, or on demand with paths | Only you, in every project |
| Project | ./CLAUDE.md or ./.claude/CLAUDE.md | At launch | Your team, through git |
| Project rules | ./.claude/rules/*.md | At launch, or on demand with paths | Your team, through git |
| Local | ./CLAUDE.local.md (add it to .gitignore) | At launch, after CLAUDE.md | Only you, in this project |
| Subdirectory | CLAUDE.md and CLAUDE.local.md below the working directory | On demand | Whoever the file is shared with |
| Auto memory | ~/.claude/projects/<project>/memory/MEMORY.md plus topic files | Start of MEMORY.md at launch | Only you, on this machine |
An organization can also put managed CLAUDE.md content straight into managed-settings.json under the claudeMd key. The docs say it loads with the same precedence as the managed file, before user and project CLAUDE.md, and that setting claudeMd in user, project or local settings has no effect.
Load order and precedence
The table is in the docs' load order, from broadest scope to most specific. Three rules decide what Claude sees:
- Files are concatenated, not overridden. All discovered CLAUDE.md files go into context together. A project instruction does not replace a user instruction; it appears after it.
- Closer files are read later. Claude Code loads
CLAUDE.mdandCLAUDE.local.mdfrom your working directory and every directory above it, ordered from the filesystem root down. Within one directory,CLAUDE.local.mdcomes afterCLAUDE.md, so your personal notes are the last thing Claude reads at that level. - Conflicts are not resolved for you. If two instructions contradict each other, the docs warn that "Claude may pick one arbitrarily." The same goes for a user rule against a project rule. Keep them consistent rather than relying on order.
Two exceptions to "everything loads": you can skip specific CLAUDE.md files with the claudeMdExcludes setting, but not the managed one; and rules with a paths field load only when Claude works with a matching file.
Where auto memory lives
Each project gets its own directory at ~/.claude/projects/<project>/memory/. The <project> part is derived from the git repository, so every worktree and subdirectory of one repo shares one memory directory. Auto memory is machine-local: it is not shared across machines or cloud environments.
The directory holds a MEMORY.md index, one line per memory, and one topic file per memory. Only the first 200 lines or 25KB of MEMORY.md, whichever comes first, load at session start. Topic files are not loaded at startup; Claude reads them on demand when it needs them. Claude labels each memory with a type of user, feedback, project or reference, and the docs say it skips anything it can derive from the code or that your CLAUDE.md already says.
Auto memory is on by default in local sessions. Turn it off with the toggle in /memory, with "autoMemoryEnabled": false in a project's settings, or with CLAUDE_CODE_DISABLE_AUTO_MEMORY=1. To move it, set autoMemoryDirectory to an absolute path or one starting with ~/.
Importing files with @
A CLAUDE.md can pull in other files with @path/to/file. The imported file is expanded into context alongside the CLAUDE.md that references it.
See @README for the project overview and @package.json for the npm scripts.
# Workflow
- git workflow @docs/git-instructions.md
- personal notes @~/.claude/my-project-instructions.md
The documented rules:
- Paths. Relative and absolute paths both work. A relative path resolves from the file that contains the import, not from your working directory.
- Depth. Imported files can import other files, up to a maximum depth of four hops.
- Spaces. Put a backslash before each space, as in
@Design\ Docs/api-conventions.md. Without it the path ends at the first space. A path in quotes isn't imported at all. - Code is skipped. Imports inside backtick code spans and fenced code blocks are ignored, so wrapping a path in backticks mentions it without importing it.
- External files need approval. In a project-level file, an import that resolves outside your working directory triggers a one-time approval dialog listing the files. If you decline, those imports stay off. Imports in your own
~/.claude/CLAUDE.mdload without the dialog. - Imports don't save context. The docs are explicit that imports help you organize a long file "but don't reduce its context cost, because imported files load together with the CLAUDE.md that imports them."
The home-directory import above is the documented fix for a common worktree problem: a gitignored CLAUDE.local.md exists only in the worktree where you created it, while a file under ~/ is shared by all of them.
Editing memory: /memory and asking Claude
/memory lists your CLAUDE.md, CLAUDE.local.md and other memory files across user and project scopes, including entries for user and project files that don't exist yet. Pick one to open it in your editor; picking a missing one creates it. The same screen has the auto memory toggle and an option to open the auto memory folder, which is the quickest way to see what Claude has saved.
Asking Claude. The docs separate the two kinds of memory by how you phrase it. "Remember that the API tests need a local Redis instance" saves to auto memory. "Add this to CLAUDE.md" writes to CLAUDE.md. You can also edit any of these files in your normal editor; they are plain Markdown.
What about the # shortcut? Older guides describe starting a message with # to save a memory. The current memory docs don't describe that shortcut, so don't rely on it: use /memory or ask Claude in plain words.
Starting from scratch. /init analyzes the codebase and writes a starting CLAUDE.md with the build commands, test instructions and conventions it finds. If a CLAUDE.md already exists, it suggests improvements instead of overwriting it.
Checking what loaded. Run /context all and look under Memory files to confirm which CLAUDE.md files loaded at launch. Subdirectory files don't appear there because they load on demand; a Loaded line with the path shows in the terminal when one does.
Writing a good CLAUDE.md
The docs' own test for when to add something is a good place to start: add to CLAUDE.md when Claude makes the same mistake twice, when code review catches something Claude should have known, when you type the same correction you typed last session, or when a new teammate would need the same context. Then keep the file worth reading.
Be specific
Write instructions that can be checked. The docs give three pairs:
- "Use 2-space indentation" instead of "Format code properly"
- "Run
npm testbefore committing" instead of "Test your changes" - "API handlers live in
src/api/handlers/" instead of "Keep files organized"
A vague rule gives Claude nothing to act on, and you nothing to check.
Be concise
The docs' target is under 200 lines per CLAUDE.md file: "Longer files consume more context and reduce adherence." Claude Code warns at startup and in /status when an instruction file is over the recommended length. Keep the file to facts Claude needs in every session. The docs suggest moving a multi-step procedure into a skill, which loads only when needed, and an instruction that matters for one part of the codebase into a path-scoped rule.
Use structure
"Group related instructions under markdown headers and bullets. Organized sections are easier for Claude to follow than dense paragraphs." A short file with sections like these covers most projects:
# Commands
- Install: `npm ci`
- Test one file: `npm test -- path/to/file.test.ts`
- Lint before committing: `npm run lint`
# Conventions
- TypeScript strict mode; no `any` in new code
- API handlers live in `src/api/handlers/`, one file per route
- Dates are stored as UTC ISO strings
# Gotchas
- The payments tests need `STRIPE_TEST_KEY` set, or they skip silently
For a larger project, split topics into .claude/rules/, one file per topic such as testing.md or api-design.md. A rule with paths frontmatter loads only when Claude touches a matching file:
---
paths:
- 'src/api/**/*.ts'
---
- Every endpoint validates its input
- Use the standard error response format
Leave out what the code already says
Claude can read your directory tree, your package.json and your source. Restating them costs context in every session and goes stale. The docs say /doctor proposes trims for a checked-in CLAUDE.md along exactly these lines: it cuts "directory layouts, dependency lists, and architecture overviews" and keeps "pitfalls, rationale, and conventions that differ from tool defaults." Write down what Claude would get wrong on its own: the command that differs from the default, the folder that must not be edited, the reason a pattern exists.
To leave a note for human maintainers without spending context on it, use a block-level HTML comment. The docs say those are stripped before the content reaches Claude.
Review it periodically
Instructions go stale as the code moves, and two that contradict each other make Claude pick one at random. The docs advise reviewing your CLAUDE.md files, nested ones and .claude/rules/ "periodically to remove outdated or conflicting instructions." On Claude Code v2.1.283 or later, /doctor prompt-audit does the first pass for you: it looks for instructions written for older models, references to files or commands that no longer exist, and files that contradict each other, then proposes edits without changing anything until you ask.
Monorepos and nested CLAUDE.md files
In a monorepo you can put a CLAUDE.md at the root and more in each package. The docs describe two different behaviours:
- Above the working directory: loaded at launch. Start Claude Code in
packages/api/and it loadspackages/api/CLAUDE.md,packages/CLAUDE.mdif there is one, and the rootCLAUDE.md, with the root first and the closest file last. - Below the working directory: loaded on demand. A
CLAUDE.mdorCLAUDE.local.mdin a subdirectory is not loaded at launch. Claude Code loads it once Claude reads, writes or edits a file in that subdirectory, including viewing one file with a command such ascatorhead.
So starting at the root gives Claude the root file straight away, and each package's file arrives when Claude starts working in that package. Two things to watch:
- A size cap. If a subdirectory CLAUDE.md or an on-demand rules file is over a per-file limit, Claude receives only the start of it, with a note to read the rest. The docs give 40,000 characters for a 200K context window. Check yours with
wc -m path/to/CLAUDE.md. - Other teams' files. If ancestor CLAUDE.md files from other teams get picked up, skip them with
claudeMdExcludesin.claude/settings.local.json. Patterns are globs matched against absolute paths:
{
"claudeMdExcludes": [
"**/monorepo/CLAUDE.md",
"/home/user/monorepo/other-team/.claude/rules/**"
]
}
After /compact, the project-root CLAUDE.md is re-read from disk. Nested CLAUDE.md files and path-scoped rules load again on demand, the next time Claude works with a matching file. The docs have a separate page on monorepos and large repos that covers the full layout.
FAQ
Where is Claude Code memory stored?
Your instructions live in CLAUDE.md files: ~/.claude/CLAUDE.md for you everywhere, ./CLAUDE.md or ./.claude/CLAUDE.md for the project, and ./CLAUDE.local.md for you in one project. Auto memory lives in ~/.claude/projects/<project>/memory/, with MEMORY.md as the index.
What is the difference between CLAUDE.md and auto memory?
You write CLAUDE.md; Claude writes auto memory. The CLAUDE.md files at and above your working directory load in full at launch, and CLAUDE.md is for rules and conventions. Auto memory is Claude's own notes on your preferences and corrections, and only the first 200 lines or 25KB of its index load at session start.
How do I make Claude Code remember something?
Ask it. "Remember that we use pnpm, not npm" saves to auto memory. "Add this to CLAUDE.md" writes it into your instructions. Or run /memory and edit the file yourself.
How long should a CLAUDE.md be?
The docs target under 200 lines per file. Longer files use more context and reduce adherence. Move topic-specific instructions into path-scoped rules in .claude/rules/ and multi-step procedures into skills.
Does Claude Code load CLAUDE.md files in subdirectories?
Yes, on demand. Files in the directories above where you start Claude Code load at launch. Files in subdirectories load the first time Claude reads, writes or edits a file in that subdirectory.
Why is Claude ignoring my CLAUDE.md?
Run /context all and check that the file is listed under Memory files. If it is, make the instruction more specific and look for a conflicting instruction in another file. For anything that must always happen, use a hook: CLAUDE.md guides Claude but doesn't enforce anything.
Related
- Claude Code subagents: how subagents get their own context window, and how their memory differs from the main conversation's.
- Agentic workflows: the orchestrate, implement, review and fix loop that a good CLAUDE.md supports.
Claude and Claude Code are trademarks of Anthropic. This page is not affiliated with or endorsed by Anthropic.