Branchwell

Become a Pro / website · becomeapro.ai / content / lessons / knowledge-work / using-ai / claude-md-guide.mdx

Raw 7.4 KB on 59dac1cc932d622dfc4d6c274a9ed8e9abcc5e5e
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
6updated: 2026-10-31
7order: 2
8---
9
10Every 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.
11
12This guide follows Anthropic's [memory documentation](https://code.claude.com/docs/en/memory) and turns it into a file you can start from today.
13
14## Create your first one with /init
15
16In your project folder, start Claude Code and run:
17
18```text
19/init
20```
21
22Claude 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.
23
24## Where CLAUDE.md files go
25
26You can have more than one, each with a different reach. Claude Code loads them from broadest to most specific:
27
28| File | Who it's for | Shared through git? |
29| --- | --- | --- |
30| `~/.claude/CLAUDE.md` | You, in every project | No |
31| `./CLAUDE.md` or `./.claude/CLAUDE.md` | Everyone working on this project | Yes |
32| `./CLAUDE.local.md` | You, in this project only (add it to `.gitignore`) | No |
33
34Organizations can also deploy a managed `CLAUDE.md` that applies to every user.
35
36Claude 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.
37
38## What to put in it
39
40Anthropic's guidance is to treat `CLAUDE.md` as the place you write down what you'd otherwise re-explain. Add to it when:
41
42- Claude makes the same mistake a second time
43- A code review catches something Claude should have known about the codebase
44- You type the same correction you typed last session
45- A new teammate would need the same context to be productive
46
47Keep 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).
48
49## Write instructions Claude can check
50
51Claude 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:
52
53| Vague | Concrete |
54| --- | --- |
55| Format code properly | Use 2-space indentation |
56| Test your changes | Run `npm test` before committing |
57| Keep files organized | API handlers live in `src/api/handlers/` |
58
59Aim 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.
60
61## A complete example
62
63Here'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:
64
65```markdown
66# Commands
67- Install: `pnpm install` (never npm or yarn)
68- Dev server: `pnpm dev` (http://localhost:3000)
69- Run one test file: `pnpm vitest run path/to/file.test.ts`
70- Before a PR: `pnpm lint && pnpm tsc --noEmit && pnpm test`
71
72# Conventions
73- Database access only through `src/lib/db.ts`, never a new Prisma client
74- Money is stored in cents as integers. Never use floats for amounts.
75- Throw errors with the helpers in `src/lib/errors.ts`, never raw strings
76- Ask before adding a dependency
77
78# Gotchas
79- `src/legacy/` is still live, but don't copy its patterns into new code
80- Integration tests need `DATABASE_URL` set; see `.env.example`
81- `src/components/ui/` is generated by shadcn/ui; don't edit it by hand
82```
83
84Every line is something Claude can act on or check, and none of it is visible from the file tree alone.
85
86## What to leave out
87
88- **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.
89- **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.
90- **Things Claude would do anyway**, like "write clean code".
91- **Your personal preferences.** Put those in `CLAUDE.local.md` or `~/.claude/CLAUDE.md`, not in the shared file.
92
93## Pull in other files with @imports
94
95A `CLAUDE.md` can import other files with `@path/to/file`. The imported file loads alongside it:
96
97```markdown
98See @README.md for an overview and @package.json for the available scripts.
99
100## Git workflow
101- @docs/git-instructions.md
102```
103
104Relative 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.
105
106<Callout type="tip">
107HTML 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.
108</Callout>
109
110## Split rules by path with .claude/rules/
111
112For 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:
113
114```markdown
115---
116paths:
117 - "src/api/**/*.ts"
118---
119
120# API rules
121- Validate every request body with the schemas in `src/api/schemas/`.
122- Return errors as `{ error: { code, message } }`.
123```
124
125## Already have an AGENTS.md?
126
127Recent 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:
128
129```markdown
130@AGENTS.md
131```
132
133[How to set up AGENTS.md for Codex](/knowledge-work/using-ai/agents-md-codex) covers the AGENTS.md side.
134
135## Check that it loaded
136
137- Run `/context` in a session and look under **Memory files**. If a file isn't listed there, Claude can't see it.
138- Run `/memory` to see every memory file location and open one in your editor.
139- A subfolder `CLAUDE.md` won't appear under Memory files until Claude opens a file in that folder.
140
141## When Claude still doesn't follow it
142
143- Make the line more specific, and check for a conflicting line in another `CLAUDE.md` or rule file.
144- 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.
145- 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".
146
147Keep the file alive: when Claude gets something wrong that a line would have prevented, add the line.