A good CLAUDE.md is a short, specific list of facts Claude Code can't learn from your code: commands, conventions, gotchas and repo etiquette, ideally under 200 lines. This guide covers what to include and cut, where each file lives, how loading works, path-scoped rules, imports, a copyable template and how to keep the file current.
- Keep each CLAUDE.md under about 200 lines; longer files cost context and Claude follows them less reliably.
- Write only what Claude can't infer from the code: build and test commands, non-default style rules, architecture decisions, environment quirks and gotchas.
- Make every instruction checkable, such as
Run npm test before committinginstead ofTest your changes. - Move rules that apply to one part of the repo into
.claude/rules/files withpathsfrontmatter, and multi-step procedures into skills. - CLAUDE.md is advice, not enforcement: anything that must happen every time belongs in a hook or a permission rule.
What are CLAUDE.md best practices?
The core CLAUDE.md best practices are to keep the file short, write only what Claude can't infer from your code, and phrase every rule so you could check whether it was followed. Anthropic's memory docs recommend targeting under 200 lines per file, because longer files consume more context and reduce adherence.
CLAUDE.md is a markdown file Claude Code reads at the start of every session. Each session starts with an empty context window, so this file is how your build commands, conventions and hard-won lessons carry over from one session to the next. If you are new to the tool, start with our Claude Code tutorial and come back once you have run a few tasks.
Three facts shape every decision below:
- CLAUDE.md is context, not configuration. The docs say Claude "treats them as context, not enforced configuration." Claude reads the file and tries to follow it, with no guarantee.
- Every line costs context in every session. The file loads whether or not the current task needs it.
- Vague and conflicting rules get dropped. If two instructions contradict each other, Claude "may pick one arbitrarily."
What should you put in CLAUDE.md?
Put in CLAUDE.md the things a new senior engineer would need to be told on day one because the code doesn't say them. The best practices page gives a clear include and exclude list.
| Include | Exclude |
|---|---|
| Bash commands Claude can't guess | Anything Claude can figure out by reading code |
| Code style rules that differ from defaults | Standard language conventions Claude already knows |
| Testing instructions and preferred test runners | Detailed API documentation (link to it instead) |
| Repository etiquette (branch naming, PR conventions) | Information that changes frequently |
| Architectural decisions specific to your project | Long explanations or tutorials |
| Developer environment quirks (required env vars) | File-by-file descriptions of the codebase |
| Common gotchas that aren't self-evident | Self-evident advice like "write clean code" |
The docs also suggest a test for each line: "Would removing this cause Claude to make mistakes?" If not, cut it.
When should you add a line?
Add to CLAUDE.md when you notice yourself repeating something. The memory docs list the triggers:
- 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 you typed last session.
- A new teammate would need the same context to be productive.
This keeps the file grounded in real failures instead of guesses about what Claude might get wrong.
How do you write instructions Claude actually follows?
Write instructions that are concrete enough to verify, grouped under headings, with no contradictions. Specific beats general every time.
| Vague | Specific |
|---|---|
| Format code properly | Use 2-space indentation |
| Test your changes | Run npm test before committing |
| Keep files organized | API handlers live in src/api/handlers/ |
| Be careful with the database | Never edit files in db/migrations/; create a new migration with npm run db:new |
| Follow our git conventions | Branch names: feat/<ticket>-<slug>. One logical change per commit |
A few more habits that help:
- Use the imperative. "Run", "use", "never". A CLAUDE.md is an instruction set, not documentation.
- Group related rules under headings. The docs note that organized sections are easier to follow than dense paragraphs.
- Say why when the reason isn't obvious. "Use
pnpm, notnpm: the lockfile is pnpm-only" helps Claude handle edge cases the rule doesn't cover. - Emphasize sparingly. If Claude keeps skipping one rule, the docs suggest adding "IMPORTANT" to that line alone. "If you emphasize many lines, none of them stands out."
- Tell Claude how to compact. A line like "When compacting, always preserve the full list of modified files and any test commands" shapes what survives summarization in long sessions.
A CLAUDE.md template you can copy
A useful CLAUDE.md for a typical web app fits on one screen. Start from this template, delete what doesn't apply, and replace the examples with your own commands.
## ProjectNext.js app with a Postgres database (Drizzle ORM). Monorepo: `apps/web`, `packages/db`.## Commands- Install: `pnpm install` (pnpm only; the lockfile breaks with npm)- Dev server: `pnpm dev` (port 3000)- Typecheck: `pnpm typecheck`- Test one file: `pnpm vitest run path/to/file.test.ts`- Never run the full e2e suite locally; CI runs it## Code style- TypeScript strict mode; no `any`, use `unknown` and narrow- Server code uses named exports; React components use default exports- Dates are stored in UTC and formatted with `formatDate()` from `lib/date.ts`## Architecture- All database access goes through `packages/db`; never import the driver in `apps/web`- Auth checks happen in `middleware.ts`, not in individual routes## Workflow- Typecheck after a series of changes, before saying you are done- Branches: `feat/<ticket>-<slug>`; open PRs as drafts- When compacting, keep the list of modified files and the test commands## Gotchas- `.env.local` needs `DATABASE_URL`; copy from `.env.example`- The `legacy/` folder is frozen; do not refactor itNotice what is missing: no description of every folder, no explanation of what TypeScript is, no general coding advice. Claude can read the folder tree and already knows TypeScript.
Run /init in a new project to generate a first draft. Claude analyzes the codebase and writes build commands, test instructions and conventions it finds. If a CLAUDE.md exists, /init suggests improvements instead of overwriting it. Then cut it down with the rules above.
Where do CLAUDE.md files live, and how do they load?
CLAUDE.md files can live at four scopes, and Claude Code concatenates all of the ones it finds instead of letting one replace another. More specific files load later, so they are read last.
| Scope | Location | Use it for | Shared with |
|---|---|---|---|
| Managed policy | /etc/claude-code/CLAUDE.md (Linux), /Library/Application Support/ClaudeCode/CLAUDE.md (macOS) | Company-wide standards set by IT | Everyone on the machine |
| User | ~/.claude/CLAUDE.md | Your personal preferences in every project | Only you |
| Project | ./CLAUDE.md or ./.claude/CLAUDE.md | Team conventions, commands, architecture | The team, via git |
| Local | ./CLAUDE.local.md (add to .gitignore) | Your sandbox URLs, personal test data | Only you, this project |
The loading rules that matter in practice:
- Upward at launch. Claude Code loads CLAUDE.md and CLAUDE.local.md from the working directory and every directory above it. Files closer to where you launched are read last.
- Downward on demand. A CLAUDE.md in a subdirectory loads once Claude reads, writes or edits a file in that subdirectory. This is how you give
packages/api/its own instructions without loading them for frontend work. - Local is appended, not an override. Within a directory, CLAUDE.local.md comes after CLAUDE.md. If the two conflict, you have a contradiction, not a precedence rule.
- HTML comments are stripped. Block-level
<!-- notes -->don't reach Claude's context, so you can leave notes for human maintainers at no token cost. - Compaction keeps the root file. After
/compact, Claude re-reads the project-root CLAUDE.md from disk. Instructions you gave only in chat do not survive, which is a good reason to move repeated corrections into the file.
To confirm what loaded, run /context and look under Memory files. Run /memory to open and edit the files from inside a session.
How do you keep CLAUDE.md short as the project grows?
Keep CLAUDE.md short by moving anything that applies to only part of the codebase into path-scoped rules, anything procedural into skills, and anything that must always happen into hooks. The root file then holds only what every session needs.
Path-scoped rules in .claude/rules/
Markdown files in .claude/rules/ work like extra CLAUDE.md files, one topic each. Add paths frontmatter and a rule loads only when Claude touches a matching file:
---paths: - "src/api/**/*.ts"---## API rules- Every endpoint validates input with the schemas in `src/api/schemas/`- Errors use `ApiError` and the standard response shape- Add an OpenAPI comment to every new routeRules without paths load at launch, like the main file. Personal rules for every project go in ~/.claude/rules/.
Imports
A CLAUDE.md can pull in other files with @path/to/file, for example See @README for an overview. Imports nest up to four levels deep. They help organize a long file but do not save context, because imported files also load at launch. To mention a path without importing it, wrap it in backticks.
Skills, subagents and hooks
| Content | Better home | Why |
|---|---|---|
| A multi-step procedure (release, deploy, fix an issue) | A skill in .claude/skills/ | Loads only when invoked or relevant |
| A specialized reviewer or researcher | A subagent in .claude/agents/ | Runs in its own context; see Claude Code subagents |
| "Always run the formatter after editing" | A PostToolUse hook | Hooks run every time, regardless of what Claude decides |
"Never touch .env" | A permission deny rule or PreToolUse hook | Enforced by the client, not by Claude's judgment |
The docs put it plainly: CLAUDE.md instructions are advisory, hooks are deterministic. Our guide to Claude Code hooks has copyable recipes for the formatter and the blocked-file cases.
How do CLAUDE.md and AGENTS.md work together?
Claude Code can read AGENTS.md, the instruction file other coding agents use, when your repository has no CLAUDE.md or CLAUDE.local.md in the working directory or above it. If both exist, Claude reads only the CLAUDE.md files by default.
If your team uses several agents, keep one shared file and import it:
@AGENTS.md## Claude CodeUse plan mode for changes under `src/billing/`.Claude reads the imported AGENTS.md first, then the Claude-specific lines. A symlink (ln -s AGENTS.md CLAUDE.md) also works, but the docs advise the import if anyone on the team uses Windows, where symlinks often check out as plain text files. You can also change which files load with the Project instructions setting in /config.
How do you keep CLAUDE.md current?
Treat CLAUDE.md like code: review it when Claude misbehaves, prune it on a schedule, and test changes by watching whether Claude's behavior shifts. The docs list two failure signals worth memorizing:
- Claude keeps doing something despite a rule against it. The file is probably too long and the rule is getting lost.
- Claude asks questions CLAUDE.md already answers. The phrasing is ambiguous.
A monthly maintenance pass takes ten minutes:
- Run
/contextand confirm every expected file loads. - Delete rules Claude already follows without being told; if a rule must stay enforced, convert it to a hook.
- Search for contradictions between the root file, nested files,
.claude/rules/and your user file. - Check that every command in the file still exists and works.
- In recent versions, run
/doctor prompt-auditto have Claude report outdated or conflicting instructions; nothing changes until you approve the edits.
Don't confuse CLAUDE.md with auto memory, the notes Claude writes for itself. Auto memory lives in ~/.claude/projects/<project>/memory/, only the first 200 lines or 25KB of its MEMORY.md load at startup, and you can browse or switch it off with /memory. Use CLAUDE.md for rules you want the whole team to share.
A well-kept CLAUDE.md matters most when nobody is watching. Agents you run unattended, for example on a remote box overnight, can't ask you which test command to use, so the file is their only briefing. If you run Claude Code that way, Orkio gives you an always-on cloud box where agents keep working and open pull requests while your laptop is closed; the same CLAUDE.md in your repository applies there. For more on what to give an agent beyond this file, see context engineering.
CLAUDE.md checklist
Use this list on your current file today:
- Under 200 lines, grouped under clear headings.
- Exact commands for install, dev, typecheck, lint and running a single test.
- Only style rules that differ from language defaults.
- Architecture decisions and gotchas the code doesn't reveal.
- Every rule checkable; no "write clean code".
- No contradictions with nested files, rules or your user file.
- Area-specific rules moved to
.claude/rules/withpaths. - Procedures moved to skills, must-always behavior moved to hooks.
- Personal preferences in
CLAUDE.local.mdor~/.claude/CLAUDE.md, not the team file. - Project CLAUDE.md committed to git and reviewed like code.
Frequently asked questions
How long should a CLAUDE.md file be?
Anthropic's docs recommend targeting under 200 lines per CLAUDE.md file. Longer files use more of the context window and reduce how consistently Claude follows them. If yours is longer, move area-specific instructions into path-scoped rules or skills.
Should I commit CLAUDE.md to git?
Yes, commit the project CLAUDE.md so the whole team shares and improves it. Keep personal preferences in CLAUDE.local.md, add that file to .gitignore, and put preferences that apply to all your projects in ~/.claude/CLAUDE.md.
Does Claude Code read AGENTS.md?
Recent versions of Claude Code read AGENTS.md when the repository has no CLAUDE.md or CLAUDE.local.md in the working directory or above it. If you have both, Claude reads CLAUDE.md only by default. To share one file across tools, put an @AGENTS.md import at the top of CLAUDE.md.
Why is Claude ignoring my CLAUDE.md?
Check first that the file loaded by running /context and looking under Memory files. If it loaded, the usual causes are a file that is too long, vague wording, or two instructions that contradict each other. Shorten it, make the rule specific, and use a hook for anything that must always happen.
What is the difference between CLAUDE.md and auto memory?
You write CLAUDE.md; Claude writes auto memory itself from your corrections and preferences. Both load at the start of every session. Auto memory lives in a per-repository folder under ~/.claude/projects and only its first 200 lines or 25KB load at startup.
