Context in practice For engineers
CLAUDE.md best practices: what to put in it, with a template
CLAUDE.md best practices come down to one rule: write only the facts Claude should hold in every session, such as build commands, conventions, project layout and "always do X" rules. Keep each file under about 200 lines and concrete enough to verify. Everything else belongs in a path-scoped rule, a skill, a hook or the task prompt.
Short answer: what should go in CLAUDE.md?
Put facts in CLAUDE.md that Claude should hold in every session and cannot learn by reading the code: commands, conventions, layout and standing rules. Anthropic's memory documentation says to keep it to those facts and to move multi-step procedures or part-of-the-codebase guidance into a skill or a path-scoped rule.
CLAUDE.md is context, not enforced configuration. Claude follows specific, concise instructions more consistently, and a hook is the right tool when an action must be blocked. Checked against the Claude Code docs on 16 November 2026.
What belongs in a project CLAUDE.md?
Add an instruction when it passes a repeat test. The documentation suggests adding to CLAUDE.md when Claude makes the same mistake twice, a code review catches something Claude should have known, or you type the same correction as last session. A new teammate needing the same context is a fourth signal.
Write each line so you can verify it. The docs contrast "Use 2-space indentation" with "Format code properly", and "Run npm test before committing" with "Test your changes".
- Build, test, lint and run commands, exactly as typed.
- Project layout: where handlers, migrations and tests live.
- Conventions that differ from the language default.
- Standing rules: branch names, commit format, review expectations.
- Gotchas Claude cannot infer, such as a required environment variable.
A copyable CLAUDE.md template
This template uses markdown headers and bullets, which the docs say are easier for Claude to follow than dense paragraphs. Replace every value with your own, and delete any section you cannot fill with a concrete fact.
# Project: <name>
## Commands
- Install: `npm ci`
- Test one file: `npm test -- path/to/file.test.ts`
- Lint and typecheck: `npm run lint && npm run typecheck`
## Layout
- API handlers: `src/api/handlers/`
- Database migrations: `db/migrations/` (never edit an applied one)
## Conventions
- TypeScript strict mode; no `any` without a comment saying why.
- Errors use the shared `AppError` class in `src/errors.ts`.
## Workflow
- Branch names: `feat/<ticket>-<slug>`.
- Run the tests for changed files before proposing a commit.
## Gotchas
- Integration tests need `DATABASE_URL` set to the local Docker database.
## More detail
- API rules load from `.claude/rules/api.md` when API files are touched.
- Architecture overview: @docs/architecture.mdWhat should you not put in CLAUDE.md?
Leave out anything that does not need to load in every session, or that Claude should not treat as a suggestion. The docs say auto memory skips what Claude can derive from the codebase, and the same logic applies to a file you write by hand.
| Do not put in CLAUDE.md | Put it here instead |
|---|---|
| Rules that must never be broken | A PreToolUse hook or a permission rule |
| Multi-step procedures such as a release runbook | A skill, which loads on demand |
| Guidance for one part of the codebase | A .claude/rules/ file with a paths field |
| Secrets, tokens and credentials | Environment variables or a secret manager, never a committed file |
| Personal preferences and sandbox URLs | CLAUDE.local.md, added to .gitignore |
| Facts the code already states | Nowhere. Claude reads the code |
| Two rules that contradict each other | Delete one. Claude may pick either |
How long should CLAUDE.md be?
Target under 200 lines per CLAUDE.md file. According to the memory documentation, longer files consume more context and reduce adherence. This is a documented target for adherence, not a hard cutoff, so treat 200 as a budget to stay under.
Group related instructions under headers and bullets, and review the file when the codebase changes. Run /doctor prompt-audit (Claude Code v2.1.283 or later) to have Claude flag outdated commands, missing files and contradictions. It proposes edits and changes nothing until you ask. Block-level HTML comments are stripped before injection, so they hold maintainer notes at no context cost.
When should you use .claude/rules or imports?
Use .claude/rules/ files when guidance applies to only part of the codebase, and use imports when you want to organize a file you already need. The two have different context costs.
A rule with a paths field in its frontmatter loads when Claude reads, writes or edits a matching file, so it keeps unrelated work clear of it. Rules without paths load at launch. An @path import does not save context: imported files load together with the CLAUDE.md that imports them, up to four hops deep. A file in a subdirectory also loads on demand once Claude touches a file there.
---
paths:
- "src/api/**/*.ts"
---
# API rules
- Validate every request body before use.
- Return errors in the shared error format.Where native tools are enough
For one engineer or a small team on one repository, CLAUDE.md, .claude/rules/ and /init cover most needs. Run /init to generate a starting file from your codebase, then add what Claude would not discover alone. Run /context all to confirm which files loaded. If your repository already uses AGENTS.md, the docs describe when Claude Code reads it, and you can import it with @AGENTS.md.
Claude Code's auto memory also records your corrections, but only on your machine. A committed CLAUDE.md is what teammates share. Nothing more is needed until you need decisions and their reasoning, not rules.
Where Shifu fits
A rule file records what to do. It rarely records why, or which incident and rejected proposal produced the rule. Shifu is a context engine for AI coding teams. It is designed to retrieve the decisions, constraints and reasoning relevant to the task at hand, with sources, so CLAUDE.md can stay short. We have not published customer results yet. Retrieval still needs an applicability check against the current code.
CLAUDE.md checklist
Run this before you commit a new or changed CLAUDE.md.
- Confirm every line is concrete enough to verify.
- Check the file is under about 200 lines.
- Move path-specific guidance into .claude/rules/ with a paths field.
- Replace any must-never rule with a hook or permission rule.
- Search for contradictions, including in nested files and rules.
- Run /context all and confirm the file appears under Memory files.
Frequently asked questions
How long should a CLAUDE.md file be?
Anthropic's memory documentation recommends targeting under 200 lines per CLAUDE.md file, because longer files consume more context and reduce adherence. Guidance that matters for only part of the codebase belongs in a path-scoped rule under .claude/rules/. Imports organize a file but do not reduce its context cost.
Does Claude Code always follow CLAUDE.md?
No. Claude Code treats CLAUDE.md as context, not enforced configuration. Specific, concise, non-contradictory instructions are followed more consistently. When an action must be blocked regardless of what Claude decides, the documentation recommends a PreToolUse hook rather than a written rule.
Should I use /init to create CLAUDE.md?
Yes, as a starting point. Running /init analyzes your codebase and generates a CLAUDE.md with build commands, test instructions and conventions it discovers. If a CLAUDE.md already exists, /init suggests improvements instead of overwriting it. Then refine it with instructions Claude would not discover on its own.
Where should CLAUDE.md go in a repository?
Project instructions go in ./CLAUDE.md or ./.claude/CLAUDE.md and are shared through version control. Personal project notes go in ./CLAUDE.local.md, which you add to .gitignore. Personal instructions for all projects live in ~/.claude/CLAUDE.md. Files in directories above your working directory load at launch.
Can CLAUDE.md import other files?
Yes. Use @path/to/file syntax. Relative paths resolve from the file containing the import, and imports can nest up to four hops deep. Imports help organization but not context cost, because imported files load together with the CLAUDE.md that references them.
Sources and further reading
- Claude Code docs: How Claude remembers your project
- Claude Code docs: Hooks guide
- Claude Code docs: Skills
Prepared with AI assistance and reviewed for source fidelity and practical usefulness. Examples marked fictional are illustrations, not customer results.