Claude Code

CLAUDE.md: how to write one that actually works

Every Claude Code session starts with an empty context window. It does not remember that your tests run with pnpm test:unit, that you banned default exports, or that the legacy/ folder is off limits. CLAUDE.md is where you write that down once so you stop retyping it.

What belongs in it#

Treat it as the page you would hand a new teammate on day one. Add a line when:

  • the agent makes the same mistake a second time,
  • a code review catches something it should have known about this repo,
  • you type the same correction into chat that you typed yesterday.

Good content is facts that hold in every session: build, test and lint commands, project layout, naming conventions, and "always do X" or "never do Y" rules.

Where the file lives#

Managed policyorganization-wide, set by IT~/.claude/CLAUDE.mdyour personal rules for every project./CLAUDE.md or ./.claude/CLAUDE.mdteam rules, committed to git./CLAUDE.local.mdyour private notes for this repo, in .gitignore
Load order, broadest first. Later (more specific) files appear later in context.

Files in parent directories load at launch; files in subdirectories load on demand when the agent works there. Run /context in a session to confirm your file loaded.

Start with /init#

Run /init in your repo. Claude analyzes the codebase and writes a first CLAUDE.md with the build commands, test instructions and conventions it finds. If one exists, it suggests improvements instead of overwriting. Then edit it: delete what the agent could work out itself and add what it could not.

Write rules it can verify#

Vague rules get ignored. Concrete ones get followed.

WeakStrong
Format code properlyUse 2-space indentation
Test your changesRun npm test before committing
Keep files organizedAPI handlers live in src/api/handlers/
Write good commitsCommit messages: imperative mood, max 72 chars, no emoji

A template that stays short#

markdown
# Project: invoice-api

## Commands
- Install: `pnpm install`
- Test one file: `pnpm vitest run path/to/file.test.ts`
- Lint + types (run before every commit): `pnpm lint && pnpm tsc --noEmit`

## Layout
- `src/api/handlers/` HTTP handlers, one file per route
- `src/domain/` pure business logic, no I/O
- `src/db/` queries; never import from here in `domain/`

## Conventions
- TypeScript strict, no `any`. Use `unknown` and narrow.
- Named exports only.
- Errors: throw `AppError` subclasses, never strings.

## Don't
- Don't edit files in `generated/`.
- Don't add dependencies without asking.

Import instead of copy#

CLAUDE.md can pull in other files with @path syntax:

markdown
See @README.md for the overview and @package.json for npm scripts.
- Git workflow: @docs/git-instructions.md

Imports load at launch, so they organize a long file but do not reduce its context cost. For rules that only matter in one area, use path-scoped rules in .claude/rules/ or a skill so they load only when relevant.

If you already have AGENTS.md#

Claude Code can read a repo's AGENTS.md in place of CLAUDE.md. To keep one source of truth, make CLAUDE.md a thin file that imports it:

markdown
@AGENTS.md

## Claude Code only
- Use plan mode for changes that touch more than three files.

Mistakes that make it worse#

  • Too long. Past roughly 200 lines, adherence drops and you pay for the tokens every session.
  • Contradictions. Two rules that disagree get resolved arbitrarily. Review it now and then; /doctor prompt-audit can find outdated and conflicting instructions.
  • Style essays. "Write clean, maintainable code" does nothing. Delete it.
  • Treating it as enforcement. It is context. For must-never-happen rules, use a hook.
  • Secrets. It is committed to git. Never put keys in it.

Quick checklist#

  1. Run /init, then cut it down.
  2. Every line is checkable by a test, a command or a glance.
  3. Under about 200 lines.
  4. Personal stuff in ~/.claude/CLAUDE.md or CLAUDE.local.md.
  5. Review it whenever the agent repeats a mistake.

Related: what is Claude Code and context engineering. Official reference: How Claude remembers your project.

Frequently asked questions

What is a CLAUDE.md file?

A markdown file of persistent instructions that Claude Code loads at the start of every session: build and test commands, conventions, project layout and always-do rules.

Where do I put CLAUDE.md?

For a project, put it at ./CLAUDE.md or ./.claude/CLAUDE.md and commit it. Personal preferences for all projects go in ~/.claude/CLAUDE.md. Private per-project notes go in CLAUDE.local.md, which you add to .gitignore.

How long should CLAUDE.md be?

Aim for under 200 lines. Longer files use more context and make the agent less likely to follow each rule. Move part-of-the-codebase instructions into path-scoped rules or skills.

Is CLAUDE.md guaranteed to be followed?

No. It is context, not enforced configuration. If an action must be blocked every time, use a hook, not an instruction.

What is the difference between CLAUDE.md and AGENTS.md?

AGENTS.md is a tool-neutral convention some repositories use for agent instructions. Claude Code can read a repository's AGENTS.md in place of CLAUDE.md, and you can import AGENTS.md from CLAUDE.md so both stay in sync.