how to write a CLAUDE.md file

How to Write a CLAUDE.md File That Claude Follows

Asfandyar Malik

Write a CLAUDE.md that Claude Code actually obeys: where it lives, what to put in, what to leave out, a copy-paste template, and how to debug an ignored rule.

6 min readMr Claude

CLAUDE.md is a plain text file Claude Code reads at the start of every session. To write a good one, run /init to get a draft, cut it down to under 200 lines, and keep only specific rules Claude can't work out from your files, like your test command and your naming habits. Then check it loaded with /context all.

What CLAUDE.md does (and doesn't do)

Claude Code starts every session with a blank memory. CLAUDE.md is how you hand it the things you'd otherwise repeat: how to run the tests, which folder holds what, what never to touch. It's loaded into context, so Claude reads it before it reads your request.

It's guidance, not enforcement. The official docs are clear about this. Claude reads it as context and can still miss a rule, especially a vague one. If something must happen every single time, the docs point you to hooks or permission rules instead. Keep that in mind when you write each line: you're asking, not commanding a machine.

Where the file goes

The docs list four main places, from broadest to narrowest:

ScopeLocationWho it's for
User~/.claude/CLAUDE.mdYou, in every project
Project./CLAUDE.md or ./.claude/CLAUDE.mdEveryone on the repo, via git
Local./CLAUDE.local.mdJust you, in this project. Add it to .gitignore.
ManagedA system folder set by an organizationCompany-wide rules

Claude also loads files from every folder above your working directory, and joins them together instead of replacing one with another. Files in subfolders load later, when Claude reads or edits something in that subfolder.

Step by step: write your first one

  1. Open Claude Code in your project folder.
  2. Run /init. It scans the project and drafts a CLAUDE.md with the build and test commands it finds. If one already exists, it suggests improvements and doesn't overwrite.
  3. Open the draft and delete anything Claude could learn by looking at the code. Long folder listings and dependency lists are the usual offenders.
  4. Add what only you know: the commands you really run, the rules your team argues about, the traps.
  5. Rewrite every vague line as something you could check. 'Format code properly' becomes 'Use 2-space indentation'.
  6. Count the lines. The docs target under 200. Longer files cost more context and Claude follows them less well.
  7. Start a fresh session and run /context all. Your file should appear under Memory files. If it isn't listed, it isn't loaded.

A template you can copy

# Project: Corner Bakery orders site

## Commands
- Install: npm install
- Run locally: npm run dev (port 3000)
- Test: npm test. Run it before saying a task is done.
- Lint: npm run lint

## How we work
- Make the smallest change that solves the problem.
- Ask before adding a new dependency.
- Never edit files in /generated. They are rebuilt by npm run build.

## Conventions
- TypeScript only. No any.
- Component files are PascalCase, everything else is kebab-case.
- Prices are stored in cents, never decimals.

## Traps
- The staging database is shared. Do not run the reset script.
- Orders over 50 items go through bulk-order.ts, not order.ts.

That's about 20 lines and it covers the four things that matter: commands, working rules, conventions and traps. Swap in your own details. The bakery is made up, so don't copy its rules.

What to put in and what to leave out

Put in the facts Claude needs every session and can't guess: commands, conventions that differ from the defaults, project quirks, and 'always do X' rules. Specific beats general every time. 'Run npm test before committing' works. 'Test your changes' doesn't.

Leave out:

  • Long procedures. A twelve-step release checklist belongs in a skill, which loads only when you ask for it. We have a walkthrough in how to create a Claude Code skill.
  • Things only true for one folder. Use a path-scoped rule in .claude/rules/ or a CLAUDE.md inside that folder.
  • Secrets. No API keys, passwords or connection strings. The file may get shared, so treat it as public.
  • Your personal taste. Put it in ~/.claude/CLAUDE.md or CLAUDE.local.md so it doesn't land on your teammates.
  • Anything that must run at a fixed moment. That's a job for a hook.

Split it up when it grows

Two tools help when one file gets crowded. The first is imports: write @docs/api-notes.md anywhere in CLAUDE.md and that file loads along with it. Be aware that imports don't save context, since the imported text loads too. They keep your files tidy, nothing more.

The second is the .claude/rules/ folder. Each markdown file there covers one topic, like testing or API design. Add a short header with paths and the rule loads only when Claude touches matching files:

---
paths:
  - "src/api/**/*.ts"
---

All API handlers return errors as { error: string, code: number }.

That keeps your always-on file small, which is the single biggest lever for getting Claude to follow it.

A worked example: fixing a rule that gets ignored

Say your file has the line 'Write good tests.' Claude writes tests, but they're thin. The line is too vague to check. Rewrite it:

- Every new function gets a test file next to it, named the same with .test.ts.
- Each test covers one normal case and one failure case.
- Run npm test and show me the result before you say you're done.

Now there's something concrete to follow and something concrete to verify. Same idea for any rule. If you can't tell by looking whether it was followed, Claude can't either.

What goes wrong and how to fix it

  • Claude ignores a rule. First run /context all and make sure the file is listed. Then look for two files that contradict each other, because when they conflict Claude may pick either one. Then shorten the file and sharpen the wording.
  • A subfolder file isn't loading. Those load on demand, after Claude reads or edits a file in that folder. Ask it to read a file there and check again.
  • You have AGENTS.md but no CLAUDE.md. The docs say Claude reads AGENTS.md in that case. To share one set of rules across tools, put @AGENTS.md inside your CLAUDE.md.
  • Instructions vanish after a long session. Anything you only said in chat can be lost when the conversation is compacted. Lasting rules belong in the file.
  • The file feels stale. Run /doctor prompt-audit. The docs describe it as a check for outdated content, missing files and contradictions. It reports, and changes nothing until you ask.

CLAUDE.md versus auto memory

Claude can also keep its own notes as it works. That's called auto memory. The difference is simple: you write CLAUDE.md, Claude writes auto memory. The first part of its index loads at session start. Use /memory to see what's there, switch it off, or open the folder. If a note is wrong, delete it. If a note matters a lot, copy it into CLAUDE.md where you control the wording.

Make it a habit

Each time Claude makes the same mistake twice, add one line to the file. After a month you'll have a short list that reflects how you really work. That beats any template, including ours. If you want a daily nudge to keep going, the 90-day accountability tracker is built for exactly that, and the tracks show what to learn after this.

If you'd rather build with other people, Build AI Business with Claude is $29 a month. It's there if you want it, and nothing above needs it.

Questions people ask

How long should a CLAUDE.md be?

The official Claude Code docs target under 200 lines per file. Longer files use more context and Claude follows them less reliably. Move special-case material into skills or path-scoped rules.

How do I create a CLAUDE.md?

Open Claude Code in your project and run /init. It drafts a file with the commands and conventions it finds. Then trim it and add the rules only you know.

Why does Claude ignore my CLAUDE.md?

Common causes are vague rules, a file that isn't loaded, two files that contradict each other, or a file that's too long. Run /context all to see which memory files loaded, then make each rule specific and checkable.

Is CLAUDE.md the same as auto memory?

No. You write CLAUDE.md. Claude writes auto memory from your corrections and preferences, and you can browse or turn it off with /memory.

Sources

Read next

Ready to learn by shipping?

Join Mr Claude for project-based Codex lessons, Claude Code workflows, SEO playbooks, and hands-on build tracks.