Sub topic 7
Which instructions win
This chapter explains what happens when more than one source of guidance applies to the same piece of work. It covers the order of precedence, how the nearest-file rule works, and where tools differ. It assumes you can write a basic AGENTS.md (see the earlier chapters on format and content) and that you know your repository's directory layout. It also assumes you have used an AI coding agent in a terminal or editor.
Why precedence matters
A single root AGENTS.md never conflicts with itself. Conflicts start when a repository has several sources of guidance: a root file, nested files in subdirectories, tool-specific files, and the instructions you type into the chat. If two of them disagree, the agent must pick one. If you don't know which one it picks, you can't predict its behavior.
Typical conflicts look like this:
- The root file says to run
npm test, but the frontend package needsnpm test --workspace packages/web. - The root file says "use default exports in React components", but the API package forbids default exports.
- The file says "always run the linter", but today you only want a quick one-line fix without it.
The order of precedence
Instructions are resolved from most specific to least specific. The more closely a source relates to the work at hand, the more weight it carries.
| Priority | Source | Scope |
|---|---|---|
| 1 (highest) | Your explicit prompt in the current session | This task only |
| 2 | The AGENTS.md closest to the file being edited | That directory and everything below it |
| 3 | AGENTS.md files in parent directories, up to the repository root | Wider parts of the tree |
| 4 | The agent's built-in defaults | Everything |
Two rules follow from this table:
- The nearest AGENTS.md wins over a more distant one.
- What you say directly in the conversation wins over every file.
The nearest file wins
"Nearest" is measured from the file being edited, walking up the directory tree. The first AGENTS.md found on the way up is the closest one. Consider this layout:
repo/
├── AGENTS.md
├── packages/
│ ├── api/
│ │ ├── AGENTS.md
│ │ └── src/
│ ├── web/
│ │ ├── AGENTS.md
│ │ └── src/
│ │ └── components/
│ │ └── Button.tsx
│ └── shared/
│ └── src/
│ └── money.ts
└── README.mdSuppose the root file contains this rule:
# AGENTS.md (repo root)
## Test
- Run all tests: `npm test`
## Style
- Use named exports; do not add default exports.And packages/web/AGENTS.md contains this:
# AGENTS.md (packages/web)
## Test
- Run tests for this package only: `npm test --workspace packages/web`
## Style
- Use function components and hooks; do not add class components.When the agent edits packages/web/src/components/Button.tsx, the closest file is packages/web/AGENTS.md. The test command from that file replaces the root's test command. The style rule about default exports lives only in the root file, so it still applies. The web file said nothing about it.
Conflict versus addition
Nearest wins only when two instructions actually conflict. If they cover different topics, the agent combines them. This is why a nested file can be short. It states only what differs for that directory and relies on the root for everything shared. The monorepo chapter shows this pattern in more detail.
A useful mental model:
- Same topic, different answer: the nearest file replaces the farther one.
- Different topics: both apply.
- Silence: the farther file keeps applying.
Say so when you override
Agents handle an explicit override better than a quiet contradiction. If a nested file intentionally departs from the root, write that down:
## Style
- Default exports are allowed in this package. This overrides the root rule against them,
because the framework router requires a default export per page.The reason helps a human reviewer too. Without it, the next person may "fix" the inconsistency and break the build.
Explicit user prompts override files
Files describe standing preferences. Your prompt describes what you want right now. When they conflict, the prompt wins.
For example, the root AGENTS.md says to run npm run lint && npm run typecheck before finishing. You type:
Fix the typo in the README heading. Don't run lint or typecheck, I'm only changing docs.The agent should skip the checks for this task. The file is unchanged, and the next session starts with the standard rule again.
Two limits apply:
- A prompt overrides a rule for the task, not for the repository. If a rule should change permanently, edit the file.
- A prompt cannot grant permissions the tool itself withholds. If the tool's sandbox or approval settings block a command, no wording in a prompt or an AGENTS.md changes that.
Worked example: predicting what applies
Using the layout above, with AGENTS.md files at the root, in packages/api/, and in packages/web/, here is which file is closest for each edit:
| File being edited | AGENTS.md files in scope | Closest (wins on conflict) |
|---|---|---|
| `packages/web/src/components/Button.tsx` | web, root | web |
| `packages/api/src/routes/invoices.ts` | api, root | api |
| `packages/shared/src/money.ts` | root | root |
| `README.md` | root | root |
The shared package has no file of its own, so the walk up the tree continues past packages/ and ends at the root. The packages/ directory itself has no AGENTS.md either, so nothing there interferes.
A task can touch files in several places. If an agent changes both packages/api/src/routes/invoices.ts and packages/web/src/components/Button.tsx in one session, a tool that follows the spec applies the api file for the first edit and the web file for the second. It does not merge the two nested files into one set of rules. If you are not sure how your tool behaves, ask it which instruction files it loaded.
Where tools differ
The nearest-file rule is a convention that most tools follow, but implementations differ in ways that affect results.
- Loading time. Some tools read the root file when the session starts and load nested files only when they read or edit something in that directory. Others read every file up front. A nested rule may not be in the agent's context until it touches that directory.
- Starting directory. Many tools treat the directory you launch them from as the starting point. If you start an agent inside
packages/web/, it may find the web file and the root, or it may find only the web file. Check your tool's documentation, and launch from the repository root when in doubt. - Competing files. If a directory contains both AGENTS.md and a tool-specific file such as CLAUDE.md, a tool may read one, the other, or both. The compatibility chapter covers this.
- Context limits. Instruction files consume space in the model's context. A very long root file plus several nested files can push out other material, and the agent may follow later or shorter instructions more reliably than buried ones.
Common errors
- Duplicating the root in every nested file. Copied rules drift apart, and you end up with contradictions that the nearest-file rule then hides. Keep shared rules in the root and put only differences in nested files.
- Contradicting the root without saying so. The agent obeys the nearest file, but humans reading the root file get misled. Add a short "overrides the root" note with the reason.
- Expecting a nested file to apply everywhere. A file in
packages/web/has no effect onpackages/api/. Put repository-wide rules in the root. - Assuming a prompt changes the file. A prompt-level override lasts for the session. If it should be permanent, edit AGENTS.md.
- Assuming all tools resolve conflicts the same way. Test with the tool your team uses, from the directory your team launches it from.
Check your understanding
Exercise
A repo has a root AGENTS.md that says "Run tests with npm test" and packages/api/AGENTS.md that says "Run tests with npm test --workspace packages/api". The root file also says "Use named exports only". An agent is asked to edit packages/api/src/services/billing.ts. Which test command and which export rule does it follow?
Show answer
It uses npm test --workspace packages/api, because the nearest file (the api file) conflicts with the root on that topic and wins. It still follows "named exports only", because the api file is silent on exports, so the root rule keeps applying.
Exercise
Your root AGENTS.md requires npm run lint && npm run typecheck before finishing. You ask the agent, in your prompt, to skip those checks for a docs-only change. What happens on the next session, and what should you do if you want the checks skipped for all docs-only changes?
Show answer
The prompt wins for that task, but the file is unchanged, so the next session applies the lint and typecheck rule again. To make the exception permanent, edit AGENTS.md, for example by adding "Skip lint and typecheck when only Markdown files changed."
Exercise
A nested file at packages/web/AGENTS.md allows default exports, but the root file forbids them. Why should the nested file say that it overrides the root, instead of just stating the new rule?
Show answer
The agent follows the nearest file either way, but the explicit note, with its reason, tells human readers the difference is intentional. Without it, a reviewer may treat the inconsistency as a mistake and "fix" it, which could break the package.