Write project-level working rules for Claude Code
Write a short, specific CLAUDE.md, split path-specific rules into .claude/rules/, and confirm with /context all that the files actually load.
Before you start · 1. Decide what is worth writing down · 2. Create the file at the project root
At a glance
Each Claude Code session starts with a fresh context window, which does not retain last week's conversation. The official documentation describes two mechanisms that carry information across sessions: CLAUDE.md, persistent instructions you write, and auto memory, notes Claude writes itself from your corrections and preferences. This guide covers the first. Write a short, verifiable CLAUDE.md for the project, move rules for specific files into .claude/rules/, then confirm the files are loaded. The sample project, notes-api, is fictional and does not match any real repository.
Workflow
Pick content
Keep only facts every session needs; put multi-step procedures and rules for part of the codebase elsewhere.
Be specific
Make each instruction checkable, stay under 200 lines, and group with headers and bullets.
Verify loading
Run /context all and look for the file under Memory files.
Before you start
- Claude Code is installed and starts in your terminal, and you have a project directory you may edit (installation and login were not verified here).
- You can name three to six facts your team keeps repeating, such as the test command, directory conventions or naming rules.
- You know which preferences are personal and should not be committed to version control.
1. Decide what is worth writing down
Open a scratch list and note what you keep re-explaining to Claude. The documentation suggests adding an entry when Claude makes the same mistake twice, a code review catches something it should have known, or you retype the same correction. Keep facts needed in every session: build and test commands, conventions, project layout and always-do-X rules. Multi-step procedures or rules for one part of the codebase belong in a skill or a path-scoped rule.
2. Create the file at the project root
In your terminal, go to the project directory and create ./CLAUDE.md, or ./.claude/CLAUDE.md. Both are project-instruction locations shared with the team through version control. For a starting point, run /init in a session and let Claude analyze the codebase and draft one; if a CLAUDE.md exists, it suggests improvements instead of overwriting. Edit the draft afterward and add what Claude could not discover itself.
3. Make every instruction checkable
Input: your draft. Prefer phrasing like "Run npm test before committing" over "Test your changes", and "API handlers live in src/api/handlers/" over "Keep files organized". Group with headers and bullets and aim for under 200 lines. If two instructions contradict each other, Claude may pick one arbitrarily, so reread the file and remove conflicts.
4. Split local rules into .claude/rules/
If some rules only matter under src/api, create .claude/rules/api-design.md and add YAML frontmatter at the top with a paths field holding a glob such as src/api/**/*.ts. Such a rule loads when Claude reads, writes or edits a matching file. Keep one topic per file with a descriptive name. Rules without paths load at launch with the same priority as .claude/CLAUDE.md.
5. Keep personal preferences in a local file
Put your own sandbox URLs or preferred test data in CLAUDE.local.md at the project root and add it to .gitignore so it is not committed. It loads alongside CLAUDE.md and is appended after the CLAUDE.md in the same directory. With several git worktrees, a gitignored CLAUDE.local.md exists only in the worktree where you created it.
6. Start a session and check what loaded
Run claude in the project directory, then type /context all and look for your CLAUDE.md under Memory files. If it is missing, Claude cannot see it. Check the location, and use /memory to open and edit files. A CLAUDE.md in a subdirectory does not appear in that list because it loads on demand when Claude touches a file there. Path-scoped rules behave the same way, so have Claude read a matching file before testing. Use a small task on a practice file matching src/api/**/*.ts. Check the actual diff for input validation and the standard error response, then inspect real output from the specified test. Mark any unperformed check as unperformed; loading a file does not establish compliance with every rule.
7. Audit periodically and know when to stop
To clean out outdated or conflicting content, run /doctor prompt-audit (the documentation says it needs Claude Code v2.1.283 or later). It reports findings and proposed edits and changes nothing until you ask it to apply them. If a rule must run at a fixed point, such as before every commit, do not rely on CLAUDE.md alone. The documentation recommends a hook for that, and a PreToolUse hook to block an action. The audit also requires the bundled /claude-api skill to remain enabled in skillOverrides and disableBundledSkills.
Original example
The following is a fictional notes-api project, shown only to illustrate the format.
./CLAUDE.md:
# notes-api
## Commands
- Run npm test before committing
## Conventions
- API handlers live in src/api/handlers/
- Use 2-space indentation
.claude/rules/api-design.md:
---
paths:
- "src/api/**/*.ts"
---
# API rules
- All endpoints must validate input
- Use the standard error response format
Add one line to .gitignore: CLAUDE.local.md
Check: start claude, run /context all, and expect to see this CLAUDE.md under Memory files; then have Claude read any .ts file under src/api so the path rule loads on demand. This is an illustrative layout, not actual session output.
Completion checks
- /context all lists the project
CLAUDE.mdunder Memory files, andCLAUDE.local.mdtoo if you created one. - Each instruction answers "how would I tell it was followed?"; vague lines like "keep it tidy" are gone.
CLAUDE.mdis under 200 lines and has no contradictions with subdirectory files or .claude/rules/.CLAUDE.local.mdis in .gitignore and does not show up in git status.- Use a small task on a practice file matching src/api/**/*.ts. Check the actual diff for input validation and the standard error response, then inspect real output from the specified test. Mark any unperformed check as unperformed; loading a file does not establish compliance with every rule.
Limits
CLAUDE.mdis context, not enforced configuration; the documentation says strict compliance is not guaranteed, so use hooks or permission settings for actions that must be blocked.- The default
AGENTS.mdfallback checksCLAUDE.md,.claude/CLAUDE.mdandCLAUDE.local.mdin the working directory and its ancestors. User-level~/.claude/CLAUDE.md, managed organizationCLAUDE.mdand .claude/rules/ do not count for this check and may load alongside AGENTS.md. Direct reading requires v2.1.277+; sessions that cannot readAGENTS.mddirectly must import it from CLAUDE.md. - This guide does not cover organization-managed
CLAUDE.md, claudeMdExcludes or auto memory setup. Auto memory is notes Claude writes itself, which differ from conventions you write by hand. - After /compact the project-root
CLAUDE.mdis re-read, but instructions given only in conversation may be lost during compaction; write anything enduring into CLAUDE.md.
Verification scope
This guide is based on reading the official documentation. Login, installation, product command or API execution, file upload and generated output were not tested; the sample file contents are original illustrations, not real session results.
Article sources
Original editorial content; official descriptions and checking suggestions are distinguished.