Why a Good CLAUDE.md Changes Everything
If you have ever retyped the same instructions into every Claude Code session — “this is a Laravel app, run php artisan test before committing, never touch deployed migrations” — you already understand the problem this file solves. Learning how to write claude.md properly is the highest-leverage thing you can do before opening Claude Code: a good CLAUDE.md turns Claude from a talented stranger into a teammate who already knows your stack, commands, and rules.
Here is the key fact, from Anthropic’s official docs: every Claude Code session begins with a fresh context window, and CLAUDE.md is the markdown file Claude reads at the start of every session to restore your project’s context. This guide covers where it lives, how Claude discovers it, what to put in it, and the mistakes that make most CLAUDE.md files useless — all verified against Anthropic’s current documentation.
What Is a CLAUDE.md File?
CLAUDE.md is a plain markdown file where you write persistent instructions for Claude Code: your project’s stack, the exact commands to build and test it, where things live in the codebase, and the conventions Claude must follow. Claude loads it into context at the start of every session, so you never have to re-explain the basics.
Anthropic describes two complementary memory systems. You write CLAUDE.md files (instructions and rules for a project, your workflow, or your organization). Claude writes auto memory — notes it takes itself from your corrections and preferences, stored in a MEMORY.md file capped at the first 200 lines or 25 KB per session. This post focuses on CLAUDE.md, the half you control.
One thing to internalize early: Claude treats CLAUDE.md as context, not enforced configuration. It shapes behavior but does not hard-block actions. If something must never happen — no matter what Claude decides — that belongs in a PreToolUse hook, not in CLAUDE.md.
Where CLAUDE.md Lives: The Four Memory Locations
CLAUDE.md can live in several locations, each with a different scope. Anthropic lists them in load order, from broadest scope to most specific, so a project instruction appears in context after a user instruction:
- Managed policy — organization-wide instructions managed by IT/DevOps. Locations: macOS
/Library/Application Support/ClaudeCode/CLAUDE.md, Linux and WSL/etc/claude-code/CLAUDE.md, WindowsC:\Program Files\ClaudeCode\CLAUDE.md. Shared with everyone in the organization. - User instructions —
~/.claude/CLAUDE.md. Your personal preferences across every project: code styling, your favorite tooling shortcuts. Just you. - Project instructions —
./CLAUDE.mdor./.claude/CLAUDE.md. Team-shared instructions committed through version control: project architecture, coding standards, common workflows. - Local instructions —
./CLAUDE.local.md. Personal project-specific preferences that should not be committed: your sandbox URLs, your preferred test data. Add it to.gitignore.
For solo developers, the two that matter most are the project file (shared with your future self and your team) and the global file (your personal defaults everywhere). Keep personal quirks out of the project file.
How Claude Code Discovers and Loads Your CLAUDE.md
Claude does not just read the file in your current directory. Here is the actual discovery behavior from the docs:
- It loads upward. Claude loads
CLAUDE.mdandCLAUDE.local.mdfrom your working directory and every directory above it. Launch infoo/bar/and it picks upfoo/bar/CLAUDE.md,foo/CLAUDE.md, and anyCLAUDE.local.mdfiles alongside them. - Files are concatenated, never overriding. Everything discovered merges into context. Within each directory,
CLAUDE.local.mdis appended afterCLAUDE.md, so your personal notes are read last at that level. - Order runs root down to your working directory.
foo/CLAUDE.mdappears in context beforefoo/bar/CLAUDE.md, so instructions closer to where you launched are read last and carry more weight. - Subdirectories load on demand. CLAUDE.md files below your working directory are not loaded at launch — they are included when Claude reads files in those subdirectories. This makes nested files ideal for monorepos: a CLAUDE.md in
packages/api/only kicks in when Claude works there. - You can exclude noisy files. In large monorepos, another team’s CLAUDE.md might get picked up; the
claudeMdExcludessetting skips specific files by path or glob. - HTML comments are free. Block-level HTML comments (
<!-- maintainer notes -->) are stripped before injection, so notes for human maintainers cost zero tokens.
Confirm your file loaded: run /context in a session and check the list under Memory files.
What to Put in Your CLAUDE.md (and What to Leave Out)
Anthropic’s guidance on when to add something is refreshingly concrete: add it when Claude makes the same mistake a second time, a code review catches something Claude should have known, you repeat a correction from a previous session, or a new teammate would need the same context. Keep it to facts Claude should hold in every session — build commands, conventions, project layout, “always do X” rules. Multi-step procedures and rules for one corner of the codebase belong in a skill or a path-scoped rule instead.
In practice, five high-signal sections cover almost every project:
1. Project overview (2–4 lines)
What the project is and who it serves. This is calibration, not documentation — two lines steer the thousands of micro-decisions Claude makes per session.
2. Commands (exact strings)
The literal commands to build, test, lint, type-check, and run the dev server, copied from your package.json or Makefile. Claude runs these verbatim.
3. Project structure (non-obvious parts only)
A short directory map focused on what Claude would guess wrong: “API v2 routes live in routes/api/v2/; legacy v1 routes are frozen, do not modify.”
4. Code conventions (only the non-default ones)
Rules Claude would not infer from the code. Anthropic’s own examples: “Use 2-space indentation” instead of “Format code properly”; “Run npm test before committing” instead of “Test your changes”. Every instruction should be concrete enough to verify.
5. Gotchas and hard rules
Things Claude must never do and traps it keeps falling into. Each rule should trace back to a mistake it prevents: “Never edit deployed migrations in database/migrations/ — create a new one” beats “be careful with migrations”.
What to leave out
- Anything Claude already knows (standard framework patterns, what a controller is).
- Anything enforced by your linter or formatter — do not send an LLM to do a linter’s job.
- Long narrative docs and history lessons. Link or
@-import them instead. - Frequently changing information — it goes stale and then actively misleads every session.
- Secrets, API keys, and tokens. The project CLAUDE.md is committed to version control and shared with your team.
A Complete Annotated Example: How to Write CLAUDE.md
Here is a full project CLAUDE.md for a realistic Laravel + Vue application. It is about 50 lines — notice how every line earns its place. The <!-- --> comments are annotations for you, the reader; Claude strips block-level HTML comments before injecting the file into context, so they cost you nothing.
# ShipTracker — multi-tenant shipment tracking SaaS
<!-- 1–2 lines of identity: what it is, who it serves. Calibration, not docs. -->
Laravel 11 API backend + Vue 3 (Nuxt) admin dashboard. PostgreSQL. Deployed on a Hetzner VPS via Laravel Forge.
## Commands
<!-- Exact strings. Claude runs these verbatim, so copy them from composer.json / package.json. -->
- Dev: `composer dev` (starts PHP server, queue worker, and Vite together)
- Test: `php artisan test` — full suite; `php artisan test --filter=OrderTest` for one file
- Lint: `vendor/bin/pint --test` (Laravel Pint, never auto-fix in CI)
- Frontend: `npm run dev` in `dashboard/`; build with `npm run build`
## Project structure
<!-- Only the non-obvious parts. Skip what Claude can see for itself. -->
- `app/Services/` — business logic; keep controllers thin, logic goes here
- `routes/api/v2/` — current API. `routes/api/v1/` is frozen legacy: never modify
- `dashboard/` — separate Nuxt app with its own package.json and node_modules
- `database/migrations/` — deployed migrations are immutable; create a new one instead of editing
## Conventions
<!-- Non-default rules only. Concrete enough to verify. -->
- Use 2-space indentation in Vue files; PHP follows Pint defaults
- All API responses use the `{ success, data, error }` envelope
- Eloquent: always eager-load relationships in list endpoints (`with()`), never lazy-load in loops
- Form request classes for validation, never inline `$request->validate()`
- Vue: Composition API with `<script setup>` only, no Options API
## Gotchas
<!-- Each line traces back to a real mistake. If you cannot name the mistake, cut the line. -->
- IMPORTANT: run `php artisan test` before every commit — the CI suite is the source of truth
- Never run `migrate:fresh` or `db:seed` against anything but local
- The `tenants` table uses UUIDs, not auto-increment IDs — join accordingly
- Queue jobs must be idempotent: Horizon retries failed jobs up to 3 times
## Reference docs
<!-- @-imports pull files into context at launch. Use for detail that is rarely needed. -->
- Deployment runbook: @docs/deployment.md
- API versioning policy: @docs/api-versioning.md
Why this works: the overview is two sentences — enough to calibrate, nothing more. The commands section is the highest-value real estate in the file; without it, Claude guesses your test runner and burns a session figuring it out. The structure section documents only surprises (the frozen v1 routes, the separate dashboard app, immutable migrations). Every convention is non-default, and every gotcha maps to a specific mistake. The reference docs use @path imports for progressive disclosure: the deployment runbook is available without living in every session’s context.
One note on import syntax, since it has sharp edges: @path/to/file accepts relative and absolute paths, but relative paths resolve relative to the file containing the import, not your working directory. Imports nest up to four hops deep and are expanded at launch — so they organize a long file but do not reduce its context cost. To mention a path without importing it, wrap it in backticks: `@README` stays literal. If a path contains spaces, put a backslash before each space, because the path otherwise ends at the first space.
6 Mistakes That Make CLAUDE.md Useless
1. Writing a novel
Anthropic’s target is under 200 lines per CLAUDE.md file. Longer files consume more context and reduce adherence — Claude starts ignoring instructions. If a rule matters for only part of the codebase, move it into .claude/rules/ with path scoping.
2. Letting it go stale
A stale CLAUDE.md confidently misleads every session. Review it whenever your stack, commands, or architecture change — /doctor prompt-audit flags outdated content, dead references, and contradictions.
3. Putting secrets in it
Your project CLAUDE.md is committed to version control and shared with the whole team. API keys, tokens, and credentials do not belong there. Personal-but-sensitive values belong in CLAUDE.local.md, which you gitignore.
4. Writing wishes instead of instructions
“Write clean, maintainable code” changes nothing — it is not concrete enough to verify. Every instruction should pass the test: could you check whether Claude followed it? “Use 2-space indentation” yes; “format code properly” no.
5. Contradicting yourself across files
Files are concatenated and broader files load first, so if two instructions contradict each other, Claude may pick one arbitrarily. Audit nested CLAUDE.md files and rules periodically for conflicts.
6. Treating it as an enforcement layer
CLAUDE.md is advisory. If a rule must hold every single time without exception — say, “never write to prod.config” — make it a PreToolUse hook, which is deterministic, instead of a line in CLAUDE.md, which Claude follows most of the time but not always.
Keeping It Fresh: The Maintenance Loop
The best CLAUDE.md files are grown, not written:
- Bootstrap with
/init. Let Claude Code analyze your project and draft a first version — it even folds in other tools’ instruction files (Cursor rules, Copilot instructions, AGENTS.md). - Prune ruthlessly. The test for every line: if removing it would not cause a mistake, delete it.
- Add from friction. When Claude repeats a mistake, add the correction as a concrete rule. The file compounds: a good CLAUDE.md in month one means you stop repeating yourself by month two.
- Audit periodically. Run
/doctor prompt-auditwhen the file feels bloated or Claude starts ignoring rules. Bloated gets ignored; tight gets followed. - Verify it loads. After any change, run
/contextand confirm the file appears under Memory files.
Related Files: AGENTS.md, .claude/rules/, and Auto Memory
Here is how the neighboring mechanisms fit, per the official docs:
- AGENTS.md. If your repo already has an
AGENTS.md(the cross-tool convention other coding agents use), Claude Code reads it by default when you have noCLAUDE.mdorCLAUDE.local.mdin your working directory or above it. The Project instructions setting (in/config) can change this to read both files together, CLAUDE.md only, or managed-only. You can also share one file across tools with an@AGENTS.mdimport at the top of your CLAUDE.md. .claude/rules/. For larger projects, split instructions into topic files liketesting.mdorapi-version.md— one topic per file. Add YAML frontmatter with apathsfield (glob patterns likesrc/**/*.ts) to scope a rule so it only loads when Claude works with matching files. Personal rules in~/.claude/rules/apply to every project on your machine.- Auto memory. Claude writes its own notes about your corrections and preferences into
MEMORY.md(per repository, shared across worktrees); the first 200 lines or 25 KB load every session. You do not write this file — but it explains why Claude sometimes “remembers” things you never put in CLAUDE.md.
Your 10-Minute Quick Start to Writing CLAUDE.md
- Open your project in Claude Code and run
/initto draft a CLAUDE.md at your project root. - Cut it to under 200 lines: delete everything Claude would do correctly anyway.
- Make every instruction concrete and verifiable — exact commands, exact paths, exact “never” rules.
- Keep personal notes in
~/.claude/CLAUDE.mdor a gitignoredCLAUDE.local.md; never commit secrets. - Run
/contextand confirm the file shows up under Memory files — then add one line every time Claude repeats a mistake.
Do this once, and every future session starts with a Claude that already knows your project. That is the whole trick — there is no trick, just a well-maintained file. If you’re still choosing between AI coding assistants, see our Claude Code vs Cursor comparison. For more free AI tools for developers, see our curated list.
Further Reading & References
- How Claude remembers your project — official Claude Code docs — the canonical reference for memory locations, load order, imports, rules, AGENTS.md, and auto memory.
- How to Write a CLAUDE.md File That Actually Works — TurboDocx — a practical include/exclude table and the WHY/WHAT/HOW structure.
- How to Write a CLAUDE.md File That Actually Makes Claude Code Useful — Nova Labs — identity, architecture, and hard-rules structure that holds up well.
- The One File That Controls Every Claude Code Session — Coding Nexus — a template plus the “highest impact lines” pattern: every rule should map to a mistake it prevents.
- Mastering CLAUDE.md — fivetaku/cc101 — a community cheat sheet covering memory locations and the
/initbootstrap. - CLAUDE.md Best Practices — Etendo — a real-world case of shrinking a 760-line file to 244 lines, plus maintenance practices.
- How to Write a Perfect CLAUDE.md (Claude Code Memory Explained) — YouTube — video walkthrough: generating with
/init, pruning on camera,@-imports, and the/memorycommand.



