Sub topic 5
Writing Instructions an Agent Can Act On
This chapter assumes you have read the earlier chapters. You know what AGENTS.md is, that it is plain Markdown, and that it usually lists setup, build, test, style, structure and pull request rules. Here the focus is how to phrase those rules so an agent can follow them and a reviewer can check them.
Why phrasing matters
An agent reads AGENTS.md as part of its working context, then decides what to do. It cannot ask you what "clean" means. If a line leaves room for interpretation, the agent fills the gap with its own habits, and those habits may not match your project.
A good instruction has three properties:
- Concrete. It names the exact command, path, or pattern.
- Verifiable. Someone can look at the result and say whether the rule was followed.
- Concise. It earns its place in the context window and does not repeat what the agent can see in the code.
Compare these two versions of the same intent:
<!-- 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 no command, no location, and no pass condition. The concrete lines each give an action and a way to tell whether it happened.
Name exact commands
The most useful content in AGENTS.md is the set of commands the agent should run. Agents tend to guess when commands are missing. In a Node project they may run npm test when your tests need a database, or run a global tsc that differs from the project version.
Write commands in backticks, one per line, with a short label:
## Build and test
- Install dependencies: `npm ci`
- 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`Notes on this pattern:
- Include the single-file test command. Agents iterate faster when they can run one test instead of the full suite.
- State the working directory if it matters, for example "run from the repository root".
- Say when a command needs a prerequisite, such as
docker compose up -d dbbefore tests that use the database. - Use the form that works non-interactively. A command that opens a prompt or a watch mode will hang an agent. Prefer
npm testover a watch-mode script.
Prefer specific rules over general values
Style guidance is where vagueness creeps in most. Words like "clean", "readable", "idiomatic" and "proper" cannot be checked. Replace each with the observable rule you actually care about.
| Vague | Concrete |
|---|---|
| Follow best practices for exports | Use named exports; do not add default exports. |
| Keep the code organized | Keep route handlers thin and put business logic in `src/services/`. |
| Be careful with the database | Never edit an existing file in `migrations/`; add a new migration instead. |
| Test your changes | Add or update tests for every behavior change. |
| Use our PR format | Use the title format `[area] Short description`. |
Each concrete rule points to something in the repository. You can search for default exports, look at which directory a change touched, or read a pull request title.
Say what to do, not only what to avoid
A prohibition tells the agent what to stay away from but not what to do instead. Pair the two:
- Do not add default exports. Use named exports.
- Do not edit existing migrations. Create a new file in `migrations/` instead.The second sentence removes the guesswork. Without it, an agent blocked from editing a migration might invent its own workaround.
Include only what the agent cannot infer
An agent can read your code. It can see that the project uses TypeScript, that files end in .ts, and that functions use camel case. Spelling out those facts spends context on information the agent already has.
Spend your words on things that are not obvious from the code:
- Commands, especially unusual ones.
- Conventions that differ from the language or framework default.
- Hidden constraints, such as "migrations are append-only".
- Locations of things that are hard to find.
- Steps that must happen in a particular order.
A useful test for each line is: "If I deleted this, would the agent likely make a mistake?" If the answer is no, delete it.
Keep it short
Length has a cost. A long file takes up context on every task, buries the rules that matter, and goes stale faster. A page or two is a reasonable target for a root file. If a section grows large, consider these options:
- Move package-specific rules into a nested AGENTS.md closer to the code (covered in a later chapter).
- Link to a longer document for background, and keep only the actionable rule in AGENTS.md.
- Remove rules that a linter or formatter already enforces. Instead of describing indentation, tell the agent to run the formatter.
The third option is worth emphasizing. A tool that enforces a rule is more reliable than a sentence that asks for it. Write "Run npm run lint before finishing" and let the linter hold the line.
Make instructions verifiable
A verifiable instruction ends with a check. There are three common forms.
A command that must pass.
- Before finishing, run `npm run lint && npm run typecheck`. Both must exit with code 0.A file or location that must exist.
- Every new module in `src/services/` has a test file next to it named `<module>.test.ts`.A format that can be matched.
- Pull request titles use the format `[area] Short description`, for example `[billing] Round invoice totals to cents`.An example inside the rule, as in the last case, helps because it shows the exact shape. Keep examples short and make sure they follow the rule.
Structure for scanning
The agent will read the whole file, but headings still help. They group related rules so that the agent finds the right section for the task at hand, and so humans can review the file quickly.
- Use short headings that match the core sections: setup, build and test, code style, project structure, pull requests.
- Use bullet lists with one rule per line. A paragraph that holds four rules makes it hard to see which applies.
- Put the most important commands near the top.
- Use the same wording for the same thing. If you say "service" in one place and "manager" in another for the same directory, the agent may treat them as different.
Handle priorities and exceptions
Some rules are firm and some are preferences. Mark the difference in words an agent can use.
## Rules
- Never commit directly to `main`.
- Never edit an existing migration.
- Prefer small, focused commits. A larger commit is acceptable for a mechanical rename."Never" and "prefer" carry different weight. Reserve "never" and "must" for rules where a violation causes real damage. If every line says "must", none of them stands out.
When a rule has an exception, put the exception next to the rule rather than in another section. Otherwise the agent may apply one half and miss the other.
A worked rewrite
Here is a first draft written quickly, followed by a revision.
# AGENTS.md
This project is a well-structured API. Please write high-quality code,
follow best practices, make sure everything is tested, and keep the
codebase tidy. Don't break anything. Use the right tools.Every sentence fails the three tests. Nothing is concrete, nothing can be checked, and most of it states goals the agent already assumes. A revision:
# AGENTS.md
## Project overview
Web API for invoice processing. TypeScript, Node.js 20, PostgreSQL.
## Build and test
- Install dependencies: `npm ci`
- Start the local database: `docker compose up -d db`
- 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`.The revision is longer, but each line gives the agent something to do or avoid, and each can be checked.
A revision workflow
When you rewrite an existing file, work through it line by line:
- Mark every line that uses an adjective without a measure ("clean", "good", "proper").
- For each marked line, ask what you would look at to decide if it was followed. Write that down as the rule.
- Delete lines the agent can infer from the code.
- Move any rule a tool can enforce into the tool, and replace the sentence with the command that runs it.
- Check each command by running it yourself from a clean checkout.
Common mistakes
- Stating goals instead of actions. "Ensure high quality" is a goal. "Run
npm run lint" is an action. - Copying the README. The agent can read the README. Link to it if needed.
- Listing commands you have not run recently. Stale commands cause failures and distrust.
- Using relative terms. "Large files", "complex functions" and "slow tests" have no threshold. Give a number or a path.
- Contradicting yourself. Two rules that conflict leave the agent to choose. Search the file for related terms before you add a rule.
Exercise
Rewrite this line so that an agent can follow it and a reviewer can verify it: "Make sure the code is well tested and follows our style." Assume the project uses npm test and npm run lint, and that business logic lives in src/services/ with tests in matching *.test.ts files.
Show answer
One acceptable answer is two lines. "Run npm run lint && npm test before finishing; fix any failures you introduced." and "New public functions in src/services/ need a unit test in the matching *.test.ts file." Each line names an exact command or location, and a reviewer can check the result by running the command or looking for the test file.
Exercise
A teammate adds this to AGENTS.md: "Use 2-space indentation, put semicolons at the end of statements, and sort imports alphabetically." The repository already has a formatter configured as npm run format. How would you improve the instruction, and why?
Show answer
Replace the three style rules with a command, such as "Run npm run format before finishing." The formatter enforces the rules reliably, the file stays shorter, and the instruction stays correct if the formatter configuration changes.