Sub topic 3
Nested AGENTS.md Files in Monorepos
Assume you know basic Git, Markdown, repository layout, and command-line build workflows. This chapter focuses on how instruction files interact when one repository contains several projects.
Nested AGENTS.md Files in Monorepos
An AGENTS.md file applies to the directory containing it and its descendants. A repository can therefore combine shared rules with package-specific rules:
repo/
├── AGENTS.md
└── packages/
├── api/
│ ├── AGENTS.md
│ └── src/
└── web/
├── AGENTS.md
└── src/An agent editing packages/api/src/routes.ts should use the root file and the API file. The more specific file adds or refines instructions for that package. If instructions conflict, the nearest applicable file normally wins. An explicit instruction in the user’s request takes priority over repository guidance.
Working Alongside CLAUDE.md and Other Files
Different tools may look for different filenames. Keep one source of truth when possible:
ln -s AGENTS.md CLAUDE.mdIf a symlink is unsuitable, make CLAUDE.md import or reproduce the shared guidance, then add only tool-specific notes. Do not maintain conflicting copies casually. A change to commands or style rules should be reflected everywhere that agents read.
Keeping Secrets and Risky Guidance Out
Treat instruction files as repository content that many tools and contributors can read. Never include passwords, tokens, private keys, or production connection strings. Avoid instructions that bypass review, disable security checks, or run destructive commands without confirmation. Document how to obtain credentials through approved environment variables or secret managers instead.
> callout note > Write instructions that are safe if an agent follows them literally. Prefer bounded commands and name the environment where they should run.
Keeping AGENTS.md Current
Review the file when scripts, directory names, frameworks, or pull-request rules change. Remove obsolete commands instead of appending exceptions. A short file with verified commands is more useful than a complete history of past workflows.
Checking That the Agent Follows Your Instructions
Use a small test task in a disposable branch or workspace. Ask the agent to inspect a package, report the applicable rules, and propose a change without applying it. Check that it chooses the correct commands, follows package-specific style, and avoids restricted actions. Then run the documented lint, type-check, and test commands yourself.
Common Pitfalls
- Writing vague rules such as “use good style.”
- Duplicating incompatible instructions in nested files.
- Assuming every tool supports the same filename or precedence model.
- Listing commands that no longer exist.
- Putting secrets or unrestricted production commands in the file.
Exercise
Which AGENTS.md should guide an edit to packages/web/src/App.tsx when both the repository root and packages/web/ contain one?
Show answer
Use both applicable files, with the nearer packages/web/AGENTS.md taking precedence when their instructions conflict.