Write and test an AGENTS.md file with Claude Code
Write a short AGENTS.md that coding agents like Claude Code and Codex actually use, test it against a baseline and cut what doesn't help, in about 45 minutes.

What you need
Claude Code reads AGENTS.md on its own from version 2.1.277. On older versions, add `@AGENTS.md` to a CLAUDE.md instead.
Step 1: Pick 3 to 5 test tasks
5 minThe same tasks, run before and after, are the fairest way to tell if the file helps.
- Choose 3 to 5 small, real tasks, such as a bug fix, a new endpoint or a missing test.
- Give each one a clear pass check: the tests pass, the build runs or the output matches.
- Include one task in the part of the code where agents slip up most.
- Commit your work, so every run starts from the same point.
You should see a short list of tasks, each with a pass check you can run.
Your repo has no tests to check against?
Use the build or a type check as the pass check. Or write down the result you expect before you start each task.
Step 2: Run a baseline with no file
12 minThe baseline shows what the agent gets wrong on its own, and that's what the file should fix.
- If the repo has an AGENTS.md or CLAUDE.md, rename it for now, for example to
AGENTS.md.off. - Start
claudein the repo and run/context all. Check that no project file shows under Memory files. - Paste a task, let it finish, then run
/usageand note the session cost. Write down every wrong guess and question. - Run
git stash -uto set the changes aside, and/clearbefore the next task.
Your test task[YOUR TEST TASK]. When you're done, run [YOUR TEST COMMAND] and tell me if it passes.
You should see a note for each task: pass or fail, the cost, and every wrong guess or question.
Claude still follows old project rules?
A CLAUDE.md in a parent folder, or a
CLAUDE.local.md, still loads. Rename those for the baseline too, then check/context allagain.- If the repo has an AGENTS.md or CLAUDE.md, rename it for now, for example to
Step 3: Write only what the agent can't see
10 minAgents follow what the file says, so every line steers them, for better or worse.
- Turn each wrong guess in your notes into one short line that can be checked.
- Add facts the code doesn't show: the exact test command, a rate limit, a banned library, where generated files live.
- Leave out repo overviews, general coding advice and anything already in the README.
- Save it as
AGENTS.mdin the repo root, and keep it well under 200 lines.
Your AGENTS.md# AGENTS.md ## Commands - Run all tests: `[TEST COMMAND]` - Run one test file: `[SINGLE TEST COMMAND]` ## Rules the code doesn't show - [A LIMIT, e.g. The payments API allows 10 requests a second. Batch calls.] - [A CONVENTION, e.g. New API handlers go in src/api/handlers/.] - Never edit files in [GENERATED FOLDER]. [BUILD COMMAND] rebuilds them. ## Before you finish - Run [LINT COMMAND] and fix every error.
You should see a short AGENTS.md where every line traces back to a mistake from your baseline.
The file keeps growing?
Move rules for one folder into an AGENTS.md inside that folder. Claude Code loads it only when it works on files there.
Never put API keys or passwords in AGENTS.md. It's committed and shared with your code.
Step 4: Check that Claude Code loads it
2 minA file the agent never reads can't help, and a CLAUDE.md quietly takes its place.
- Start a new
claudesession in the repo. - Look for a line such as
no CLAUDE.md found; AGENTS.md loaded. - Run
/context alland findAGENTS.mdunder Memory files.
You should see
AGENTS.mdlisted under Memory files.Claude reads your CLAUDE.md and skips AGENTS.md?
By default it reads AGENTS.md only when there's no CLAUDE.md. Add the line
@AGENTS.mdto your CLAUDE.md, or set Project instructions toclaude-md-and-agents-mdin/config.Codex reads the same file from the repo root. Its default size limit for all instruction files together is 32 KiB.
- Start a new
Step 5: Run the same tasks again
12 minOnly the same tasks, from the same starting point, show what the file changed.
- Run each test task again in a fresh session, with the same wording as before.
- Note pass or fail, the cost from
/usage, and any wrong guesses that are left. - Put the two sets of notes side by side.
You should see a before and after table with pass or fail and the cost for each task.
Results change from run to run?
Agents vary, even with the same prompt. Run each task twice and trust only changes that show up both times.
Step 6: Cut the lines that didn't help
4 minEvery line is read on every task, so a line that changes nothing only adds cost.
- For each line, check if it fixed a baseline mistake. If it didn't, delete it.
- If a rule must hold every time, back it with a test or a hook. The file is advice, not a lock.
- Run
/doctor prompt-auditto find lines that are out of date or contradict each other.
You should see a shorter file where every line matches a fixed mistake, plus an audit report.
Claude Code doesn't recognize /doctor prompt-audit?
It needs version 2.1.283 or later. Run
claude updatein your terminal, then try again.A 2026 study from ETH Zurich found that context files written by an AI didn't raise success rates and added about 20% to cost.
You get
A short AGENTS.md that Claude Code and Codex load, with every line tested against a baseline
Try next
Did this guide work for you?
Thanks. It helps us fix the steps.


