Sub topic 6
Where the file lives and what it covers
This chapter assumes you know basic Git and repository layout, can read Markdown, and have used an AI coding agent. It explains where an AGENTS.md file goes and which files an agent applies to the code it is editing.
The root file
The most common placement is the repository root, next to .git and your top-level package.json or equivalent. An agent started anywhere in the repository can find this file. It is the right home for guidance that applies to the whole project: how to install dependencies, how to run the full test suite, and the pull request title format.
repo/
├── AGENTS.md
├── src/
└── package.jsonScope is a directory tree
An AGENTS.md file covers the directory that contains it and everything below that directory. A file at the root covers the whole repository. A file at packages/api/AGENTS.md covers only files under packages/api/.
This gives you a simple rule for deciding what applies to a given file. Start in the file's directory and walk upward toward the root. Each AGENTS.md you meet on the way is in scope for that file.
repo/
├── AGENTS.md # covers everything
├── packages/
│ ├── api/
│ │ ├── AGENTS.md # covers packages/api/**
│ │ └── src/
│ └── web/
│ └── src/ # no file here
└── docs/For this layout:
| File being edited | Files in scope |
|---|---|
| `packages/api/src/users.ts` | `packages/api/AGENTS.md`, then root `AGENTS.md` |
| `packages/web/src/App.tsx` | root `AGENTS.md` only |
| `docs/guide.md` | root `AGENTS.md` only |
Directories without their own file are not a problem. They simply inherit from the nearest parent that has one.
Choosing what goes where
Put each rule at the broadest level where it is true, and no broader.
- Root file: project overview, shared setup, repo-wide commands, pull request conventions, and security notes that apply everywhere.
- Subdirectory file: commands that only work in that package, such as a package-scoped test command, and style rules that differ from the rest of the repository.
Avoid copying root content into nested files. Duplicated text drifts, and then two files give different answers. A nested file can say that root rules also apply and then list only what is different.
# 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.Common placement errors
- A file in the wrong directory. An
AGENTS.mdinsidesrc/does not coverdocs/orscripts/. If a rule should reach those, move it up. - A nested file written as if it were the only one. If
packages/api/AGENTS.mdomits the lint command because "the root has it," that is fine. If it contradicts the root, the agent has to choose, and you should not rely on its choice. - Files outside the repository. An agent working in a clone reads files inside that clone. A file in your home directory or a parent folder is not part of the project and teammates will not have it.
- Uncommitted files. An
AGENTS.mdthat exists only on your machine helps only you. Commit it so every contributor and every agent run sees the same instructions.
Check your layout
List every instruction file in a repository with a quick command:
git ls-files '*AGENTS.md'The output shows which directories have their own files. Using git ls-files also confirms that each file is committed, because it only lists tracked files.
Exercise
A repository has AGENTS.md at the root and services/billing/AGENTS.md. There is no file in services/billing/migrations/. Which files are in scope when an agent edits services/billing/migrations/001_init.sql, and which file is the most specific?
Show answer
Both services/billing/AGENTS.md and the root AGENTS.md are in scope. The migrations directory has no file, so it inherits from its parents. The most specific is services/billing/AGENTS.md, because it is the nearest one above the edited file.
Exercise
Your root AGENTS.md and packages/api/AGENTS.md both repeat the same 20 lines of setup steps. What is the risk, and how do you fix it?
Show answer
The copies can drift apart, so the two files eventually give conflicting setup steps. Keep the setup steps only in the root file. Reduce the nested file to the commands and rules that are specific to packages/api/.