CLAUDE.md best practices: examples and AGENTS.md

What CLAUDE.md is, how Claude Code loads it, what to put in it, a file you can copy, and how to share one file with Codex and Cursor.

Last updated

Abstract illustration of a single glowing sheet of paper with a few short lines, resting on a stack of fainter pages

CLAUDE.md is a plain Markdown file of instructions that Claude Code reads at the start of every session. For a project, put it at the root of your repository (./CLAUDE.md) and commit it so your whole team gets it. The one rule that matters most: only write down what Claude can't work out by reading your code, and keep it short, because every line is in context on every task.

Everything else is detail, and the detail is below: where the file can live and in which order Claude Code loads it, how it relates to AGENTS.md (the shared file Codex, Cursor and many other agents read), what belongs in it and what doesn't, a complete example you can copy, and how to check that Claude actually read it. All of it was checked against Anthropic's Claude Code docs, the agents.md site, OpenAI's Codex docs and Cursor's docs on November 30, 2026, with Claude Code 2.1.332 installed. This area changes fast, so if your screen disagrees, the official memory docs win.

What is CLAUDE.md

Every Claude Code session starts with an empty context window. Claude doesn't remember yesterday's conversation, so anything it should know every time has to be written somewhere it reads every time. That's CLAUDE.md: the place for what you'd otherwise re-explain.

Anthropic's docs suggest adding to it when:

  • Claude makes the same mistake a second time
  • A code review catches something Claude should have known about this codebase
  • You type the same correction into chat that you typed last session
  • A new teammate would need the same context to be productive

Two things to understand before you write a line.

It's context, not configuration. Claude Code delivers CLAUDE.md as a user message after the system prompt, not as part of the system prompt. Claude reads it and tries to follow it, but nothing enforces it. Rules that must hold every time belong in hooks or permission rules (more on that below).

It's not the same as auto memory. Claude Code also keeps notes of its own in ~/.claude/projects/<project>/memory/, with a MEMORY.md index whose first 200 lines or 25KB load each session. You write CLAUDE.md; Claude writes auto memory. When you say "remember to use pnpm", Claude saves it to auto memory. If you want it in CLAUDE.md, say "add this to CLAUDE.md".

Where to put CLAUDE.md and how Claude Code loads it

There are four places a CLAUDE.md can live, each with a different reach. Claude Code loads them in this order, broadest first:

Scope Location Use it for Shared with
Managed policy /Library/Application Support/ClaudeCode/CLAUDE.md (macOS), /etc/claude-code/CLAUDE.md (Linux and WSL), C:\Program Files\ClaudeCode\CLAUDE.md (Windows) Company standards set by IT Everyone on the machine
User (global) ~/.claude/CLAUDE.md Your personal preferences in every project Only you
Project ./CLAUDE.md or ./.claude/CLAUDE.md Commands, conventions and gotchas for this repo Your team, through git
Local ./CLAUDE.local.md Your sandbox URLs, test accounts, personal habits for this repo Only you (add it to .gitignore)

The rules for how they combine:

  • Files are added together, not overridden. Claude Code concatenates everything it finds. A project instruction appears after a user instruction, so it's read later, but neither cancels the other. If two files contradict each other, Claude may pick either one, so keep them consistent.
  • Parent folders load at launch. Start Claude Code in foo/bar/ and it loads foo/CLAUDE.md and foo/bar/CLAUDE.md (plus any CLAUDE.local.md next to them), ordered from the top down, so the file closest to where you started is read last. In each folder, CLAUDE.local.md comes after CLAUDE.md.
  • Subfolders load on demand. A CLAUDE.md in packages/api/ isn't loaded at launch. It loads the first time Claude reads, writes or edits a file in that folder. That's how monorepos give each package its own instructions without paying for all of them up front.
  • The project file survives /compact. After compaction, Claude Code re-reads the root CLAUDE.md from disk. Nested files and path-scoped rules come back the next time Claude touches a matching file.
  • HTML comments are free. Block-level <!-- notes for humans --> are stripped before the content reaches Claude, so you can leave maintainer notes without spending tokens.

Imports with @

A CLAUDE.md can pull in other files with @path/to/file:

See @README.md for the project overview and @package.json for the npm scripts.

- Git workflow: @docs/git-instructions.md

Relative paths resolve from the file that contains the import. Imported files can import others, up to four levels deep. Paths inside backticks or code blocks are not imported, so `@README.md` stays plain text. The first time a project file imports something outside your working directory (like @~/.claude/my-notes.md), Claude Code asks you to approve it.

The catch: imports organize a file, they don't shrink it. An imported file loads together with the CLAUDE.md that imports it, so it costs the same context as pasting it in.

Path-scoped rules in .claude/rules/

For instructions that only matter in one part of the code, Claude Code has a better tool than a long CLAUDE.md: Markdown files in .claude/rules/. A rule without frontmatter loads at launch like CLAUDE.md. A rule with a paths field loads only when Claude works on a matching file:

---
paths:
  - "src/api/**/*.ts"
---

- Every endpoint validates its input with the schemas in src/api/schemas/
- Errors use the standard error response format

Personal rules can go in ~/.claude/rules/ and apply to every project on your machine.

/init and /memory

Run /init inside a session and Claude analyzes the repo and writes a starting CLAUDE.md with the build commands, test instructions and conventions it finds. If one already exists, it suggests improvements instead of overwriting it. /init also reads Cursor rules (.cursor/rules/ or .cursorrules) and .github/copilot-instructions.md and folds the relevant parts in. Anthropic's best-practices page is honest about what comes next: "From there the work is mostly removing lines."

/memory lists every CLAUDE.md, CLAUDE.local.md and memory location for your session, including ones that don't exist yet. Pick one to open it in your editor (picking a missing one creates it). It's also where you turn auto memory on or off.

CLAUDE.md vs AGENTS.md

AGENTS.md is the same idea under a neutral name. The agents.md site calls it "a simple, open format for guiding coding agents" and "a README for agents". It's plain Markdown with no required fields, it's used by more than 60,000 open-source projects according to the site, and it's now stewarded by the Agentic AI Foundation under the Linux Foundation.

Here's who reads what, as of November 30, 2026:

Tool Reads AGENTS.md? Its own file
Claude Code Yes, since v2.1.277, but by default only when there's no CLAUDE.md CLAUDE.md, CLAUDE.local.md, .claude/rules/
Codex Yes, it's Codex's main instruction file AGENTS.override.md for overrides; other names through config
Cursor Yes, in the project root and in subfolders Project rules in .cursor/rules/
Others The agents.md site lists Jules, Gemini CLI, GitHub Copilot's coding agent, Windsurf, Zed, Warp, Aider, Devin, Junie, Amp, opencode and more Varies

How Claude Code treats an existing AGENTS.md

This is the part most guides get wrong or skip. Claude Code now reads AGENTS.md on its own, with no import or setting. By default the rule is either/or:

Your repository has Claude reads
An AGENTS.md, and no CLAUDE.md or CLAUDE.local.md in your working directory or above it Your AGENTS.md
An AGENTS.md and a CLAUDE.md or CLAUDE.local.md Your CLAUDE.md files only
A CLAUDE.md that imports AGENTS.md with @AGENTS.md Both, through the import

Your global ~/.claude/CLAUDE.md, a managed CLAUDE.md and .claude/rules/ files don't count for that check; they load alongside AGENTS.md. But CLAUDE.local.md does count. So if your team relies on AGENTS.md and you add a CLAUDE.local.md for your own notes, Claude silently stops reading AGENTS.md for you.

You can change the default: type /config and set Project instructions to claude-md-and-agents-md (read both), claude-md (ignore AGENTS.md) or back to claude-md-or-agents-md. Claude Code doesn't read AGENTS.local.md, AGENTS.override.md or anything in a .agents/ folder.

How Codex and Cursor read their files

Codex reads ~/.codex/AGENTS.override.md or ~/.codex/AGENTS.md globally, then walks from the project root down to your current folder, taking at most one file per folder (AGENTS.override.md first, then AGENTS.md). Files are joined top-down, so closer files win, and it stops adding once the total reaches project_doc_max_bytes, 32 KiB by default. Codex only looks for other file names if you list them in project_doc_fallback_filenames in ~/.codex/config.toml. More on Codex in what is Codex.

Cursor reads AGENTS.md in the project root and in subfolders, combining nested files with their parents, with the more specific ones taking precedence. Its own system is project rules: .mdc files in .cursor/rules/ with description, globs and alwaysApply frontmatter. Cursor's docs warn that a plain .md file in .cursor/rules/ is ignored, and recommend keeping each rule under 500 lines. Cursor's docs don't say it reads CLAUDE.md, so don't count on it. For how the two tools differ beyond config files, see Claude Code vs Cursor.

One file for Claude Code, Codex and Cursor

If more than one agent works in your repo, make AGENTS.md the source of truth. It's the only file all three read. Then pick one of these for Claude Code:

  1. Nothing at all. With Claude Code 2.1.277 or later and no CLAUDE.md anywhere on the path, Claude reads AGENTS.md directly. Before 2.1.281, some sessions (on Amazon Bedrock, or with telemetry turned off) didn't, so update if yours doesn't.
  2. A CLAUDE.md that imports it (example below). Do this if you want Claude-only instructions, keep a CLAUDE.local.md, or have teammates on older versions. Anthropic's docs say the import never makes Claude read AGENTS.md twice.
  3. A symlink, ln -s AGENTS.md CLAUDE.md. It works, but Anthropic recommends the import if anyone on the team uses Windows, where git can check a symlink out as a one-line text file.

If you're choosing between agents rather than between files, Codex vs Claude Code compares them.

CLAUDE.md best practices

These come from Anthropic's memory and best-practices docs. The reason matters more than the rule, so each comes with one.

Keep it under 200 lines

Anthropic's target is under 200 lines per file. The reason: the whole file is in context on every task, relevant or not, and longer files "consume more context and reduce adherence." Claude Code warns you at startup and in /status when a file is over the recommended length. Shorter isn't just cheaper; it's better followed.

Write what Claude can't see

Anthropic's list of what belongs in the file:

  • How to check work: the exact test, typecheck and lint commands, and how to run a single test file
  • Where the code is misleading: an old pattern that's everywhere but new code must not copy, or a folder that looks dead but isn't
  • Decisions that aren't in the repo: which helper the team settled on, how deploys work, what needs extra review
  • Anything you've corrected twice

The test command is the most valuable line you'll write. A Claude that can run your tests can check its own work instead of stopping when it looks done.

Leave out what Claude can read

Skip the folder tree, the dependency list and the architecture overview. Claude reads the repo itself, and Anthropic's /doctor checkup proposes cutting exactly those sections. For each line, ask whether removing it would make Claude get something wrong. If not, delete it.

Be concrete enough to verify

"Use 2-space indentation" beats "format code properly". "Run pnpm test before committing" beats "test your changes". A vague line gives Claude nothing to check against, and two vague lines that overlap are how contradictions sneak in.

Don't shout

Piling on IMPORTANT, YOU MUST and capital letters was a habit from older models. Anthropic's best-practices page says current models follow a plainly worded line, and extra emphasis "mostly makes Claude over-apply the rule." If a line keeps getting missed, the file is too long or the rule should be a hook.

Move procedures to skills

A multi-step procedure (cutting a release, the incident runbook, how to write a migration) doesn't need to sit in every session. Put it in a skill at .claude/skills/<name>/SKILL.md: Claude sees only its short description until the task calls for it, so it costs almost nothing the rest of the time. Anthropic's own wording is to create a skill "when a section of CLAUDE.md has grown into a procedure rather than a fact." Our guide to Claude Code skills shows how.

Move hard rules to hooks

Never read .env, always run the formatter, don't touch migrations/: Claude follows lines like these almost every time. For the few where almost isn't good enough, Anthropic says to make them hooks or deny rules, which Claude Code runs itself, every time, whatever the model decides. A PreToolUse hook can block an edit; a Stop hook can send Claude back to work until the tests pass. You don't have to write one by hand: ask Claude to "write a hook that blocks edits to the migrations folder" and check the result with /hooks. Keep that set small, though. "Prefer named exports" has exceptions, so it stays in CLAUDE.md.

Point to docs instead of importing them

@docs/billing.md loads the whole file into every session. "Before changing billing code, read docs/billing.md" costs one line, and Claude opens the file only when it's working on billing. Use imports for things Claude always needs and plain pointers for the rest.

Treat it like code

Commit it, review changes to it, and prune it. Run /doctor prompt-audit now and then (Claude Code 2.1.283 or later): it flags instructions written for older models, references to files or commands that no longer exist, and files that contradict each other, and it changes nothing until you say so.

A CLAUDE.md example you can copy

Here's a complete CLAUDE.md for a small, realistic web app: a TypeScript project with Next.js, Postgres and Prisma, tested with Vitest. Swap in your own commands and gotchas. Notice what isn't there: no folder tree, no list of dependencies, no "write clean code".

<!-- Keep this under 60 lines. Procedures go in .claude/skills/. -->

# Commands
- Install: pnpm install
- Dev server: pnpm dev (http://localhost:3000)
- All tests: pnpm test
- One test file: pnpm vitest run path/to/file.test.ts
- Typecheck: pnpm tsc --noEmit
- Lint: pnpm lint
- New migration: pnpm prisma migrate dev --name <short_name>

# Before you say a task is done
- Run the typecheck and the tests for the files you changed
- Show me the command you ran and its output

# Conventions
- Server code calls the database through src/server/db.ts, never a new PrismaClient
- Use the error helpers in src/lib/errors.ts; never throw raw strings
- Dates are stored in UTC and formatted with src/lib/dates.ts
- New UI uses the components in src/components/ui/ before adding a library
- Money is an integer number of cents, never a float

# Gotchas
- src/legacy/ is still live (old checkout flow) but don't copy its patterns
- Integration tests need DATABASE_URL; copy .env.example to .env.test
- Stripe webhooks reach localhost only while this runs: stripe listen --forward-to localhost:3000/api/webhooks/stripe
- Changing prisma/schema.prisma needs a migration; never edit applied migrations
- The /api/public/* routes are called by the mobile app; keep responses backward compatible

# Workflow
- Branch names: feature/<short-name> or fix/<short-name>
- One logical change per commit, message in the imperative ("Add invoice export")
- Before changing billing code, read docs/billing.md

# Ask first
- Adding a dependency
- Anything under src/server/auth/

That's about 40 lines. Each one is either a command Claude needs to check its work, or something it would otherwise get wrong. The comment at the top is for humans and is stripped before Claude sees it. The billing pointer is a plain sentence, not an @ import, so the billing doc loads only when it's needed.

Two things in there are worth copying even if nothing else fits your project: the "one test file" command, and the "show me the command you ran" line. Together they turn "all tests pass" from a claim into evidence.

A CLAUDE.md that only imports AGENTS.md

If your instructions already live in AGENTS.md for Codex and Cursor, your CLAUDE.md can be this short:

@AGENTS.md

## Claude Code
- Use plan mode for changes under src/billing/

Claude reads the imported AGENTS.md first, then the Claude-specific lines below it. If you have nothing Claude-specific to add and everyone runs Claude Code 2.1.281 or later, you can delete the CLAUDE.md entirely and let Claude read AGENTS.md directly.

One trap: a CLAUDE.md that says "please read AGENTS.md" in words is not an import. Claude sees AGENTS.md only if it decides to open the file. Use the @AGENTS.md line.

RailsFast, our Rails starter, ships with an AGENTS.md, a CLAUDE.md and Cursor rules in the repo; here's why we think that matters for agents.

Common CLAUDE.md mistakes

  • Letting /init write the final version. It's a good first draft. Then cut everything Claude could have learned from the repo.
  • Describing the architecture. Anthropic lists this as a habit to drop: Claude reads the repo itself.
  • Turning it into a manual. A 600-line file costs context on every task and buries the lines that matter. Procedures go to skills, file-specific rules to .claude/rules/.
  • Treating it as a guarantee. "Never commit secrets" in CLAUDE.md is a strong request, not a lock. Use a hook or a deny rule.
  • Contradicting yourself across files. Your global file says tabs, the project file says spaces, a nested file says something else. Claude may follow any of them.
  • Adding a CLAUDE.local.md to an AGENTS.md project. It silently stops Claude from reading AGENTS.md unless you switch Project instructions to claude-md-and-agents-md or import AGENTS.md.
  • Importing big docs with @. Imports don't save context. Point to the file instead.
  • Instructions only in chat. Anything you said only in the conversation can disappear after /compact. If it should last, put it in the file.
  • Never pruning it. Commands get renamed and folders move. A line pointing to something that no longer exists is worse than no line.

How to check that Claude reads your CLAUDE.md

Don't assume. Check:

  1. Run /context all in a session and look under Memory files. Every CLAUDE.md and CLAUDE.local.md that loaded at launch is listed. If yours isn't there, Claude can't see it.
  2. Run /memory to see the file locations for your session. For an AGENTS.md read directly, its path should be in this list too, and in an interactive session you'll see a line like no CLAUDE.md found; AGENTS.md loaded: /path/to/repo/AGENTS.md.
  3. For a nested CLAUDE.md, it won't be under Memory files at launch, because it loads on demand. Ask Claude to read a file in that folder and look for a Loaded line with the file's path.
  4. Ask Claude. "What do your project instructions say about running tests?" is a quick smoke test, and it's the method Anthropic suggests on versions before 2.1.280, where /memory didn't list a directly read AGENTS.md.
  5. Log it with a hook. The InstructionsLoaded hook fires every time a CLAUDE.md or rules file loads, at session start or later, with the reason (such as session_start, nested_traversal or path_glob_match). It's the reliable way to debug path-scoped rules.

If the file loads and Claude still ignores a line, the usual causes are a line that's too vague, a contradiction with another file, or a file that has grown long enough to bury it. If Claude Code itself is misbehaving, check whether Claude Code is down before you debug your setup. For the bigger picture of working with Claude Code day to day, read how to use Claude Code.

Frequently asked questions

What is a CLAUDE.md file?

A CLAUDE.md file is a Markdown file of instructions that Claude Code reads at the start of every session. It holds what Claude can't learn from your code: test and build commands, team conventions, and known traps. Claude treats it as context it tries to follow, not as enforced configuration.

Where should I put CLAUDE.md?

Put the project file at the root of your repository as CLAUDE.md (or .claude/CLAUDE.md) and commit it. Personal instructions for every project go in ~/.claude/CLAUDE.md, and personal notes for one project go in CLAUDE.local.md, which you add to .gitignore. A CLAUDE.md in a subfolder loads when Claude starts working on files in that folder.

What is the global CLAUDE.md?

The global, or user, CLAUDE.md lives at ~/.claude/CLAUDE.md and loads in every project you open on that machine. Use it for personal preferences that aren't specific to one codebase, like how you like commit messages written. Project files load after it, but both stay in context, so keep them from contradicting each other.

How long should a CLAUDE.md be?

Anthropic recommends keeping each CLAUDE.md under 200 lines. The whole file loads on every task, so longer files cost more context and get followed less reliably. Move procedures into skills and part-specific rules into .claude/rules/.

What is AGENTS.md?

AGENTS.md is an open, plain-Markdown format for giving coding agents project instructions, described on agents.md as "a README for agents." Codex, Cursor, Jules, Gemini CLI, GitHub Copilot's coding agent and many other tools read it, and it's stewarded by the Agentic AI Foundation under the Linux Foundation. It has no required fields or headings.

Does Claude Code read AGENTS.md?

Yes. Since Claude Code 2.1.277, Claude reads AGENTS.md by itself when there is no CLAUDE.md or CLAUDE.local.md in your working directory or above it. If you have both, it reads only CLAUDE.md unless you import AGENTS.md with an @AGENTS.md line or set Project instructions to claude-md-and-agents-md in /config.

Should I use CLAUDE.md or AGENTS.md?

If only Claude Code works in the repo, a CLAUDE.md is enough. If you or your team also use Codex, Cursor or another agent, write your shared instructions in AGENTS.md and either let Claude Code read it directly or add a CLAUDE.md containing @AGENTS.md plus any Claude-only lines. That keeps one source of truth instead of two files that drift apart.

What is CLAUDE.local.md?

CLAUDE.local.md is a personal instruction file at the root of a project that loads right after CLAUDE.md and is meant to stay out of git. Use it for your sandbox URLs, test accounts or personal habits in that repo. Be aware that it counts as a CLAUDE.md for the AGENTS.md check, so in a project that relies on AGENTS.md it stops Claude from reading that file unless you change the setting.

Should I use /init to create CLAUDE.md?

Yes, as a starting point. /init reads your codebase and drafts a file with the commands and conventions it finds, and it also folds in existing Cursor or Copilot rules. Then edit it down: Anthropic's own advice is that most of the work after /init is removing lines.

Does Codex read CLAUDE.md?

Not by default. Codex reads AGENTS.md and AGENTS.override.md, and looks for other file names only if you list them in project_doc_fallback_filenames in ~/.codex/config.toml. The simpler setup is to keep shared instructions in AGENTS.md and import it from your CLAUDE.md.

What is the difference between CLAUDE.md and Claude's memory?

You write CLAUDE.md; Claude writes auto memory. Auto memory is a set of notes Claude keeps for itself in ~/.claude/projects/<project>/memory/, with the first 200 lines or 25KB of its MEMORY.md index loaded each session. Both are managed from /memory, and if you want something in CLAUDE.md rather than auto memory, ask Claude to "add this to CLAUDE.md".

Sources

All checked on November 30, 2026.

Checklist

Grab your free launch checklist 🚀

The definitive checklist to launch any project. It includes pre-launch steps & a detailed launch checklist you can go through to get your new project off the ground.

Secure connection. No spam.