Mascot Logo
ai-agents-tutorial

Part 2 · Lesson 12 of 16

Build Lasting Project Memory with CLAUDE.md

Give the agent durable project memory.

10 min

Step 1 of 5 · What CLAUDE.md is

Every Claude Code session starts with a fresh context window — the agent doesn't remember your last conversation. A CLAUDE.md file is how you carry knowledge across sessions: it's a plain Markdown file of persistent instructions that Claude reads at the start of every session.

Think of it as the project's onboarding doc, written for the agent. The build command, where things live, the conventions your team follows — anything you'd otherwise have to re-explain every time goes here once.

Step 2 of 5 · Generate one with /init

You don't have to write CLAUDE.md from scratch. Inside a Claude Code session, run:

/init

Claude analyzes your codebase and generates a starter CLAUDE.md with the build commands, test instructions, and conventions it can discover on its own. If a CLAUDE.md already exists, /init suggests improvements rather than overwriting it.

Treat the generated file as a first draft. Refine it with the things Claude couldn't infer — architectural decisions, "always do X" rules, and the context a new teammate would need.

Step 3 of 5 · Where the files live

CLAUDE.md can live in a few places, each with a different scope. Claude walks up the directory tree from where you launched it and loads every file it finds, broadest scope first:

  • Project memory./CLAUDE.md or ./.claude/CLAUDE.md. Team-shared, committed to source control. This is the one you'll use most.
  • User memory~/.claude/CLAUDE.md. Your personal preferences across all projects (just you).
  • Local memory./CLAUDE.local.md. Personal, project-specific notes; add it to .gitignore so it isn't committed.
  • Managed policy — an org-wide file deployed by IT (for example, /Library/Application Support/ClaudeCode/CLAUDE.md on macOS).

Files closer to your working directory are read last, so a project instruction layers on top of a user one.

Step 4 of 5 · What to put in it (and what not to)

The goal is facts and conventions Claude should hold in every session — not step-by-step procedures. Good entries are concrete and verifiable:

  • Build and test commands: "Run npm test before committing."
  • Conventions: "Use 2-space indentation."
  • Project layout: "API handlers live in src/api/handlers/."
  • "Always do X" rules specific to this repo.

What to leave out:

  • Multi-step procedures or task-specific workflows → make a skill instead, so it loads only when relevant.
  • Instructions that only matter for one part of the codebase → use a path-scoped rule in .claude/rules/.
  • Vague guidance like "format code nicely" or "test your changes" — too fuzzy to follow reliably.

Keep each CLAUDE.md under ~200 lines: it's loaded in full on every session, so a bloated file burns context and actually reduces how well Claude follows it. Use Markdown headers and bullets so the agent can scan it the way you do.

Step 5 of 5 · Edit it later with /memory

As you work, you'll spot things to add — a mistake Claude repeated, a correction you keep retyping. To see and edit your memory files, run:

/memory

/memory lists every CLAUDE.md, CLAUDE.local.md, and rules file loaded in the current session and lets you open any of them in your editor. (It's also the fastest way to confirm a file is actually being loaded — if it's not in the list, Claude can't see it.) You can also just ask Claude "add this to CLAUDE.md," and it will edit the file for you.

Recap

  • CLAUDE.md is persistent project memory — a Markdown file Claude reads at the start of every session.
  • /init generates a starter file from your codebase; /memory lists and opens the memory files loaded in a session.
  • Files load by scope: project (./CLAUDE.md), user (~/.claude/CLAUDE.md), local (./CLAUDE.local.md), and managed policy.
  • Put facts and conventions in it; move procedures to skills and path-specific rules to .claude/rules/. Keep it under ~200 lines.

Frequently asked questions

How do I create a CLAUDE.md in Claude Code?

Run /init inside a session. Claude analyzes your codebase and writes a starter CLAUDE.md with the build commands, test instructions, and conventions it can discover. If a CLAUDE.md already exists, /init suggests improvements instead of overwriting it. You can also create the file by hand — it's just Markdown.

Where should CLAUDE.md live?

For team-shared, project-level instructions, put it at ./CLAUDE.md or ./.claude/CLAUDE.md and commit it. For personal preferences across all your projects, use ~/.claude/CLAUDE.md. For private, project-specific notes, use ./CLAUDE.local.md and add it to .gitignore. Claude loads all of these by walking up the directory tree from where you launched it.

What's the difference between CLAUDE.md and /memory?

CLAUDE.md is the file; /memory is the command for viewing and editing it. Running /memory lists every CLAUDE.md, CLAUDE.local.md, and rules file loaded in your session and opens any of them in your editor — handy for confirming a file is actually being loaded.

What should I not put in CLAUDE.md?

Avoid multi-step procedures and task-specific workflows — package those as a skill so they load only when relevant. Keep instructions that matter for only one part of the codebase in a path-scoped rule under .claude/rules/. And skip vague guidance like "format code nicely"; specific, verifiable instructions work far better.

Why isn't Claude following my CLAUDE.md?

CLAUDE.md is context, not enforced configuration, so compliance isn't guaranteed — especially for vague or conflicting instructions. Run /memory to confirm the file is loaded, make instructions specific and concrete, and remove any rules that contradict each other. If something must run at a fixed point, write it as a hook instead.