Skip to content

LearnTutorials

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.

By Recaply EditorsIntermediate45 min6 stepsUpdated

A hand with scissors trims a long paper scroll, with cut strips falling below it

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.

  1. Step 1: Pick 3 to 5 test tasks

    5 min

    The same tasks, run before and after, are the fairest way to tell if the file helps.

    1. Choose 3 to 5 small, real tasks, such as a bug fix, a new endpoint or a missing test.
    2. Give each one a clear pass check: the tests pass, the build runs or the output matches.
    3. Include one task in the part of the code where agents slip up most.
    4. 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.

  2. Step 2: Run a baseline with no file

    12 min

    The baseline shows what the agent gets wrong on its own, and that's what the file should fix.

    1. If the repo has an AGENTS.md or CLAUDE.md, rename it for now, for example to AGENTS.md.off.
    2. Start claude in the repo and run /context all. Check that no project file shows under Memory files.
    3. Paste a task, let it finish, then run /usage and note the session cost. Write down every wrong guess and question.
    4. Run git stash -u to set the changes aside, and /clear before 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 all again.

  3. Step 3: Write only what the agent can't see

    10 min

    Agents follow what the file says, so every line steers them, for better or worse.

    1. Turn each wrong guess in your notes into one short line that can be checked.
    2. Add facts the code doesn't show: the exact test command, a rate limit, a banned library, where generated files live.
    3. Leave out repo overviews, general coding advice and anything already in the README.
    4. Save it as AGENTS.md in 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.

  4. Step 4: Check that Claude Code loads it

    2 min

    A file the agent never reads can't help, and a CLAUDE.md quietly takes its place.

    1. Start a new claude session in the repo.
    2. Look for a line such as no CLAUDE.md found; AGENTS.md loaded.
    3. Run /context all and find AGENTS.md under Memory files.

    You should see AGENTS.md listed 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.md to your CLAUDE.md, or set Project instructions to claude-md-and-agents-md in /config.

    Codex reads the same file from the repo root. Its default size limit for all instruction files together is 32 KiB.

  5. Step 5: Run the same tasks again

    12 min

    Only the same tasks, from the same starting point, show what the file changed.

    1. Run each test task again in a fresh session, with the same wording as before.
    2. Note pass or fail, the cost from /usage, and any wrong guesses that are left.
    3. 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.

  6. Step 6: Cut the lines that didn't help

    4 min

    Every line is read on every task, so a line that changes nothing only adds cost.

    1. For each line, check if it fixed a baseline mistake. If it didn't, delete it.
    2. If a rule must hold every time, back it with a test or a hook. The file is advice, not a lock.
    3. Run /doctor prompt-audit to 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 update in 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

Did this guide work for you?

Sources