Coderefercoderefer
CoursesWebinarsBlogAbout
Coderefercoderefer

Discover. Learn. Automate. Grow.

Learn

  • Courses
  • Webinars
  • Blog
  • Search

Company

  • About
  • Contact
  • FAQ

Legal

  • Privacy Policy
  • Terms of Service
  • Refund Policy
© 2026 Coderefer. All rights reserved.
HomeBlogHow to Write a CLAUDE.md File: Best Practices and a Copy-Paste Template
ai-toolsDecember 1, 202610 min read

How to Write a CLAUDE.md File: Best Practices and a Copy-Paste Template

What actually belongs in a CLAUDE.md file, what to leave out, where it lives, and a ready-to-use template based on Anthropic's own Claude Code docs.

V

Vamsi Tallapudi

Manager, Architect Technology at Cognizant

How to Write a CLAUDE.md File: Best Practices and a Copy-Paste Template

A CLAUDE.md file is a plain markdown file, usually sitting at the root of your repo, that Claude Code reads at the start of every session so it doesn't have to re-learn your build commands, your conventions, and your gotchas from scratch each time. That's the whole idea. It's not a config file Claude Code enforces. It's context the model reads and tries to follow, the same as a message you typed yourself.

Anthropic's own docs put it plainly: CLAUDE.md is for guidelines, and Claude is allowed to decide a guideline doesn't apply. If you need something to happen every single time with zero exceptions, that's a job for a hook or a permission rule, not a line in CLAUDE.md. Keep that distinction in mind and most of the confusion about this file goes away.

What actually goes in a CLAUDE.md file

The official best-practices page gives a simple test before you write anything down: could Claude find this by reading the code, and would it actually go look? If both answers are yes, leave it out. What's left is usually four things:

  • Exact commands: the real build, test, lint, and single-test-file commands, in code blocks, so Claude runs them verbatim instead of guessing a variant that doesn't exist in your repo
  • Misleading code: an old pattern that's everywhere in the codebase but that new code must not copy, or a directory that looks dead but isn't
  • Decisions that aren't in the repo: which error helper the team settled on, how deploys actually work, what change needs a migration review
  • Anything you've already corrected twice: if you've typed the same clarification into chat two sessions in a row, that's the signal it belongs in the file

Here's the example straight from Anthropic's docs, and it's a good reference for how short this should be:

# Commands
- Install: pnpm install
- Run one test file: pnpm vitest run path/to/file.test.ts
- Typecheck: pnpm tsc --noEmit

# Conventions
- Use the error helpers in src/lib/errors.ts, never throw raw strings
- src/legacy/ is still live but don't copy its patterns into new code

# Gotchas
- Integration tests need DATABASE_URL set, see .env.example

Eleven lines. That's it. No architecture diagram, no list of dependencies, no paragraph explaining what React is. Claude already knows React. What it doesn't know is that your src/legacy/ folder looks abandoned but still ships to production.

What to leave out

This is the part most people get backward. The instinct when setting up a new tool is to over-document, and CLAUDE.md punishes that instinct directly: every line loads into every session whether it's relevant to the current task or not. Anthropic's docs list a specific table of advice that used to matter and now actively hurts you, including "describe your architecture and file layout in CLAUDE.md", since Claude reads the repo itself, and writing it down a second time just adds dead weight.

Also skip:

  • Style rules a linter already enforces (Prettier, ESLint, rustfmt): point at the config instead
  • Full API documentation (link to it; don't paste it in)
  • "Write clean code," "test your changes," or anything else Claude already does by default
  • IMPANT-style emphasis on lines Claude keeps missing. Piling on capital letters doesn't fix a rule current models ignore: either the file's too long for that line to land, or the rule has exceptions a hook should be handling instead

That last point is worth sitting with. If you've ever written **IMPORTANT: ALWAYS run tests before committing** in all caps three times in the same file, you've found a rule that belongs in a Stop hook, not a CLAUDE.md line. A hook exits with code 2 and blocks Claude from finishing until tests actually pass. No amount of bold text does that.

Where a CLAUDE.md file lives

Claude Code loads these in order, from broadest to most specific, concatenating all of them into context rather than letting one override another:

ScopeLocationShared with
Managed policy/etc/claude-code/CLAUDE.md (Linux/WSL), equivalent on macOS/WindowsEveryone in the org
User~/.claude/CLAUDE.mdOnly you, across all projects
Project./CLAUDE.md or ./.claude/CLAUDE.mdTeam, via git
Local./CLAUDE.local.mdOnly you, this project (gitignore it)

Claude loads CLAUDE.md and CLAUDE.local.md from your working directory and every directory above it at launch. Files in subdirectories load on demand, the moment Claude reads, writes, or edits a file in that subdirectory, not before. That matters if you're debugging why a nested CLAUDE.md "isn't working": it's often just that Claude hasn't touched that folder yet in the session.

Run /context all in a session any time you want to confirm exactly which memory files loaded.

CLAUDE.md vs. AGENTS.md

As of Claude Code v2.1.277, Claude reads a repository's AGENTS.md directly, in place of CLAUDE.md, if there's no CLAUDE.md or CLAUDE.local.md anywhere in the working directory or above it. Thariq Shihipar, who works on Claude Code at Anthropic, announced the change on X: "Starting today in version 2.1.277, if there is no CLAUDE.md in a folder, Claude will check for and use AGENTS.md."

If both files exist, Claude reads CLAUDE.md and ignores AGENTS.md by default. You can change that in /config by setting Project instructions to claude-md-and-agents-md, which loads both, CLAUDE.md first. If your project is already shared with other coding agents and you don't want two files to maintain, the cleanest move is a one-line import:

@AGENTS.md

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

That keeps AGENTS.md as the single source every tool reads, with anything Claude-specific layered underneath.

CLAUDE.md vs. auto memory

Claude Code also ships "auto memory" — notes Claude writes itself based on your corrections, separate from CLAUDE.md. The difference comes down to who's holding the pen:

CLAUDE.mdAuto memory
Who writes itYouClaude
ContainsInstructions and rulesLearnings and patterns
Loaded intoEvery session, in fullFirst 200 lines or 25KB of its index
Best forCoding standards, commands, architecture notesYour preferences and context Claude can't derive from code

The practical rule: if you want Claude to always know it, put it in CLAUDE.md. If you just want Claude to stop repeating a mistake you corrected once, let auto memory pick it up, or say "remember that" and move on.

How long should it be?

Anthropic's docs give a specific number: target under 200 lines per CLAUDE.md file. Go over, and Claude Code shows a warning at startup and in /status. Files over 4 MiB get skipped entirely. For subdirectory CLAUDE.md files and path-scoped rules that load on demand, there's a per-file character limit too — 40,000 characters on a 200K-token context window, 200,000 characters on a 1M window for Opus 4.6 and Sonnet 4.6, and 150,000 characters on newer models. Go past that limit and Claude only sees the start of the file, with a note telling it to read the rest manually if it decides to.

None of this is a hard ceiling Claude Code refuses to cross. It's a point past which the file stops pulling its weight. Nick Babich, who writes regularly about Claude Code workflows, put a related point well in a thread on X breaking down five practices for a well-crafted CLAUDE.md — the idea that a file packed with low-value detail trains Claude to deprioritize the whole thing, not just the noisy parts.

If your file keeps growing past 200 lines because the project genuinely has that much to say, that's the signal to split it, not to keep cramming:

  • .claude/rules/ — topic files like testing.md or api-design.md, optionally scoped to a paths: glob in frontmatter so they only load when Claude touches matching files
  • Skills — a full procedure, like cutting a release or running a security review, that only needs to load when that specific task comes up. We've covered how to set up skills with a SKILL.md file if you haven't used them yet
  • @path imports — pull a README or a git-workflow doc into CLAUDE.md with @docs/git-instructions.md syntax. Worth knowing: imports help you organize the file, they don't reduce its context cost, since the imported content still loads with it

Should you run /init?

Yes, as a starting point — not as a finished product. /init analyzes your codebase and writes a CLAUDE.md with the commands, structure, and conventions it can detect automatically. Anthropic's own docs describe the work that follows as "mostly removing lines," which is the opposite of what most people expect from a generator. /doctor goes further and proposes specific cuts for anything Claude can already infer from the code, and /doctor prompt-audit (available from Claude Code v2.1.283) flags instructions written for older models, references to files that no longer exist, and rules that quietly contradict each other across your CLAUDE.md, rules, and skills.

Run /init, read every line it generated, and delete anything that passes the "Claude would find this anyway" test. That's the whole workflow.

A copy-paste CLAUDE.md template

Here's a structure based directly on what Anthropic's docs recommend keeping, adjusted so you can drop in your own project's details:

# Commands
- Install: <your install command>
- Dev server: <your dev command>
- Run all tests: <your test command>
- Run one test file: <your single-test command>
- Typecheck: <your typecheck command>
- Lint: <your lint command>

# Conventions
- <A decision Claude can't infer, e.g. "all API routes live under src/api/v1/">
- <An error-handling convention, e.g. "use the helpers in src/lib/errors.ts">
- <A naming or import-order rule that's non-obvious>

# Gotchas
- <A directory that looks dead but isn't, and why>
- <An environment variable integration tests need, and where it's documented>
- <An old pattern that's everywhere but new code must not copy>

# Workflow
- <When Claude should ask before proceeding, e.g. "plan mode for changes under src/billing/">
- <Anything about PRs, branch naming, or commit messages your team actually follows>

Fill in only the sections where you have something Claude genuinely can't get from the code. An empty section is better than a section padded with filler just to look thorough.

Common mistakes worth avoiding

  • Writing it once and never touching it again. Claude's own docs suggest treating it as a living document: when Claude makes the same mistake twice, that's the trigger to add a line, and when a line stops being true, delete it the same day
  • Documenting your architecture. It's the single most common thing people put in CLAUDE.md that Anthropic explicitly says to stop doing, because Claude reads the repo itself and does it faster than you can write it down
  • Putting secrets in it. CLAUDE.md goes into version control. Treat it like any other committed file — no API keys, no connection strings, no internal URLs you wouldn't put in a README
  • Confusing a guideline with a rule. "Always run the formatter after editing" sounds absolute, but as a CLAUDE.md line it's still something Claude can skip under pressure. If missing it even once is unacceptable, it belongs in a PostToolUse hook, which Anthropic's docs walk through for exactly this case
  • Never checking what loaded. Run /context all after any change to confirm the file Claude is actually reading matches the file on disk

Watch: CLAUDE.md in practice

CLAUDE.md best practices walkthrough

If you're setting up Claude Code on a new project, pair this with a look at how hooks can enforce the rules CLAUDE.md can't, and if you're scaling across a team, the subagent starter pack covers delegating work without bloating your main context. For connecting external tools Claude can't see on its own, setting up MCP servers is the next step after your CLAUDE.md is solid.

A good CLAUDE.md file is boring. It's a handful of commands, a short list of things that would trip up a new teammate, and nothing else. If yours reads like documentation you'd hand to a new hire on day one, you're probably doing it right. If it reads like an architecture decision record, start deleting.

FAQ

What is a CLAUDE.md file?
It's a plain markdown file, usually at the root of a repo, that Claude Code reads at the start of every session. It holds persistent instructions (build commands, conventions, project quirks) so Claude doesn't have to rediscover them each time. Per Anthropic's docs, it's context the model reads, not configuration the system enforces.
How long should a CLAUDE.md file be?
Anthropic recommends keeping each CLAUDE.md file under 200 lines. Files over that consume more context per session and reduce how reliably Claude follows them. Some teams keep theirs under 60 lines and push everything else into .claude/rules/ or skills.
Does Claude Code read AGENTS.md instead of CLAUDE.md?
Since Claude Code v2.1.277, Claude reads a repository's AGENTS.md automatically if there's no CLAUDE.md or CLAUDE.local.md in the working directory or above it. If a CLAUDE.md exists, Claude reads that instead, unless you set Project instructions to claude-md-and-agents-md in /config.
Should I run /init to generate my CLAUDE.md?
Yes, as a starting point. /init analyzes your codebase and writes commands, structure, and conventions it can detect. Treat the output as a draft to cut down, not a finished file: Anthropic's docs describe the work after /init as mostly removing lines, not adding them.
What's the difference between CLAUDE.md and auto memory?
You write CLAUDE.md yourself; it holds rules and instructions and loads every session. Auto memory is notes Claude writes itself from your corrections and preferences, stored per project, and only the first 200 lines or 25KB of its MEMORY.md index loads at session start.
Can I put secrets or API keys in CLAUDE.md?
No. Treat CLAUDE.md like any other file in version control: assume it's shared. Keep API keys, credentials, and connection strings out of it entirely, and use environment variables or a gitignored CLAUDE.local.md for anything personal.
ai-tools claude-code claude-md 2026

Comments

    Get AI tricks that save you hours every week

    New AI tools, automation workflows, and course drops — straight to your inbox. Join 2,400+ builders.

    Read Next

    Claude Code Hooks Tutorial: PreToolUse and PostToolUse Examples You Can Copy
    ai-tools

    Claude Code Hooks Tutorial: PreToolUse and PostToolUse Examples You Can Copy

    Copy-paste PreToolUse and PostToolUse hook configs for Claude Code: block rm -rf, protect .env files, auto-format on save, and feed test failures back to Claude.

    November 29, 20267 min read
    How to Create a Claude Code Skill: SKILL.md Step-by-Step Guide (2026)
    ai-tools

    How to Create a Claude Code Skill: SKILL.md Step-by-Step Guide (2026)

    A step-by-step guide to writing SKILL.md for Claude Code: required frontmatter, instructions, scripts, testing, and the spec vs Claude Code differences.

    November 28, 20267 min read
    How Claude Code and Playwright MCP Write Real Browser Tests
    ai-tools

    How Claude Code and Playwright MCP Write Real Browser Tests

    Claude Code and Playwright MCP write real Playwright tests from a live DOM snapshot instead of guessing selectors. Setup steps and honest limits inside.

    November 26, 20265 min read
    View all posts →