Persistent context via CLAUDE.md files — how to write them and how they load in monorepos.
| ← Back to Claude Code Best Practice |
A well-structured CLAUDE.md is the single most impactful way to improve Claude Code's output for your project. Humanlayer has an excellent guide covering what to include, how to structure it, and common pitfalls.
When working with Claude Code in a monorepo, understanding how CLAUDE.md files are loaded into context is crucial for organizing your project instructions effectively.
Claude Code uses two distinct mechanisms for loading CLAUDE.md files:
When you start Claude Code, it walks upward from your current working directory toward the filesystem root and loads every CLAUDE.md it finds along the way. These files are loaded immediately at startup.
CLAUDE.md files in subdirectories below your current working directory are NOT loaded at launch. They are only included when Claude reads files in those subdirectories during your session. This is known as lazy loading.
Consider a typical monorepo with separate directories for different components:
/mymonorepo/
├── CLAUDE.md # Root-level instructions (shared across all components)
├── frontend/
│ └── CLAUDE.md # Frontend-specific instructions
├── backend/
│ └── CLAUDE.md # Backend-specific instructions
└── api/
└── CLAUDE.md # API-specific instructions
When you run Claude Code from /mymonorepo/:
cd /mymonorepo
claude| File | Loaded at Launch? | Reason |
|---|---|---|
/mymonorepo/CLAUDE.md |
Yes | It's your current working directory |
/mymonorepo/frontend/CLAUDE.md |
No | Loaded only when you read/edit files in frontend/ |
/mymonorepo/backend/CLAUDE.md |
No | Loaded only when you read/edit files in backend/ |
/mymonorepo/api/CLAUDE.md |
No | Loaded only when you read/edit files in api/ |
When you run Claude Code from /mymonorepo/frontend/:
cd /mymonorepo/frontend
claude| File | Loaded at Launch? | Reason |
|---|---|---|
/mymonorepo/CLAUDE.md |
Yes | It's an ancestor directory |
/mymonorepo/frontend/CLAUDE.md |
Yes | It's your current working directory |
/mymonorepo/backend/CLAUDE.md |
No | Different branch of the directory tree |
/mymonorepo/api/CLAUDE.md |
No | Different branch of the directory tree |
-
Ancestors always load at startup — Claude walks UP the directory tree and loads all CLAUDE.md files it finds. This ensures you always have access to root-level, repository-wide instructions.
-
Descendants load lazily — Subdirectory CLAUDE.md files only load when you interact with files in those subdirectories. This prevents irrelevant context from bloating your session.
-
Siblings never load — If you're working in
frontend/, you won't getbackend/CLAUDE.mdorapi/CLAUDE.mdloaded into context. -
Global CLAUDE.md — You can also place a CLAUDE.md at
~/.claude/CLAUDE.mdin your home folder, which applies to ALL Claude Code sessions regardless of project.
-
Shared instructions propagate down — Root-level CLAUDE.md contains repository-wide conventions, coding standards, and common patterns that apply everywhere.
-
Component-specific instructions stay isolated — Frontend developers don't need backend-specific instructions cluttering their context, and vice versa.
-
Context is optimized — By lazily loading descendant CLAUDE.md files, Claude Code avoids loading potentially hundreds of kilobytes of irrelevant instructions at startup.
-
Put shared conventions in root CLAUDE.md — Coding standards, commit message formats, PR templates, and other repository-wide guidelines.
-
Put component-specific instructions in component CLAUDE.md — Framework-specific patterns, component architecture, testing conventions unique to that component.
-
Use CLAUDE.local.md for personal preferences — Add it to
.gitignorefor instructions that shouldn't be shared with the team.
