CLAUDE.md best practices: write rules Claude Code follows
If Claude Code keeps ignoring your CLAUDE.md, the file is usually too long, too vague, or full of steps that belong somewhere else. Anthropic's own docs say to keep each CLAUDE.md under about 200 lines and to write instructions specific enough to check, like "Use 2-space indentation" instead of "format code properly".
This guide shows where Claude Code looks for the file, what to put in it, what to move out of it, and a short example you can copy.
Where Claude Code looks for CLAUDE.md
Claude Code loads every CLAUDE.md it finds and joins them together; a later file does not replace an earlier one. Files closer to the folder you started Claude in are read last. If two instructions disagree, the docs warn that Claude may pick either one, so remove the conflict instead of hoping the right one wins.
| Scope | Path | Use it for |
|---|---|---|
| Managed policy | macOS /Library/Application Support/ClaudeCode/CLAUDE.md, Linux /etc/claude-code/CLAUDE.md, Windows C:\Program Files\ClaudeCode\CLAUDE.md | Company-wide rules set by IT |
| User | ~/.claude/CLAUDE.md | Your own habits in every project |
| Project | ./CLAUDE.md or ./.claude/CLAUDE.md | Team rules, committed to git |
| Local | ./CLAUDE.local.md | Your notes for this repo only; keep it out of git |
CLAUDE.md files in subfolders are not loaded at launch. Claude Code reads one the first time it reads or edits a file in that folder, so rules for one package can live next to that package.
Seven rules that make it stick
- Keep it short. Aim for under 200 lines per file. Every line costs context in every session, and the docs say longer files reduce adherence.
- Make every line checkable. "Run npm test before saying a task is done" can be checked; "write good code" cannot.
- Put the commands first: how to install, build, test and lint. Most bad output starts with Claude guessing the test command.
- Group rules under headings and bullets. Do not write paragraphs of prose.
- Say what not to do when it matters, with the reason: "Never edit files in /generated: they are rebuilt from schema.prisma."
- Move long procedures to a skill. A skill's body loads only when it is used, so a 300-line release checklist costs nothing until you release.
- Move rules that only apply to some files into .claude/rules/ with a paths list (below).
Split it up: imports and path rules
A CLAUDE.md can pull in another file with an @ import. Paths are relative to the file that holds the import, and imports can nest up to four levels deep.
# CLAUDE.md
@docs/architecture.md
@AGENTS.mdFor rules that only matter in part of the codebase, put a Markdown file in .claude/rules/ and scope it with a paths list in the frontmatter. paths is the only field Claude Code reads from a rule file.
---
paths:
- "src/api/**/*.ts"
---
# API rules
- Validate every request body with zod before using it.
- Return errors as { error: string } with the right HTTP status.A starter CLAUDE.md you can copy
# Project: acme-web (Next.js, TypeScript, Postgres)
## Commands
- Install: npm ci
- Dev server: npm run dev
- Tests: npm test (must pass before you say a task is done)
- Types: npx tsc --noEmit
- Lint: npx eslint .
## Code style
- TypeScript strict. No `any` unless you explain why in a comment.
- Use 2-space indentation.
- Server code reads the database only through src/lib/db.ts.
## Working rules
- Change only the files the task needs. Ask before deleting a file.
- When a test fails, fix the code, not the test, unless the test is wrong; say which.
- Never commit secrets or .env files.
## Where things are
- Pages: src/app
- Shared code: src/lib
- Database schema: prisma/schema.prismaRun /init in Claude Code to have it write a first draft from your repo. If a CLAUDE.md already exists, /init suggests improvements instead of overwriting it. Then cut the draft down to the lines you would actually check.
If Claude still ignores it
- Check for a second CLAUDE.md (user, parent folder or CLAUDE.local.md) that says the opposite.
- Open /memory in a session to see which memory files are in play.
- In a long session, the rules were read at the start; after heavy work and compaction they carry less weight. Start a fresh session for a new task.
- Turn the rule into a check that fails: a test, a lint rule or a hook. A rule that a script enforces cannot be forgotten.
Questions people ask
- How long should CLAUDE.md be?
- Under about 200 lines per file, per Anthropic's docs. Move procedures into skills and folder-specific rules into .claude/rules/ so the file that loads every session stays short.
- Does Claude Code read AGENTS.md?
- Yes. Recent versions read AGENTS.md as project instructions when there is no CLAUDE.md. If you have both, add the line @AGENTS.md to CLAUDE.md so one copy of the rules serves every agent.
- Should CLAUDE.md be committed to git?
- Yes for ./CLAUDE.md, so the whole team gets the same rules. Keep personal notes in CLAUDE.local.md and leave that file out of git.