Become a Pro / website · becomeapro.ai / content / lessons / knowledge-work / using-ai / claude-md-guide.mdx
| 1 | --- |
| 2 | title: "How to write a CLAUDE.md that Claude Code follows (with examples)" |
| 3 | description: "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." |
| 4 | level: Beginner |
| 5 | minutes: 10 |
| 6 | updated: 2026-12-03 |
| 7 | order: 2 |
| 8 | sources: |
| 9 | - https://code.claude.com/docs/en/memory |
| 10 | - https://code.claude.com/docs/en/skills |
| 11 | - https://code.claude.com/docs/en/best-practices |
| 12 | - https://code.claude.com/docs/en/hooks-guide |
| 13 | - https://code.claude.com/docs/en/commands |
| 14 | --- |
| 15 | |
| 16 | Every 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. |
| 17 | |
| 18 | This guide follows Anthropic's memory documentation and turns it into a file you can start from today. |
| 19 | |
| 20 | ## Create your first one with /init |
| 21 | |
| 22 | In your project folder, start Claude Code and run: |
| 23 | |
| 24 | ```text |
| 25 | /init |
| 26 | ``` |
| 27 | |
| 28 | Claude 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. If it finds instruction files from Codex or Gemini CLI, it offers to carry them over. 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. |
| 29 | |
| 30 | ## Where CLAUDE.md files go |
| 31 | |
| 32 | You can have more than one, each with a different reach. Claude Code loads them from broadest to most specific: |
| 33 | |
| 34 | | File | Who it's for | Shared through git? | |
| 35 | | --- | --- | --- | |
| 36 | | `~/.claude/CLAUDE.md` | You, in every project | No | |
| 37 | | `./CLAUDE.md` or `./.claude/CLAUDE.md` | Everyone working on this project | Yes | |
| 38 | | `./CLAUDE.local.md` | You, in this project only (add it to `.gitignore`) | No | |
| 39 | |
| 40 | Organizations can also deploy a managed `CLAUDE.md` that applies to every user. |
| 41 | |
| 42 | <Diagram caption="Claude Code reads every CLAUDE.md from the broadest to the most specific. Files read later sit closer to your work."> |
| 43 | <svg viewBox="0 0 600 250" role="img" aria-label="Four stacked boxes: managed file, your user file, the project file, and your local file, with an arrow showing they load in that order"> |
| 44 | <rect x="40" y="20" width="400" height="44" rx="8" fill="none" stroke="currentColor" strokeWidth="2" /> |
| 45 | <text x="240" y="48" fontSize="16" textAnchor="middle" fill="currentColor">Managed CLAUDE.md (your organization)</text> |
| 46 | <rect x="40" y="76" width="400" height="44" rx="8" fill="none" stroke="currentColor" strokeWidth="2" /> |
| 47 | <text x="240" y="104" fontSize="16" textAnchor="middle" fill="currentColor">~/.claude/CLAUDE.md (you, every project)</text> |
| 48 | <rect x="40" y="132" width="400" height="44" rx="8" fill="none" stroke="currentColor" strokeWidth="2" /> |
| 49 | <text x="240" y="160" fontSize="16" textAnchor="middle" fill="currentColor">./CLAUDE.md (the whole team)</text> |
| 50 | <rect x="40" y="188" width="400" height="44" rx="8" className="soft" stroke="currentColor" strokeWidth="2" /> |
| 51 | <text x="240" y="216" fontSize="16" textAnchor="middle" fill="currentColor">./CLAUDE.local.md (you, this project)</text> |
| 52 | <line x1="490" y1="30" x2="490" y2="215" stroke="currentColor" strokeWidth="2" /> |
| 53 | <path d="M480 203 L490 222 L500 203" fill="none" stroke="currentColor" strokeWidth="2" /> |
| 54 | <text x="520" y="110" fontSize="14" textAnchor="middle" fill="currentColor">Read</text> |
| 55 | <text x="520" y="130" fontSize="14" textAnchor="middle" fill="currentColor">in this</text> |
| 56 | <text x="520" y="150" fontSize="14" textAnchor="middle" fill="currentColor">order</text> |
| 57 | </svg> |
| 58 | </Diagram> |
| 59 | |
| 60 | Claude 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. |
| 61 | |
| 62 | ## What to put in it |
| 63 | |
| 64 | Anthropic's guidance is to treat `CLAUDE.md` as the place you write down what you'd otherwise re-explain. Add to it when: |
| 65 | |
| 66 | - Claude makes the same mistake a second time |
| 67 | - A code review catches something Claude should have known about the codebase |
| 68 | - You type the same correction you typed last session |
| 69 | - A new teammate would need the same context to be productive |
| 70 | |
| 71 | Keep it to facts that matter in every session. A long multi-step procedure fits better in a skill, and rules for one part of the codebase fit better in a path-scoped rule (below). |
| 72 | |
| 73 | ## Write instructions Claude can check |
| 74 | |
| 75 | Claude 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: |
| 76 | |
| 77 | | Vague | Concrete | |
| 78 | | --- | --- | |
| 79 | | Format code properly | Use 2-space indentation | |
| 80 | | Test your changes | Run `npm test` before committing | |
| 81 | | Keep files organized | API handlers live in `src/api/handlers/` | |
| 82 | |
| 83 | Aim 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. |
| 84 | |
| 85 | ## A complete example |
| 86 | |
| 87 | Here'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: |
| 88 | |
| 89 | ```markdown |
| 90 | # Commands |
| 91 | - Install: `pnpm install` (never npm or yarn) |
| 92 | - Dev server: `pnpm dev` (http://localhost:3000) |
| 93 | - Run one test file: `pnpm vitest run path/to/file.test.ts` |
| 94 | - Before a PR: `pnpm lint && pnpm tsc --noEmit && pnpm test` |
| 95 | |
| 96 | # Conventions |
| 97 | - Database access only through `src/lib/db.ts`, never a new Prisma client |
| 98 | - Money is stored in cents as integers. Never use floats for amounts. |
| 99 | - Throw errors with the helpers in `src/lib/errors.ts`, never raw strings |
| 100 | - Ask before adding a dependency |
| 101 | |
| 102 | # Gotchas |
| 103 | - `src/legacy/` is still live, but don't copy its patterns into new code |
| 104 | - Integration tests need `DATABASE_URL` set; see `.env.example` |
| 105 | - `src/components/ui/` is generated by shadcn/ui; don't edit it by hand |
| 106 | ``` |
| 107 | |
| 108 | Every line is something Claude can act on or check, and none of it is visible from the file tree alone. |
| 109 | |
| 110 | ## What to leave out |
| 111 | |
| 112 | - **Your folder layout and architecture.** Anthropic's best practices now say Claude reads the repo itself, so only write down what isn't in there. |
| 113 | - **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. |
| 114 | - **Things Claude would do anyway**, like "write clean code". |
| 115 | - **Your personal preferences.** Put those in `CLAUDE.local.md` or `~/.claude/CLAUDE.md`, not in the shared file. |
| 116 | |
| 117 | ## Pull in other files with @imports |
| 118 | |
| 119 | A `CLAUDE.md` can import other files with `@path/to/file`. The imported file loads alongside it: |
| 120 | |
| 121 | ```markdown |
| 122 | See @README.md for an overview and @package.json for the available scripts. |
| 123 | |
| 124 | ## Git workflow |
| 125 | - @docs/git-instructions.md |
| 126 | ``` |
| 127 | |
| 128 | Relative 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. |
| 129 | |
| 130 | <Callout type="tip"> |
| 131 | HTML 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. |
| 132 | </Callout> |
| 133 | |
| 134 | ## Split rules by path with .claude/rules/ |
| 135 | |
| 136 | For 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: |
| 137 | |
| 138 | ```markdown |
| 139 | --- |
| 140 | paths: |
| 141 | - "src/api/**/*.ts" |
| 142 | --- |
| 143 | |
| 144 | # API rules |
| 145 | - Validate every request body with the schemas in `src/api/schemas/`. |
| 146 | - Return errors as `{ error: { code, message } }`. |
| 147 | ``` |
| 148 | |
| 149 | ## Already have an AGENTS.md? |
| 150 | |
| 151 | Current versions of Claude Code 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. A `CLAUDE.local.md` counts as a `CLAUDE.md` here, so adding one stops Claude from reading your `AGENTS.md` unless you import it. Codex's `AGENTS.override.md` is never read by Claude Code. To keep one shared file for every tool, put this at the top of your `CLAUDE.md` and add Claude-specific lines below it: |
| 152 | |
| 153 | ```markdown |
| 154 | @AGENTS.md |
| 155 | ``` |
| 156 | |
| 157 | [How to set up AGENTS.md for Codex](/knowledge-work/using-ai/agents-md-codex) covers the AGENTS.md side. |
| 158 | |
| 159 | ## Check that it loaded |
| 160 | |
| 161 | - Run `/context all` in a session and look under **Memory files**. If a file isn't listed there, Claude can't see it. |
| 162 | - Run `/memory` to see every memory file location, open one in your editor, or turn auto memory on or off. |
| 163 | - A subfolder `CLAUDE.md` won't appear under Memory files until Claude opens a file in that folder. |
| 164 | |
| 165 | ## When Claude still doesn't follow it |
| 166 | |
| 167 | - Make the line more specific, and check for a conflicting line in another `CLAUDE.md` or rule file. |
| 168 | - For something that must happen every time, like formatting after each edit or blocking a command, use a hook instead. Hooks run as shell commands at fixed points and don't depend on Claude choosing to follow them. |
| 169 | - 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". |
| 170 | |
| 171 | Keep the file alive: when Claude gets something wrong that a line would have prevented, add the line. |