How to Write a CLAUDE.md File That Helps

CLAUDE.md is where you write down what you would otherwise keep re-explaining to Claude Code. The best ones are short, specific and maintained, not long lists of good intentions.

How to Write a CLAUDE.md File That Helps — Troiana insight cover

In short

A CLAUDE.md file gives Claude Code project instructions at the start of each session. Include the commands to build and test, conventions it can't guess, key architecture and warnings. Anthropic recommends keeping each file under 200 lines. Run /init for a first draft, then cut. It's context rather than enforcement, so use hooks for rules that must never be broken.

What it is

Each Claude Code session begins fresh. CLAUDE.md files carry instructions across sessions: they're plain Markdown files, loaded at the start. Anthropic's docs suggest treating one as the place for what you'd otherwise re-explain.

Add something when:

  • Claude makes the same mistake a second time
  • A code review catches something that it should've known
  • You've typed the same correction into a chat before
  • A new teammate would need the same context

See what Claude AI is and context engineering vs prompt engineering.

Where to put it

LocationScopeGood for
./CLAUDE.md or ./.claude/CLAUDE.mdProject, shared in GitTeam standards and commands
./CLAUDE.local.mdJust you; add to .gitignoreYour sandbox URLs and test data
~/.claude/CLAUDE.mdAll your projectsPersonal preferences
./.claude/rules/*.mdProject; can apply to certain pathsTopic or folder-specific rules

Files in the directories above where you launch are loaded in full, and files in subdirectories load when needed. They're combined, rather than overriding each other, so contradictions matter. If two instructions conflict, Claude may pick one arbitrarily.

Start with /init

Run /init in your project. Claude analyses your code and drafts a file with its build commands and conventions. Then edit it. Remove what it could discover by itself, and add what it couldn't.

A template

# Acme Shop

Online store. Next.js front end, Postgres database, Stripe payments.

## Commands
- Dev server: npm run dev
- Run one test file: npm test -- path/to/file
- Full check before finishing: npm run lint and npm run typecheck and npm test

## Architecture
- Routes in src/app; shared components in src/components/ui
- All database access goes through src/db; never query from a component
- Payments live in src/payments. See docs/payments.md first.

## Conventions
- British English in all user-facing text
- Use existing components in src/components/ui before creating new ones
- Colours and spacing come from tokens in src/styles/tokens.css

## Don't
- Don't edit files in src/generated; run npm run codegen
- Don't change existing database migrations; add a new one
- Don't commit or push unless asked

## Definition of done
- New logic has tests, and all checks pass
- For UI changes, check in the browser at mobile width

Yours will be different. The point is that each line tells Claude something it can't easily work out for itself.

What to include

  • Exact commands, particularly for running a single test
  • Non-obvious structure: where things live, and why
  • Project conventions which differ from the default
  • Warnings: the things that have caused problems before
  • What “done” means, so that it verifies its work
  • Pointers to longer docs, which it can read when needed

What to leave out

Earns its line
npm test -- path/to/file
API handlers go in src/api
Never edit old migrations
Costs a line, adds nothing
Write clean code
Be careful with performance
This project uses React
Each line loads in every session, so it has to tell Claude something it could not work out alone. Example lines.
  • What it can see from the code. It doesn't need your entire folder tree.
  • Standard practice, like “write clean code” or “handle errors”.
  • Long tutorials. Link to them instead.
  • Frequently changing details, such as current sprint tasks.
  • Secrets. Never put keys or passwords in here. See how to handle secrets and API keys.
  • Vague aspirations. “Be careful with performance” won't change behaviour.

Keep it short

Anthropic's guidance is to target under 200 lines per file, since longer files use more context and reduce adherence. Every line costs tokens in every session. See how to control AI API costs.

If it's growing, then:

  • Use path-scoped rules. Put your API rules in .claude/rules/api.md, and make them load only when Claude works on matching files.
  • Use skills for longer procedures, which load only when needed.
  • Use imports for organisation, with the @path/to/file syntax. But note that imported files load with the parent, so imports don't save on context.

Write instructions that work

Anthropic's docs recommend specific, verifiable instructions.

VagueSpecific
Format code properlyUse 2-space indentation
Test your changesRun npm test before finishing
Keep files organisedAPI handlers go in src/api/handlers/
Be careful with the databaseAdd a new migration; never edit an existing one

Use headers and bullets, as they make it easier to scan, for people and for Claude.

It's context, not enforcement

This is the most common misunderstanding. The docs state that Claude treats these files as context, not enforced configuration. It can ignore them, particularly in long sessions.

If something must never happen, use a real control:

  • Hooks to block an action, or to run a check automatically
  • Permissions to deny access to certain commands or files
  • CI to reject failing code

For example, “never push to main” belongs in branch protection, as well as your file.

Does it work with other tools?

Many other agents use AGENTS.md. According to its docs, Claude Code can read an AGENTS.md as project instructions, if there's no CLAUDE.md in your working directory or above it. If you have both, by default it reads just your CLAUDE.md. To share a single file, keep your instructions in AGENTS.md, and put @AGENTS.md in a short CLAUDE.md, adding any Claude-specific notes below it.

Keep it maintained

Treat it like code:

  • Review changes in pull requests for shared files.
  • Prune it regularly. Remove what's no longer true. Outdated instructions cause real harm.
  • Check for contradictions across CLAUDE.md, nested files and rules.
  • Test by removing. If Claude behaves just as well without a line, then delete it.
  • Update it after each surprise. When something goes wrong, ask: would a line here have prevented this?

Claude Code also has auto memory, in which Claude keeps notes on its own. That's complementary, but it's no substitute for team-wide rules you've written deliberately.

How to tell if it's working

  • You stop repeating yourself
  • First attempts follow your conventions
  • It runs the right tests without being asked
  • New team members get good results sooner

If your instructions aren't being followed, check that the file is loading with /memory. Then check whether it's too long, too vague or contradicted elsewhere.

If you'd like help setting up Claude Code or other AI agents for your team, book a call.

Common questions

What should I put in a CLAUDE.md file?

Include exact build and test commands, non-obvious architecture, project conventions, warnings about past problems and what done means. Leave out anything Claude can work out from the code, generic advice, frequently changing details and secrets. Keep it short and specific.

How long should a CLAUDE.md file be?

Anthropic recommends aiming for under 200 lines per file, as longer files use more context and reduce adherence. If yours is growing, move topic-specific rules into .claude/rules with path scoping, or put longer procedures into skills.

Does Claude always follow CLAUDE.md?

No. It's treated as context, not enforced configuration, so it can be missed or ignored. Specific and concise instructions are followed more reliably. For things that must never happen, use hooks, permissions or CI checks instead.

Should I use CLAUDE.md or AGENTS.md?

If you only use Claude Code, either works. If your team uses several agents, keep shared instructions in AGENTS.md. Claude Code can read it when there's no CLAUDE.md, or you can import it with @AGENTS.md from a short CLAUDE.md.

Have something worth building right?