AGENTS.md template for Fastify
A ready AGENTS.md for a Fastify API in TypeScript. It covers plugins, route schemas, the commands that verify a change, and the Express habits agents bring with them.
# AGENTS.md A Fastify API written in TypeScript, built from plugins with a JSON schema on every route. ## Commands - `npm run dev`: start the server with reload on http://localhost:3000 - `npm run build`: compile TypeScript to `dist/` - `npm test`: run the tests (they use `app.inject`, no open port) - `npm run lint`: ESLint ## Project structure - `src/app.ts`: builds the Fastify instance and registers plugins - `src/server.ts`: starts listening. Nothing else - `src/plugins/`: shared plugins: database, auth, config - `src/routes/`: route plugins, one folder per resource - `test/`: tests ## Rules for agents - This is Fastify, not Express. No `(req, res, next)` middleware and no `res.json()`. Use hooks, plugins and `reply`. - Handlers are `async` and return the response value. Do not also call `reply.send()`. - Every route declares a schema for its body, query, params and response. - A plugin whose decorators must be seen by other plugins is wrapped with `fastify-plugin`. ## Conventions ### Fastify - Use the JSON schema validation built in — don't reach for Zod unless you need its types. - Plugins for everything reusable (auth, db). Register order matters. - Return objects from handlers; let Fastify serialize via schemas for speed. ### Node.js - Node 20+ features are fair game (top-level await, fetch, test runner). - ESM only for new code. If the repo is CJS, match it — don't mix. - Never block the event loop. Long work goes to workers or a queue. - Env vars via `process.env.X` — validate at startup (Zod or `envsafe`). Fail fast on missing config. ### TypeScript - Prefer `type` for object shapes, `interface` only when extending third-party types. - Never use `any` — use `unknown` and narrow, or write a proper type. - No non-null assertions (`!`) except in tests or when a type guard just ran. - Use `readonly` for props, return types, and array params that don't mutate. - Prefer discriminated unions over enums. Enums only for stable public APIs. - Import types with `import type { ... }` to avoid runtime import cost. ## Avoid - Declaration-merging global namespaces. - Using `Function`, `Object`, or `{}` as types. ## Guardrails - Never commit secrets, API keys, or credentials. If you spot one in a diff, stop and flag it. - Do not invent dependencies. If a package is not already in the manifest, confirm before adding it. - When a command fails, read the actual error. Do not retry blindly or bypass with `--force`. - Prefer editing existing files over creating new ones. - Run the tests and the linter before saying a task is done, and say what you ran.
The commands and folders are the common defaults for Fastify. Change them to match your project before you commit the file: an agent will run exactly what is written here.
How to use it
Save the file as AGENTS.md in the root of your repository and commit it. Then check each tool you use:
| Tool | What to do |
|---|---|
| Claude Code | Reads AGENTS.md only when no CLAUDE.md exists in or above the folder. If you have a CLAUDE.md, put @AGENTS.md on its first line. |
| Cursor | Reads AGENTS.md from the project root, in the desktop app and the terminal. In the app, rules in .cursor/rules must be .mdc files. The terminal reads only plain .md files there. |
| Codex | Reads AGENTS.md from the root down to the folder you are in, with a shared 32 KiB limit across all of them. |
| GitHub Copilot | Reads AGENTS.md on some surfaces only. Keep a .github/copilot-instructions.md as well if your team relies on Copilot. |
Details and sources: the AGENTS.md compatibility matrix.
What agents get wrong in Fastify projects
These are the reasons the “Rules for agents” section says what it says.
Writing Express middleware
Agents write (req, res, next) and call res.json(). Fastify uses hooks and reply, so that code either throws or never sends a response.
Returning and sending in one handler
An async handler that calls reply.send() and also returns a value sends twice, and Fastify logs a "reply already sent" error. The template picks one style: return the value.
Leaving out the schema
Without a schema a route accepts anything and responses are serialised the slow way. Agents skip it because the route works without it.
Decorators that vanish
A plugin's decorators are visible only inside that plugin unless it is wrapped with fastify-plugin. Agents register a database plugin and then find fastify.db undefined in the routes.
Questions
Where does AGENTS.md go in a Fastify project?
In the root of the repository, next to your manifest file. Tools read it from the folder you start them in and, for most tools, from parent folders too.
Does Claude Code read AGENTS.md?
Only when there is no CLAUDE.md in or above the working folder. If you already have a CLAUDE.md, keep it and make its first line @AGENTS.md so both tools read the same rules.
How long should the file be?
Short enough that every line is something the agent would get wrong without it. Codex shares a 32 KiB budget across all the AGENTS.md files it loads, and long files get followed less closely by every tool.
Can I use Zod with this?
Yes. Say so in the file and name the type provider you use. The template defaults to plain JSON schema because it needs no extra packages, and an agent told nothing will mix both.