CLAUDE.md Best Practices (With Examples and a Template)

A useful CLAUDE.md is short and full of things Claude can't work out by reading your code, like the command that runs one test file or the reason money is stored in cents. A long tour of your folder structure does the opposite: it costs context on every task and tells Claude what it could have found for itself. This guide covers CLAUDE.md best practices as Anthropic's docs describe them today, with four annotated example files and a template you can copy.
Anthropic's memory documentation and best practices page are the reference. Every Claude Code behavior below was checked against them on 25 November 2026.
The Short Answer
CLAUDE.md is a Markdown file that Claude Code loads into context at the start of every session. Use it for what Claude can't learn from your repo: exact build, test and lint commands, decisions that aren't written down, and places where the code is misleading. Keep each file under 200 lines. When you've corrected Claude on the same thing twice, add a line.
CLAUDE.md File Location and Load Order
Claude Code reads CLAUDE.md files from four scopes, listed here from broadest to most specific:
| Scope | Location | Who it's for |
|---|---|---|
| Managed policy | macOS: /Library/Application Support/ClaudeCode/CLAUDE.mdLinux and WSL: /etc/claude-code/CLAUDE.mdWindows: C:\Program Files\ClaudeCode\CLAUDE.md | Everyone in an organization, set by IT |
| User | ~/.claude/CLAUDE.md | You, in every project |
| Project | ./CLAUDE.md or ./.claude/CLAUDE.md | Your team, committed to the repo |
| Local | ./CLAUDE.local.md | You, in this project only (add it to .gitignore) |
How the files combine:
- At launch, Claude Code loads
CLAUDE.mdandCLAUDE.local.mdfrom your working directory "and every directory above it." - CLAUDE.md files in subdirectories load later, once Claude reads, writes or edits a file in that subdirectory.
- Nothing overrides anything. Anthropic says all discovered files "are concatenated into context rather than overriding each other," ordered from the filesystem root down to your working directory, with
CLAUDE.local.mdafterCLAUDE.mdin each directory. - If two instructions contradict each other, "Claude may pick one arbitrarily." Fix the conflict rather than counting on the order.
You can also put rules files in .claude/rules/. A rules file without paths in its frontmatter loads at launch, like .claude/CLAUDE.md. One with paths stays out of context until Claude works on matching files, which makes it the place for instructions that only matter in one part of the codebase.
What to Put in CLAUDE.md
Anthropic's best practices page puts the test in one line: "Only write down what Claude can't find by reading the repo." That usually means:
- Exact commands. Anthropic names "the exact test, typecheck, and lint commands." Include the command for a single test file as well as the full suite.
- Decisions that aren't in the repo. Why money is stored in cents, or which changes need your sign-off first.
- Places where the code is misleading. A file that looks unused but isn't, or a path that an outside service depends on.
- Anything you've corrected twice. If Claude keeps making the same mistake, that's the line to add.
What to Leave Out
- What Claude can read for itself. Anthropic now lists "Describe your architecture and file layout in CLAUDE.md" under things you can stop doing, because Claude reads the repo. Skip directory trees and dependency lists unless something about them is surprising.
- Rules your tools already enforce. If a formatter or linter checks it, give Claude the command to run instead of restating the rules. The MCP TypeScript SDK's CLAUDE.md says it in one line: "Style belongs to Prettier, ESLint and the compiler."
- Secrets. Anthropic's CLAUDE.md article says not to include "sensitive information, API keys, credentials, database connection strings, or detailed security vulnerability information." To keep Claude out of files like
.env, add apermissions.denyrule such asRead(./.env)to your Claude Code settings. Settings and hooks are also the place for anything that must never happen, because CLAUDE.md is context Claude reads and can't block an action. - Extra emphasis. Anthropic lists adding "IMPORTANT or ALL CAPS to instructions Claude keeps missing" under things to stop doing. It says current models follow a plainly worded line and that "piling on emphasis mostly makes Claude over-apply the rule."
- Anything that goes stale. The current sprint or this week's branch name. It will be wrong soon, and Claude can't tell.
CLAUDE.md Best Practices
Start with /init, then cut
Run /init in a project and Claude "analyzes your codebase and creates a file with build commands, test instructions, and project conventions it discovers." If a CLAUDE.md already exists, /init "suggests improvements rather than overwriting it." Treat the result as a first draft and delete anything Claude could have found by reading the code.
Stay under 200 lines
Anthropic's guidance is to "target under 200 lines per CLAUDE.md file. Longer files consume more context and reduce adherence." Its best practices page explains why: "every line in it takes a little of Claude's attention on every task, whether it's relevant or not."
Splitting the file with imports doesn't help here, because imports "don't reduce its context cost." To keep instructions out of context until they're needed, move them into a subdirectory CLAUDE.md or a path-scoped rule in .claude/rules/.
Write rules a reviewer could check
Anthropic's example is "Use 2-space indentation" instead of "Format code properly." (If a formatter handles indentation, you need neither.) A good test for your own rules is whether a reviewer could check one against a diff. "Keep functions small" fails that test. "No SQL in route handlers; database access goes through src/repo/" passes it.
Update it when Claude gets something wrong
When Claude repeats a mistake, tell it to "add this to CLAUDE.md," or open the file with /memory and edit it yourself. The # shortcut that older guides describe was removed in Claude Code 2.0.70 (December 2025).
Claude Code also has auto memory, which is separate from CLAUDE.md and on by default in local sessions. It's stored in ~/.claude/projects/<project>/memory/, and /memory is where you view its entries or turn it off.
Import other files with @
A CLAUDE.md can pull in another file with @path/to/file. Relative paths resolve from the file that contains the import. Absolute paths and home paths like @~/.claude/notes.md work too. Imports can go four hops deep, and paths inside code spans or code blocks are ignored. The first time a project imports a file from outside the project, Claude Code asks you to approve it.
Imports are good for reusing a file you already maintain, such as AGENTS.md. As noted above, they won't make CLAUDE.md any cheaper.
Commit the shared file, keep personal notes local
The project CLAUDE.md is "shared through version control, so focus on project-level standards rather than personal preferences." Put your own preferences in CLAUDE.local.md and add it to .gitignore, or in ~/.claude/CLAUDE.md if they apply to every project.
Leave notes for humans in HTML comments
Block-level HTML comments in CLAUDE.md "are stripped before the content is injected into Claude's context." Use them to tell the next person who edits the file why a rule exists. The note costs Claude no context.
What Real CLAUDE.md Files Look Like
On 25 November 2026 we checked 36 well-known open-source repositories for a root CLAUDE.md. Of those, 22 had one, and 14 of the 22 did little more than point to AGENTS.md: 8 held a single @AGENTS.md line, 5 were symlinks to AGENTS.md, and 1 was a five-line file ending "See @AGENTS.md."
The files with their own content ranged from 13 lines to over 400. React's root file is 13 lines, with a second CLAUDE.md in its compiler/ directory. The MCP TypeScript SDK's is 14 lines of principles, and Excalidraw's is 34 lines: a package map and three commands. At the long end, Bun's is 244 lines covering exact build and test commands, the architecture and what reviewers catch, while Deno's and PyTorch's run to 433 and 381 lines. Line counts will drift after that date.
CLAUDE.md Examples
These four files were written for this guide, and the projects are made up. After each one are notes on why its lines are there.
Example 1: a Next.js web app
# Acme Dashboard
Next.js App Router + TypeScript. Postgres via Prisma. Deployed on Vercel.
## Commands
- Install: `pnpm install`
- Dev server: `pnpm dev` (http://localhost:3000)
- Type check: `pnpm typecheck`
- Unit tests: `pnpm test` (Vitest). One file: `pnpm test lib/billing.test.ts`
- E2E: `pnpm test:e2e` (Playwright; needs `pnpm dev` running)
- After changing `prisma/schema.prisma`: `pnpm prisma migrate dev --name <change>`
## Code notes
- `lib/db.ts` is the only place that creates a Prisma client.
- `components/ui/` matches shadcn's output so the shadcn CLI can update it. Wrap those components; don't edit them.
- Money is stored in cents as integers. Format it with `formatCents()` from `lib/money.ts`.
## Gotchas
- `pnpm dev` needs `.env.local`. Copy `.env.example`; never commit real values.
- Don't move `app/api/stripe/route.ts`. The Stripe dashboard sends webhooks to `/api/stripe`.
- The one-file test command stops Claude running the whole suite after every change.
- "The only place that creates a Prisma client" is a rule no single file reveals.
- The shadcn line comes with its reason, so Claude knows why those files are treated differently from the rest of the code.
- Money in cents is a domain decision that looks arbitrary from the code alone.
- The webhook path depends on a setting outside the repo, which reading the code would never reveal.
Example 2: a Python library
# acmegeo
Python library for geocoding with pluggable providers. Supports Python 3.11+.
## Commands
- Set up: `uv sync --all-extras`
- Tests: `uv run pytest`. While iterating: `uv run pytest -m "not network"`
- Lint and format: `uv run ruff check --fix . && uv run ruff format .`
- Types: `uv run mypy src/`
## Code notes
- Each provider in `src/acmegeo/providers/` subclasses `BaseProvider`. Keep `BaseProvider` backward compatible; third-party providers subclass it too.
- `tests/cassettes/` holds recorded HTTP responses (pytest-recording). Re-record with `--record-mode=rewrite` only when a provider's API has changed.
## Ask before
- Changing anything exported from `src/acmegeo/__init__.py`. That's the public API, and it needs a CHANGELOG entry under "Unreleased".
- Adding a runtime dependency. Optional ones go under `[project.optional-dependencies]`.
- The "not network" marker gives Claude a fast, reliable test run while it works.
- The cassette rule prevents an accidental re-record that rewrites dozens of fixtures.
- "Ask before" lists the decisions you want to make yourself, and defines the public API so Claude can tell when a change needs a changelog entry.
Example 3: a monorepo
The root file covers the whole repo. A package with its own rules gets its own short file, which Claude Code loads once Claude works on a file in that package.
# Acme monorepo
pnpm workspaces + Turborepo. Node 22.
## Commands (run from the repo root)
- Install: `pnpm install`
- Build all: `pnpm turbo build`
- Test one package: `pnpm --filter @acme/api test`
- Lint all: `pnpm turbo lint`
## How the packages relate
- `packages/schema` holds the Zod schemas shared by `apps/web` and `apps/api`. Change the schema first, then both apps.
- After changing `packages/schema`, build it and everything that depends on it: `pnpm turbo build --filter ...@acme/schema`
- Import other packages by name. Never import from another package's `src/`.
And apps/api/CLAUDE.md:
# apps/api
- Dev: `pnpm --filter @acme/api dev` (port 4000)
- Every route validates its input with a schema from `@acme/schema`.
- No SQL in route handlers. Database access goes through `src/repo/`.
- The
--filtercommands stop Claude rebuilding and testing every package when only one changed. - "Change the schema first" records an order of work that the folder structure doesn't show.
- The per-package file keeps API-only rules out of context until Claude works in
apps/api.
Example 4: a docs site
# Acme Docs
Astro Starlight site. One Markdown file per page in `src/content/docs/`.
## Commands
- Dev: `npm run dev`
- Build (also checks internal links): `npm run build`
- Prose lint: `npm run lint:prose` (Vale, rules in `.vale/`)
## Writing rules
- Second person, present tense: "Run the command", not "The user should run the command".
- Every page opens with one sentence saying what the reader will be able to do.
- Code samples must run as written. Use the placeholder `YOUR_API_KEY`, never a real-looking key.
- The product is "Acme Cloud", not "acme cloud" or "AcmeCloud".
## Gotchas
- A new page has no sidebar link until it's added to `sidebar` in `astro.config.mjs`.
- Pages under `reference/` are generated from `openapi.yaml`. Edit the spec, not the pages.
- "Build also checks internal links" tells Claude how to verify its own work.
- The writing rules matter more than code rules here, because most changes to a docs site are prose.
- The sidebar gotcha catches a page that builds fine but has no link in the navigation.
- The note on generated pages sends edits to the spec, where the next regeneration won't overwrite them.
A CLAUDE.md Template to Copy
The format is plain Markdown, and the sections are up to you. This template uses the ones from the examples above.
# <Project name>
<One sentence: what this is, the main stack, where it runs.>
<!-- Notes for humans go in HTML comments like this one. Claude Code strips them before loading the file. -->
## Commands
- Install: `<command>`
- Dev: `<command>`
- Test (all): `<command>`
- Test (one file): `<command> <path>`
- Lint and format: `<command>`
- Build: `<command>`
## Code notes
- <Something the code doesn't make obvious, and why it matters>
- <A generated folder, or a file that must not be edited by hand>
## Gotchas
- <A mistake someone has made before>
- <A path or name that something outside the repo depends on>
## Ask before
- <Adding dependencies, changing the public API, touching migrations, ...>
Delete any section you have nothing specific to put in. A generic line still costs context on every task.
CLAUDE.md and AGENTS.md
Since version 2.1.277 (September 2026), Claude Code reads AGENTS.md on its own, but only when there's no CLAUDE.md, .claude/CLAUDE.md or CLAUDE.local.md in your working directory or any directory above it. (A user-level ~/.claude/CLAUDE.md doesn't count.) If a project has both, Claude Code reads the CLAUDE.md files only.
To load both, add an @AGENTS.md line to CLAUDE.md, or set Project instructions in /config to claude-md-and-agents-md. A symlink also works, but on Windows Anthropic says to use the import instead.
Two consequences:
- The one-line
@AGENTS.mdfiles and CLAUDE.md symlinks from our sample are no longer needed for Claude Code to see AGENTS.md. Anthropic says an existing import can stay, and an import is still how you add Claude-specific lines on top. - A
CLAUDE.local.mdcounts too. If a project relies on native AGENTS.md loading and you add one, AGENTS.md stops loading for you. Set Project instructions in/configto load both, or switch the project to a CLAUDE.md that imports@AGENTS.md.
For what goes in AGENTS.md itself, see our guide to AGENTS.md.
CLAUDE.md and AGENTS.md help agents work inside your repository. If you also publish documentation that coding agents read, different things help there, such as an llms.txt file, docs that load without JavaScript and a robots.txt that doesn't block AI agents. Our guide to agent readiness covers them, and the free agent-readiness scan checks a docs site for them. You get one scan a day without an account and five a day with a free one.
Frequently Asked Questions
How long should CLAUDE.md be? Anthropic's docs say to target under 200 lines per CLAUDE.md file, because longer files consume more context and reduce adherence. Of the open-source projects we checked, React's root CLAUDE.md is 13 lines and Excalidraw's is 34.
Should I commit CLAUDE.md? Yes, commit the project CLAUDE.md so your team shares it. Put personal preferences for one project in CLAUDE.local.md and add that file to .gitignore. Preferences for all your projects go in ~/.claude/CLAUDE.md.
Does Claude Code read AGENTS.md? Yes, since version 2.1.277 (September 2026), but only when there's no CLAUDE.md or CLAUDE.local.md in your working directory or above it. If both exist, Claude Code reads CLAUDE.md only, unless you import AGENTS.md with an @AGENTS.md line or set Project instructions in /config to load both.
Do other tools read CLAUDE.md? Some do, according to their own docs. GitHub Copilot reads it in its cloud agent, code review, Copilot CLI and its JetBrains, Eclipse and Xcode extensions. Zed, Amp (in folders without an AGENTS.md) and Devin CLI read it too. OpenAI Codex and Gemini CLI don't by default, and Cursor's docs don't mention it.
Is there still a # shortcut for adding to CLAUDE.md? No. Anthropic removed it in Claude Code 2.0.70 in December 2025. Ask Claude to add something to CLAUDE.md, or edit the file yourself through /memory.