Sub topic 8
Nested AGENTS.md files in monorepos
A monorepo holds several packages or services in one repository. They often differ in language, test runner, and style rules. A single root AGENTS.md either becomes long and full of exceptions, or it stays generic and says little about any one package. Nested files solve this by letting each package describe itself.
This chapter assumes you have read the earlier chapters on the file format, core content, and file placement. You should know that an AGENTS.md covers the directory tree it lives in, and that the nearest file takes precedence over files higher up.
Why one file stops working
Consider a repository with a TypeScript API, a React frontend, and a shared library. A single root file has to answer questions such as these:
- Which test command applies when I edit
packages/api/? - Are class components allowed in
packages/web/? - Does the shared library need a changelog entry?
Putting all of this in one file causes two problems. The file grows, and the agent reads rules that do not apply to the files it is editing. Irrelevant rules cost context and can cause mistakes, such as running frontend tests after a backend change. Splitting the guidance by directory keeps each file short and relevant.
The layout
Each package gets its own AGENTS.md. The root file holds only what is true for the whole repository.
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 packages/api/src/routes/invoices.ts, the closest file is packages/api/AGENTS.md. When it edits packages/web/src/App.tsx, the closest file is packages/web/AGENTS.md. A change to package.json at the repository root falls under the root file only.
What goes at each level
A useful rule: put a statement at the highest level where it is true for everything below, and no higher.
| Level | Typical content |
|---|---|
| Root | Repository overview, how to install dependencies, shared conventions such as the pull request title format, security rules, and the list of packages |
| Package | The commands to build and test only this package, language and framework conventions, local directory layout, package-specific hazards |
Here is a package-level file. It adds commands and rules and does not repeat the root.
# 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 worth keeping. Tools differ in how they combine files, and an explicit sentence removes doubt for both humans and agents.
How combining works in practice
Support for nested files varies by tool, so check your tool's documentation. Common behaviors are:
- The tool reads the root file at the start of a session, and reads a nested file when the agent works on files in that directory.
- The tool reads every file on the path from the root down to the edited file and treats the deepest one as winning in a conflict.
- The tool reads only the single closest file and ignores the others.
The third behavior matters most. If your tool reads only the nearest file, a package file that says "root rules also apply" will not pull in the root content. In that case, either test the behavior (see the chapter on verification) or repeat the few essential shared rules in the package file.
Conflicts are resolved by distance: the nearest file wins. An explicit instruction from the user in the prompt overrides any file. Use that when you need a one-off exception.
Writing package files that do not drift
Override only what differs
If the root says "Use named exports; do not add default exports," do not copy that line into each package. Copies diverge over time. State a rule in a package file only when it is new or when it replaces a root rule.
Make replacements explicit
When a package must break a root rule, say so and say why. Compare these two lines in packages/legacy-admin/AGENTS.md:
- Default exports are allowed in this package only, because the framework router requires them.An agent that sees the reason can judge edge cases. An agent that sees an unexplained contradiction may follow the root rule instead.
Keep commands runnable from a known directory
Monorepo commands depend on the working directory. State where each command runs. These two lines behave differently:
- From the repo root: `npm test --workspace packages/api`
- From `packages/api/`: `npm test`Pick one convention for the whole repository and say it in the root file. Mixing them is a common cause of an agent running the wrong tests or failing with "missing script" errors.
Prefer the narrowest verification command
Agents iterate faster when they can test only the package they changed. Give each package a single-package test command and a single-package lint command. Also give the root a command for the full suite, to run before a pull request.
A complete example
A root file for the layout above:
# AGENTS.md
## Project overview
Invoice platform. npm workspaces: `packages/api` (Node.js 20, PostgreSQL),
`packages/web` (React), `packages/shared` (types and validators).
## Setup
- Install all dependencies from the repo root: `npm ci`
## Conventions
- Run package commands from the repo root with `--workspace packages/<name>`.
- Use named exports; do not add default exports.
- Run the full suite before opening a pull request: `npm test --workspaces`
## Pull requests
- Add or update tests for every behavior change.
- Use the title format `[area] Short description`, where area is `api`, `web`, or `shared`.
## Packages
Each package has its own AGENTS.md with its test, lint, and style rules.
Read it before editing files in that package.And a matching file for packages/api/:
# AGENTS.md (packages/api)
## Build and test
- Test this package: `npm test --workspace packages/api`
- Run one file: `npm test --workspace packages/api -- src/services/invoice.test.ts`
- Lint and type-check: `npm run lint --workspace packages/api && npm run typecheck --workspace packages/api`
## Structure
- `src/routes/`: HTTP handlers. Keep them thin.
- `src/services/`: business logic.
- `migrations/`: never edit an existing migration; add a new one.The root file ends with a pointer to the package files. That line helps tools that load only the root at startup, because it tells the agent to look further.
Cross-package changes
Some changes touch several packages, for example a new field in packages/shared that api and web both use. Several package files then apply in one task. Handle this in three ways:
- Document the dependency order in the root file, such as "build
sharedfirst." - Tell the agent to run the full suite for any change in
packages/shared. - Where two package files give conflicting rules, the rule for the file being edited applies. Avoid designing conflicts in the first place.
Common mistakes
- Duplicating the root in every package. The copies drift, and a later root edit leaves stale rules behind.
- Creating a file for every directory. Add a nested file when a directory has different commands or rules, not when it merely exists. Many tiny files are hard to keep current.
- Package files that depend on unstated root context. If a package command only works after a root setup step, say so in that file.
- Assuming the tool merges files. Verify with a test prompt in each package.
- Contradicting the root without explanation. The agent may not know which rule to trust.
Exercises
Exercise
A repository has AGENTS.md, packages/api/AGENTS.md, and packages/web/AGENTS.md. An agent edits packages/web/src/components/Button.tsx. Which file is closest, and what happens if the root file says "use default exports" and the web file says "do not use default exports"?
Show answer
The closest file is packages/web/AGENTS.md. Because the nearest file wins, the agent should not use default exports in that file. The user's prompt would still override both if it said otherwise.
Exercise
Your tool reads only the single closest AGENTS.md. Your package file says "Repo-wide rules also apply." Why might this fail, and what are two ways to fix it?
Show answer
The tool never loads the root file when it reads the package file, so the root rules are missing. Fix it by repeating the few essential shared rules in the package file, or by changing the setup so the tool loads the root file too, if it supports that. Verify the result with a test prompt.
Exercise
The line "Run tests with npm test" appears in the root file of a monorepo. What is wrong with it, and how would you rewrite it?
Show answer
It does not say where to run it or which package it covers, so it may run the wrong tests or fail. Rewrite it with a working directory and a package, for example "From the repo root: npm test --workspace packages/api," and add npm test --workspaces for the full suite.