Branchwell

Become a Pro / website · becomeapro.ai / content / tutorials / claude / claude-md-guide.mdx

Raw 7.4 KB on 6c7ce85c3441baadd17890ab9cc9bf8ed5112901
1---
2title: "How to write a CLAUDE.md that Claude Code follows (with examples)"
3description: "Where CLAUDE.md files go, what to put in them, a full example, and how to check that Claude Code actually loaded and follows your instructions."
4level: Beginner
5minutes: 10
6publishedAt: 2026-10-31
7---
8
9Every Claude Code session starts with a fresh context window. A `CLAUDE.md` file is how you carry knowledge from one session to the next: build commands, conventions, where things live, and the rules you'd otherwise repeat every day. Claude reads it at the start of every session.
10
11This guide follows Anthropic's [memory documentation](https://code.claude.com/docs/en/memory) and turns it into a file you can start from today.
12
13## Create your first one with /init
14
15In your project folder, start Claude Code and run:
16
17```text
18/init
19```
20
21Claude reads your codebase and writes a starting `CLAUDE.md` with the build commands, test instructions and conventions it finds. If a `CLAUDE.md` already exists, `/init` suggests improvements instead of overwriting it. Treat the result as a draft. Anthropic's own advice is that from there the work is mostly removing lines: the ones worth keeping are the ones Claude couldn't work out on its own.
22
23## Where CLAUDE.md files go
24
25You can have more than one, each with a different reach. Claude Code loads them from broadest to most specific:
26
27| File | Who it's for | Shared through git? |
28| --- | --- | --- |
29| `~/.claude/CLAUDE.md` | You, in every project | No |
30| `./CLAUDE.md` or `./.claude/CLAUDE.md` | Everyone working on this project | Yes |
31| `./CLAUDE.local.md` | You, in this project only (add it to `.gitignore`) | No |
32
33Organizations can also deploy a managed `CLAUDE.md` that applies to every user.
34
35Claude Code loads the `CLAUDE.md` files in your working directory and every folder above it when a session starts. A `CLAUDE.md` inside a subfolder loads later, the first time Claude reads or edits a file in that folder. That makes subfolder files a good place for rules that only matter there.
36
37## What to put in it
38
39Anthropic's guidance is to treat `CLAUDE.md` as the place you write down what you'd otherwise re-explain. Add to it when:
40
41- Claude makes the same mistake a second time
42- A code review catches something Claude should have known about the codebase
43- You type the same correction you typed last session
44- A new teammate would need the same context to be productive
45
46Keep it to facts that matter in every session. A long multi-step procedure fits better in a [skill](https://code.claude.com/docs/en/skills), and rules for one part of the codebase fit better in a path-scoped rule (below).
47
48## Write instructions Claude can check
49
50Claude treats `CLAUDE.md` as context, not as enforced configuration, so how you write each line affects how reliably it's followed. The docs' rule of thumb is to be concrete enough to verify:
51
52| Vague | Concrete |
53| --- | --- |
54| Format code properly | Use 2-space indentation |
55| Test your changes | Run `npm test` before committing |
56| Keep files organized | API handlers live in `src/api/handlers/` |
57
58Aim for under 200 lines per file. Longer files take up more context and are followed less closely. Group related lines under headings, and remove instructions that contradict each other: when two lines disagree, Claude may pick either.
59
60## A complete example
61
62Here's a `CLAUDE.md` for a typical Next.js app. It sticks to what Claude can't work out by reading the code: how to check its work, decisions the team made, and places where the code is misleading. Copy the shape, not the details:
63
64```markdown
65# Commands
66- Install: `pnpm install` (never npm or yarn)
67- Dev server: `pnpm dev` (http://localhost:3000)
68- Run one test file: `pnpm vitest run path/to/file.test.ts`
69- Before a PR: `pnpm lint && pnpm tsc --noEmit && pnpm test`
70
71# Conventions
72- Database access only through `src/lib/db.ts`, never a new Prisma client
73- Money is stored in cents as integers. Never use floats for amounts.
74- Throw errors with the helpers in `src/lib/errors.ts`, never raw strings
75- Ask before adding a dependency
76
77# Gotchas
78- `src/legacy/` is still live, but don't copy its patterns into new code
79- Integration tests need `DATABASE_URL` set; see `.env.example`
80- `src/components/ui/` is generated by shadcn/ui; don't edit it by hand
81```
82
83Every line is something Claude can act on or check, and none of it is visible from the file tree alone.
84
85## What to leave out
86
87- **Your folder layout and architecture.** Anthropic's [best practices](https://code.claude.com/docs/en/best-practices) now say Claude reads the repo itself, so only write down what isn't in there.
88- **IMPORTANT and ALL CAPS.** Current models follow a plainly worded line. If a line keeps getting missed, the file is probably too long, or the rule needs a hook.
89- **Things Claude would do anyway**, like "write clean code".
90- **Your personal preferences.** Put those in `CLAUDE.local.md` or `~/.claude/CLAUDE.md`, not in the shared file.
91
92## Pull in other files with @imports
93
94A `CLAUDE.md` can import other files with `@path/to/file`. The imported file loads alongside it:
95
96```markdown
97See @README.md for an overview and @package.json for the available scripts.
98
99## Git workflow
100- @docs/git-instructions.md
101```
102
103Relative paths resolve from the file that contains the import, and imports can nest up to four levels deep. Imports help organize a long file, but they don't save context: imported files load in full.
104
105<Callout type="tip">
106HTML comments in a CLAUDE.md, like `<!-- why this rule exists -->`, are stripped before Claude sees the file. Use them for notes to your teammates that shouldn't cost context.
107</Callout>
108
109## Split rules by path with .claude/rules/
110
111For bigger projects, put topic files in `.claude/rules/`, such as `testing.md` or `api-design.md`. Rules without frontmatter load every session. Add a `paths` field and a rule only loads when Claude works on matching files:
112
113```markdown
114---
115paths:
116 - "src/api/**/*.ts"
117---
118
119# API rules
120- Validate every request body with the schemas in `src/api/schemas/`.
121- Return errors as `{ error: { code, message } }`.
122```
123
124## Already have an AGENTS.md?
125
126Recent versions of Claude Code (v2.1.277 and later) read `AGENTS.md` on their own when your project has no `CLAUDE.md`, so a repo set up for Codex or other agents works as it is. If you have both, Claude reads only `CLAUDE.md` by default. To keep one shared file for every tool, put this at the top of your `CLAUDE.md` and add Claude-specific lines below it:
127
128```markdown
129@AGENTS.md
130```
131
132[How to set up AGENTS.md for Codex](/chatgpt/agents-md-codex) covers the AGENTS.md side.
133
134## Check that it loaded
135
136- Run `/context` in a session and look under **Memory files**. If a file isn't listed there, Claude can't see it.
137- Run `/memory` to see every memory file location and open one in your editor.
138- A subfolder `CLAUDE.md` won't appear under Memory files until Claude opens a file in that folder.
139
140## When Claude still doesn't follow it
141
142- Make the line more specific, and check for a conflicting line in another `CLAUDE.md` or rule file.
143- For something that must happen every time, like formatting after each edit or blocking a command, use a [hook](https://code.claude.com/docs/en/hooks-guide) instead. Hooks run as shell commands at fixed points and don't depend on Claude choosing to follow them.
144- If you ask Claude to "remember" something, it saves it to auto memory, its own notes. To put it in `CLAUDE.md`, say "add this to CLAUDE.md".
145
146Keep the file alive: when Claude gets something wrong that a line would have prevented, add the line.