AGENTS.md in a monorepo: nested files
Put repo-wide commands in a root AGENTS.md and package details in nested ones. How Codex, Claude Code and Cursor load them, with examples of what goes where.
In a monorepo, keep one AGENTS.md at the root for repo-wide commands and conventions, and add a smaller AGENTS.md inside each package that needs its own rules. The AGENTS.md format says agents read the nearest file in the directory tree, so the closest one takes precedence. Codex loads the chain from the root down to the directory you started in. Claude Code and Cursor also load a subdirectory's file once you work on files there, and Claude Code needs a CLAUDE.md that imports it.
How do Codex, Claude Code and Cursor load nested files?
All three read the root file and the files between the root and the place you're working. They differ on what happens below your working directory, and on what happens when two files disagree.
| Tool | What loads at the start | Files in other directories | Two files disagree |
|---|---|---|---|
| Codex | One file per directory from the repository root down to the directory you started in | The documented chain stops at your working directory | Later files, which are closer, appear later in the prompt and win by position |
| Claude Code | CLAUDE.md in the working directory and every directory above it. Put @AGENTS.md in each one to load the matching AGENTS.md |
A subdirectory's CLAUDE.md loads when Claude reads or edits a file there |
Files are concatenated and Claude may follow either one |
| Cursor | AGENTS.md at the project root |
Nested files apply when you work on files in that directory or below | The more specific file takes precedence |
For Codex, each directory contributes at most one file, checked in the order AGENTS.override.md, then AGENTS.md. Codex joins them root first, and it stops adding files once the combined size reaches project_doc_max_bytes, which defaults to 32 KiB. If your instructions are getting cut off, the Codex docs suggest raising the limit or splitting the content across nested directories. Codex AGENTS.md guide
Claude Code reads CLAUDE.md, and reads AGENTS.md directly only under the conditions described in the AGENTS.md vs CLAUDE.md guide. In a monorepo the dependable setup is a CLAUDE.md containing @AGENTS.md on its own line, in the root and in each package that has an AGENTS.md. Anthropic recommends keeping each file under about 200 lines. Instructions that contradict each other aren't resolved by position: the memory documentation says Claude may pick one arbitrarily. Cursor documents nested AGENTS.md files in its rules documentation.
Start Codex inside the package you're changing, so the chain includes that package's file. And write package files as additions to the root, never as rewrites of it, because Claude Code may follow either file when they conflict.
What goes in the root AGENTS.md of a monorepo?
The root file covers what is true everywhere: how to install, how to run every check, how the workspaces are laid out, and the rules every package shares. Here is a root file for a pnpm workspace with two apps and one shared package:
# acme
Instructions for coding agents working in this repository.
## Commands
Run from the repo root.
| Command | What it does |
| --- | --- |
| `pnpm install` | Install every workspace |
| `pnpm -r lint` | Lint every package |
| `pnpm -r test` | Run every package's tests |
| `pnpm --filter <name> test` | Run one package's tests |
| `pnpm check` | Lint, typecheck and test everything. Run before finishing |
## Layout
- `apps/web`: the Next.js site
- `apps/api`: the HTTP API
- `packages/ui`: React components shared by the apps
Apps import from `packages/*`, never from each other.
## Conventions
- Add a dependency to the package that uses it, not the root.
- Keep changes inside the package you were asked about. Mention problems you find elsewhere.What goes in a package's AGENTS.md?
A package file holds what a person working only in that package needs, and nothing the root already says. It starts with that package's commands and then its conventions:
# apps/api
## Commands
| Command | What it does |
| --- | --- |
| `pnpm --filter api dev` | Start the API on port 4000 |
| `pnpm --filter api test -- users.test.ts` | Run one test file |
## Conventions
- Handlers validate input with the schemas in `src/schemas/`, then call a service. Handlers never query the database.
- Migrations live in `migrations/` and are plain SQL. Never edit one that has been committed.
- Tests start a real server against a throwaway SQLite file. Don't mock the database.If a rule would be identical in every package, move it up to the root. If it matters in only one place, it belongs next to the code. A package file that restates the root costs tokens in every session that loads it and is a second place to update, and when the two drift Claude may follow the stale one.
The import line in each package's CLAUDE.md is the whole file:
@AGENTS.mdCodex needs no equivalent, since it reads AGENTS.md itself. When one package needs different instructions from the rest, AGENTS.override.md in that directory takes priority over its AGENTS.md for Codex.
How can I tell which instruction files an agent loaded?
Ask from inside the package. Open the agent in apps/api and ask which instruction files apply to this directory and what the first heading of each says. The answer is the agent's own account, so confirm it with a rule that has an effect you can see. Add a line like End each reply with API-RULES. to apps/api/AGENTS.md, ask a short question from that directory, then ask the same from apps/web. The marker should appear only in the first reply. Delete the line afterward.
Can stacks write the root AGENTS.md for an existing monorepo?
Run stacks add agent-ops --no-launch at the repository root. It writes a root AGENTS.md skeleton, a CLAUDE.md that imports it, and Claude Code hooks. If the repository already has an AGENTS.md, stacks leaves it alone and parks its skeleton under .stacks/incoming/ for your agent to merge.
The skeleton has a Commands table and sections for the architecture map, conventions and testing. Each section holds a stacks:fill comment that tells the agent what to write from the real project. stacks doesn't write the nested package files, so add those yourself and list them in the root file's architecture map.
A project created with add skips its four hygiene checks in a plain stacks verify, and the output says so. stacks verify --all runs them. On the untouched skeleton, the agents-md-filled step fails while any stacks:fill marker remains in the root AGENTS.md:
verify: failed (full run; 2 passed, 2 failed, 0 skipped; 0.4s)
failed check agents-md-filled (exit 1, 0.0s) exited with status 1
failed check claude-readme-filled (exit 1, 0.0s) exited with status 1That step reads only the root file. A leftover marker in a nested file goes unnoticed, so search for one before you merge: grep -rn "stacks:fill" --include=AGENTS.md ..
Add agent guardrails to this existing Go repo. Keep its project rules.
I’ll inspect the existing rules and check whether the TypeScript harness fits.
●Bash(cat AGENTS.md)
●Bash(stacks show claude-harness)
claude-harness targets Bun TypeScript projects. This is a Go CLI, so I’ll add agent-ops alone.
●Bash(stacks add agent-ops --no-launch)
●Bash(find .claude -type f | sort)
●Bash(cat .stacks/incoming/AGENTS.md)
●Bash(cat AGENTS.md)
The hooks are written. My existing AGENTS.md is unchanged; stacks parked its template and Claude settings under .stacks/incoming/. I’ll merge those next, then verify.