Claude Code Memory: The Complete Guide

Everything you need to know about how Claude Code remembers context — from CLAUDE.md project files to auto memory, context window management, and building persistent AI coding workflows.

Updated Feb 2026 12 min read Intermediate

How Claude Code Memory Works

Claude Code has a layered memory system that works across three timescales:

1. Session memory (current conversation)
   Everything in the current context window: your prompts,
   Claude's responses, tool outputs, file contents read.
   Lost when you start a new session (unless you use claude -c).

2. Project memory (CLAUDE.md files)
   Loaded automatically at the start of every session.
   You write these. They contain project instructions,
   build commands, architecture notes, conventions.

3. Auto memory (~/.claude/projects/<project>/memory/)
   Claude writes these. Patterns, preferences, and insights
   learned across multiple sessions. Persists indefinitely.

Together, these three layers let Claude Code feel like a team member who remembers your project — your coding conventions, your build system, your architectural decisions — without you having to re-explain everything each session.

CLAUDE.md: Project Instructions

The CLAUDE.md file is the most important memory mechanism in Claude Code. It's a Markdown file that you write and maintain, loaded automatically at session start.

File Locations & Scope

~/.claude/
  CLAUDE.md ← Global: applies to ALL projects

~/my-project/
  CLAUDE.md ← Project root: applies to this repo
  src/
    CLAUDE.md ← Directory: applies when working in src/
  .claude/
    CLAUDE.local.md ← Personal: gitignored, your preferences only

All matching CLAUDE.md files are loaded and concatenated. If you're working in src/components/, Claude sees: global + project root + src/ CLAUDE.md files.

What to Put in CLAUDE.md

# My Project

## Build & Test Commands
- `npm run dev` - start dev server (port 3000)
- `npm test` - run Jest test suite
- `npm run lint` - ESLint + Prettier check
- `npm run build` - production build

## Architecture
- Next.js 15 app router in src/app/
- PostgreSQL database, Drizzle ORM
- Tailwind CSS + shadcn/ui components
- API routes in src/app/api/

## Conventions
- TypeScript strict mode, no `any`
- Functional React components with hooks
- Use server components by default
- Conventional commits (feat:, fix:, refactor:)
- Tests required for new features

## Important Notes
- Never commit .env files
- Use `bun` instead of `npm` for package management
- Always run tests before suggesting a commit
Keep it concise

CLAUDE.md is loaded every session and counts against your context window. Aim for 50-200 lines max. Focus on information Claude can't infer from the code itself: build commands, hidden conventions, architectural decisions, and team preferences.

Creating CLAUDE.md Automatically

Use the /init slash command to have Claude analyze your project and generate a CLAUDE.md:

$ claude
> /init

Claude will scan your project structure, package files, config files, and README to generate an initial CLAUDE.md. Review and edit it — Claude's first draft is a starting point, not the final version.

CLAUDE.local.md: Personal Preferences

For preferences that shouldn't be shared with your team (they're gitignored), use .claude/CLAUDE.local.md:

# Personal Preferences

- I prefer verbose explanations when debugging
- Always use dark mode examples in code
- Don't auto-commit; let me review first
- Use bun instead of npm

Auto Memory: Persistent Learning

Auto memory is Claude Code's ability to learn and remember across sessions without you writing anything. When enabled, Claude saves important patterns, preferences, and project insights to a persistent directory.

How It Works

  1. As you work, Claude identifies stable patterns: coding conventions, file organization, preferred tools, debugging approaches
  2. Claude writes these observations to ~/.claude/projects/<project>/memory/MEMORY.md
  3. For detailed topics, Claude creates separate files (e.g., debugging.md, patterns.md)
  4. At the start of each session, MEMORY.md is loaded into context (first 200 lines)

Memory Directory Structure

~/.claude/projects/<project-hash>/memory/
  MEMORY.md ← Main memory file (auto-loaded, keep under 200 lines)
  patterns.md ← Coding patterns observed across sessions
  debugging.md ← Debugging insights for this project
  architecture.md ← Architecture notes

What Gets Saved Automatically

What Doesn't Get Saved

Manual Memory Control

You can explicitly ask Claude to remember things:

> Remember: always use bun instead of npm in this project
Claude saves this to MEMORY.md

> Forget the preference about using tabs
Claude removes the relevant entry from memory

Context Window Management

Claude Code uses Claude's full 200K token context window. But effective management matters more than raw size. Here's how context flows:

Context window budget (200K tokens)
├── System prompt + CLAUDE.md files (~2-5K tokens)
├── Auto memory MEMORY.md (~1-3K tokens)
├── Conversation history (grows over time)
│   ├── Your prompts
│   ├── Claude's responses
│   └── Tool outputs (file reads, bash results, etc.)
└── Available space for new work

Tool outputs are the biggest context consumers. Every file read, every bash command output, every search result gets added to the conversation. A single large file read can consume 10-20K tokens.

Automatic Compaction

When the conversation approaches the context limit, Claude Code automatically compresses earlier messages into a summary. This happens transparently — you'll see a note that context was compressed, but your conversation continues seamlessly.

Using /compact Effectively

The /compact command manually triggers context compression. Use it proactively — don't wait for automatic compaction.

When to Use /compact

Custom Compact Prompts

You can provide a focus hint to guide what gets preserved:

> /compact Focus on the auth migration plan and database schema changes

This tells Claude to prioritize preserving information about auth and database schema while compressing everything else more aggressively.

The Memory Hierarchy

Understanding the full hierarchy helps you use the right mechanism for each type of information:

Priority   Source                    Lifetime
────────────────────────────────────────────────────
1 (highest)  System instructions         Built-in
2            Global CLAUDE.md            Permanent
3            Project CLAUDE.md           Permanent
4            CLAUDE.local.md             Permanent
5            Auto memory (MEMORY.md)     Persistent
6            Conversation history        Session
7 (lowest)   Compacted summaries         Session

Higher-priority sources override lower ones. If your CLAUDE.md says "use tabs" but a compacted summary says "use spaces", the CLAUDE.md instruction wins.

Best Practices

CLAUDE.md Best Practices

  1. Start with /init, then refine — Let Claude generate the first draft, then edit it based on what you know matters
  2. Include build commands first — The most useful thing in CLAUDE.md is how to build, test, and lint your project
  3. Document non-obvious conventions — Don't describe what Claude can see in the code. Document the "why" and hidden rules
  4. Keep it under 200 lines — Every line costs tokens. Be ruthlessly concise
  5. Update it regularly — CLAUDE.md should evolve with your project. Remove outdated info
  6. Use CLAUDE.local.md for personal prefs — Don't pollute the shared CLAUDE.md with individual preferences

Context Window Best Practices

  1. Use /compact proactively — Don't wait until Claude starts struggling
  2. Use claude -c to continue sessions — Preserves context instead of starting fresh
  3. Reference files by path instead of pasting content — Claude reads files efficiently with its Read tool
  4. Use subagents for researchSubagents protect the parent context from verbose search results
  5. Break large tasks into focused sessions — Don't try to do everything in one conversation

Auto Memory Best Practices

  1. Let it learn organically — Don't try to front-load memory. Let Claude discover patterns naturally
  2. Correct mistakes explicitly — If Claude remembers something wrong, say "forget that" or "actually, we use X"
  3. Review MEMORY.md periodically — Check what Claude has learned and remove outdated entries
  4. Keep MEMORY.md under 200 lines — Only the first 200 lines are auto-loaded

Troubleshooting

Claude isn't following my CLAUDE.md instructions

Claude keeps forgetting things mid-session

Auto memory seems wrong or outdated

Frequently Asked Questions

Does CLAUDE.md work in all IDEs?

CLAUDE.md is loaded by Claude Code (the CLI). IDE integrations like the VS Code extension also support it. Other tools (Cursor, Windsurf) have their own configuration file formats but don't read CLAUDE.md.

Can I have multiple CLAUDE.md files?

Yes. Claude loads CLAUDE.md from the global directory, project root, and any parent directories of the file you're working in. They're concatenated, with more specific files taking priority.

Does auto memory sync across machines?

No. Auto memory is stored locally in ~/.claude/projects/. If you work on the same project from multiple machines, each will have its own memory. Consider using CLAUDE.md (committed to git) for shared context.

How do I see what's in my context window?

Use /cost to see token usage for the current session. There's no direct way to view the full context, but you can estimate: each file read adds roughly 1 token per 4 characters.

Can I disable auto memory?

Auto memory can be disabled in your settings. However, it's recommended to keep it enabled — it makes Claude significantly more effective over time as it learns your project's patterns.