What actually belongs in a CLAUDE.md file, what to leave out, where it lives, and a ready-to-use template based on Anthropic's own Claude Code docs.
Manager, Architect Technology at Cognizant
A CLAUDE.md file is a plain markdown file, usually sitting at the root of your repo, that Claude Code reads at the start of every session so it doesn't have to re-learn your build commands, your conventions, and your gotchas from scratch each time. That's the whole idea. It's not a config file Claude Code enforces. It's context the model reads and tries to follow, the same as a message you typed yourself.
Anthropic's own docs put it plainly: CLAUDE.md is for guidelines, and Claude is allowed to decide a guideline doesn't apply. If you need something to happen every single time with zero exceptions, that's a job for a hook or a permission rule, not a line in CLAUDE.md. Keep that distinction in mind and most of the confusion about this file goes away.
The official best-practices page gives a simple test before you write anything down: could Claude find this by reading the code, and would it actually go look? If both answers are yes, leave it out. What's left is usually four things:
Here's the example straight from Anthropic's docs, and it's a good reference for how short this should be:
# Commands
- Install: pnpm install
- Run one test file: pnpm vitest run path/to/file.test.ts
- Typecheck: pnpm tsc --noEmit
# Conventions
- Use the error helpers in src/lib/errors.ts, never throw raw strings
- src/legacy/ is still live but don't copy its patterns into new code
# Gotchas
- Integration tests need DATABASE_URL set, see .env.example
Eleven lines. That's it. No architecture diagram, no list of dependencies, no paragraph explaining what React is. Claude already knows React. What it doesn't know is that your src/legacy/ folder looks abandoned but still ships to production.
This is the part most people get backward. The instinct when setting up a new tool is to over-document, and CLAUDE.md punishes that instinct directly: every line loads into every session whether it's relevant to the current task or not. Anthropic's docs list a specific table of advice that used to matter and now actively hurts you, including "describe your architecture and file layout in CLAUDE.md", since Claude reads the repo itself, and writing it down a second time just adds dead weight.
Also skip:
That last point is worth sitting with. If you've ever written **IMPORTANT: ALWAYS run tests before committing** in all caps three times in the same file, you've found a rule that belongs in a Stop hook, not a CLAUDE.md line. A hook exits with code 2 and blocks Claude from finishing until tests actually pass. No amount of bold text does that.
Claude Code loads these in order, from broadest to most specific, concatenating all of them into context rather than letting one override another:
| Scope | Location | Shared with |
|---|---|---|
| Managed policy | /etc/claude-code/CLAUDE.md (Linux/WSL), equivalent on macOS/Windows | Everyone in the org |
| User | ~/.claude/CLAUDE.md | Only you, across all projects |
| Project | ./CLAUDE.md or ./.claude/CLAUDE.md | Team, via git |
| Local | ./CLAUDE.local.md | Only you, this project (gitignore it) |
Claude loads CLAUDE.md and CLAUDE.local.md from your working directory and every directory above it at launch. Files in subdirectories load on demand, the moment Claude reads, writes, or edits a file in that subdirectory, not before. That matters if you're debugging why a nested CLAUDE.md "isn't working": it's often just that Claude hasn't touched that folder yet in the session.
Run /context all in a session any time you want to confirm exactly which memory files loaded.
As of Claude Code v2.1.277, Claude reads a repository's AGENTS.md directly, in place of CLAUDE.md, if there's no CLAUDE.md or CLAUDE.local.md anywhere in the working directory or above it. Thariq Shihipar, who works on Claude Code at Anthropic, announced the change on X: "Starting today in version 2.1.277, if there is no CLAUDE.md in a folder, Claude will check for and use AGENTS.md."
If both files exist, Claude reads CLAUDE.md and ignores AGENTS.md by default. You can change that in /config by setting Project instructions to claude-md-and-agents-md, which loads both, CLAUDE.md first. If your project is already shared with other coding agents and you don't want two files to maintain, the cleanest move is a one-line import:
@AGENTS.md
## Claude Code
Use plan mode for changes under src/billing/.
That keeps AGENTS.md as the single source every tool reads, with anything Claude-specific layered underneath.
Claude Code also ships "auto memory" — notes Claude writes itself based on your corrections, separate from CLAUDE.md. The difference comes down to who's holding the pen:
| CLAUDE.md | Auto memory | |
|---|---|---|
| Who writes it | You | Claude |
| Contains | Instructions and rules | Learnings and patterns |
| Loaded into | Every session, in full | First 200 lines or 25KB of its index |
| Best for | Coding standards, commands, architecture notes | Your preferences and context Claude can't derive from code |
The practical rule: if you want Claude to always know it, put it in CLAUDE.md. If you just want Claude to stop repeating a mistake you corrected once, let auto memory pick it up, or say "remember that" and move on.
Anthropic's docs give a specific number: target under 200 lines per CLAUDE.md file. Go over, and Claude Code shows a warning at startup and in /status. Files over 4 MiB get skipped entirely. For subdirectory CLAUDE.md files and path-scoped rules that load on demand, there's a per-file character limit too — 40,000 characters on a 200K-token context window, 200,000 characters on a 1M window for Opus 4.6 and Sonnet 4.6, and 150,000 characters on newer models. Go past that limit and Claude only sees the start of the file, with a note telling it to read the rest manually if it decides to.
None of this is a hard ceiling Claude Code refuses to cross. It's a point past which the file stops pulling its weight. Nick Babich, who writes regularly about Claude Code workflows, put a related point well in a thread on X breaking down five practices for a well-crafted CLAUDE.md — the idea that a file packed with low-value detail trains Claude to deprioritize the whole thing, not just the noisy parts.
If your file keeps growing past 200 lines because the project genuinely has that much to say, that's the signal to split it, not to keep cramming:
.claude/rules/ — topic files like testing.md or api-design.md, optionally scoped to a paths: glob in frontmatter so they only load when Claude touches matching files@path imports — pull a README or a git-workflow doc into CLAUDE.md with @docs/git-instructions.md syntax. Worth knowing: imports help you organize the file, they don't reduce its context cost, since the imported content still loads with itYes, as a starting point — not as a finished product. /init analyzes your codebase and writes a CLAUDE.md with the commands, structure, and conventions it can detect automatically. Anthropic's own docs describe the work that follows as "mostly removing lines," which is the opposite of what most people expect from a generator. /doctor goes further and proposes specific cuts for anything Claude can already infer from the code, and /doctor prompt-audit (available from Claude Code v2.1.283) flags instructions written for older models, references to files that no longer exist, and rules that quietly contradict each other across your CLAUDE.md, rules, and skills.
Run /init, read every line it generated, and delete anything that passes the "Claude would find this anyway" test. That's the whole workflow.
Here's a structure based directly on what Anthropic's docs recommend keeping, adjusted so you can drop in your own project's details:
# Commands
- Install: <your install command>
- Dev server: <your dev command>
- Run all tests: <your test command>
- Run one test file: <your single-test command>
- Typecheck: <your typecheck command>
- Lint: <your lint command>
# Conventions
- <A decision Claude can't infer, e.g. "all API routes live under src/api/v1/">
- <An error-handling convention, e.g. "use the helpers in src/lib/errors.ts">
- <A naming or import-order rule that's non-obvious>
# Gotchas
- <A directory that looks dead but isn't, and why>
- <An environment variable integration tests need, and where it's documented>
- <An old pattern that's everywhere but new code must not copy>
# Workflow
- <When Claude should ask before proceeding, e.g. "plan mode for changes under src/billing/">
- <Anything about PRs, branch naming, or commit messages your team actually follows>
Fill in only the sections where you have something Claude genuinely can't get from the code. An empty section is better than a section padded with filler just to look thorough.
PostToolUse hook, which Anthropic's docs walk through for exactly this case/context all after any change to confirm the file Claude is actually reading matches the file on diskIf you're setting up Claude Code on a new project, pair this with a look at how hooks can enforce the rules CLAUDE.md can't, and if you're scaling across a team, the subagent starter pack covers delegating work without bloating your main context. For connecting external tools Claude can't see on its own, setting up MCP servers is the next step after your CLAUDE.md is solid.
A good CLAUDE.md file is boring. It's a handful of commands, a short list of things that would trip up a new teammate, and nothing else. If yours reads like documentation you'd hand to a new hire on day one, you're probably doing it right. If it reads like an architecture decision record, start deleting.
New AI tools, automation workflows, and course drops — straight to your inbox. Join 2,400+ builders.
Copy-paste PreToolUse and PostToolUse hook configs for Claude Code: block rm -rf, protect .env files, auto-format on save, and feed test failures back to Claude.
A step-by-step guide to writing SKILL.md for Claude Code: required frontmatter, instructions, scripts, testing, and the spec vs Claude Code differences.
Claude Code and Playwright MCP write real Playwright tests from a live DOM snapshot instead of guessing selectors. Setup steps and honest limits inside.
Comments