Sub topic 4
What to put in AGENTS.md
This chapter assumes you know basic Git and repository layout, can write Markdown, and run build and test commands from a terminal. You should also have used an AI coding assistant at least a few times. By the end, you will be able to draft an AGENTS.md whose sections cover what an agent cannot guess from the code alone.
The selection rule
An agent can read your source files, so you do not need to describe what it can discover quickly. It cannot know which command your team actually runs before a merge, which directory it must never touch, or which conventions reviewers enforce. AGENTS.md is the place for that second kind of knowledge.
A useful test for any line you consider adding:
- Could the agent find this by reading the code or running
ls? If yes, leave it out or keep it to one line. - Would a mistake here cost a failed build, a rejected pull request, or a broken migration? If yes, include it.
- Is the statement specific enough that a reviewer could check whether it was followed? If not, rewrite it.
The file the Topic Spine provides as its main example follows this rule. It has six groups of content, and each group is covered below.
# AGENTS.md
## Project overview
Web API for invoice processing. TypeScript, Node.js 20, PostgreSQL.
## Setup
- Install dependencies: `npm ci`
- Start the local database: `docker compose up -d db`
## Build and test
- Build: `npm run build`
- Run all tests: `npm test`
- Run one test file: `npm test -- path/to/file.test.ts`
- Lint and type-check before finishing: `npm run lint && npm run typecheck`
## Code style
- Use named exports; do not add default exports.
- Keep route handlers thin and put business logic in `src/services/`.
## Project structure
- `src/routes/`: HTTP handlers
- `src/services/`: business logic
- `migrations/`: database migrations (never edit an existing migration)
## Pull requests
- Add or update tests for every behavior change.
- Use the title format `[area] Short description`.Project overview
Start with one or two lines: what the project does and its main technologies. The example says "Web API for invoice processing. TypeScript, Node.js 20, PostgreSQL." That is enough for the agent to choose the right language idioms and to avoid suggesting, for instance, a MongoDB client.
Include version numbers when they change what code is valid. "Node.js 20" tells the agent it can use built-in fetch and does not need a polyfill. Leave out the project's history, mission statement, and marketing description. Those belong in the README, which is written for people.
Setup
The setup section lists the commands that take a fresh checkout to a working state. Write each one exactly as you would type it.
- Dependency installation:
npm cirather thannpm install, if your team wants installs that follow the lockfile exactly. - Services the code needs, such as
docker compose up -d db. - Required tool versions, if they are not pinned elsewhere.
Name the variables the project needs without including their values. For example, "Copy .env.example to .env" is useful. Pasting the contents of a real .env is not, and the security chapter covers why.
Build and test
This is usually the most valuable section, because it lets the agent verify its own work. Without it, an agent often guesses at commands, and a wrong guess wastes a cycle or produces a false sense of success.
List at least these commands:
- How to build.
- How to run the full test suite.
- How to run a single test file, which is much faster while iterating.
- How to run lint and type checks.
The single-file command matters more than it looks. A full suite might take ten minutes. If the agent only knows the full command, it either waits each time or skips testing. In the example, npm test -- path/to/file.test.ts gives it a fast loop.
State when to run the checks, too. "Lint and type-check before finishing" tells the agent to run npm run lint && npm run typecheck as a last step. Chaining with && also tells it that a lint failure should stop the sequence.
Code style
Include only style rules that a formatter or linter does not already enforce, or that the tools cannot express. If Prettier handles indentation and quotes, do not restate them. If your team has a convention that no tool checks, state it.
The example has two such rules:
- "Use named exports; do not add default exports."
- "Keep route handlers thin and put business logic in
src/services/."
The first is a checkable pattern. The second is an architectural rule, and it ties to a directory the agent can find. Both name something concrete.
Phrase rules with the action you want, and add the prohibition only when the wrong behavior is common. "Use function components and hooks; do not add class components" works because it names the alternative.
Project structure
A short map of the directories saves the agent from searching. List only the directories where placement decisions happen, and say what each one holds.
## Project structure
- `src/routes/`: HTTP handlers
- `src/services/`: business logic
- `migrations/`: database migrations (never edit an existing migration)The migrations/ line shows a second use of this section: attaching a constraint to a location. The parenthetical "never edit an existing migration" is an edge case that causes real damage. An applied migration that is edited after the fact leaves databases in different states depending on when they were created. Put that kind of rule next to the path it protects, so the agent reads it at the moment it matters.
Do not list every file. A tree of two hundred entries goes stale quickly and pushes the useful lines out of the agent's attention.
Pull request conventions
If the agent opens branches or drafts pull requests, tell it what your reviewers expect. The example has two rules:
- "Add or update tests for every behavior change."
- "Use the title format
[area] Short description."
Other items that belong here are commit message format, branch naming, and whether to include a changelog entry. Each should be as exact as the title format: a pattern the agent can match, not "write good titles."
Writing concrete content
Vague guidance gives an agent nothing to act on and gives a reviewer nothing to check. Compare these two forms:
<!-- Vague: hard to act on or check -->
- Write clean code and test it properly.
<!-- Concrete: an agent can follow it and a reviewer can verify it -->
- Run `npm run lint && npm test` before finishing; fix any failures you introduced.
- New public functions in `src/services/` need a unit test in the matching `*.test.ts` file.The vague line has two problems. "Clean" and "properly" mean different things to different readers. And there is no way to tell afterward whether the instruction was followed. The concrete lines name a command and a location. You can check the first by looking at whether the checks ran, and the second by looking for a test file.
The phrase "fix any failures you introduced" is deliberate. It stops the agent from trying to repair unrelated failing tests that already existed, which would enlarge the change.
What to leave out
Some content looks helpful but does harm.
| Content | Why to leave it out | Where it belongs |
|---|---|---|
| Secrets, tokens, passwords | The file is committed and read by tools and people | A secrets manager or untracked `.env` |
| Rules a linter already enforces | They duplicate configuration and drift from it | The linter config |
| Long architecture essays | They bury the commands the agent needs | `docs/` and a link from AGENTS.md |
| Instructions for human onboarding | Different audience and tone | README or CONTRIBUTING |
| Details that change weekly | The file will be wrong within a month | The code, or a pointer to where it lives |
Length matters because everything in the file competes for the agent's attention. A file of thirty precise lines is usually followed better than one of three hundred lines with the same thirty buried in it. If a section grows large, move the detail into a separate document and link to it with one line saying when to read it.
A drafting checklist
Use this order when writing a first version:
- Write the overview in one or two lines.
- Run your build, test, lint, and type-check commands, then copy them in exactly as they ran.
- Add the single-file test command.
- Add three to five style or architecture rules that reviewers actually repeat in pull request comments.
- List the directories where placement matters, with any hard constraints next to them.
- Add pull request rules, if the agent will open them.
- Reread each line and apply the three-question test from the start of this chapter.
Step 4 is a good source of material. Skim recent pull request reviews and note the comments you have written more than once. Each repeated comment is a candidate rule.
Exercise
A teammate proposes adding these three lines to AGENTS.md: "Use 2-space indentation" (Prettier already enforces this), "Keep the code readable", and "Never edit an existing file in migrations/". Which of the three should go in, and why do the other two not belong?
Show answer
Only the migrations rule belongs. It is specific, checkable by looking at the diff, and a violation causes real damage because applied migrations must not change. The indentation rule duplicates what Prettier already enforces, so it adds length and can drift from the formatter config. "Keep the code readable" is vague: the agent cannot tell what action it requires, and a reviewer cannot verify it.
Exercise
Write a "Build and test" section for a Python project that uses pytest and ruff. It must include the command to run all tests, the command to run one test file, and a lint step to run before finishing.
Show answer
One valid answer:
## Build and test
- Run all tests: pytest
- Run one test file: pytest tests/test_invoices.py
- Lint before finishing: ruff check .
Any answer is acceptable if it names real commands for all three purposes, states that lint runs before finishing, and works when typed from the repository root.