AGENTS.md and CLAUDE.md: How to Write Instructions Coding Agents Actually Follow
Coding agents write most of the pull requests in our main repository: Claude Code sessions run by the team, and Cogny's own coding agent working through tickets. A dozen or more merge on an ordinary day. After a year of that, our instruction files are the most-read documentation we have, and we've learned what makes an agent follow them.
The short version: one root file with the rules every change needs, one file per subsystem next to its code, the reason behind every rule, and a check wherever a rule can be checked. The rest of this post is the detail, and a template you can copy at the end.
AGENTS.md or CLAUDE.md? Keep one file, link the other
Claude Code reads CLAUDE.md. Codex, Cursor, GitHub Copilot's coding
agent and a growing list of other tools read AGENTS.md. If your team
uses more than one agent, and most teams do, you want both names
showing the same text.
Two ways to do it:
# Option 1: a symbolic link (git records it as a link)
ln -s AGENTS.md CLAUDE.md
git add CLAUDE.md
# Option 2: a one-line CLAUDE.md that imports the other file
echo "@AGENTS.md" > CLAUDE.md
Recent versions of create-next-app use option 2 when they set up a
new project.
Don't use a hard link. It looks fine on your machine, but git doesn't record hard links. Everyone who clones the repository gets two separate files, and the first edit to one of them makes them disagree. Agents then follow different rules depending on which tool reads which file.
On Windows, a symlink only checks out as a link when git's
core.symlinks setting is on. If your team works on Windows, the
one-line import is the safer choice.
The same applies to instruction files in subfolders: put an
AGENTS.md link next to every CLAUDE.md (or the other way round).
One root file, then a file per subsystem
Our root file started as one document and grew past a thousand lines. Every agent paid for every line on every task, including the billing rules that had nothing to do with a CSS fix.
What works better:
- The root file holds only what every change needs. How to run the project, how work is reviewed and merged, what always needs a human, and the few rules that break production when ignored.
- Each subsystem gets its own file in its own folder. Claude Code
loads a
CLAUDE.mdfrom a subfolder when it first reads a file there. Most tools that readAGENTS.mddo the same, with the closest file winning. Detail lives where the work happens. - The root file lists every subsystem file in one line: the path, plus the one rule you need even if you never open it. An agent touching billing from the outside learns the rule that matters without loading the whole billing file.
## Where the rest lives
- **Billing** — `src/billing/AGENTS.md`. Every charge goes through
`chargeCustomer()`; never write to the ledger table directly.
- **Email** — `src/email/AGENTS.md`. Sends are queued, never inline in
a request handler.
Write the reason next to the rule
"Never call the payment API from a page component" gets followed in page components. "Never call the payment API from a page component: in March a retry loop in one charged a customer eleven times" gets followed in API routes, cron jobs and scripts too. The agent understands what the rule protects and applies it to cases the rule doesn't name.
Rules without reasons also rot. Nobody dares delete a line they don't understand, so the file keeps growing. A rule with its reason can be retired once the reason is gone.
Two kinds of reasons work well:
- What went wrong, with a date: what broke, how long it stayed broken, how it was found.
- What the rule protects, when nothing has gone wrong yet: who depends on it, what it costs if it breaks.
Name the stale value, not just the right one
Agents read far more than your instruction file: old pull requests, comments, commit messages, documentation written before the last change. When a fact changes (a price, a limit, an endpoint, a plan name), writing only the new value isn't enough. Old values live on elsewhere, and an agent that finds both will pick one.
Write the dead value down: "The Pro plan is $90/month. $49 is the old price and is wrong." Then the agent knows which one to trust.
Better still, don't copy numbers into the file at all. Point at the
one place they live ("prices come from pricing.ts; never copy them
into page copy") and let a test compare the two.
A rule a test can check should be a test
Instruction files are good at judgment ("write error messages that name the cause"). They're bad at anything mechanical. An agent keeps a mechanical rule most of the time, and in a busy repository most of the time means a broken page every week.
Whenever a rule can be checked, write the check and have the instruction file name it:
- "Every public page has a canonical tag" → a test that crawls the sitemap and fails on a page without one.
- "Every new route gets a smoke test" → a list of URLs and expected status codes that CI fetches on every preview deploy.
- "The docs' prices match the code" → a test that reads both.
The instruction file then says what the test protects and how to fix a failure: "fix the content, not the assertion." Agents are good at making a red test green. Tell them which side of it is the truth.
Say what not to build on
Every codebase has dead ends: an old table still in the schema, a deprecated client that still compiles, a v1 endpoint nobody calls. An agent searching the code finds them, and they look as real as the live paths.
Give each dead end a short section that says what it is, when it stopped being used, what replaced it, and what still depends on it. That saves more wrong pull requests than any other kind of line.
Put the tracker in the loop
If work is planned anywhere outside the repository (an issue tracker, a ticket board, a backlog an agent generates), tell agents to check it before they start and to link their pull request when they finish.
We learned this one the expensive way. A ticket written in May asked for a competitor comparison page. A person built the page in November without the ticket. The ticket was still open, so a coding agent picked it up weeks later and built a second page for the same searches. One line in the instruction file ("search open tickets for the same work before you open a pull request, and link the one you close") would have prevented it.
Change the file in the same pull request
An instruction file is only as good as its last update. Make it part of the change:
- A pull request that makes a line wrong fixes that line.
- A pull request that adds a subsystem adds its file and its one-line entry in the root.
- Reviewers, human or AI, ask "does this change make any instruction wrong?" as a standard question.
What always needs a human
Agents follow "ask first" rules well, but only when the boundary is specific. "Be careful with the database" isn't specific. These are:
- Database changes that drop, rename or rewrite existing data.
- Anything that deletes a user's data, or can't be undone.
- Sending anything outward for the first time: emails, posts, payments.
- Secrets: never in the repository, never in a deployment file as plain text.
State the safe path next to each one ("additive migrations are fine in a pull request; destructive ones wait for a maintainer's approval"), so the agent isn't blocked on the 90% of changes that carry no risk.
Template: AGENTS.md for a web app with public pages
Copy this, delete what doesn't apply, and fill in the angle brackets. It covers a typical product: a web app behind sign-in, plus public marketing pages that need to rank.
# <Project> — instructions for coding agents
CLAUDE.md is a link to this file. Subsystem rules live in an AGENTS.md
next to their code; the list is at the bottom. Every rule here applies
to every change.
## Commands
- `<install>` · `<dev server>` · `<tests>` · `<lint>` · `<build>`
- Run tests and lint before you open a pull request.
## How work ships
- Never push to main. Branch (`feature/…`, `fix/…`, `docs/…`), open a
pull request, describe what changed and why.
- Before you start, search open issues/tickets for the same work. Link
the one your pull request closes; close any it makes obsolete.
- A change that makes a line in this file wrong fixes that line in the
same pull request.
## Needs a human first
- Migrations that drop, rename or rewrite existing data. Additive ones
(new table, new column, new index) are fine in a pull request.
- Deleting user data, or anything that can't be undone.
- Sending anything outward (email, posts, payments) from a new code path.
- Secrets never go in the repository or in plain text in deploy files.
## Public pages
- Every public page declares a canonical URL to itself, absolute, on
the main host. A canonical never points at a redirect.
- Public pages live on one host only; other hosts redirect to it.
- A removed or renamed page gets a redirect to its closest live page,
never a 404.
- Signed-in pages are noindex.
- The sitemap's lastmod is the date the page's content changed, not
the build time.
- A new public URL or redirect gets a row in `<smoke list file>`
(path, expected status code).
## Copy users see
- No placeholder text on any path a user can reach: no "coming soon",
"TODO" or "Something went wrong" without a cause.
- Error messages name the real cause and a next step.
- Prices, limits and plan names come from `<source file>`; never copy
them into page copy.
## Don't build on
- `<old table / client / endpoint>`: unused since <date>, replaced by
`<new thing>`. Only `<remaining dependency>` still reads it.
## Where the rest lives
- **<Subsystem>** — `<path>/AGENTS.md`. <The one rule you need even
without opening it.>
Setting it up in an existing repository
- Move your current instructions into
AGENTS.md(or keepCLAUDE.mdas the source; either direction works). - Link the other name:
ln -s AGENTS.md CLAUDE.md, or a one-lineCLAUDE.mdwith@AGENTS.md. - Cut every paragraph that only matters inside one folder and move it into a file in that folder. Leave a one-line pointer in the root.
- Read each remaining rule and add its reason. Delete the rules nobody can explain.
- Pick the three rules agents break most often and turn them into tests.
- Add the "update this file in the same pull request" rule, and ask for it in review.
The file you end up with is shorter than the one you started with, and agents follow more of it.
Cogny connects your coding agent, or any agent, to your marketing data and tools over MCP. See how to connect your agent.