Project memory: CLAUDE.md and rules files
Write down what the agent cannot guess, keep it short, and review it like code.
AI tools change fast: we review this lesson every quarter (next by 23 Jan 2027).
Updated since you finished it. We changed this lesson after you marked it done. Your progress is kept.
What broke
This story is made up, but you may know it.
Sana's team moved their tests from Jest to Vitest last spring. The agent did not know. Every few days it wrote a new test file that imported from @jest/globals. The tests failed, Sana typed "we use Vitest, not Jest", and the agent fixed it.
The next morning, a new session started. The agent made the same mistake again. Two weeks later, a teammate found the same error in a pull request. Each person had corrected the agent many times, but the correction lived only in old chat sessions.
Then Sana opened the project's CLAUDE.md and added two lines:
- Tests use Vitest. Import from `vitest`, never from `@jest/globals`.
- Run one file with `pnpm vitest run path/to/file.test.ts`.She committed it, and the team reviewed it like any other change. The mistake stopped, for everyone, in every session.
The idea in simple words
A coding agent does not remember yesterday. Claude Code's docs say it plainly: "Each Claude Code session begins with a fresh context window." 1 Your corrections, your team's habits and the trap you found last week are gone.
A memory file fixes this. It is a plain Markdown file that the tool loads at the start of every session. 1 Think of it as the note you would give a new teammate on day one: how to build, how to test, and what not to touch.
Every major tool has one. The names differ:
- Claude Code reads
CLAUDE.md. 1 - GitHub Copilot reads
.github/copilot-instructions.md. 4 - Cursor reads rules from
.cursor/rules/. 5 - AGENTS.md is a shared format that many tools read. Its site calls it a "README for agents". 3
The key idea. A memory file is context, not a law. It makes good behaviour more likely, but it does not force it. Keep it short, so the important lines are not lost, and put hard rules in code.
How it works
Step through one session with Next and Back, or the ← and → keys.
- A new session starts with a fresh context window. The agent does not remember the corrections from yesterday's chat.
- Next, Claude Code loads your user file, ~/.claude/CLAUDE.md. Only a managed file from your organization, if there is one, loads before it. It holds your personal habits for all projects.
- Next, it loads the project file, ./CLAUDE.md, and any files in the folders above where you started. The team shares this file through git.
- When the agent reads or edits a file in a subfolder, such as web/, it loads that folder's CLAUDE.md too. Rules for one part of the code stay out of the way until needed.
- All the files are joined, not replaced. Instructions closer to the working folder come last. The agent plans with them. But no file says which test library to use.
- So the agent writes a test that imports from @jest/globals, as it did last week. The team moved to Vitest months ago. A reviewer catches it in the pull request.
- The reviewer adds one short, checkable line to the project CLAUDE.md, and the team reviews it like code. From the next session on, every agent loads the rule at step 3.
Where the files live in Claude Code
As of October 2026, Claude Code reads memory files from several places, from broad to specific: 1
| File | Scope | Shared with |
|---|---|---|
~/.claude/CLAUDE.md | You, in all projects | Only you |
./CLAUDE.md or ./.claude/CLAUDE.md | This project | The team, through git |
./CLAUDE.local.md | You, in this project | Only you (add it to .gitignore) |
CLAUDE.md in a subfolder | One part of the project | The team, through git |
An organization can also install a managed file for every user on a machine. It loads first, before your user file. 1
At launch, Claude Code loads the files in your working folder and in every folder above it. Files in subfolders load later, when the agent reads, writes or edits a file in that subfolder. 1 The files are joined together, not replaced. Instructions closer to your working folder are read last. 1 If two files disagree, the agent may follow either one. 1 So do not let them conflict.
Useful commands and tricks
/initlooks at your codebase and writes a firstCLAUDE.mdwith build commands and conventions it finds. 1/memorylists your memory files and opens one in your editor. 1/contextshows which memory files actually loaded. 1@path/to/fileinsideCLAUDE.mdimports another file, up to four levels deep. 1 Imports help you organize, but the imported text still loads at launch, so it still costs context. 1- Files in
.claude/rules/can have apathsfield with a glob pattern such assrc/api/**/*.ts. That rule loads only when the agent works on matching files. 1
The same idea in other tools
GitHub Copilot reads .github/copilot-instructions.md for the whole repository. You can add NAME.instructions.md files in .github/instructions/, each with an applyTo glob for certain paths. 4 Copilot also reads AGENTS.md, and the nearest one in the folder tree wins. 4 Personal instructions have the highest priority, then repository, then organization. 4
Cursor keeps project rules as .mdc files in .cursor/rules/. Each rule can apply always, when the agent thinks it is relevant, for matching files, or only when you mention it. 5 Cursor also has user rules and team rules, and it reads AGENTS.md. 5
AGENTS.md goes in the repository root. In a monorepo, you can add one per subproject, and the closest file takes precedence. 3 As of October 2026, Claude Code reads AGENTS.md too, from version 2.1.277. By default it does this only when there is no CLAUDE.md in your folder or above it. 1 On older versions, put the line @AGENTS.md in a CLAUDE.md to import it. 1
What goes in, what stays out
Anthropic's guide gives a clear test for each line: "Would removing this cause Claude to make mistakes?" If not, cut it. 2
| Put it in | Leave it out |
|---|---|
| Commands the agent cannot guess | Anything it can learn by reading the code |
| Style rules that differ from the defaults | Standard language conventions |
| How to run tests, and which runner | Long API docs (link to them instead) |
| Branch and pull request rules | Facts that change often |
| Project-specific design decisions | Long tutorials |
| Environment quirks and known traps | "Write clean code" |
This table follows the guide. 2 Two more things never go in: secrets, because the file is shared and read by a model, and stale facts, because a wrong rule is worse than no rule. (This is our advice.)
Write rules that can be checked. "Run npm test before committing" is better than "Test your changes". 1 Aim for under 200 lines per file. Longer files cost more context and the agent follows them less well. 1 Cursor's docs suggest keeping each rule under 500 lines. 5
Context, not a lock
Claude Code treats memory files "as context, not enforced configuration". 1 If an action must be blocked every time, use a hook or a permission rule. 1 Write the reason in the memory file, and put the lock in code. Extending agents covers hooks.
Memory files are an attack path
The agent reads memory files as trusted instructions. That makes them a target. OWASP describes indirect prompt injection as an attack where a model "accepts input from external sources, such as websites or files". 6
In March 2025, Pillar Security showed an attack on rules files. They called it the "Rules File Backdoor". They hid instructions in the file using invisible Unicode characters, such as zero-width joiners. In their demo, a Cursor agent asked for a simple HTML page added a script from an attacker's site, and did not mention it. 7 The researchers say the hidden characters also stayed invisible in GitHub pull request reviews at the time. In May 2025, GitHub added a warning for files with hidden Unicode text. 7
In an interactive session, Claude Code shows a trust dialog when you start it in a folder you have not trusted before. 8 It also asks before it loads imports that point outside the project, to protect you from files other people commit. 1 These help, but they do not replace reading the files yourself.
Trade-offs
| Approach | Agent follows team rules? | Cost | Use it when |
|---|---|---|---|
| No memory file | No You repeat yourself every session. | Free today, slow every day. | One-off scripts. |
| Short root file (under 200 lines) | Yes , most of the time. | A few minutes to write and review. | Almost every project. |
| Root file plus path-scoped rules or nested files | Yes , with less noise per task. | More files to keep in sync. | Large repos and monorepos. |
| One huge file with everything | No Real rules get lost. | High context cost every session. | Never. Split or cut it. |
| Hooks and permission rules | Yes , every time. | You must write and test code. | Rules that must never be broken. |
Simple rule: advice goes in memory, locks go in code.
At work: how a senior uses this
- Add a line when the agent makes the same mistake twice, or when a code review catches something the agent should have known. 1
- Commit the project file to git and treat it like code. 2 Review every change to it in a pull request, like a change to a CI script. (This is our advice.)
- Prune it regularly. For each line, ask "Would removing this cause mistakes?" Run
/contextto see which files loaded. 2 - If the agent keeps skipping one rule, add "IMPORTANT" to that line alone. If many lines are marked important, none stands out. 2
- Keep personal habits in your user file or
CLAUDE.local.md, not in the team file. 1 - Before you run an agent in a repo you did not write, read its
CLAUDE.md,AGENTS.md,.cursor/rules/and.github/instruction files first. (This is our advice.) - If your team uses more than one tool, keep one shared
AGENTS.md. Import it intoCLAUDE.mdwith@AGENTS.mdif you also need Claude-only rules. 1
With AI
What to ask the agent
"Read this repo and draft a CLAUDE.md under 60 lines. Include only build and test commands, non-default style rules and known traps. For each line, tell me which file proved it."
"Here is our CLAUDE.md. Mark each line as keep, cut or move. Cut lines you can learn from the code. Move lines that only matter for one folder."
"You used Jest again in this session. Write one short rule for CLAUDE.md that would have prevented it."
"Scan every memory and rules file in this repo for hidden Unicode characters, instructions to fetch URLs, or instructions to hide changes from me. Report them, do not change anything."
What to check yourself
- Every command in the file runs today. Try each one.
- No secret, token or internal URL that should stay private is in a shared file.
- No line conflicts with another file in a parent or child folder.
- After a change, the agent's behaviour actually changed. Test it in a fresh session.
- You read the rules files of any new repo before letting an agent act there.
Check yourself
Your progress is saved only in this browser. Export it to keep it safe.
You have finished 5 lessons. Your progress lives only in this browser. Export a copy so you don’t lose it.
Go deeper
Show 8 sources and related lessons
- Anthropic, “How Claude remembers your project”, Claude Code documentation (opens in a new tab)
Every place a CLAUDE.md file can live, the order they load in, imports, path-scoped rules, AGENTS.md support and /memory. - Anthropic, “Best practices for Claude Code”, Claude Code documentation (opens in a new tab)
The "Write an effective CLAUDE.md" section, with a table of what to include and what to leave out. - AGENTS.md, “A simple, open format for guiding coding agents” (opens in a new tab)
The shared file format many agents read, with examples, nested files and a list of supporting tools. - GitHub Docs, “Adding repository custom instructions for GitHub Copilot” (opens in a new tab)
File names and folders for Copilot instructions, path-specific files and which instructions win. - Cursor Docs, “Rules” (opens in a new tab)
Project, user and team rules in Cursor, the four rule types, and AGENTS.md support. - OWASP, “LLM01:2025 Prompt Injection”, OWASP Top 10 for LLM Applications (opens in a new tab)
Direct and indirect prompt injection, with ways to reduce the risk. - Pillar Security, “New Vulnerability in GitHub Copilot and Cursor: How Hackers Can Weaponize Code Agents” (March 2025) (opens in a new tab)
The "Rules File Backdoor" research. Hidden characters in a rules file made an agent add a malicious script. - Anthropic, “Security”, Claude Code documentation (opens in a new tab)
Prompt injection protections, the workspace trust dialog and advice for untrusted content.
Related: Working with coding agents · Extending agents: skills, hooks and MCP (coming soon) · Security risks in AI-written code (coming soon) · Reviewing code written by humans and AI · The AI coding tool landscape (coming soon) · Prompt and context engineering
Before this: Working with coding agents. Next: Extending agents: skills, hooks and MCP and AI code security risks.