Published · Updated · 8 min read
AGENTS.md: rules a coding agent actually follows
Every repo I work in now has an AGENTS.md. What goes in mine, from a one-line pointer to a 600-line rulebook, and the scripts that hold agents to it.
Every repository I work in now has an AGENTS.md. It started as a README for robots. It turned into the place where I write the rules I used to repeat in every chat with a coding agent.
The files range from one line to the rulebook of a healthcare platform, which is about 600. This is what earned its place in them.
1. Next.js 16 writes one for you
Run next dev while a coding agent is working in the terminal, and Next.js 16 creates or updates AGENTS.md itself. It writes a managed block between two markers. Abridged:
<!-- BEGIN:nextjs-agent-rules -->
## This is NOT the Next.js you know
This version has breaking changes. Read the relevant guide in
`node_modules/next/dist/docs/` before writing any code.
Heed deprecation notices.
<!-- END:nextjs-agent-rules -->The idea is right. The agent's training data is older than your next. The docs now ship inside node_modules, versioned with the code, so the agent can read the docs for the version you run instead of the one it remembers. Old App Router habits break on Next.js 16, as I found building this site on Cache Components, and an agent's habits are older than mine.
The block changes between releases. One client site has the older, shorter one. This site and another client site have the newer one, which adds that next dev re-adds the block if you delete it. So commit it. Removing it from every diff is a fight you lose.
Your own rules go outside the markers. Next.js only rewrites what sits between them.
2. One rulebook. Everything else points at it
Every tool wants its own file: CLAUDE.md, GEMINI.md, QODER.md. Copies drift. In my repos, one file holds the rules and the others point at it:
| Repo | File | Whole content |
|---|---|---|
| A client marketing site | CLAUDE.md | @AGENTS.md |
| A healthcare platform | QODER.md | A short map that opens with "AGENTS.md is the rulebook. Read it first." |
| A CRM backend | AGENTS.md | Read docs/ai/project-context.md first. |
@AGENTS.md is Claude Code's import syntax: the file is pulled in as if its text were written there. One source, every tool.
The one agent file I did not write myself shows the trap. That marketing site's GEMINI.md reads like it was generated: 51 lines of stack, folder tree, and a guess about the client, "likely for a creative or logistics agency". It contains no rules. An agent learns nothing from it that ls and package.json would not tell it.
Describing the tree is not a rule. Rules are what the agent cannot read from the code.
3. Say which document wins
The healthcare platform has product requirements, architecture docs, API contracts, and a delivery tracker. They disagree sometimes, because a client changes their mind after a contract was written. An agent that finds two answers picks the plausible one. So AGENTS.md opens with a ladder:
## Authority
When two documents disagree, the higher one is right.
1. docs/product/requirements/ and docs/product/vision.md
2. docs/architecture/
3. docs/implementation/02-api-contracts.md
4. docs/DELIVERY.md
5. This fileRight under it is the rule that saves me the most cleanup:
Never infer product behaviour from incomplete code. Never implement an item marked
BLOCKED,OPEN,DEFERREDor a named seam without an explicit documented decision from Seif.
Agents are very good at filling gaps. On a health platform, a filled gap is a product decision nobody made. Marking items BLOCKED in the docs, and saying that BLOCKED means stop, turns a guess into a question. The AI summaries on that platform were built that way: every open legal point stayed open in the code.
4. Exact commands, including the missing ones
The commands section is meant to be pasted, not paraphrased:
pnpm --filter @app/contracts build # its dist/ is gitignored, so build before anything imports it
pnpm style:check # three gates
pnpm env:check # the environment manifest gate
pnpm --filter @app/api test # jest, unit and integration
pnpm --filter @app/api test:e2e # jest, database-constrained end to endIt also says what does not exist: "There is no root test task. Tests run per package." An agent that guesses pnpm test at the root gets nothing useful, and "nothing failed" is easy to report as "tests pass". One sentence closes that gap.
The same goes for setup traps. The contracts package has to be built before anything imports it, and the comment says why, so the agent does not "fix" the import instead.
5. Put the rules in scripts, and test the scripts
A rule an agent reads is a suggestion. A rule CI runs is a rule. The platform's pnpm style:check runs three gates:
- no raw design values outside the token layer,
- no line-number citations in the docs,
docs/DELIVERY.mdunder 150 lines, with a real commit named on everydonerow.
The citation gate has a history. Line numbers are the easiest citation to write and the first to rot. One Prettier reflow, commit ca42be9, moved 49 lines of the API contract, and about 250 citations across the tree started pointing at the wrong row or at a blank line. Now any L followed by two to four digits fails, and docs cite a section plus a concept instead.
Every gate tests itself before it scans:
if (process.argv.includes("--self-test")) {
const CAUGHT = ["slots (§10 L410, §22 L746)", "therapist authoring L602"];
const CLEAN = ["The §9.4 shape minus the ledger.", "L1 cache locality is irrelevant here."];
// fail if a CAUGHT line passes, or if a CLEAN line is flagged
}The comment above that block is the best sentence in the repo: a gate that silently stops matching reports OK.
This site works the same way. content/blog/WRITING.md is the style guide "for any AI tool or human", and npm run posts:check runs before every build. It fails on broken frontmatter and warns on em dashes and on dialect words in the Arabic. The agent drafting a post and I get the same warnings.
6. Define "done", or the agent will
The most important paragraph in the platform's file:
A type check and a test suite prove the code is correct. They do not prove the feature works. A user-interface change needs a browser check, and if you cannot run one, say so rather than reporting success.
It goes one step further. Some screens must be checked in a production build, because next build prefetches links and streams responses in pieces, and next dev does neither. One bug, HRD-16, where router.refresh() stopped showing fresh server data, only ever appeared in production builds.
This is what a report looks like under that rule. It is from the commit message of a Next.js security upgrade I made with an agent:
on the patient's home, « Suivi », « Exercices », « Forfaits » and « Paiements », a notification marked read and router.refresh() showed the server's count 30 times of 30, within 52 to 156 ms, with no reload and no console error.
Not "tests pass". Five named screens, a count, and a timing range.
7. Write the fences the tooling cannot
Some guards do not exist, and the file says so:
Branch protection is unavailable on the current GitHub plan, so nothing technical stops you; the rule is the only guard.
Then the rule. Work flows feature/* to develop to staging to main. A push to staging deploys the dev environment, and a push to main deploys production. Neither happens without my explicit instruction. Same for cloud configuration and anything that touches real user data.
An agent with shell access can do all of those. AGENTS.md is where I say which ones it may not, and where I admit that the rule is all there is.
8. Write it for a person too
The last section of that file is about reporting: outcome first, then the files and commit, the real test results, any gate still open, and whether a deployment happened. "Normally none did."
It also asks for plain English: short sentences, name the actor, no idioms. English is my second language, and a rulebook I cannot read quickly is one I stop checking. That rule applies to the file itself.
Skills are the next layer. This site has one, write-post: seven steps, from reading WRITING.md first to reporting the file paths, the word count, and anything it could not verify. The agent loads it only when it writes a post, so the main file stays short.
What I'd do again
- One rulebook in
AGENTS.md. Every other agent file is a pointer. - Let
next devown its block, and commit it. - An authority ladder, plus "
BLOCKEDmeans stop". - Exact commands, and a line for each one that does not exist.
- Rules in scripts, and scripts that test themselves.
- A written definition of done that includes a browser.
- The fences tooling cannot enforce, written down as rules.
If your team is starting to work with coding agents and wants a repo they cannot wander off in, get in touch.