Guides

CLAUDE.md best practices

Keep it under 200 lines. Make every line something a reviewer could check. Move rules that only apply to part of the code into .claude/rules, and never use the file for things that must not happen. That's the short version. The long version is below: ten rules, a before and after for each, and the numbers from linting the steering files of 300 popular repos.

If you only read one paragraph. A good CLAUDE.md is a short list of facts Claude can't work out from the code (commands, layout, conventions), written so a reviewer could say yes or no to each line. Rules for one part of the codebase go in .claude/rules/ with a paths list. Procedures go in skills. Anything that must never happen goes in a permission rule or a hook. Then run /context once to see that it loaded.

What CLAUDE.md is, and what it isn't

CLAUDE.md is a markdown file Claude Code reads at the start of every session. The memory docs list where it can live: ./CLAUDE.md or ./.claude/CLAUDE.md for the project and the team, ./CLAUDE.local.md for your own uncommitted notes, and ~/.claude/CLAUDE.md for preferences that follow you into every project. Files in the folders above where you started load at launch. Files in subfolders load later, when Claude reads or edits something there. None of them override each other: the docs say they "are concatenated into context rather than overriding each other".

The sentence I'd pin above every desk is this one from the docs: "Claude treats them as context, not enforced configuration." Everything below follows from it. The file is advice, weighed against the rest of the conversation. Short, specific advice gets followed. Long, vague advice gets averaged away.

About the numbers. I build AsItIs, a read-only linter for these files. In November 2026 I ran it over the steering files of the 300 most-starred GitHub repos that have one and read a sample of every rule's findings by hand before quoting it. Where a number is used below, at least 9 of the 10 sampled findings held up; where they didn't, I say so. The full write-up, with the method and the two rules that turned out to be wrong, is at I linted the CLAUDE.md and AGENTS.md files in 300 popular repos.

1. Keep it under 200 lines

The docs give a number: "target under 200 lines per CLAUDE.md file. Longer files consume more context and reduce adherence." Both halves matter. A project CLAUDE.md sits in the context window for the whole session, so every request carries it. And the more rules there are, the less weight each one gets.

How far off are real projects? In the 300 repos, the median Claude Code session starts with about 2,400 tokens of steering text already loaded, before you type anything. 54 repos load more than 5,000 tokens, 19 load more than 10,000, and the largest loads about 32,600. 60 repos had at least one steering file past a vendor's recommended length. For scale, a 200-line file of short bullets is usually 1,500 to 3,000 tokens; the 368-line CLAUDE.md I measured for the token cost guide came to about 7,900.

Two hundred is a target, not a limit. Nothing breaks at line 201. But when a file gets long it is nearly always carrying one of the same few things: history, procedures that are needed now and then, or rules for one corner of the code. Each has a better home, covered below. History is the easiest cut. Claude doesn't need to know why the project moved frameworks twice; it needs to know which one is current.

## Background
We started on Express in 2022, moved to Fastify in 2023 for the plugin
system, then to Hono in 2024 for the edge runtime. The old Express
handlers are still under legacy/ but nothing calls them any more.
(another 30 lines of history)
## Layout
- HTTP handlers: `src/routes/` (Hono)
- `legacy/` is dead code. Don't edit it.

2. Write rules a reviewer can check

The docs ask for instructions "concrete enough to verify" and give three pairs worth copying as a pattern: "Use 2-space indentation" instead of "Format code properly", "Run npm test before committing" instead of "Test your changes", and "API handlers live in src/api/handlers/" instead of "Keep files organized".

My test is simpler: could a reviewer look at Claude's diff and say yes or no to the rule? "Write clean code" fails. "Every new endpoint has a test in tests/api/" passes. Exact commands, paths and names are cheap in tokens, and they're the part Claude can't work out by reading the code. "Be careful" costs tokens and tells it nothing.

Please write clean, well-tested code and follow best practices.
Be careful with the database.
- Every new endpoint gets a test in `tests/api/`
- Database access goes through `src/db/client.ts`; nothing else opens a connection

Structure helps too. The docs suggest grouping related instructions "under markdown headers and bullets" because "organized sections are easier for Claude to follow than dense paragraphs." Short headings such as Commands, Layout, Conventions and Don't also make the file easier for the next person to edit.

3. Commands and paths that are still true

Steering files rot faster than code, because nothing runs them. A folder gets renamed, a script is dropped from package.json, and the CLAUDE.md keeps saying the old thing. Claude tries the command, it fails, and then it guesses, which is the exact situation the file was meant to prevent.

From the scan: 36 repos had a relative link in a steering file pointing at a file or heading that no longer exists, and 10 of the 12 I checked were real, so call it about 30. Backticked paths that aren't in the repo were the noisiest rule I have. It fired in 63 repos, but only 4 of the 15 I read were genuinely stale; the rest were gitignored output folders, placeholders and paths that belong to a different repository. So I won't put a number on that one, only a habit: when you rename a folder or a script, grep the steering files too.

- Run `npm run test:unit` before committing
- Shared types live in `packages/common/types/`
- Run `pnpm test` before committing (vitest; there is no separate unit script)
- Shared types live in `packages/shared/src/types/`

The linter's stale-path, dead-command and broken-link rules catch the ones you miss.

4. Scope rules to the code they're about

A rule about API handlers costs tokens in every session, including the one where Claude is fixing CSS. A rule file in .claude/rules/ with a paths list in its frontmatter loads only when Claude works with files matching those globs. This is the biggest saving for most repositories, and it's the move that gets a 400-line CLAUDE.md under 200 without deleting anything.

Two things to know. Rules without paths load at launch exactly like CLAUDE.md, so they tidy the text without reducing it. And a paths glob that matches nothing (usually after a rename) means the rule never loads, silently; the path-rule-matches-nothing rule is there for that.

## API
- Validate every request body with the schemas in src/api/schemas
- Return errors as { error: string, code: string }
- Log with the request id from ctx.get("reqId")
(and 25 more lines that only matter under src/api/)
---
paths:
  - "src/api/**/*.ts"
---
# API rules
- Validate every request body with the schemas in src/api/schemas
- Return errors as { error: string, code: string }
- Log with the request id from ctx.get("reqId")

If a rule file doesn't seem to load, .claude/rules not loading lists the reasons. For a repo with several packages, CLAUDE.md in a monorepo covers what loads from which start folder.

5. Procedures go in skills

A release checklist is 40 lines you need once a month. In CLAUDE.md it's 40 lines in every session. As a skill, the skills docs say, "full skill content only loads when invoked", and only the one-line description sits in context all the time.

This is now normal: 192 of the 300 repos ship at least one SKILL.md. Two things from the scan worth knowing before you write one. 80 repos have a skill whose body is over 5,000 tokens, past what the Agent Skills spec recommends, so a skill can be too long as well. And 20 repos have skill or command frontmatter that isn't valid YAML, nearly always the same mistake: a description with an unquoted colon in it.

## Releasing
1. Bump the version in package.json and every packages/*/package.json
2. Run `pnpm changeset version`
3. Update CHANGELOG.md from the changeset output
(12 more steps, read in every session, used once a month)
---
name: release
description: "Cut a release: bump versions, run changesets, tag and publish"
---
1. Bump the version in package.json and every packages/*/package.json
2. Run `pnpm changeset version`
3. Update CHANGELOG.md from the changeset output
...

Is my SKILL.md valid has the frontmatter rules, and Claude Code skill not triggering covers the description.

6. Imports are for structure, not savings

CLAUDE.md can pull in other files with @path/to/file, up to four hops deep. It's tempting to treat that as a way to make the file smaller. It isn't: the docs say imports "help you organize a long file but don't reduce its context cost, because imported files load together with the CLAUDE.md that imports them". Use imports to share one file between CLAUDE.md and AGENTS.md, or to keep a long file readable, and nothing else.

When you do use them, write them so they load. The path is everything after the @ up to the next whitespace, so a full stop, colon or bracket touching the end of the path becomes part of it, and the import loads nothing, with no message. Backticks mean the opposite: "import parsing skips Markdown code spans and fenced code blocks", so that's how you mention a file, or an npm scope such as @acme/ui, without loading it. An import that points outside the project (a home-folder path, say) triggers an approval dialog the first time, and your teammates don't have that file anyway.

Follow @docs/style.md.
Conventions: @docs/conventions.md: keep them.
We publish @acme/ui from packages/ui.
@docs/style.md
@docs/conventions.md

We publish `@acme/ui` from packages/ui.

I tested every failing shape I know of against a headless Claude Code session; the results are in CLAUDE.md @import not working. The broken-import and import-outside-root rules report both cases above.

7. Say each rule once

Files grow by accretion. Someone adds "Always use named exports" in March, someone else adds "Default exports are fine for pages" in June, and now the file disagrees with itself. The docs are blunt about what happens: "if two instructions contradict each other, Claude may pick one arbitrarily." Duplicates are the milder version. They cost tokens twice and drift apart the first time someone edits only one copy.

How common is it? My linter flagged a repeated rule in 58 of the 300 repos, but when I read a sample only 6 of 10 were real repeats; the rest were parallel sections that happen to share a sentence. So treat that as "common", not as a number. What I can say is that the fix is always the same: one rule, with its exception written into it.

## Conventions
- Always use named exports.

## Pages
- Default exports are fine for pages.
## Conventions
- Named exports everywhere, except `app/**/page.tsx` and `layout.tsx`,
  which Next.js needs as default exports

Contradictions span files. Your project CLAUDE.md, a nested one in a subfolder, the rules folder and your own ~/.claude/CLAUDE.md all land in the same context. Read them together every few months and delete what's no longer true. The contradiction, duplicate-rule and cross-file-contradiction rules look for the same wording with opposite polarity, in one file and across files.

8. If you have an AGENTS.md too, import it

Claude Code reads AGENTS.md only when there is no CLAUDE.md. So a CLAUDE.md that says "See AGENTS.md" in a sentence reads fine to a person and loads nothing. The docs recommend the fix: a CLAUDE.md whose first line is @AGENTS.md, with anything Claude-specific after it. A symlink works too.

This one is common. 119 of the 300 repos have both files at the root. Most handle it on purpose: 29 of the CLAUDE.md files are symlinks and 52 import AGENTS.md. In 25 repos the CLAUDE.md neither imports, links to nor copies it, so Claude Code never sees the AGENTS.md. Counting subfolders, 49 repos have at least one AGENTS.md that Claude Code skips this way.

# Project
See AGENTS.md for the conventions. Claude-specific notes below.
@AGENTS.md

# Claude Code notes
- Use the `test` skill for the integration suite; it needs Docker

AGENTS.md vs CLAUDE.md has the full picture of which tool reads which file. The agents-md-shadowed rule flags an AGENTS.md that a CLAUDE.md above it hides.

9. Comments are for people

Block-level HTML comments in CLAUDE.md "are stripped before the content is injected into Claude's context", according to the docs, so a <!-- note --> on its own line costs no tokens. That makes it a good place for "why" notes aimed at whoever maintains the file. A comment on the same line as a rule is a different thing: it isn't block-level, it reaches Claude, and nobody reading the rendered file sees it.

Two cautions from the docs. "When you open a CLAUDE.md file directly with the Read tool, comments remain visible", and other agents may not strip them at all. So don't put anything in a comment you wouldn't want an agent to act on, and read the comments in files you didn't write. In the scan, 58 of the 300 repos had HTML comments in a steering file. I pulled all of them out and searched them for anything that read like an injected instruction. Every one was a generator marker, a licence header, a lint pragma or a note to maintainers. Same for secrets: not one API key or private key in 300 repos. So the risk is real in theory and rare in well-maintained repos; the file you should worry about is the one that arrived in a pull request.

- Run `pnpm test` before every commit <!-- Claude: skip this when the change is small -->
<!-- Maintainers: the test rule is here because the CI run takes 20 minutes. -->
- Run `pnpm test` before every commit. For a docs-only change, `pnpm test --filter docs` is enough.

More on this in hidden instructions in AGENTS.md. The hidden-comment rule counts comments; hidden-instruction fires only when one reads like an order to the agent.

10. Hard limits go in hooks, not sentences

Back to the sentence from the top: "context, not enforced configuration." Writing NEVER in capitals makes a rule louder, not binding. A permission rule or a PreToolUse hook runs before the tool call and can refuse it, whatever Claude has decided. Keep the sentence as well, so Claude knows why and plans around it, but don't rely on it for anything you'd be unhappy to see happen once.

## Don't
- NEVER EVER run `git push --force`. This is EXTREMELY important!!!
## Don't
- Don't force-push; main needs a linear history for the release tooling.

(and in .claude/settings.json)
{
  "permissions": {
    "deny": ["Bash(git push --force:*)", "Bash(git push -f:*)"]
  }
}

A full before and after

Here is a typical first draft. It's short, which is good, but almost none of it can be checked, it mentions AGENTS.md without loading it, and it contradicts itself in one line.

# Project
This is a Next.js app. Please write clean, well-tested code and follow best practices.
Keep things organized. Be careful with the database.
See AGENTS.md for more. Always use named exports. Default exports are fine for pages.
NEVER push to main!!!

And a version that loads the shared file, gives Claude the facts it couldn't guess, makes every rule checkable, writes the one exception into the rule, and leaves enforcement to a deny rule:

@AGENTS.md

# Acme web

## Commands
- Install: `pnpm install` (not npm)
- Test: `pnpm test`; run it before every commit
- Types: `pnpm typecheck`

## Layout
- Pages: `app/`; API handlers: `src/api/handlers/`
- Database access only through `src/db/client.ts`

## Conventions
- Named exports everywhere, except `app/**/page.tsx` and `layout.tsx`
- New API handlers get a test in `tests/api/`

## Don't
- Don't edit `src/db/migrations/`; create a new migration instead
- Don't push to main (a deny rule in .claude/settings.json enforces this)

Checklist

  • Under 200 lines, with short headings and bullets
  • Every rule is something a reviewer could say yes or no to
  • Exact commands, paths and names, and they match what's in the repo today
  • Rules for one area are in .claude/rules/ with a paths list that matches real files
  • Procedures are skills, with the description quoted if it contains a colon
  • Imports on their own lines; npm scopes and other @names in backticks
  • Each rule once, with its exception written into it, across every file that loads
  • If there is an AGENTS.md, the CLAUDE.md imports it
  • No instructions in comments, nothing in a comment you wouldn't want an agent to read, no secrets
  • Anything that must never happen is a permission rule or a hook, not a sentence
  • /context shows the file under Memory files

Check your file

Paste a CLAUDE.md into the steering file checker and it reports the length and token cost, repeated or contradicting rules, imports that won't load as written and hidden comments. It runs in your browser; nothing is uploaded.

Open the steering file checker

To check every steering file in a project from inside Claude Code, including links, @imports and what loads every session, install the free AsItIs plugin. It is read-only and makes no network calls.

claude plugin marketplace add Katta041/asitis-agent-tools
claude plugin install asitis@asitis

About the pluginSource on GitHub

MIT licensed. A star on GitHub helps other people find it.

Questions

How long should a CLAUDE.md be?

Under 200 lines. Anthropic's Claude Code docs say to target under 200 lines per file, because longer files consume more context and reduce adherence. In the 300 popular repos I scanned, the median Claude Code session starts with about 2,400 tokens of steering text loaded, and 54 repos load more than 5,000. If yours is well past 200 lines, move rules for one part of the code into .claude/rules with a paths list and procedures into skills.

Does splitting CLAUDE.md into @imports save tokens?

No. The docs say imports help you organise a long file but don't reduce its context cost, because the imported files load together with the CLAUDE.md that imports them, up to four hops deep. The only things that cut what loads every session are deleting text, path-scoped rules and skills.

Does Claude Code always follow CLAUDE.md?

No. The docs say Claude treats these files as context, not enforced configuration. A rule in CLAUDE.md is weighed against everything else in the conversation. For something that must never happen, such as a force-push or an edit under migrations/, use a permission deny rule or a PreToolUse hook, which run before the tool call and can refuse it.

Should I write CLAUDE.md or AGENTS.md?

Both, with one line of glue. Put the shared instructions in AGENTS.md, which Codex, Copilot, Cursor and others read, and make CLAUDE.md a file whose first line is @AGENTS.md followed by anything Claude-specific. Claude Code reads AGENTS.md only when there is no CLAUDE.md, so a CLAUDE.md that just says "see AGENTS.md" means Claude never sees it. In my scan, 25 of the 119 repos with both files at the root had exactly that problem.

How do I check what Claude Code actually loaded?

Run /context in the session and read the list under Memory files; a CLAUDE.md, rule or expanded import that loaded is listed there. For a log you can grep, add an InstructionsLoaded hook. Outside a session, asitis loads prints what each tool reads at launch for a folder, and asitis lint reports imports that won't load, files past the length limits and contradictions.

See what an agent changed in your CLAUDE.md. Keep or revert each change.

The linter is free and needs no signup. I'm also building a desktop editor for the markdown your agents write and read. It shows what an agent changed since you last looked, lets you keep or revert each change, and never touches a line you didn't edit. No release date yet. One email to confirm, one when the paid beta opens, one when 1.0 ships. $29 once for a founding licence, with 24 months of updates.

Or add the linter to CI, no email needed.

Your address is stored with Cloudflare, which hosts this site. No IP address is kept. See the privacy policy.

Related: Claude Code ignores my CLAUDE.md, how many tokens does my CLAUDE.md cost, CLAUDE.md in a monorepo, CLAUDE.md @import not working, AGENTS.md vs CLAUDE.md, hidden instructions in AGENTS.md, why did my editor change my CLAUDE.md. The same advice for other tools: Cursor rules, Copilot custom instructions, GEMINI.md.

Sources: Claude Code docs, How Claude remembers your project and Skills; quotes are exact, checked on 14 October and 23 November 2026. Numbers are from the 300-repo scan of 18 November 2026 with asitis 0.4.5, each rule's findings sampled by hand; the research page has the method and the caveats.