Topic 3 of 5 · 5 min read
Layering global, repository and folder instructions
Goal: Predict which instructions an agent will load for a given file path from a set of global, repo, folder and path-scoped files, and resolve a conflict between them.
Memory files stack: the agent loads a global file, then the repository file, then any folder files on the path to the code it is touching, and the closest, most specific file should win a conflict.
Check yourself · 1 of 3
Where does a rule that applies only to the services/billing/ package best live?
Why it matters
One file cannot serve every context. Your own habits (answer style, editor tools) do not belong in the team's repository. A monorepo has a Python backend and a TypeScript frontend with different test commands. Layering lets each rule live at the narrowest level where it is true. It also creates a new failure: two files that disagree.
The layers
The names differ by tool, but the shape is the same:
- User or global file. Your personal preferences across all projects, such as
~/.claude/CLAUDE.mdfor Claude Code or a globalAGENTS.mdfor Codex. - Repository root file.
CLAUDE.mdorAGENTS.mdat the repo root: team commands and conventions, committed to git. - Folder files.
packages/api/AGENTS.mdfor rules that apply only to that package. Tools such as Codex read files from the root down to the working directory, and Claude Code loads a subfolder file when the agent works with files there. - Path-scoped rules. Claude Code supports rule files under
.claude/rules/that name file patterns they apply to, and Copilot supports path-specific instruction files. These load only when matching files are in play. - Organization policy and local overrides. A managed file for a whole company, or a git-ignored personal file for one repository.
Many teams keep a single AGENTS.md and let the other tools read it through an import or symlink. Imports (for example @docs/testing.md in a Claude Code file) pull another file in without copying it.
Conflicts
When two layers disagree, the rule is: more specific beats more general, and later in the load order beats earlier. But models do not "compile" rules; they read all of them as text. A real conflict ("use tabs" against "use spaces") can produce inconsistent behaviour. Do not rely on precedence. Remove the contradiction.
Predicting what loads
Given these files:
~/.claude/CLAUDE.md # global: "reply briefly"
CLAUDE.md # repo: "run pnpm test"
packages/api/CLAUDE.md # api: "use vitest, not jest"
.claude/rules/sql.md # paths: db/**/*.sql
Editing packages/api/src/user.ts loads the global file, the root file and the api file. The SQL rule stays out because no .sql file is involved. Editing packages/web/App.tsx loads global and root only.
What goes wrong
- Duplication across layers. The same rule in root and folder files drifts apart.
- Contradictions. Global says "always commit when done", the repo says "never commit". Result: unpredictable.
- Personal rules in the team file. Everyone inherits one person's taste.
- Wrong assumptions about the tool. A file that one tool loads automatically may be ignored by another. Check the tool's documentation, and test it (see the testing topic).
Examples
A monorepo with two test commands
AGENTS.md # "Use pnpm. Never edit lockfiles by hand."
apps/web/AGENTS.md # "Test: pnpm --filter web test"
services/billing/AGENTS.md # "Test: pytest -q. Money is integer cents."
Working in services/billing/ the agent sees the root rules plus the billing rules. It never reads the web test command, so it cannot run the wrong one.
Resolving a conflict
Global file: "Always commit after finishing a task." Repo file: "Never commit; the human commits." The repo is the right place for team policy, so delete the global line, or change it to "Commit only if the repository allows it". Now no precedence is needed.
Key points
- Put each rule at the narrowest layer where it is true: global, repo, folder or path-scoped.
- Loading is cumulative: the agent reads the global file, the repo file and the folder files on the path to its work.
- Do not rely on precedence to settle a contradiction; remove the conflicting line.
- Imports and symlinks let several tools share one source file without copying it.
- Each tool has its own file names and loading rules, so verify them in its documentation.
Common mistakes
- Repeating the same rule in several layers, which later drift apart.
- Leaving a contradiction in place and trusting 'closest wins' to settle it.
- Putting personal preferences in the committed repo file.
- Assuming a nested file loads at session start, when the tool may load it only on demand.
Check yourself · 2 of 3
The global file says 'always commit when done' and the repo file says 'never commit'. What is the best fix?
Check yourself · 3 of 3
A path-scoped rule matches only **/*.sql. The agent edits src/users.ts. What happens?
Practice items, not counted towards your mastery.
Start here
- AGENTS.md (opens in a new tab)DocsFree
The open format, what to put in the file and which agents read it.
- How Claude remembers your project (Claude Code docs) (opens in a new tab)DocsFree
How CLAUDE.md, AGENTS.md, imports and path-scoped rules load, and how to troubleshoot ignored files.
- Custom instructions with AGENTS.md (OpenAI Codex docs) (opens in a new tab)DocsFree
How Codex discovers AGENTS.md files from global to nested, and its 32 KiB default size limit.
More for this topic
- Adding repository custom instructions for GitHub Copilot (opens in a new tab)DocsFree
Repository-wide and path-specific instruction files in Copilot.