Post your build ideas
LocalASO Track App Store keyword ranks in every countryFind your vibe. Find you job which is just for you and your dataDealtab Brand Deal Tracker That Does The Work For YouIs Major Cooked? Free AI risk score + roast for your college majorWiral.Social LinkedIn, Scheduler, Scheduling Tool, Ghostwriter
Solo Era solo activities, solo maxxing, sidequests, challengeCadentiq daily routine, wellbeing, self care, healthy habitsTexeva Your businessKeepsake Project Management, Freelancers, DesignersOrvyx AI You create

How to write a CLAUDE.md file: where it loads and what goes in it

CLAUDE.md is a Markdown file of standing instructions that Claude Code reads at the start of every session. Put it at the project root as ./CLAUDE.md or .claude/CLAUDE.md, run /init to generate a first draft, and keep it under about 200 lines of concrete facts: build and test commands, conventions and layout. Personal preferences go in ~/.claude/CLAUDE.md for every project, or CLAUDE.local.md for one.

What is a CLAUDE.md file?

Each Claude Code session starts with a fresh context window. CLAUDE.md is one of two ways knowledge survives from one session to the next. You write it in plain Markdown, and Claude reads it at the start of every conversation. The other mechanism is auto memory, which is notes Claude writes itself from your corrections and preferences. The official memory documentation covers both.

CLAUDE.md versus auto memory
CLAUDE.mdAuto memory
Who writes itYouClaude
What it holdsInstructions and rulesLearnings and patterns
ScopeProject, user or organizationPer repository
Use it forCoding standards, workflows, project architectureYour preferences, corrections, context Claude cannot derive from code

The part most people miss is that Claude treats CLAUDE.md as context, not as enforced configuration. The file is delivered as a message after the system prompt. Claude tries to follow it, and the docs say there is no guarantee of strict compliance, especially for vague or conflicting instructions. That shapes how you write it, and it tells you when not to use it: for a rule that must hold every time, use a hook.

A CLAUDE.md is the cheapest improvement you can make to a Claude Code project. It turns a tool that asks the same questions every morning into one that already knows how your project runs. If you are new to the tool, start with the Claude Code tutorial and come back.

Where does Claude Code load CLAUDE.md from?

CLAUDE.md files can live in several places, each with a different scope. The table lists them from broadest to most specific, which is also the order they load, so a project instruction lands in context after a user instruction.

CLAUDE.md locations, from the official docs
ScopeLocationShared with
Managed policymacOS: /Library/Application Support/ClaudeCode/CLAUDE.md. Linux and WSL: /etc/claude-code/CLAUDE.md. Windows: C:\Program Files\ClaudeCode\CLAUDE.mdEveryone in the organization
User~/.claude/CLAUDE.mdOnly you, in all projects
Project./CLAUDE.md or ./.claude/CLAUDE.mdYour team, through version control
Local./CLAUDE.local.md (add it to .gitignore)Only you, in this project

Loading follows the folder tree. Claude Code loads CLAUDE.md and CLAUDE.local.md from your current working directory and every directory above it. Start Claude in foo/bar/ and it reads foo/bar/CLAUDE.md and foo/CLAUDE.md. All of them are concatenated into context; none overrides another. The ones closer to where you launched Claude are read last, and within a folder, CLAUDE.local.md comes after CLAUDE.md.

Files in subdirectories below your working directory are different. They load on demand, the first time Claude reads or edits a file in that subdirectory. That makes nested CLAUDE.md files a good home for instructions that only matter in one part of a monorepo.

  • Check what loaded. Run /context and look under Memory files. Use /memory to open and edit them.
  • AGENTS.md. If your repository uses AGENTS.md for other coding tools, Claude reads it only when no CLAUDE.md exists in your working directory or above. To share one file, create a CLAUDE.md containing @AGENTS.md on its own line and put any Claude-specific notes below it.
  • Extra directories. Folders you add with --add-dir do not load their CLAUDE.md files unless you set CLAUDE_CODE_ADDITIONAL_DIRECTORIES_CLAUDE_MD=1.

How do you create a CLAUDE.md?

  1. Open Claude Code in your project root.
  2. Run /init. Claude analyzes the codebase and writes a CLAUDE.md with the build commands, test instructions and conventions it finds. If one already exists, /init suggests improvements instead of overwriting it.
  3. Open the result and cut it down. Delete anything Claude could work out from the code itself, such as a directory listing or a dependency list.
  4. Add what Claude cannot guess: why a decision was made, what bit you last time, which commands you actually use.
  5. Run /context to confirm the file loads, then work normally.
  6. Whenever Claude makes the same mistake twice, add a one-line rule.

The docs give a useful test for what to add: write down what you would otherwise re-explain. Add an entry when Claude repeats a mistake, when a code review catches something Claude should have known, when you type the same correction you typed last session, or when a new teammate would need the same context.

Setting the environment variable CLAUDE_CODE_NEW_INIT=1 before /init gives an interactive, multi-step flow that also offers to set up skills and hooks.

What should go in CLAUDE.md, and what should stay out?

Keep to facts Claude should hold in every session: build commands, conventions, project layout, and always-do-X rules. Everything else belongs somewhere else.

Where each kind of instruction belongs
Instruction typePut it inExample
A fact needed every sessionCLAUDE.md"Run tests with pnpm test"
A convention for the whole projectCLAUDE.md"Use 2-space indentation"
A convention for one file type or folder.claude/rules/ with paths"API handlers must validate input"
A multi-step procedure you run sometimesA skill"How we cut a release"
A rule that must hold every timeA hook"Never edit .env"
Anything secretNowhere in the repositoryAPI keys, passwords, tokens

The single biggest lever is being concrete enough to verify. The docs' own pairs:

Vague versus concrete instructions
VagueConcrete
Format code properlyUse 2-space indentation
Test your changesRun npm test before committing
Keep files organizedAPI handlers live in src/api/handlers/

Also include reasons when a rule looks arbitrary. "Do not use the ORM for reports, it generated a slow query in production" prevents Claude from "fixing" your workaround. Avoid contradictions: if two instructions conflict, Claude may pick one arbitrarily, and that includes conflicts between a nested CLAUDE.md and the root one.

What does a good CLAUDE.md look like? A full template

Copy this, delete the lines that do not apply, and replace every bracket. It is written for a typical web project; the structure carries over to any stack. Aim to keep your finished file under 200 lines.

CLAUDE.md template for a web app
# [Project name]

[One or two sentences: who uses this and what it does.]

## Commands
- Install: `[pnpm install]`
- Dev server: `[pnpm dev]` (runs on port [3000])
- Run all tests: `[pnpm test]`
- Run one test file: `[pnpm test path/to/file.test.ts]`
- Type check: `[pnpm tsc --noEmit]`
- Lint and format: `[pnpm lint]`

## Layout
- `[src/app/]`: pages and routes
- `[src/lib/]`: shared logic; no UI code here
- `[src/components/]`: UI components
- `[supabase/migrations/]`: SQL migrations, write new files, never edit applied ones

## Conventions
- [TypeScript strict mode. No `any`.]
- [2-space indentation, double quotes, no semicolons.]
- [Validate every external input with Zod at the boundary.]
- [Money is stored in integer cents. Dates are UTC.]
- [Name test files next to the code: `thing.ts` and `thing.test.ts`.]

## Workflow
- Before you start a feature, read the files involved and state a plan. Do not edit during planning.
- Make the smallest change that works. Do not refactor code you were not asked to touch.
- After a change, run the type check and the tests. Fix failures before saying you are done.
- Commit in small steps with a message that says why, not just what.

## Do not
- Do not edit `.env*` files or anything under `[migrations/applied/]`.
- Do not add a dependency without asking. Say what it replaces and how big it is.
- Do not delete or skip a failing test to make the suite pass.

## Known pitfalls
- [The staging database resets every night; never rely on data surviving a day.]
- [The `[date lib]` parses '01/02' as [format]. Always pass an explicit format.]
- [Why we do X instead of the obvious Y: reason.]

## More detail
- Architecture notes: @docs/architecture.md
- API conventions: @docs/api.md

Two notes on that template. The @docs/architecture.md lines are imports: the files are expanded into context alongside the CLAUDE.md that references them. Imports help you organize a long file, but they do not reduce its cost, because imported files load together with the importer. And the "Known pitfalls" section is where a CLAUDE.md earns its keep, because those are the facts no tool can read off your code.

What if you are not a developer?

A CLAUDE.md is just as useful if you are building your first project from an idea and cannot read the code Claude produces. Use it to set the working rules, not the tech details. Here is a short version for that case.

CLAUDE.md for a first-time builder
# [App name]

## What we are building
[Who it is for, in one sentence. The one thing it does, in one sentence.]

## How to work with me
- I am not a developer. Explain what you changed in plain language after every step.
- Build one feature at a time. Stop after each one so I can try it.
- Before installing any package or running a command that deletes files, tell me and wait for a yes.
- If my request is unclear, ask one question rather than guessing.

## Rules for the code
- Pick the simplest approach that works. No extra features I did not ask for.
- Tell me how to run the app and how to check that the change worked.
- Commit to git after each working step with a short message.

## Not in the first version
- [Accounts and logins]
- [Payments]
- [Anything I list here later]

The "not in the first version" list does more than it looks. It stops the scope from growing without you noticing. The same idea underlies the guide to scoping an MVP. If you do not yet have an idea to build, browse the ideas on the board, where each one comes with a generated build prompt you can paste in after your CLAUDE.md is in place.

How do imports and .claude/rules keep it short?

Once a CLAUDE.md passes about 200 lines, adherence drops and context cost rises. The docs suggest moving instructions that matter for only part of the codebase into path-scoped rules. Put Markdown files in .claude/rules/, one topic per file, and add a paths field in the frontmatter so each loads only when Claude works with matching files.

.claude/rules/api.md, a path-scoped rule (from the docs)
---
paths:
  - "src/api/**/*.ts"
---
# API Development Rules

- All API endpoints must include input validation
- Use the standard error response format
- Include OpenAPI documentation comments
  • Rules without `paths` load at launch with the same priority as .claude/CLAUDE.md.
  • Imports use @path/to/file, resolve relative to the file containing them, and nest up to four hops. Wrap a path in backticks to mention it without importing it.
  • Private notes for maintainers: block-level HTML comments in CLAUDE.md are stripped before the content reaches Claude, so they cost no context.
  • Personal, uncommitted preferences go in CLAUDE.local.md. If you work across several git worktrees, import a file from your home directory instead, such as @~/.claude/my-project-instructions.md.
  • Size limits. A file over 4 MiB is skipped. Files that load on demand have a per-file character limit, and Claude receives only the start of anything longer.

To have Claude audit your instruction files for stale or conflicting content, run /doctor prompt-audit. It reports findings with proposed edits and changes nothing until you ask.

Why is Claude not following my CLAUDE.md?

Work down this list in order. It follows the troubleshooting section of the memory docs.

  1. Is it loaded? Run /context and check Memory files. If your file is missing, Claude cannot see it. A CLAUDE.md in a subdirectory will not appear there, because it loads on demand.
  2. Is it in a location that loads for your session? Check the table above, and the folder you started Claude from.
  3. Is the instruction concrete? "Use 2-space indentation" beats "format code nicely".
  4. Does anything contradict it? Search your other CLAUDE.md files and .claude/rules/ for conflicting guidance.
  5. Is the file too long? Past about 200 lines, adherence suffers. Trim it or split it into rules.
  6. Must it hold every time? Then it is not a CLAUDE.md job. Write a hook, which runs as a shell command at a fixed event regardless of what Claude decides.

One reassurance for long sessions: the project-root CLAUDE.md survives /compact. Claude re-reads it from disk afterwards. An instruction you only gave in conversation does not, so add it to the file if it should persist.

Frequently asked questions

What is CLAUDE.md?

CLAUDE.md is a Markdown file that gives Claude Code persistent instructions for a project, for you personally or for an organization. Claude reads it at the start of every session. It typically holds build and test commands, coding conventions, project layout and workflow rules. Claude treats it as context rather than enforced configuration.

Where should I put CLAUDE.md?

For a project, put it at the repository root as ./CLAUDE.md or in ./.claude/CLAUDE.md, and commit it so your team shares it. Personal preferences for all projects go in ~/.claude/CLAUDE.md. Personal notes for one project go in CLAUDE.local.md, which you should add to .gitignore.

How long should a CLAUDE.md be?

The official guidance is to target under 200 lines per file. Longer files use more context and reduce how well Claude follows them. Move instructions that apply to only part of the codebase into path-scoped rules in .claude/rules/, or procedures into skills.

What is the difference between CLAUDE.md and /init?

/init is a command that generates a starting CLAUDE.md by analyzing your codebase. CLAUDE.md is the file itself. If one already exists, /init suggests improvements rather than overwriting it. Treat the generated file as a draft: remove what Claude could derive from the code and add what it cannot guess.

Does Claude Code read AGENTS.md?

Yes, but only when there is no CLAUDE.md in your working directory or above it. If both exist, Claude reads the CLAUDE.md files only. To use one shared file for several tools, create a CLAUDE.md containing the line @AGENTS.md and add Claude-specific notes below it.

Should I commit CLAUDE.md to git?

Commit the project CLAUDE.md so everyone working on the repository gets the same instructions. Keep personal, uncommitted notes in CLAUDE.local.md and add that file to .gitignore. Never put secrets like API keys in either file, since the project one is shared through version control.

Created with AI assistance and edited for this site. Examples are illustrative and do not represent customer research. About the publisher.