Sub topic 7
Which instructions win
This chapter assumes you can write a basic AGENTS.md and know that the file can appear in more than one directory. It covers what happens when two sets of instructions disagree.
Why conflicts happen
An agent can receive guidance from several places at once:
- An AGENTS.md at the repository root.
- An AGENTS.md in a subdirectory near the files it is editing.
- The prompt you type in the current session.
These sources often overlap. A root file might say "run npm test" while a package file gives a narrower command. The agent needs a rule for choosing between them.
The precedence rules
Two rules cover most cases.
- The nearest file wins. The agent uses the AGENTS.md closest to the file it is working on, found by walking up the directory tree. A file in
packages/web/outranks the root file for anything underpackages/web/. - Explicit user prompts override files. If you tell the agent something directly in the session, that instruction beats any AGENTS.md.
| Source | Applies to | Priority |
|---|---|---|
| Your prompt in the current session | This task only | Highest |
| Nearest AGENTS.md | Files in its directory tree | Medium |
| Parent AGENTS.md files, up to the root | Everything beneath them | Lowest |
The order reflects how specific and how recent each instruction is. Your prompt describes the exact task in front of the agent. A nested file describes one part of the codebase. The root file describes the whole repository.
A worked example
Suppose the root file contains this:
# AGENTS.md
## Test
- Run all tests: `npm test`And packages/web/AGENTS.md contains this:
# AGENTS.md (packages/web)
## Test
- Run tests for this package only: `npm test --workspace packages/web`When the agent edits packages/web/src/Button.tsx, the nearest file is packages/web/AGENTS.md. The agent runs the package-scoped command. When it edits packages/api/src/server.ts, that package has no AGENTS.md here, so the agent falls back to the root file and runs npm test.
Now suppose you type: "Just run the Button test file, nothing else." That prompt overrides both files for this task.
Conflicts versus additions
Precedence only matters when two instructions contradict each other. Where they don't, the more general guidance still applies. The nested file in the example ends with this line:
Repo-wide rules in the root AGENTS.md also apply unless they conflict with this file.That sentence states the intended behavior. A nested file overrides the specific rules it contradicts and leaves the rest of the root rules in force. If the root says "use named exports" and the nested file says nothing about exports, named exports still apply.
Common mistakes
- Restating the root file in every nested file. Copies drift apart, and then the nested copy silently wins with outdated rules. Put only differences in nested files.
- Contradicting a rule without saying so. If a nested file changes a root rule, name the change. A reader, or an agent, should not have to guess which one was meant.
- Assuming the file beats your prompt. If an agent ignores a rule because you asked for something else in the session, that is the expected behavior.
Quick check
Exercise
A repo has a root AGENTS.md saying "Use default exports are forbidden; use named exports" and packages/web/AGENTS.md saying "Use function components and hooks." An agent edits packages/web/src/Nav.tsx. Which rules apply? Then you type "Use a default export for this one file." What does the agent do?
Show answer
Both files apply, because they do not contradict each other. The agent uses named exports (root) and function components with hooks (nested). When you ask for a default export, your explicit prompt overrides the root rule for that task, so the agent uses a default export.