Sub topic 8
Nested AGENTS.md files in monorepos
This chapter assumes you know basic Git and repository layout, can read Markdown, and have used a coding agent. It builds on the earlier chapters about file placement and precedence.
Why one root file is not enough
A monorepo holds several packages in one repository. A frontend package and an API package often use different test commands, frameworks and style rules. A single root AGENTS.md has two bad options. It can list every package's rules, which makes it long and full of instructions that don't apply to the file being edited. Or it can stay generic, which leaves the agent guessing the right command.
Nested files solve this. Each package gets its own AGENTS.md, and the root file keeps only what the whole repository shares.
A layout with nested files
repo/
├── AGENTS.md # repo-wide rules
├── packages/
│ ├── api/
│ │ ├── AGENTS.md # API-specific commands and rules
│ │ └── src/
│ └── web/
│ ├── AGENTS.md # frontend-specific commands and rules
│ └── src/
└── package.jsonWhen an agent edits a file under packages/api/, the closest file in that directory tree is packages/api/AGENTS.md. That file guides the work. The root file still applies as background.
What goes where
Put guidance in the root file when it is true for every package:
- Installing dependencies and starting shared services.
- Pull request title format and review rules.
- Rules about files at the repository level, such as never editing an existing migration.
Put guidance in a nested file when it is true only for that package:
- The command that tests only that package.
- Framework conventions, such as hooks instead of class components.
- Package-specific directories and build outputs.
Here is a nested file for the web package:
# 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.The last line is useful. It tells the reader, human or agent, that the nested file adds to the root file instead of replacing it. When the two disagree, the nearest file wins.
Predicting which file applies
Use this procedure for any file path:
- Start in the directory that holds the file.
- Look for an
AGENTS.mdthere. If one exists, it is the nearest file. - If not, move to the parent directory and repeat until you reach the repository root.
- Read the nearest file first, then the files above it. Where they conflict, the nearest wins.
For packages/web/src/Button.tsx, the nearest file is packages/web/AGENTS.md. For packages/shared/index.ts, no packages/shared/AGENTS.md exists, so the root file applies. An explicit instruction in the user's prompt still overrides every file.
Common mistakes in monorepos
- Duplicating the root file. Copied rules drift apart. Keep shared rules in one place.
- Contradicting the root without saying so. If a package overrides a root rule, state the override and the reason in the nested file.
- Creating a file per directory. Add a nested file only when a package has commands or rules that differ from the root.
- Using root-relative commands in nested files. A command such as
npm testcan behave differently depending on the directory it runs from. Use workspace flags or state the directory.
Exercise
Your repo has AGENTS.md at the root and packages/api/AGENTS.md. An agent edits packages/api/src/routes/invoices.ts and then packages/shared/utils.ts. Which file is nearest for each edit, and where do the two files conflict on a rule?
Show answer
For packages/api/src/routes/invoices.ts, the nearest file is packages/api/AGENTS.md. For packages/shared/utils.ts, there is no nested file in packages/shared/, so the root AGENTS.md applies. Where the root and API files conflict, the API file wins for edits under packages/api/, unless the user's prompt says otherwise.
Exercise
A root AGENTS.md lists the test command for both the web and API packages, and the file is now 300 lines long. How would you restructure it?
Show answer
Move each package's test command and style rules into that package's own AGENTS.md. Keep only the shared setup, pull request rules and repository-level constraints in the root file.