Sub topic 6
Where the file lives and what it covers
This chapter assumes you can read a repository layout, know what a working directory is, and have used an AI coding agent on a real project. It covers where AGENTS.md files can be placed, which files an agent reads for a given task, and the errors that come from misplacing them. The next chapter covers which instruction wins when two files disagree.
Placement decides scope
An AGENTS.md file applies to the directory that contains it and to everything below that directory. The file has no front matter and no field that declares its scope. Its location is the only scope setting.
That gives you three common placements:
- Repository root. Rules for the whole project: setup, build and test commands, general style, pull request conventions.
- A subdirectory. Rules for one package, service or component, such as its own test command or framework conventions.
- A user-level location. Some tools also read a personal file from a home or configuration directory. This holds your own preferences across all projects. Whether and where a tool reads it varies by tool, so check its documentation.
The root file is the default choice. Add a nested file only when a part of the repository needs guidance that would be wrong, or just noise, elsewhere.
repo/
├── AGENTS.md # applies to every file below
├── docs/
│ └── guide.md # root file applies
├── packages/
│ ├── api/
│ │ ├── AGENTS.md # applies to packages/api/ and below
│ │ └── src/
│ │ └── server.ts # root + api files apply
│ └── web/
│ └── src/
│ └── app.tsx # root file applies (no web/AGENTS.md)
└── package.jsonIn this tree, server.ts falls under two files and app.tsx falls under one. A directory without its own file inherits from the nearest ancestor that has one.
How an agent finds the files
Agents differ in the details, but the usual process has two parts.
- At the start of a session, the tool looks for AGENTS.md in the directory where you launched it, and usually in its parent directories up to the repository root. It loads what it finds into the model's context.
- During the task, when the agent reads or edits a file in a subdirectory, a tool may also pick up the AGENTS.md closest to that file.
The second part is where tools disagree. Some load every nested file eagerly when the session starts. Others load a nested file only when the agent works under that directory. A few read only the root file. Do not assume your tool does the lazy lookup. Test it, using the method in the verification chapter.
The working directory matters
Where you start the agent changes what it sees, because the starting directory is the first place the tool looks.
| Launched from | Root file loaded | `packages/api/AGENTS.md` loaded |
|---|---|---|
| `repo/` | Yes | Only when the agent works under `packages/api/` (tool-dependent) |
| `repo/packages/api/` | Yes, if the tool walks up to the root | Yes |
| `repo/docs/` | Yes, if the tool walks up to the root | No |
| A directory outside the repository | No | No |
A tool that does not walk up to parent directories would miss the root file when you launch from packages/api/. If you see an agent ignoring repo-wide rules in a subdirectory session, this is a likely cause.
What scope means in practice
A nested file narrows guidance. It answers the question: what is different or additional here?
Here is a nested file for the frontend package from a monorepo:
# 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.
Repo-wide rules in the root AGENTS.md also apply unless they conflict with this file.Notice what it leaves out. It does not repeat the Node version, the PR title format, or the lint command that already live in the root file. Each repeated line is a second place to update, and duplicated rules drift apart over time.
The last line states the relationship explicitly. Most tools already combine the files, but saying it removes doubt for a tool that does not.
What goes where
Use these questions to decide placement for a given instruction.
| Question | If yes, put it in |
|---|---|
| Is it true for every directory in the repository? | Root file |
| Does it concern one package, service or folder only? | That directory's file |
| Is it a personal habit, such as preferred editor tools? | Your user-level file, not the repository |
| Does it apply to one tool only? | That tool's own file, see the compatibility chapter |
The personal case matters. A repository file is shared with the whole team and read by every contributor's agent. A rule like "always explain your changes in Spanish" belongs in your own configuration.
Where a nested file earns its place
Create a nested file when at least one of these holds:
- The directory uses a different language or framework from the rest of the repository.
- Its build or test command differs (for example,
go test ./...inside one folder of a Node project). - It has constraints that would mislead the agent elsewhere, such as "generated code, do not edit by hand" for a
generated/directory. - A different team owns it and has different review rules.
Here is an example for a directory of generated code. It is short on purpose.
# AGENTS.md (src/generated)
Files in this directory are produced by `npm run codegen`.
- Do not edit them by hand.
- To change them, edit `schema/api.graphql` and run `npm run codegen`.Avoid creating a nested file for a single rule that could be one line in the root file. Many small files are harder to find and keep current.
Scope is about files, not about tasks
A nested file applies because of where the files being changed live, not because of what you asked for. This causes a common surprise: a task that touches both packages/api/ and packages/web/ is covered by two different nested files. An agent that has loaded only one of them may apply API rules to frontend code.
Two habits reduce the risk:
- Keep nested files free of rules that would be harmful if applied to the wrong package. State commands with their directory, such as
npm test --workspace packages/web, rather than a barenpm test. - For cross-package tasks, name the directories in your prompt so the agent visits both and loads both files.
Paths in instructions
Write paths relative to the repository root unless the file says otherwise. A nested file that says "see src/services/" is ambiguous, because it could mean the nested directory's src/ or the root's src/. Prefer one of these:
- A root-relative path, stated as such: "
packages/api/src/services/(from the repo root)". - A path relative to the file, stated as such: "
./src/services/in this package".
Pick one convention for the whole repository and say which one in the root file.
Common placement errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Agent ignores all rules | File is named `agents.md` or `Agents.md` on a case-sensitive file system, or sits in a directory outside the launch path | Use the exact name `AGENTS.md` and check the location |
| Agent follows root rules but not package rules | Tool loads nested files lazily and the agent never worked under that directory, or the tool reads only the root file | Launch from the package directory, or name the package in your prompt |
| Package rules appear in unrelated work | Nested file placed too high, for example in `packages/` instead of `packages/api/` | Move the file to the narrowest directory it fits |
| Same rule appears in three files with different wording | Duplication across levels | Keep the rule in the highest file where it is true and delete the copies |
| Instructions missing in a fresh clone | File is in `.gitignore` or was never committed | Commit it like any other source file |
Treat AGENTS.md as source code. It should be committed, reviewed in pull requests, and changed alongside the code it describes.
Putting it together
For a repository like the one in the layout above, a reasonable setup is:
- A root file with setup, build, test, lint, style and PR rules that hold everywhere.
- A nested file in each package only where its commands or conventions differ.
- A short nested file for special directories such as generated code.
- Commands that name their directory, and paths written in one consistent convention.
- A check, run once per tool, that confirms the tool loads the files you expect.
Exercise
Your repository has a root AGENTS.md and services/billing/AGENTS.md. An agent session is launched from services/billing/ and asked to fix a bug in services/billing/src/invoice.go. Then you ask it to update services/notifications/src/send.go, which has no AGENTS.md of its own. Which files apply to each edit, and what would you check if the agent ignored repo-wide rules during the billing session?
Show answer
For invoice.go, both the root file and services/billing/AGENTS.md apply. For send.go, only the root file applies, because services/notifications/ has no file and inherits from the nearest ancestor that has one. If repo-wide rules were ignored in the billing session, check whether the tool walks up from the launch directory to load the root file. A tool that reads only the launch directory would miss it. Then check the file name and that the file is committed.
Exercise
A nested file at packages/api/AGENTS.md says "Run tests with npm test." Why is this risky in a monorepo, and how would you rewrite it?
Show answer
The command runs in whatever directory the agent is in, so it may test the whole repository or the wrong package. Rewrite it with an explicit target, for example npm test --workspace packages/api, so it behaves the same from any directory.