Topic 3 of 5 · 5 min read
Layering user, repo and folder files
Goal: Place each rule at the right level (user, repo, folder or on-demand file) and predict which instructions an agent loads in a given folder.
Agents load memory files in layers, from your user folder down to the folder you are working in, so each rule should sit at the widest level where it is still true and no wider. A rule at the wrong level is either missing when needed or noise everywhere else.
Check yourself · 1 of 3
Where should a personal preference like 'keep answers short' go?
Why it matters
One flat file cannot serve a personal preference, a team convention and a rule for one package at once. The tools solve this with layers, and the details differ, so check your tool's docs.
The usual layers
- User level. Your own habits across all projects, such as "explain commands before running them". Claude Code reads
~/.claude/CLAUDE.md; Codex and Gemini CLI have a similar file in their own home folder. It is not shared with the team. - Repo level. Team rules in
CLAUDE.mdorAGENTS.mdat the repository root, committed to git. - Folder level. A file inside a subfolder with rules for that area only, such as
packages/api/AGENTS.md. - Local, uncommitted. Claude Code supports
CLAUDE.local.mdfor your private notes about one repo, kept out of git. - On demand. Files the agent reads only when relevant: docs it is pointed to, path-specific rules in
.claude/rules/, or notes indocs/.
Which one loads, and which one wins
For Claude Code, the docs say files in the directory hierarchy above your working directory load at launch, while files in subdirectories load on demand when the agent works there. Codex builds its instructions from the global folder down to the current directory. Gemini CLI also combines files up and down the tree. In all of them, text closer to the work is read last. Do not rely on that to settle conflicts: the Claude Code docs say that if two instructions contradict each other, the agent may pick one arbitrarily. Remove the contradiction instead.
Imports help organise a file. In Claude Code, @path/to/file pulls another file in, but the docs note that imports do not reduce context cost, because the imported file loads with its parent.
Before and after
Weak: one root file with everything.
# Monorepo
- API: use Fastify, run `pnpm --filter api test`
- Web: use React, run `pnpm --filter web test`
- I like short answers.
- Mobile: use Expo, run `pnpm --filter mobile test`
Layered:
~/.claude/CLAUDE.md -> I like short answers.
CLAUDE.md -> workspace commands, shared boundaries
packages/api/CLAUDE.md -> Fastify rules, api test command
packages/web/CLAUDE.md -> React rules, web test command
docs/release.md -> read only when releasing (linked from root)
Now an agent working in packages/web is not told about Expo, and your style preference does not appear in the team repo.
What goes wrong
- Personal taste in the team file. Teammates inherit your preferences.
- The same rule at two levels, worded differently. The agent follows one and you cannot tell which.
- Assuming a subfolder file is always loaded. In some tools it loads only when the agent works in that folder.
Examples
A rule that belongs in one folder
In a monorepo only packages/payments must use integer cents and a specific retry helper. Put those two lines in packages/payments/AGENTS.md. Work in packages/web then never sees them.
A private note kept out of git
You use a local database on a nonstandard port. Put "local DB is on port 5544" in CLAUDE.local.md, which stays uncommitted, instead of the shared CLAUDE.md where it would confuse teammates.
Key points
- Put each rule at the widest level where it is true: user, repo, folder, or an on-demand file.
- Personal preferences belong in the user-level file, not in the committed team file.
- Subfolder files hold rules for one area; some tools load them only when the agent works there.
- Do not rely on which file wins; remove contradictions, because the agent may pick either instruction.
- Imports organise a file but do not reduce context cost, since imported content loads with its parent.
Common mistakes
- Putting every rule in the root file, so each task loads rules for areas it never touches.
- Putting personal preferences in the committed file.
- Repeating a rule at two levels with slightly different wording.
- Assuming all tools load folder files the same way without checking each tool's docs.
Check yourself · 2 of 3
A rule only applies to packages/payments in a monorepo. Where is the best place for it?
Check yourself · 3 of 3
Two memory files give opposite instructions about the test command. What does the Claude Code guidance say may happen?
Practice items, not counted towards your mastery.
Start here
- AGENTS.md (opens in a new tab)DocsFree
The open format for a plain-markdown instruction file that many coding agents read, with examples.
- How Claude remembers your project (Claude Code docs) (opens in a new tab)DocsFree
Where CLAUDE.md files load from, the under-200-lines target, imports, rules and troubleshooting.
- Custom instructions with AGENTS.md (OpenAI Codex docs) (opens in a new tab)DocsFree
How Codex finds and layers AGENTS.md files from the global folder down to the working directory.
More for this topic
- Provide context with GEMINI.md files (Gemini CLI docs) (opens in a new tab)DocsFree
How Gemini CLI loads GEMINI.md files hierarchically and lets you import other files.