Understanding CLAUDE.md
Independent educational resource, not an official Anthropic course. Facts about the API, prices and limits were checked against Anthropic's documentation on 15 November 2026 and the code was run against a local mock of the API, not the live service. Check the linked official pages before relying on a number, and test with your own key.
Claude Code starts every session knowing nothing about your project beyond what it can read. CLAUDE.md is how you fix that: a plain markdown file that Claude reads at the start of every session. A good one saves you from re-explaining the same things forever. A bad one, which is the usual kind, is long, vague, and quietly ignored. This lesson is about which kind you write.
What you'll learn
- Where
CLAUDE.md files can live, and the order they load in.
- What belongs in one, and what doesn't.
- Imports, comments, local files and size limits.
- How it differs from auto memory, and what survives compaction.
- How to check that yours is working.
Before you start
What it is, and what it isn't
Each session begins with a fresh context window. Two things carry knowledge across sessions: CLAUDE.md files that you write, and auto memory, notes that Claude writes for itself. Both are loaded at the start of every conversation.
The most important sentence in Anthropic's documentation on this is: Claude treats CLAUDE.md as context, not enforced configuration. The file is delivered as a user message after the system prompt, and Claude tries to follow it, but there's no guarantee, especially for vague or conflicting instructions. If something must happen every single time, or must never happen, that's a job for a hook or a deny rule (lesson 5.7), not for another line in this file.
Where the files live
Files are listed from broadest to most specific, which is also their load order, so more specific instructions come later in context:
| Scope | Location | Shared with |
| Managed policy | macOS /Library/Application Support/ClaudeCode/CLAUDE.md; Linux and WSL /etc/claude-code/CLAUDE.md; Windows C:\Program Files\ClaudeCode\CLAUDE.md | Everyone in the organisation |
| User | ~/.claude/CLAUDE.md | Only you, in every project |
| Project | ./CLAUDE.md or ./.claude/CLAUDE.md | Your team, through version control |
| Local | ./CLAUDE.local.md | Only you, in this project. Add it to .gitignore |
How they combine: Claude Code loads CLAUDE.md and CLAUDE.local.md from your working directory and every directory above it, and concatenates them rather than overriding. Start in foo/bar/ and you get foo/CLAUDE.md first and foo/bar/CLAUDE.md after it, with CLAUDE.local.md appended last at each level. Files in subdirectories below your working directory aren't loaded at launch; each loads when Claude reads, writes or edits a file in that subdirectory. That makes nested files a good home for area-specific notes, such as a CLAUDE.md in app/Billing/.
If your repository uses AGENTS.md for other coding tools, Claude Code can read that in place of a CLAUDE.md. By default it reads AGENTS.md only when there's no CLAUDE.md in the path, so a stray CLAUDE.md is the usual reason an AGENTS.md seems ignored. To share one file between tools, put a short CLAUDE.md next to it that imports it.
What to put in it
Anthropic's guidance is to treat CLAUDE.md as the place you write down what you'd otherwise re-explain. Add a line when:
- Claude makes the same mistake a second time;
- a code review catches something Claude should have known about this codebase;
- you type the same correction into chat that you typed last session;
- a new teammate would need the same context to be productive.
The best-practices page narrows that further: include what Claude couldn't work out from the code itself. The exact test, typecheck and lint commands, and how to run one test file. Places where the code is misleading. Decisions that aren't in the repository. Anything you've had to correct twice. And what to leave out: the page lists "describe your architecture and file layout" among advice you can stop following, because Claude reads the repository itself.
Here's the example project's file. It's short on purpose:
# Ridge & Co shop (Laravel 13, Inertia, React)
## Commands
- Install: `composer install && npm install`
- Dev server: `composer run dev`
- Run all tests: `vendor/bin/pest` (read the first gotcha below before you do)
- Run one test file: `vendor/bin/pest tests/Feature/OrderTest.php`
- Format PHP: `vendor/bin/pint --dirty`
- Typecheck the frontend: `npm run types:check`
## Conventions
- Controllers stay thin. Business rules live in `app/Actions/`, one class per action.
- Validate with Form Requests, never inline in a controller.
- Money is an integer number of cents. Never use floats for prices.
- New endpoints need a Pest feature test that covers the unauthorised case.
## Gotchas
- Tests must use a database whose name ends in `_test`. A cached config (`bootstrap/cache/config.php`)
ignores `phpunit.xml`, so run `php artisan config:clear` first or set `APP_CONFIG_CACHE`.
- `app/Support/ClaudeFoundations.php` defines the course curriculum; do not rename its slugs.
- Do not edit migrations that are already merged. Add a new one.
<!-- Maintainers: keep this file under 200 lines. Move area-specific rules to .claude/rules/ -->
Look at what each section does. Commands let Claude check its own work. Conventions record decisions that aren't obvious from the code (money is cents, validation goes in Form Requests). Gotchas are the things that go wrong: a cached config that ignores phpunit.xml, a file you mustn't rename, a migration rule. Each line is concrete enough to check, which the docs recommend: "Use 2-space indentation" works, "format code properly" doesn't.
Size and shape
- Under 200 lines per file. Longer files consume more context and reduce adherence. Claude Code warns at startup and in
/status when a file is too long. It skips any file over 4 MiB entirely.
- Structure with headers and bullets. Organised sections are easier to follow than dense paragraphs.
- No contradictions. If two files disagree, Claude may pick one arbitrarily. Review nested files and rules now and then.
/doctor prompt-audit looks for instructions written for older models, references to files that no longer exist, and files that contradict each other.
- Imports organise, they don't shrink.
@docs/git-instructions.md pulls a file in, up to four levels deep, but imported files still load into context with the file that imports them. Relative paths resolve from the file containing the import.
- HTML comments are free. Block-level
<!-- notes --> are stripped before the content reaches Claude, so you can leave maintainer notes without spending tokens. To mention a path without importing it, wrap it in backticks; @README outside backticks imports the file.
- Personal preferences go in
CLAUDE.local.md (gitignored), or in an import from your home directory if you use several git worktrees.
Auto memory
Claude also keeps its own notes, which you didn't write. It saves four kinds: user (your role and preferences), feedback (corrections and approaches you confirmed), project (ongoing work and decisions it can't derive from the code) and reference (where to find things outside the project). It skips anything derivable from the codebase or already in your CLAUDE.md. Each project gets a folder at ~/.claude/projects/<project>/memory/, shared by all worktrees of the same repository. The first 200 lines or 25 KB of its MEMORY.md index load every session; the topic files it points to are read on demand.
It's plain markdown. Run /memory to browse it, edit it or delete it, and to toggle auto memory (autoMemoryEnabled in settings, or CLAUDE_CODE_DISABLE_AUTO_MEMORY=1). Subagents don't inherit the main conversation's auto memory.
Starting one, and checking it
- Run
/init for a first draft generated from your codebase. Then delete lines Claude could work out itself.
- Run
/context all and look for your files under "Memory files". If it isn't listed, Claude can't see it. (Nested files don't appear there, because they load on demand; a "Loaded" line appears in the terminal when one does.)
- Test an instruction with a task that would break it. Make it more specific if Claude drifts.
- If a line must hold every time, move it to a hook or a deny rule.
A bundled linter for this module's example project checks line count, capitalised emphasis, and broken @ imports, among other things. Its relevant part:
lines = text.count("\n") + 1
if lines > 200:
rep.add("WARN", rel, f"{lines} lines. Anthropic recommends under 200; longer files reduce adherence. "
"Move area-specific rules to .claude/rules/.")
shouting = len(EMPHASIS.findall(text))
if shouting >= 4:
rep.add("WARN", rel, f"{shouting} capitalised emphasis words. If a rule truly cannot have exceptions, "
"enforce it with a hook or a deny rule instead.")
The full script is in lesson 5.11. We tested these checks with a file of 210 lines plus shouting and a broken import, and with the clean example (no findings).
Surviving compaction
When a long session compacts, the project-root CLAUDE.md and auto memory are re-injected from disk, so they survive. Nested CLAUDE.md files and path-scoped rules load again on demand. Instructions you gave only in conversation do not survive, which is the practical reason to put lasting rules in the file. You can also add a "Compact Instructions" section to CLAUDE.md to guide what a summary keeps.
Real-world use case (illustrative scenario)
This scenario is an invented example to show how the ideas fit together. It is not a case study and its details are not measured results.
A developer notices Claude keeps running the whole test suite against the development database. They've told it twice. The second time they add a gotcha to CLAUDE.md: tests must use a database whose name ends in _test. It works for a while, then one day it doesn't, because the line is buried in a 300-line file full of architecture notes. They cut the file to 40 lines, which fixes most of it. For the rest they add a PreToolUse hook that blocks the destructive commands outright, because a rule that can't have exceptions belongs in the harness, not in a note.
Mistakes to avoid
- A 600-line
CLAUDE.md that describes the whole architecture.
- Vague rules ("write clean code") instead of checkable ones.
- Contradictions between the root file, nested files and rules.
- Adding IMPORTANT and NEVER to lines Claude misses. Anthropic's advice is that emphasis mostly makes Claude over-apply a rule; shorten the file or enforce it with a hook.
- Putting secrets in
CLAUDE.md. It's committed and sent to a model.
Security notes
A committed CLAUDE.md shapes what Claude does on every contributor's machine, so review changes to it in pull requests like code. Imports from outside your working directory trigger an approval dialog the first time, to protect you from files other people commit. And remember it's guidance, not a lock: protect secrets with deny rules, not with a sentence asking Claude not to read them.
Cost and speed
Every line is read on every task, so a bloated file costs tokens each time and dilutes the lines that matter. /context shows how much it takes. Move instructions that matter for only part of the codebase into path-scoped rules or nested files, and procedures into skills (lessons 5.5 and 5.6).
Key takeaways
CLAUDE.md is persistent context, not enforcement. Use hooks and deny rules for must-always rules.
- Files load from broad to specific; ancestors at launch, subdirectories on demand;
CLAUDE.local.md is yours.
- Write only what Claude can't infer: commands, gotchas, decisions. Keep it under 200 lines and concrete.
- Check with
/context all and /memory; auto memory is separate, written by Claude.
Lesson recap: where CLAUDE.md lives, what goes in it, and what it can't do.
Quick quiz
- Why is
CLAUDE.md not a reliable place for a rule that can never be broken?
- In which order do
foo/CLAUDE.md and foo/bar/CLAUDE.md appear when you start in foo/bar/?
- When does a
CLAUDE.md in a subdirectory load?
- What does the 200-line guideline protect?
- Where is auto memory stored, and how much of
MEMORY.md loads each session?
Answers and explanations
- It's delivered as context, and Claude tries to follow it but may not. Enforced rules belong in hooks, deny rules or managed settings.
foo/CLAUDE.md first, then foo/bar/CLAUDE.md, so the instructions closest to your working directory are read last.
- When Claude reads, writes or edits a file in that subdirectory, not at launch.
- Adherence and context cost. Longer files consume more context and reduce how reliably instructions are followed.
- In
~/.claude/projects/<project>/memory/. The first 200 lines or 25 KB, whichever comes first.
Official references and further reading
Related tutorials