← All posts

How to Write a Good AGENTS.md File (With Examples)

A good AGENTS.md is short, imperative, and verifiable: under 200 lines, full of exact commands and concrete rules an agent can follow without interpreting them, and honest about what must never happen. A bad one is a mood board — "write clean, maintainable code" repeated in six sections.

The difference isn't writing talent; it's testability. Every line in your instruction file should be something you could check in a code review with yes or no. This post shows a weak file, a strong file, and the section-by-section rules that separate them.

What makes an AGENTS.md good

Four properties, in order of importance:

  • Specific — npm run lint && npx tsc --noEmit, not "make sure it passes the checks."
  • Verifiable — you can tell whether the agent violated it, from the diff.
  • Short — 50–150 lines. Claude Code recommends under 200; Codex stops reading combined instructions at 32 KiB.
  • Current — every command in it works on main today. A stale command is worse than none, because the agent will run it faithfully and fail.

Notice what's missing: praise for the stack, architecture essays, and anything you'd only say once in a meeting.

A weak AGENTS.md example

markdown
# AGENTS.md

Welcome to our project! This is a high-quality codebase that follows
modern best practices.

Please write clean, readable, maintainable code. Use good naming
conventions and keep functions small. Always think about performance
and security.

We care a lot about testing — make sure your changes are well tested.
Write documentation when appropriate. Follow the existing style
throughout the codebase.

Be careful with production data. Don't break things. If you're unsure
about something, ask the user.

This is a TypeScript project using React. Thanks for helping out!

Why it fails

  • Nothing is actionable. "Well tested" could mean one smoke test or full branch coverage — the agent will guess, and it will guess toward less work.
  • No commands. The agent has to explore package.json for install/lint/test anyway, which is exactly the guesswork the file exists to prevent.
  • No map. "Follow the existing style" sends it hunting through the codebase for precedent — and it will find three different precedents.
  • The only guardrail is a pleasantry. "Be careful with production data" doesn't say how, or what happens instead.
  • It's all adjectives. Not one line could fail a code review, because not one line makes a claim you can check.

A better AGENTS.md example

Same project, rewritten. This one is a Python service, but the shape applies to any stack:

markdown
# Project instructions

## Overview

FastAPI service (Python 3.12, uv). PostgreSQL 16 via SQLAlchemy 2.
Deployed as one container; no background workers.

## Commands

- Install: `uv sync`
- Dev server: `uv run uvicorn app.main:app --reload`
- Lint: `uv run ruff check .`
- Format: `uv run ruff format .`
- Typecheck: `uv run mypy app`
- Tests: `uv run pytest -x -q`

Run lint, typecheck, and tests before calling any task done. CI runs
exactly these commands — no others.

## Architecture

- `app/api/` — route handlers. Thin: parse, call service, respond.
- `app/services/` — business logic. No FastAPI imports here.
- `app/db/` — models and session setup. Queries live in repositories
  (`app/db/repositories/`), never in services.

## Code style

- Type-annotate every public function; mypy strict.
- Raise domain errors from services; handlers convert them to HTTP.
- Prefer dataclasses over dicts for anything crossing a boundary.

## Testing

- Pytest only; tests in `tests/`, named `test_<module>.py`.
- Use the `client` fixture — never the live database.
- New behavior needs a test; bug fixes need a regression test first.

## Never do

- Do not read or print `SECRET_KEY` or anything from `.env`.
- Do not edit `alembic/` migrations that are already merged.
- Do not add a dependency without asking first.

What changed: every rule names a location, a command, or a file. "No FastAPI imports here" is a line a reviewer can grep for. "Regression test first" is a rule the agent can violate visibly. And the whole file still fits on one screen.

The rules, section by section

Commands

List the six that matter: install, dev, lint, typecheck, test, build — copied from package.json or CI, character for character. Then add the one sentence that makes them stick: run these before you call the task done. Agents frequently verify only what the file explicitly requires, so this line is worth more than the commands themselves.

Architecture

Five to ten lines: where routes live, where logic lives, where shared code lives, and the one-way dependency between them. The goal is that "where do I put this?" never requires exploration. If your project has a real trap — a barrel file everyone's told not to import, a generated directory — name it here.

Testing

State three things: the framework and command, what to mock (and what's forbidden — no real network, no shared dev database), and your coverage expectation for new behavior. If tests are flaky in a known area, say so; the agent will otherwise debug the flakiness as if it were a product bug.

Code style

Only what tooling can't enforce. Ruff, ESLint, and Prettier already own formatting — restating them wastes lines. What they can't express: naming schemes, error-handling strategy, "dataclasses over dicts," where comments are welcome, what to do when a type feels awkward.

Never do

The highest-leverage section in the file. Write it as past-tense incidents: the migration someone force-applied, the env file that got committed, the dependency added without review. Each line should prevent a specific, expensive thing — if you can't name the thing, cut the line.

Length and structure

  • Target 50–150 lines. Claude Code recommends under 200 per file; Codex CLI truncates combined project instructions at 32 KiB (roughly 150–250 lines of prose) — silently, so you won't see the cut.
  • One rule per bullet, under a Markdown header. Header-scanned documents are how both humans and models re-find rules mid-session.
  • Past the 150-line mark, split. Session-wide rules stay in the root file; area-specific detail moves to a nested AGENTS.md that loads only when the agent works in that directory.
  • Order by frequency of use: commands first, then architecture, then style, then guardrails.

Common mistakes

Mistake Why it hurts Fix
Vague adjectives ("clean," "robust") Not checkable, so not followable Rewrite as a rule you could grep
No test or lint command Agent skips verification or invents one Copy the exact CI commands
Copying the README Browsing prose dilutes the rules Keep README for humans; file stays imperative
Stale commands Agent runs them, fails, improvises Update in the same PR as the script change
Everything at once Rules past ~200 lines get skimmed Split into nested, scoped files
Duplicating across CLAUDE.md and AGENTS.md Copies drift, then contradict One source; the other imports or points

If your project uses Claude Code alongside other tools, the last row matters most — CLAUDE.md vs AGENTS.md covers the pointer pattern in detail.

Check your file before you trust it

Two free, browser-only checks, both running locally so nothing leaves your machine:

  1. The checker — paste your file for a 0–100 score across structure, commands, clarity, safety, size, and portability, with a concrete fix for every issue it finds. It's the fastest way to catch a missing typecheck command or an over-long file.
  2. The generator — answer a short form (stack preset, commands, conventions) and get a correctly structured AGENTS.md you can then edit down. Also converts an existing CLAUDE.md with a change report.

Then run the one test that matters: start a fresh session and ask the agent what rules it's following and what commands it would run. If it hesitates, the file isn't doing its job — instructions are read at session start, so restart before you blame the model.

FAQ

How long should an AGENTS.md be?

Aim for 50–150 lines and stay under 200. Codex CLI truncates combined instructions at 32 KiB by default, and longer files are followed less reliably because every rule competes with the conversation for attention.

What sections should AGENTS.md have?

Overview, Commands, Architecture, Code style, Testing, and Never do. That order works because it mirrors the agent's questions in sequence: what is this, how do I run it, where does the change go, how is it written, how do I prove it works, what must I not touch.

Should I include the whole team's conventions?

Include what changes what the agent does. Style rules that linters enforce don't belong; error-handling strategy and file-placement rules do. If you can remove a line and the diff wouldn't change, remove it.

How is AGENTS.md different from a linter?

A linter catches violations after the fact in one dimension. The instruction file shapes choices linters can't see — where a file goes, what gets tested, when to stop and ask — before the code exists.

Over to you

Write the weak file first if you have to — then run it through the checker, fix what it flags, and compare the score. Most files jump 20+ points just from adding exact commands and a "never do" section. If you'd rather start from a structured template, generate one and edit from there.