# How to Set Up CLAUDE.md in Claude Code: Repo Audit and Rules the Agent Actually Follows

> AI Release · @ai_releaseorg · https://ai-release.org/guides/kak-nastroit-claude-md-v-claude-code-audit-repozitoriya_en.html

## TL;DR

- Time to complete: 25 minutes
- Difficulty: Basic
- You will need: Claude Code CLI installed and signed in (`claude --version`), Git 2.x, a repository with a clean working tree, a text editor. Optional: an MCP client if you later add a memory server.

## What you'll get

- A `CLAUDE.md` at the repository root with a repo map built from real `git ls-files` output, the exact build/test commands, and 6–10 hard rules.
- A fixed report format the agent must print before saying "done" — a `git diff --stat` block you can compare against the task.
- A three-command verification that proves the rules are binding on your installed build, not just written down.

## Quick start

```bash
git checkout -b chore/claude-md
git ls-files | cut -d/ -f1 | sort | uniq -c | sort -rn | head -10
printf '# CLAUDE.md\n\n## Repo map\n\n## Commands\n\n## Rules\n\n## Report format\n' > CLAUDE.md
git add CLAUDE.md && git commit -m "chore: add CLAUDE.md skeleton"
```

Details and checks are in the steps below.

## Copy-paste prompt

Send this to Claude Code inside the repository root before Step 1. It produces the file content; you review and commit it.

```prompt
You are auditing the repository <repo-name>, branch <branch>, to produce CLAUDE.md.
Modify no file except CLAUDE.md. If a fact is missing from the repository, write
"check the docs" instead of guessing. Never invent flags, script names or ports.

1. Repo map: list top-level directories with tracked file counts taken from
   `git ls-files`. Mark generated/vendored directories and say which must never be
   edited by hand.
2. Commands: quote the install, build, lint and test commands verbatim from
   <package.json / Makefile / pyproject.toml>. Copy the strings; do not paraphrase.
3. Rules: write 6-10 rules as hard constraints, each with a command that proves it.
   The first three must be:
   - edit only the files named in the task and their direct importers; stop and ask
     before touching anything else;
   - never reformat, rename, reorder imports or bump dependencies unless asked;
   - never edit paths matched by .gitignore.
   The last rule must be: before reporting completion, print `git diff --stat`.
4. Report format: a fixed block with Done, Changed files, Not changed but related,
   Open questions.
Output the whole file as one markdown block.
```

Replace `<repo-name>`, `<branch>` and the manifest file name with your values; delete the manifest you do not have.

## Requirements and setup

- macOS 13+, a modern Linux distribution, or Windows 10/11 with PowerShell 7.
- Claude Code CLI installed and authenticated. Confirm with:

```bash
claude --version
```

You should see a version string. If the command is not found, install the CLI for your platform; if your build rejects the flag, check the documentation. If you work in the desktop build instead, the setup flow differs — see [Claude Code Desktop: setup, limits and how it differs from the CLI](https://ai-release.org/guides/claude-code-desktop-ustanovka.html?utm_source=tg&utm_medium=channel&utm_campaign=guide_inline&utm_content=guide_to_guide).

- Git repository with a clean tree: `git status --short` prints nothing.
- About 25 minutes.

Why rules must live in the repo: a year-long review published on caleb.software counted 244 Claude Code releases in one year, and more than three quarters of them changed the system prompt or tool descriptions. Anything you say once in chat is not durable; `CLAUDE.md` is the part you control.

**Example:** the same review notes that the Bash tool description alone runs to roughly 3000 tokens — the built-in instructions are already long, so your file has to be short and specific to win attention.

## Step 1. Audit the repository before writing a single rule

**What to do:** collect four lists — size and shape, build/test entry points, hottest files, generated paths. Run these four commands at the repo root.

```bash
# 1. Size and top-level shape
git ls-files | wc -l
git ls-files | cut -d/ -f1 | sort | uniq -c | sort -rn | head -10

# 2. Build and test entry points
git ls-files | grep -E '(^|/)(package\.json|Makefile|pyproject\.toml|go\.mod|Cargo\.toml)$'

# 3. Hottest files over the last 60 days
git log --since="60 days ago" --name-only --pretty=format: | grep -v '^$' | sort | uniq -c | sort -rn | head -15

# 4. Generated and vendored paths
git ls-files | grep -E '^(dist|build|vendor|generated)/' | cut -d/ -f1 | sort -u
```

**Example output** for a repository with 312 tracked files:

```text
312
     84 src
     51 tests
     33 docs
     19 scripts
package.json
dist
```

**Why:** rules written from memory are wrong — they name directories that do not exist and scripts that were renamed. The audit gives the agent facts it can act on, and command 3 shows which files are risky to touch, because that is where edits collide.

**How to check:** you end up with one number (tracked files), one short directory list, at least one manifest path, and an explicit list of generated directories. If command 3 prints nothing, the repository is younger than 60 days — rerun with `--since="1 year ago"`.

## Step 2. Write CLAUDE.md: repo map, commands, hard rules, report format

**What to do:** fill the skeleton with the four audit lists, then add the rules that stop scope creep. Use this template and replace every path with a path from your audit.

```markdown
# CLAUDE.md

## Repo map
- `src/` — application code (84 tracked files). Entry point: `src/main.ts`.
- `tests/` — test suites (51 files). Single file: `npx vitest run tests/<name>.test.ts`.
- `dist/`, `build/` — generated. Never edit by hand; rebuild with `npm run build`.

## Commands
- Install: `npm ci`
- Dev server: `npm run dev` → http://localhost:3000
- Test: `npm test`
- Lint: `npm run lint`

## Rules
1. Edit only the files named in the task and their direct importers. Before touching any other file, stop and ask.
2. Never reformat, rename, reorder imports or bump dependencies unless the task says so.
3. Never edit a path matched by `.gitignore` (`dist/`, `build/`). Change the source and rebuild.
4. Run `npm test` after any change under `src/`.
5. Before saying "done", print `git diff --stat`.

## Report format
![Illustration: Report format](https://ai-release.org/guides/img/kak-nastroit-claude-md-v-claude-code-audit-repozitoriya-en-2.jpg?v=1795941925)

Done: <one line>
Changed files:
<git diff --stat output>
Not changed but related: <paths>
Open questions: <list or "none">
```

If a script from the template does not exist in your `package.json`, delete that line — do not invent a replacement.

**Why:** the report format is the actual enforcement. The channel covered an r/AI_Agents case where a request to change one button color ended with CSS variables renamed in 40 files and the test suite reformatted, while the agent's report said only "done". Rule 5 makes that visible in one screen of output, and rules 1–3 forbid it up front.

If the repository already keeps an `AGENTS.md`, pick one source of truth. Anthropic announced support for `AGENTS.md` in Claude Code; the precedence between the two files is version-dependent, so check the documentation before keeping both. The safe pattern is to keep the rules in one file and make the other a one-line pointer.

**How to check:** 

```bash
wc -l CLAUDE.md
grep -nE 'try to|be careful|if possible|when appropriate' CLAUDE.md
git diff --name-only
```

`wc -l` should print a number under 120. The `grep` must print nothing — any hedge word means the rule cannot be verified. `git diff --name-only` must list only `CLAUDE.md`.

Once the rules are stable and repetitive procedures appear, move them out of the file and into separate agents — see [Claude Code for Teams: Setting Up Subagents and a Shared AI Superagent](https://ai-release.org/guides/claude-code-dlya-komandy-kak-nastroit-subagentov-i-obsc.html?utm_source=tg&utm_medium=channel&utm_campaign=guide_inline&utm_content=guide_to_guide).

## Step 3. Verify the agent actually follows the rules
![Illustration: Step 3. Verify the agent actually follows the rules](https://ai-release.org/guides/img/kak-nastroit-claude-md-v-claude-code-audit-repozitoriya-en-3.jpg?v=1795941929)


**What to do:** run one controlled task that a rule-following agent finishes with a single-file diff. Start a fresh session in the repo root and send:

```text
Change the primary button color in src/components/Button.tsx from #1f6feb to #0b5cad. Edit no other file.
```

Then check the result yourself:

```bash
git diff --name-only
git diff --stat
```

**Example:** a compliant run prints exactly one path from `git diff --name-only` (`src/components/Button.tsx`) and a `git diff --stat` line such as `1 file changed, 2 insertions(+), 2 deletions(-)`, plus the same stat block pasted into the agent's report.

**Why:** the check tests the rule, not the answer. A color value can be right while the diff is wrong, and the diff is what you merge. Diff discipline pays off more as repositories grow — if you want a version-control layer built for agent changes, compare options in [Jujutsu or Git: What to Choose for AI Agent Workflows](https://ai-release.org/guides/jujutsu-ili-git-chto-vybrat-dlya-raboty-s-ii-agentami-i.html?utm_source=tg&utm_medium=channel&utm_campaign=guide_inline&utm_content=guide_to_guide).

**How to check:** run the checklist.

- [ ] `git diff --name-only` outputs exactly the file named in the task
- [ ] The agent's message contains a `git diff --stat` block
- [ ] `git diff` contains no reordering, renaming or whitespace-only hunks
- [ ] `git status --short` shows no changes inside `dist/` or `build/`
- [ ] Asking "which CLAUDE.md rules apply here, quote them" returns rules 1 and 5 verbatim

If any box fails, the document is guidance, not a contract — go back to Step 2 and tighten the wording.

## Troubleshooting

**Problem. The agent says "done" and the diff touches dozens of files.**

- Error: `git diff --stat` ends with `40 files changed`
- Cause: rules written as preferences ("keep changes focused") with no report format. In the r/AI_Agents case, the agent renamed CSS variables across 40 files while the request was a one-color fix, and its own summary reported only success.
- Solution: revert and rerun the same prompt after fixing the rules.

```bash
git checkout -- src tests
# add rules 1-3 and 5 to CLAUDE.md, commit, then resend the original task
git add CLAUDE.md && git commit -m "docs: harden CLAUDE.md rules"
```

**Problem. The audit commands fail before printing anything.**

- Error: `bash: rg: command not found`
- Cause: `ripgrep` is not installed on the machine, and a snippet that uses `rg` was copied instead of the Git-only version.
- Solution: the audit in Step 1 needs no extra tools — every command starts with `git`. Either install ripgrep or keep using `git ls-files | grep ...`, which produces the same lists.

**Problem. The agent edited files, but `git status` reports a clean tree.**

- Error: `nothing to commit, working tree clean`, while `ls -l dist/app.js` shows a fresh timestamp
- Cause: the target directory is listed in `.gitignore`, so the change never appears in a diff and you cannot review it. The agent fixed the build output instead of the source.
- Solution: confirm the ignore rule, restore the tree, and rely on rule 3.

```bash
git check-ignore -v dist/app.js   # prints the .gitignore line that matches
git checkout -- src
npm run build
```

## Summary and next steps

You now have a `CLAUDE.md` built from real audit output, with rules that are checkable by command and a report format that exposes every touched file. Next, keep the file under 120 lines — when it grows past that or you need context to survive between sessions, look at kaeru, a local MCP memory server for Claude Code and other clients, or automate long sessions with [claude-code-unlimited](https://ai-release.org/guides/kak-nastroit-claude-code-unlimited-avtomaticheskaya-rot.html?utm_source=tg&utm_medium=channel&utm_campaign=guide_inline&utm_content=guide_to_guide).

## FAQ

**Does CLAUDE.md replace AGENTS.md?**
No. Anthropic announced `AGENTS.md` support in Claude Code, so both files can exist. Keep the rules in one of them to avoid contradictory instructions, and make the other file a pointer. Check the documentation for the precedence order in your build.

**How long should CLAUDE.md be?**
Short enough to be read in one pass — keep it under roughly 120 lines, checked with `wc -l CLAUDE.md`. Move long procedures into separate agents, and move session-to-session context into a memory server.

**Will my rules still hold after a Claude Code update?**
Your file is not rewritten by updates, but agent behavior shifts: of 244 releases counted over one year, more than three quarters changed the system prompt or tool descriptions. Rerun the Step 3 check after each update.

**Why does the agent still reformat code if the rule exists?**
Because "be careful with formatting" is advice. Replace it with a constraint that produces output you can inspect — "never reformat unless asked" plus "print `git diff --stat` before saying done" — and verify with `git diff --name-only`.
