MAX guide 13 min read

CLAUDE.md Best Practices: How to Write Project Rules Claude Code Follows

Max points to the label Context engineering for code below the title CLAUDE.md Best Practices

What are the CLAUDE.md best practices?

Keep a short CLAUDE.md in your project that holds only what Claude Code can’t learn from your code, such as how to run the tests, your conventions and what must never change, and write each line so you can check it.

That puts your rules in front of Claude Code in every session. In our test, one rule line in CLAUDE.md took Claude Code from following the rule in 0 of 10 runs to 10 of 10.

You tell Claude Code to use your own request helper instead of axios (a popular library for web requests). It does. In a new session, axios is back. CLAUDE.md best practices fix this: you write the rule once, in a plain text file Claude Code reads at the start of every session. The wider field is Context Engineering for Code.

What you need, and where Claude Code looks for rules

You need Claude Code and a project with at least one rule your team follows by heart: a test style, a banned library, a date format. If you are still choosing an agent, our guide to choosing between Claude Code, Codex, Cursor and Devin compares them on real engineering work.

If the terms are new, agentic coding means the agent plans, edits files and runs commands on its own, while vibe coding means describing what you want and accepting what comes back.

CLAUDE.md is a Markdown file: plain text with simple marks such as # for a heading and - for a list item. You can have several of them. Anthropic’s memory documentation lists where Claude Code looks:

FileWhere it livesWhat it is for
CLAUDE.md or .claude/CLAUDE.mdProject folderRules for everyone on the project. Commit it so the team shares it.
CLAUDE.local.mdProject folderYour own preferences for this project. Add it to .gitignore, the list of files Git leaves out.
~/.claude/CLAUDE.mdYour home folderYour preferences for all projects.
CLAUDE.md in a subfolderThat subfolderRules for one part of the project.
.claude/rules/*.mdProject folderSmaller rule files, one topic each, optionally limited to certain paths.
Managed fileSet by your IT teamRules for the whole organization.

Two loading rules explain most surprises. The project file and the files in the folders above your working directory (the folder where you start Claude Code) load at launch. Files in subfolders load on demand, when Claude Code reads, writes or edits a file there.

Claude Code combines every file it finds. None replaces another.

Where Claude Code finds its rules. Source: Anthropic's memory documentation, read 30 October 2026.

Why Claude Code ignores your rules

Claude Code usually breaks a convention for one of three reasons. Nobody wrote the convention down, the file sits where Claude Code doesn’t load it, or the file is too long or too vague to act on. From the outside all three look the same: the agent ignores you.

In our test below, with no rule in sight, Claude Code wrote the delivery date in two different styles from run to run.

On the third reason, Anthropic’s documentation says longer files “consume more context and reduce adherence”. It also calls CLAUDE.md context, not enforced configuration, with no guarantee of strict compliance. Step 2 covers that.

A rules file is the cheapest form of context engineering for code. Our explainer on how context engineering shapes what a coding assistant writes covers why it changes the output.

Step 1: Create the project file

Start Claude Code in your project folder and run /init, a command you type inside a session. It generates a starting CLAUDE.md automatically. If the project already has one, /init suggests improvements and doesn’t overwrite it.

The file belongs in the project root as CLAUDE.md, or in .claude/CLAUDE.md. Commit it, so everyone on the team gets the same rules. Treat the generated text as a draft. Step 2 shows what to keep.

Step 2: Write what the code doesn’t show

Put in the file only what Claude Code can’t work out by reading your code. Its best-practices page lists the exact commands to test, typecheck (verify the data types in your code) and lint (scan it for style problems), and how to run a single test file.

It also lists code that looks wrong but is right, decisions that aren’t in the repo, and anything you have corrected twice.

Leave out your architecture and folder layout. Claude Code reads the repo itself.

Write each line so you can check it. The documentation contrasts “Run npm test before committing” with “Test your changes”. Use headers and bullets: it says organized sections are easier to follow than dense paragraphs.

Keep it short, because every line takes room in the agent’s context window. Claude Code targets under 200 lines per CLAUDE.md and warns you at startup, and in /status, when a file is longer.

Start with a handful of lines and add one when Claude Code makes the same mistake a second time. Writing IMPORTANT in capitals probably won’t fix a line it missed.

A starting file. Replace each part in angle brackets with your own:

# Project rules

## Commands
- Run all tests: `<your test command>`
- Run one test file: `<your single-test command>`
- Before you finish, run `<your lint command>` and fix what it reports.

## Conventions
- Web requests go through `<your request helper>`. Never add `axios`.
- Dates shown to customers are written as DD.MM.YYYY, for example 26.10.2026.
- <one naming or error-handling rule your team follows by heart>

## Never change
- `<generated folder or migrations folder>`: edit the source, then regenerate.

The file carries instructions only; tools come in separately, for example through the model context protocol.

Save the file and start a new session. Step 4 shows how to check that Claude Code loaded it.

Step 3: Layer rules from the repo root to subfolders

This step and the next are for teams and monorepos (one repository that holds several projects). If one small project is all you work on, you can stop here and skim the rest later.

Put the rules every folder shares in the root file. Give a subfolder its own CLAUDE.md for rules only that part needs. If two lines conflict, the documentation says “Claude may pick one arbitrarily”, so a narrower file should add rules and never contradict the root.

To limit a rule to certain files, save it in .claude/rules/ with a paths field in the frontmatter, the settings block at the top of the file:

---
paths:
  - "src/api/**/*.ts"
---
- Endpoints return errors as JSON with a single `error` field.

The paths field takes a list or a comma-separated string, and it is the only field Claude Code reads in a rule. The rule loads when Claude Code uses its Read, Write or Edit tool on a matching file. A rule without paths loads at launch, with the same priority as .claude/CLAUDE.md.

Long reference material can live in another file. A line such as @docs/testing.md imports it. Relative paths resolve from the file that holds the import, imports can nest up to four hops, and the first import from outside the working directory asks for your approval.

An import saves no context: the imported file loads together with the file that imports it.

Keep personal rules out of the shared file: CLAUDE.local.md in the project, and ~/.claude/CLAUDE.md or ~/.claude/rules/ in your home folder. If a personal rule and a project rule conflict, Claude may follow either. In a monorepo, the claudeMdExcludes setting skips other teams’ CLAUDE.md files by path or glob pattern.

Step 4: Check that Claude Code loaded the file

Skip the check and you can spend a week asking why Claude Code ignores your rules when the file sits where it never loads.

  • Run /context and look under Memory files. A subfolder CLAUDE.md won’t appear there, because it loads on demand.
  • Run /memory. It lists your CLAUDE.md, CLAUDE.local.md and other memory file locations, and toggles auto memory: notes Claude Code saves for itself, on by default in local sessions. Those notes are not your rules file.
  • To log which files load, when and why, add an InstructionsLoaded hook, a command Claude Code runs at a fixed moment.
  • A root CLAUDE.md survives /compact, the command that shortens a long conversation: Claude Code re-reads it from disk.

Then try the rule itself. Make a request that your rule should change and see whether it did.

Our test: one rule line, from 0 of 10 runs to 10 of 10

We ran that check on 30 October 2026, with Claude Code 2.1.314 and the model claude-sonnet-5-5. One request, always the same: add the expected delivery date to an order confirmation email. One rule: “Dates shown to customers are always written as DD.MM.YYYY, for example 26.10.2026.” We ran each setup 10 times.

SetupRuns with DD.MM.YYYYOther formats
CLAUDE.md with three general rules and no date rule0/10“03 November 2026” style 6, 2026-11-03 4
The same CLAUDE.md plus the date rule10/10none

Without the rule, Claude Code switched between two date styles. With it, Claude Code followed the rule every time. All runs finished and produced working code.

The limits: one task, one rule, one model, 10 runs per setup. These runs used project settings only, so our personal ~/.claude/CLAUDE.md did not load (checked with a probe run). It has no rule about customer dates anyway.

Mistakes that make Claude Code ignore your file

MistakeWhy it failsFix
An essay: history, architecture, folder layoutThe rules get buried. Anthropic’s documentation says longer files reduce adherence, and Claude Code reads the repo itself.Rules only, under 200 lines. Cut what Claude Code can read from the code, and use a linter for style rules instead of pasting a style guide. /doctor can also trim content it can derive.
Vague lines such as “Test your changes”Claude Code can’t tell whether it complied.Name the command: “Run npm test before committing”.
The same rule in several filesConflicting lines: “Claude may pick one arbitrarily”. A user rule and a project rule may be followed either way.Keep each rule in one place. Run /doctor prompt-audit (version 2.1.283 or later) to flag outdated or conflicting lines.
Treating the file as enforcementIt is context, not enforced configuration. Anthropic’s example “Always typecheck before you finish” works “most of the time, not every time”.For a rule with no exceptions, add a PreToolUse hook that exits with code 2, a permissions.deny rule, a test or an automated check in your CI pipeline. The more files an agent rewrites unattended, as in code migration with AI, the more a single missed rule costs.
Expecting a subfolder file to load at launchIt loads on demand, and /context doesn’t list it. On-demand files are also limited to 40,000 characters on a 200K-token context window (since version 2.1.309).Keep subfolder files short. Check them after Claude Code has read a file in that folder.

Add a line only after the second mistake

Treat CLAUDE.md as a log of repeated mistakes, not a wish list. A line earns its place when Claude Code gets something wrong twice. A line your linter or tests now enforce can go.

Run /doctor prompt-audit now and then to flag outdated or conflicting lines. Block-level HTML comments are removed before the text reaches the model, so notes for your teammates cost no context.

If your team also uses AGENTS.md

AGENTS.md is a plain Markdown rules file that several other agents share. agents.md calls it a “README for agents”, and OpenAI released it in August 2025, according to the Linux Foundation. Codex, Cursor and the Copilot cloud agent read it directly. Gemini CLI reads it after you set context.fileName in settings.json.

Since version 2.1.277 (September 2026), Claude Code reads AGENTS.md too, but only when the project has no CLAUDE.md. If both files exist, by default it reads CLAUDE.md alone. A CLAUDE.local.md counts as a CLAUDE.md.

Will Claude Code read your AGENTS.md? Source: vendor documentation, read 30 October 2026.

The fix is one line. Start CLAUDE.md with @AGENTS.md and put Claude-only lines below it. The @ line is an import: Claude Code loads the named file at that spot. A symlink also works (ln -s AGENTS.md CLAUDE.md), but on Windows use the import.

We tested it with the same date rule, 10 runs per setup:

SetupAgentRuns with DD.MM.YYYYOther formats
Only AGENTS.md, with the ruleClaude Code10/10none
CLAUDE.md without the rule + AGENTS.md with the ruleClaude Code0/10“03 November 2026” style 7, 2026-11-03 3
CLAUDE.md starting with @AGENTS.md + AGENTS.md with the ruleClaude Code10/10none
Only AGENTS.md, with the ruleCodex (CLI 0.159.2, model gpt-6.1-sol, effort high)10/10none
Same request, 60 runs: the rule worked in every setup where the agent read the file that held it.

The rule worked whenever the agent read the file that held it. With both files and no import, the result matched having no rule at all. The documentation says Claude Code then reads only CLAUDE.md. We did not log which files loaded, so that step comes from the documentation.

Our Codex runs also loaded our personal ~/.codex/AGENTS.md, which has no date rule.

A prompt to set this up with Claude Code

Paste this into Claude Code. Replace each bracketed placeholder with the value from your repo before sending.

You are helping me write the project rules file for Claude Code in this repo.

Step 1 - Create the file:
If the repo has no CLAUDE.md, create one at the repo root.
If one exists, suggest improvements and do not overwrite it.

Step 2 - Write only what the code does not show (under 200 lines):
- Commands: [test command, single-test command, typecheck and lint command]
- Conventions: [list: banned libraries, required patterns, naming]
- Never change: [list: generated folders, migrations]
Each line must be concrete enough to check. Do not describe the architecture or folder layout.

Step 3 - Layering (skip if there is no monorepo):
- Subfolders that need their own rules: [list]
For each, propose a subfolder CLAUDE.md, or a .claude/rules/ file with a paths field.

Step 4 - Verify:
Tell me how to check what Claude Code loaded (/context and /memory).
Then propose one request whose result changes if a rule was loaded,
so I can run it before and after.

Rules for you: do not invent rules I did not list. Where a placeholder is unfilled,
leave a TODO marker. Remind me that this file is context, not enforcement:
for any rule without exceptions, propose a hook, a deny rule, a test or a CI check.

Cheat sheet

The article on one page: open it full size, save it or share it.

CLAUDE.md best practices cheat sheet: where Claude Code reads rules, a CLAUDE.md to copy, how to keep it followed, and a 0/10 to 10/10 test
PNG

AI-assisted content, human-reviewed. Images AI-generated. Editorial Standards · Our Editors