← Back to Blog🇮🇹 Leggi in Italiano

How to Write a Good CLAUDE.md for Claude Code: A Practical Guide

What to put in CLAUDE.md, what to cut, and how to check Claude Code actually follows it: a practical guide with a before/after example and 5-minute checks.

CLAUDE.md is the first file Claude Code reads when a session starts, and in most projects it's also the worst-written one. The usual story: you start with /init, then add a line every time something goes wrong, then a paragraph, then a log of changes. A few weeks later the file is long, self-contradicting, and Claude seems to ignore it. It isn't ignoring it: there's just too much in there, and the parts that matter get lost.

This guide covers the practical side: where the file is loaded from, what to put in it, what to cut, and how to check it works. Every Claude Code behavior mentioned here was checked against the official documentation (the "How Claude remembers your project" page on code.claude.com) at the time of publishing.

$ /initWiring knowledge graph... doneStarting MCP servers... done
01
Know where it gets loaded from

There isn't just one CLAUDE.md. Claude Code reads several, each with its own scope, and concatenates them rather than letting one override another:

ScopeLocationUse it for
User~/.claude/CLAUDE.mdPersonal preferences across all your projects
Project./CLAUDE.md or ./.claude/CLAUDE.mdProject rules, shared with the team through git
Local./CLAUDE.local.mdYour own preferences for that project, added to .gitignore

There's also a "managed policy" level controlled by company IT, which only matters inside organizations.

Two practical details. First, at launch Claude Code loads the files in the directory you start it from and in every directory above it; CLAUDE.md files in subdirectories load only when Claude reads or edits a file in that subdirectory. Second, order matters: instructions closer to your working directory are read last, and CLAUDE.local.md is appended after the CLAUDE.md at the same level.

The rule of thumb that follows: your personal habits (language, answer style) go in the user file, rules that anyone working on the project must follow go in the project file.

02
Start with /init, then cut

The /init command analyzes your codebase and generates a starting CLAUDE.md with the build commands, test instructions, and conventions it can discover. If the file already exists, it suggests improvements instead of overwriting it.

That's a starting point, not a finished file. /init describes what it sees in the code, while the real value of the file is what Claude can't discover on its own: why you dropped a library, which folder must not be touched, which command has to run before a commit. After /init, go through the file line by line and ask: "Would Claude figure this out anyway by opening the code?" If yes, you can probably delete it.

03
Write rules you can verify

Claude treats CLAUDE.md as context, not enforced configuration: it reads it and tries to follow it, but strict compliance isn't guaranteed, especially with vague or conflicting instructions. That's why the shape of each instruction matters more than how many you have.

A good instruction can be checked by looking at the result:

  • "Use 2-space indentation" instead of "Format code properly"
  • "Run npm test before committing" instead of "Test your changes"
  • "API handlers live in src/api/handlers/" instead of "Keep files organized"

Three categories pay off the most: commands (build, test, migrations, deploy), conventions (naming, folders, libraries to use and to avoid), and architecture constraints (what must not change and why, in one line). If you use MCP servers, it's worth noting which tool to use for what: the MCP protocol guide covers that side.

One more habit: if two instructions contradict each other, Claude may pick one arbitrarily. When you add a rule, check whether an existing one says the opposite.

04
Remove whatever isn't a rule

This is where most of the space comes back. These don't belong in CLAUDE.md:

  • session history ("on March 12 we fixed the login"): that's meeting minutes, not an instruction;
  • changelogs: git already has them, and Claude can read it when needed;
  • descriptions of things the code already shows ("the project uses React, components are in components/");
  • generic advice ("write clean code"): it doesn't change behavior, it just takes up room.

Here's a realistic example of a bloated file:

# Order Manager

Very important project for the client. Write clean,
high-quality code and follow best practices.

## History
- 03/12: fixed login bug, the token expired too early
- 03/14: added reports page, the client wants pie charts
- 03/20: tried Prisma, then went back to Drizzle

## Structure
The project uses React. Components are in the components folder.
APIs are in src/api. There's a utils.ts file with various helpers.

And the same content rewritten as rules:

# Order Manager

## Commands
- Dev: `pnpm dev`
- Test: `pnpm test`, run before every commit
- Always use `pnpm`, never `npm` or `yarn`

## Rules
- ORM: Drizzle. Don't introduce Prisma (evaluated and rejected,
  rationale in `docs/decisions/orm.md`)
- One handler per resource in `src/api/handlers/`
- Dates always stored in UTC, timezone conversion only in the frontend

## Constraints
- JWT tokens last 15 minutes: don't extend them,
  refresh is handled in `src/auth/refresh.ts`

Look at what happened to the history: the token bug became a constraint ("don't extend them"), the Prisma experiment became a rule with a pointer to the rationale. Lines that didn't produce a rule are gone. The docs/decisions/orm.md path is in backticks on purpose: an @path written outside backticks gets imported into context every session, while this way Claude knows where to look and opens it only when it needs to.

05
Keep it short and move the rest out

The official docs give a concrete target: under 200 lines per CLAUDE.md file. Longer files consume more context and reduce adherence. If you also care about the cost side, the guide on reducing context tokens in Claude Code goes deeper on exactly that.

When the file grows, you have three options:

  • Path-scoped rules: Markdown files in .claude/rules/ with a paths field in the frontmatter. They load only when Claude works on files matching the patterns, for example only on the API or only on tests.
  • Skills: for long multi-step procedures (a release, a migration), loaded only when relevant. More on that in specialist agents and skills.
  • Comments for humans: block-level HTML comments like <!-- note for the team --> are stripped before the content reaches Claude. You can leave notes for colleagues without spending context.

A warning about @path imports: they help with organization but don't reduce the cost, because the imported file loads together with the CLAUDE.md that references it. Imports can nest up to four levels deep.

06
Check that it loads and gets followed

A CLAUDE.md that Claude can't see is worse than no file, because it gives you false confidence. Three checks, in order:

  1. Is it loaded? Run /context all and look at the list under Memory files. If the file isn't there, Claude can't see it. Remember that CLAUDE.md files in subdirectories don't show up there, because they load only when Claude touches a file in that folder.
  2. Is it the right file? The /memory command lists your memory files across user and project scopes and lets you open them in your editor.
  3. Is it followed? Ask something that depends on a specific rule, without mentioning the rule:
Add a dependency for handling dates.

If your file says "always use pnpm", the answer should use pnpm add. If it doesn't, the rule is too vague, conflicts with another one, or is buried under too much text.

One thing to know about /compact: the project-root CLAUDE.md survives compaction, because Claude re-reads it from disk. An instruction given only in chat is lost: if you're typing it for the second time, that's the signal it belongs in the file.

Finally, if something must happen every time, no exceptions (a check before every commit, a formatter after every edit), CLAUDE.md is the wrong tool: you need a hook, which runs regardless. The automation hooks guide explains how they work.

07
Update it only when there's a reason

The docs suggest a simple test for when to add a line: Claude makes the same mistake a second time, a code review catches something Claude should have known, you find yourself typing the same correction you typed last session, or a new teammate would need the same information.

The reverse applies to removing: every so often, reread the file and delete rules that no longer apply, ones that reference files that are gone, ones superseded by a later decision. Recent Claude Code versions also include /doctor prompt-audit, which looks for outdated or conflicting instructions in your files and proposes edits without applying them until you ask.

CLAUDE.md and persistent memory are not the same thing

Once history and decisions are out of CLAUDE.md, the obvious question is where they go. They're useful information, just not rules to read in every session.

Claude Code already has a second mechanism, auto memory: notes Claude writes itself in ~/.claude/projects/<project>/memory/ when you correct it or ask it to remember something. Each session loads the first 200 lines (or first 25 KB) of its index file, MEMORY.md. The division of roles is clear: you write CLAUDE.md and it holds instructions, Claude writes auto memory and it holds what Claude has learned.

On projects that run for months, though, that has a limit too: decisions pile up, and an index loaded in full doesn't scale. That's the problem described in why Claude Code forgets context, and it's also one of the reasons AI-generated code breaks as sessions go by. The fix is a queryable memory, where Claude retrieves only the decisions relevant to the question instead of loading all of them: a knowledge graph. How to wire one into Claude Code, step by step, is covered in the persistent memory for Claude Code guide.

In practice, each piece of information has its place:

  • CLAUDE.md: the project's laws, short and verifiable;
  • auto memory: preferences and corrections Claude learns from you;
  • knowledge graph: decisions, rationale, and lessons that grow over time;
  • git: the history of what changed.

If several people work on the same repository, the project CLAUDE.md also becomes an agreement between colleagues, with its own problems: who edits it, how to avoid conflicting rules. That's the subject of the guide on Claude Code in teams and shared context.

Want to try it? The QuickStart installs the free, open source VibeCoded Orchestrator in about 10 minutes of active time.