CH 38 / 86Chapter 5: Claude Code (The Core of Development)

CLAUDE.md and Memory

On this page8 items

In 10 seconds

This is the file read at the start of every session.
Being read every time means it costs tokens every time. Aim to keep it around 200 lines.

In Chapter 3, #25, the advice was “write down what you say every time.” In Claude Code, the place for that is CLAUDE.md.

The repository for this guide has one too. Writing style, heading conventions, front matter definitions, where to check official information. It is being loaded even now, as this page is written.

Where to put it and what it affects#

This is the table from the official documentation. The further down, the narrower the scope, and the later it is read.

LocationScopeShared with
Organization policy locationEvery session on that machineEveryone in the organization (cannot be excluded)
~/.claude/CLAUDE.mdAll of your projectsOnly you
./CLAUDE.md or ./.claude/CLAUDE.mdThis projectThe team (shared via Git)
./CLAUDE.local.mdThis project, just for youOnly you (gitignored)

The files found are concatenated, not overridden. They are read starting from the higher directories, and the one in the directory where you launched comes last.

A rule of thumb for choosing: things the team must follow go in the project, your personal preferences go in ~/.claude/, and project-specific notes just for you go in CLAUDE.local.md. Add that last one to .gitignore.

What to write#

Only facts you want Claude to have in every session.

  • Build and test commands
  • Code conventions (concretely)
  • Project structure (what lives where)
  • “Always do it this way” rules

The official docs also suggest when to add entries, which is practical.

  • When the same mistake happens a second time
  • When a review turns up “this is something it should have known”
  • When you type the same correction as in a previous session
  • When a new team member needs the same explanation

The second time is the key. The first time is an accident; from the second time on, it is a problem with the system.

What not to write#

Don’t write procedures. Multi-step procedures, or things that only apply to part of the codebase, belong in skills (#40) or path-scoped rules.

The reason is simple: CLAUDE.md is loaded every time. If you write a detailed PR review procedure there, you keep paying for those tokens even during unrelated work.

The official guidance is about 200 lines; beyond that, it eats into context and is followed less reliably. The longer it gets, the less effective it becomes, which is counterintuitive but matches experience.

How to write it#

The four points the official docs list work exactly as stated.

Be specific. Not “write clean code” but “indent with 2 spaces.” Bring it down to a level you can verify.

Add structure. Organize with headings and bullet points. A structure that is easy for people to read is easy for Claude to read too.

Don’t contradict yourself. When two rules conflict, one of them gets picked arbitrarily. Reread it regularly and delete outdated lines.

Know that it is not enforced. This is the most important point. The official docs say it plainly: CLAUDE.md is context, not an enforced configuration.

Anything that must always run should be written as a hook, not in CLAUDE.md.

Producing this guide led to the same conclusion. The same formatting rule was broken three times in one day, so instead of rewriting it more forcefully in the instructions, it was built into the pre-publish check program (#25, #41).

Auto memory#

Separate from CLAUDE.md, there is memory that Claude writes itself. It is enabled by default.

Four kinds of things are saved: your role and preferences, corrections you have given, project circumstances that cannot be read from the code, and external references. Things that can be learned from the codebase, or that are already in CLAUDE.md, are not saved.

It lives in ~/.claude/projects/<project>/memory/, and since it is plain Markdown, you can read it and delete it. /memory opens a list.

CLAUDE.mdAuto memory
Written byYouClaude
ContentsInstructions and rulesThings learned and habits
SharingWith the team via GitThat machine only

It is machine-local, so it is not shared with the team. Anything you want to share should go in CLAUDE.md.

Try it: give your CLAUDE.md a health check (5 minutes)

Try this in a project that already has a CLAUDE.md.

Prompt
“`
Read CLAUDE.md and identify the following.

  • Lines stating things you could learn by reading the codebase (candidates for deletion)
  • Lines that are procedures and should move to a skill
  • Lines that end with “be careful” and won’t actually be followed
  • Lines that contradict other lines

Don’t fix anything. Just point them out.
“`

Expected result: Findings come back in the four categories. There are surprisingly many deletion candidates.

If it doesn’t work: /doctor can do a similar check. It suggests trimming what the codebase already tells you and keeping history and conventions.

Common pitfalls#

Only adding, never deleting. Add a line after every failure, and in six months you have a document nobody wants to read. When you add something, think about whether you can delete something else.

Writing explanations. If you write at length about “why we do this,” you pay for it every time. Keep the reason to one line.

Assuming it will be followed. It is context, not enforcement. Anything that must happen reliably goes in a hook.

Not checking that it is loaded. If it doesn’t appear under “Memory files” in /context, Claude isn’t seeing it.

Summary#

  • There are four levels of location. Team matters go in the project, preferences in ~/.claude/
  • Write facts you want present every time. Procedures go in skills
  • About 200 lines is the guideline. The longer it is, the less it works
  • It is context, not enforcement. Anything that must run goes in a hook

Read next#

Permission modes, sandboxing, and rewinding