Fix the cause

CLAUDE.md best practices: how long it should be, what to put in it and what to leave out

CLAUDE.md best practices from Anthropic's docs. What to put in it, how long it should be, imports, path-scoped rules, and AGENTS.md for Codex.

A long CLAUDE.md, trimmed and moved into scoped files, becomes a lean CLAUDE.md.Long CLAUDE.mdTrim and scopeMDLean CLAUDE.md
On this page
  1. Where CLAUDE.md files live
  2. What to put in it
  3. How long should CLAUDE.md be?
  4. Scope instructions to where they matter
  5. A lean CLAUDE.md example
  6. What to leave out
  7. AGENTS.md for Codex
  8. How TokenNibbler helps you keep it lean
  9. Checklist

Short answer: Keep CLAUDE.md under about 200 lines and fill it only with facts Claude needs in every session: build and test commands, conventions, project layout and “always do X” rules. Write each instruction so it can be checked, group them under headings, and move anything that matters for only part of the codebase into path-scoped rules or skills. Imports organise a long file but don’t make it cheaper.

CLAUDE.md is a plain Markdown file of instructions that Claude Code reads at the start of every session. You write it once, and Claude starts each session already knowing your build commands, your conventions and how the project is laid out. Because it loads every time, it is also part of every request’s context, so its size matters as much as its content.

This guide collects Anthropic’s own CLAUDE.md best practices, with a short example and what to leave out.

Where CLAUDE.md files live

Claude Code reads CLAUDE.md files from several places, from broadest to most specific (memory docs):

ScopeLocationUse it for
Managed policyOn Windows, C:\Program Files\ClaudeCode\CLAUDE.mdOrganisation-wide standards set by IT
User~/.claude/CLAUDE.mdYour personal preferences, in every project
Project./CLAUDE.md or ./.claude/CLAUDE.mdTeam-shared instructions, committed to the repository
Local./CLAUDE.local.mdYour own notes for this project; add it to .gitignore

Files in the folders above where you start Claude are loaded at launch. A CLAUDE.md in a subfolder loads on demand, once Claude reads or edits a file in that folder. All of them are combined, not overridden, so a project instruction is read after a user instruction.

If you are starting from nothing, /init analyses your codebase and writes a first CLAUDE.md with build commands, test instructions and conventions it finds. If a CLAUDE.md already exists, it suggests improvements instead.

What to put in it

Anthropic’s advice is to treat CLAUDE.md as “the place you write down what you’d otherwise re-explain.” Add 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 you typed last session.
  • A new teammate would need the same context to be productive.

Keep it to facts Claude should hold in every session: build commands, conventions, project layout and “always do X” rules (memory docs).

Write each instruction so it can be checked. The docs give clear pairs:

  • “Use 2-space indentation” instead of “Format code properly”.
  • “Run npm test before committing” instead of “Test your changes”.
  • “API handlers live in src/api/handlers/” instead of “Keep files organized”.

How long should CLAUDE.md be?

Anthropic’s guidance is to “target under 200 lines per CLAUDE.md file”, because “longer files consume more context and reduce adherence” (memory docs). The costs page says the same: “Aim to keep CLAUDE.md under 200 lines by including only essentials.”

Size matters twice over. Anthropic’s help centre notes that CLAUDE.md is prepended to every turn (help centre), so every line you add is sent again with each request in each session. Prompt caching makes the repeat cheaper, but a long file still adds up across a busy week.

Imports don’t reduce the cost

You can pull other files in with @path/to/file. That helps you organise a long file, but the docs are explicit that imports “don’t reduce its context cost, because imported files also load at launch.” To mention a path without importing it, wrap it in backticks.

Structure helps Claude follow it

“Group related instructions under markdown headers and bullets. Organized sections are easier for Claude to follow than dense paragraphs.” Also check for instructions that contradict each other: Claude “may pick one arbitrarily.” Running /doctor prompt-audit asks Claude to look for outdated or conflicting instructions and propose edits, without changing anything until you ask.

Scope instructions to where they matter

Not every rule belongs in the file that loads every session. Claude Code gives you two places to move them.

Path-scoped rules. Put a Markdown file in .claude/rules/ with a paths list in its frontmatter, and it loads only when Claude reads or edits a matching file:

---
paths:
- "src/api/**/*.ts"
---
# API rules
- Every endpoint validates its input.
- Use the standard error response format.

Rules without paths load at launch, like CLAUDE.md itself.

Skills. Multi-step procedures, such as a release checklist or a database migration, belong in a skill. A skill’s body “loads only when it’s used”, so the costs page recommends moving specialised instructions there: “those tokens are present even when you’re doing unrelated work” when they sit in CLAUDE.md.

One trade-off: path-scoped rules and nested CLAUDE.md files are summarised away when a session compacts, and come back only when Claude touches a matching file again. The project-root CLAUDE.md is re-read from disk after /compact (context window docs). Keep anything that must always apply in the root file.

A lean CLAUDE.md example

This is the shape to aim for: short, specific and checkable.

# Project: billing-api
## Commands
- Install: `pnpm install`
- Test one file: `pnpm test path/to/file.test.ts`
- Lint and type-check before committing: `pnpm lint && pnpm typecheck`
## Layout
- HTTP handlers: `src/api/handlers/`
- Business logic: `src/core/` (no framework imports here)
- Database access only through `src/db/repo.ts`
## Conventions
- TypeScript strict mode; no `any`.
- Money is integer cents, never floats.
- New endpoints need a test in `tests/api/`.
## Always
- Run the single relevant test file, not the whole suite.
- Ask before changing anything in `migrations/`.
# Compact instructions
When compacting, keep test output and code changes.

The last section uses a pattern from Anthropic’s costs page: a short note in CLAUDE.md that tells Claude what to keep when it compacts.

What to leave out

  • Long procedures. Move them to skills.
  • Rules for one folder or file type. Move them to path-scoped rules.
  • Whole documents pulled in with @. They load in full every session. Link to them in backticks and let Claude read them when needed.
  • Vague advice. “Write clean code” can’t be checked, so it adds tokens without changing behaviour.
  • Notes for humans. Block-level HTML comments (<!-- like this -->) are stripped before CLAUDE.md enters Claude’s context, so use them for maintainer notes.
  • Old instructions. Review the file now and then and delete what no longer applies.

One more tip: edits to the project or user CLAUDE.md don’t apply to a running session. The new version loads on the next /clear, /compact or restart (prompt caching docs).

AGENTS.md for Codex

Codex reads AGENTS.md instead. According to OpenAI’s Codex docs, it checks your Codex home folder (~/.codex) first, then walks from the project root down to your current folder, taking at most one file per folder and joining them with blank lines. It stops adding files once the combined size reaches project_doc_max_bytes, 32 KiB by default. The same advice applies: short, specific, and only what every session needs.

If one repository serves both agents, Claude Code can read AGENTS.md directly when there is no CLAUDE.md, or you can put @AGENTS.md at the top of a CLAUDE.md and add Claude-specific lines below it. On Windows, Anthropic recommends the import over a symlink (memory docs).

How TokenNibbler helps you keep it lean

TokenNibbler, a desktop app for Windows 11 and Mac, shows what your instruction files actually cost and helps you fix them from your own data.

See the size. Open a project in Projects and choose Where tokens go. The Loaded with every request card lists each instruction file Claude Code and Codex start every request with, its size in tokens, and which files are @-imported by another. If an instruction file grows sharply, the Drain Detector can name it as a likely cause when your plan drains faster than usual.

Get recommendations. Choose Analyse project and your own Claude Code or Codex reviews the project’s numbers (usually 1–3 minutes). It suggests Instructions for future sessions tailored to how you work in that project.

Add them safely. Choose Add to CLAUDE.md or Add to AGENTS.md and confirm. TokenNibbler adds one section between TokenNibbler markers, so a later analysis replaces that section instead of adding another copy. Anything already in the file stays, and the current file is backed up first. To undo, delete the marked section or restore the backup. Prefer to do it yourself? Choose Copy and paste it in.

Compare before and after. The Stretch Report (the Results tab on a project) compares the four weeks before your first change from a recommendation with the time since. Each measure is per prompt, per active hour or per session, so a busier week does not look worse. Under Also changed it lists anything else that moved at the same time, such as the model mix, the agent version or how much you worked, so you can see what else moved alongside your fix. Each side needs at least three active days before it shows a result.

See Recommendations and Projects for the details.

Checklist

  • Under about 200 lines.
  • Only facts every session needs: commands, layout, conventions, firm rules.
  • Each instruction specific enough to check.
  • Grouped under headings, with no contradictions.
  • Procedures in skills; folder-specific rules in .claude/rules/ with paths.
  • No large @ imports.
  • Reviewed when Claude repeats a mistake, and trimmed when something stops applying.

Guidance can change between Claude Code versions, so check the memory docs for the current details at the time of writing (October 2026). One-click fixes and the Stretch Report are part of Pro; see pricing, or download TokenNibbler for 14 days of Pro, free.

Sources

  1. Claude Code docs, How Claude remembers your project (CLAUDE.md) checked 15 October 2026
  2. Claude Code docs, Manage costs effectively checked 15 October 2026
  3. Claude Code docs, Explore the context window checked 15 October 2026
  4. Claude Code docs, How Claude Code uses prompt caching checked 15 October 2026
  5. Claude Help Center, Models, usage, and limits in Claude Code checked 15 October 2026
  6. OpenAI Codex docs, Custom instructions with AGENTS.md checked 15 October 2026

Limits and plans change. This post describes them as of 14 October 2026, and is re-checked when Anthropic or OpenAI change them.

Asif Syed Founder of TokenNibbler

Asif Syed is the founder of TokenNibbler, the desktop app that shows developers where their Claude Code and Codex usage goes and how to make their plans last. Every post here is checked against the app’s code and the official Anthropic and OpenAI documentation before it is published.

See where your own usage goes.

TokenNibbler shows your live Claude and ChatGPT limits, which project used them, and the fix. 14 days of Pro, free.

14 days of Pro, free with one click. No account, no card. Managing a team? Start a free team trial for everyone you invite.

Windows 11, 64-bit · On a Mac? Download for Mac → · Pricing →