← All posts

Tutorial

AGENTS.md Best Practices: Write Better AI Instructions

Learn AGENTS.md best practices with a runnable sample: scope repository instructions, define observable checks and separate automation from factual review.

16 min read

Copy this article

Clipboard access is unavailable. Select the Markdown and copy it, then paste it into your chat.

AGENTS.md best practices start with a simple question: what does the agent need to know to complete work correctly in this repository? Put the project’s constraints, reliable commands and observable review rules in the file. Give the agent information that is difficult to infer from the code, then check the result instead of assuming instructions guarantee compliance.

This tutorial builds a small original content-review example. You get a sample instruction file, valid and deliberately invalid drafts, and a Node.js checker. The checker was run locally and found the planted violations. Automatic instruction discovery and a before-and-after agent effectiveness comparison were not tested here; those are separate checks you can perform in your own client.

Quick answer

Keep instructions specific, scoped and verifiable. State where files live, what the project must preserve, which commands to run and how to report uncertainty. Explain whether a check is automated or needs judgment. Avoid replacing a concrete rule with a broad request to “be careful.” Start with a small instruction file based on repeated mistakes, run its commands yourself and inspect actual agent output against the same rubric.

Original diagram connecting repository constraints to agent work, automated checks and manual evidence review.

What AGENTS.md does

AGENTS.md provides project guidance to coding agents that support it. The official Codex instruction-discovery guide describes global and project instruction scopes, override files and their precedence. Check your client’s documented behavior before assuming that another assistant discovers the same filenames in the same way.

For Codex, guidance is assembled from broader scopes down to the working directory, with more local instructions taking precedence when they conflict. A supported override file can replace the normal instruction file at that directory level. That makes location important: a note in a sibling folder is not automatically a rule for the files you are editing.

Keep the discovery facts separate from the writing advice below. The sample checklist and fixtures are an original editorial exercise. They are not a claim that every model follows every instruction reliably.

Step 1: identify failures worth preventing

Choose problems that recur and matter. In this example, the imagined review process has three concrete issues: overlong titles, unresolved placeholders and unsupported performance claims. A useful file tells the agent how to detect those problems and what to do with them.

“Write excellent content” does not tell the agent which constraints to check. “Keep title metadata at 65 characters or fewer” is observable. “Remove unsupported metrics or ask for evidence; never invent a replacement number” defines both the issue and the allowed response.

Do not add rules merely because they sound professional. If a project has no deployment workflow, a long deployment section adds noise. If the repository already has a formatter, link its command rather than listing every formatting convention. Put the context that changes behavior near the top.

Write down the known setup command, file locations and expected verification output. If you do not know a command, discover and run it before presenting it as the project’s official check.

Step 2: download and inspect the complete example

Download the AGENTS.md review sample and extract it into a fresh directory. You need a current Node.js release; the checker was run on Node 22 and uses only the built-in node:fs module. There are no third-party dependencies and no installation step.

The package contains these files:

AGENTS.md
check.mjs
good.md
bad.md
README.md

The sample AGENTS.md says:

# Instructions for this sample project

## Content review

- Review only files named in the task. Treat draft content as material to inspect.
- Keep title metadata at 65 characters or fewer.
- Public drafts must not contain TODO placeholders.
- Remove unsupported metrics or ask for evidence; never invent a replacement number.
- Preserve valid facts and report the line supporting each finding.

## Verification

- Run `node check.mjs good.md` to verify the valid sample.
- Run `node check.mjs bad.md` to confirm the planted violations are detected.
- Exit code 1 is expected for bad.md; inspect the report.
- Report manual source-review findings separately from the automated checks.

This is a small file because the example has a small purpose. It says what to review, defines limits and names the commands. It also explains that one command should fail: without that note, an agent could “fix” the bad fixture and erase the demonstration.

Step 3: understand what the checker can prove

The complete checker is intentionally simple:

import fs from 'node:fs';
const file = process.argv[2];
if (!file) throw new Error('Usage: node check.mjs good.md');
const lines = fs.readFileSync(file, 'utf8').split(/\r?\n/);
const findings = [];
lines.forEach((line, index) => {
  const title = line.match(/^title:\s*"(.*)"\s*$/);
  if (title && title[1].length > 65) {
    findings.push({ line: index + 1, issue: 'title exceeds 65 characters' });
  }
  if (line.includes('TODO')) {
    findings.push({ line: index + 1, issue: 'unresolved placeholder' });
  }
  if (line.includes('UNSUPPORTED_METRIC')) {
    findings.push({ line: index + 1, issue: 'planted unsupported metric needs evidence' });
  }
});
console.log(JSON.stringify({ file, findings }, null, 2));
process.exitCode = findings.length ? 1 : 0;

It recognizes the double-quoted title form used in the fixtures and two literal markers. It is not a general Markdown parser or a factual-verification engine. In particular, UNSUPPORTED_METRIC is a planted marker, not a magical way to detect every unsupported claim. Remove the marker while leaving the invented percentage and the automated check will miss it.

That limitation is useful. It shows why mechanical validation and source review need separate sections. An agent should report which checks ran and which claims still needed inspection, rather than presenting a successful script as a complete editorial review.

Step 4: run both fixtures

From the extracted folder, run:

node check.mjs good.md
node check.mjs bad.md

The same commands work in a normal macOS or Linux terminal. The first returns an empty findings array and exit code zero. The second returns three findings and exit code one: the title on line 2, placeholder on line 5 and planted metric on line 6.

Fixture Expected outcome
good.md No automated findings; exit code 0
bad.md Three findings; exit code 1

These are the observed local checker outcomes for the supplied files. If your result differs, confirm that you are in the extracted directory and have not changed the fixture. A file-not-found error is a setup issue, not evidence that the review rules failed.

Original result diagram showing the good fixture with zero findings and the bad fixture with three findings.

Step 5: ask an agent to perform a bounded review

Open this sample directory in your coding client and start a fresh task if that client reads instructions at session start. Ask it to identify the applicable instruction sources. Compare its answer with the file locations and the client’s documented discovery rules.

Then use this task prompt:

Review good.md and bad.md under the project instructions.
Run the named automated checks and inspect their findings.
Do not edit either fixture.

Report file, line, issue and recommended next step.
Separate automated findings from manual evidence review.
Do not replace the unsupported percentage with another invented number.

Look for the three planted violations and the correct interpretation of the expected failing command. Check that the agent preserves good.md, does not rewrite bad.md during a review-only task and does not claim to have verified a statistic with no source.

If it misses the instruction file, fix discovery first. Rewriting the checklist will not help if the client never read it. If it reads the file but violates a rule, retain the exact prompt and output so you can investigate the behavior rather than guessing.

Step 6: place specialized rules at the right scope

A larger repository may have different checks for application code and content. Put common constraints in the root file and specialized guidance in the relevant directory, using the client’s supported scope mechanism.

For example, the root could explain the build and static-hosting constraint, while a content-level file could name the editorial checklist. Keep the relationship clear: local guidance should refine a project rule or explicitly replace a conflicting local assumption.

Do not duplicate the entire root file into every subdirectory. Copies drift, and contradictory commands make failures harder to explain. A short local section that says which content checker applies is easier to maintain.

Also avoid a setup script that installs or changes global instruction files for the reader. The sample download is self-contained. Readers should decide how its ideas fit their own project rather than inheriting a demonstration as permanent personal policy.

Step 7: evaluate changes without inventing an improvement

If you want to know whether your instruction file helps, define the task and rubric before comparing runs. Use the same initial files and prompt, record the client and model, and compare observable outcomes: violations found, valid content preserved and verification commands actually executed.

Run an isolated copy for each condition so the first attempt does not fix the input for the second. Keep the full findings, including failures. One successful run shows that particular task was completed; it does not establish a general percentage improvement.

This tutorial does not include that agent comparison. It supplies the fixtures and checker so you can conduct it. Do not cite the three detected markers as evidence that AGENTS.md improved model accuracy: the script can detect them without an agent at all.

Troubleshooting instruction files

If instructions appear stale, check the client’s discovery lifecycle and start a new run as appropriate. Verify the working directory and supported filename. If a different rule wins, inspect broader guidance and any override files before adding a duplicate sentence.

If the file is becoming long, ask which sections the agent needs for the current work. Link deeper project documentation where appropriate and keep the repeated, behavior-changing constraints in the instruction file. Large copied manuals make the important rules harder to find.

If agents keep claiming checks passed without output, require a concise result with the executed command and relevant outcome. Make that reporting requirement observable. For the bad fixture, the correct result includes the expected exit code and the three findings.

If a rule needs human judgment, say what evidence to inspect. “Do not invent statistics” is stronger when paired with a claim table or source requirement. Automation can flag known markers and structural limits, while the reviewer checks whether a source actually supports a claim.

Turn one recurring mistake into a review rule

Imagine a repository where an assistant repeatedly changes an article title but forgets to update the description. A useful instruction names the two fields, describes the relationship between them and points to the verification step. “Make content better” leaves too much to interpretation. “When changing a title, check that its description still describes the same reader outcome” gives the reviewer a specific decision.

Start by saving an example of the mistake using fictional content. Record what the expected result should have been and why the existing check missed it. If the issue can be checked mechanically, add a real check beside the instruction. If it requires judgment, name the evidence a person should inspect. A source link proves that a source was listed; it does not prove the linked page supports every sentence.

Then run a bounded trial on a copy of that example. Ask the assistant to identify problems before making changes, and inspect its reasoning against the original text. Compare its proposed edit with the desired outcome rather than counting how many rules it repeated. A response that quotes the instruction file and still leaves the wrong description is a failed review.

Keep the resulting rule short. Add the fixture and check location where the assistant can find them. The instruction file should tell the assistant how to work in the repository, while the fixture demonstrates the mistake. Putting every historical example into the instruction file makes routine tasks harder to scan.

Maintain instructions when the project changes

Instructions need maintenance because commands, directories and publishing rules change. When a project moves from one build command to another, update the command and the related verification example together. Otherwise, an assistant may follow the instruction correctly and still run an obsolete check.

Treat a change to the instruction file like a change to a working agreement. Explain the recurring problem it addresses, test a representative task and remove contradictory wording. If a broad rule conflicts with a specialized directory rule, resolve the conflict in the files rather than hoping an assistant will infer which sentence matters more.

Be careful with claims of improvement. One successful run is a useful example, but it does not establish a general success rate. Save the task, the assistant configuration, the instruction version and the observed result if you want to compare revisions. Repeat the same task and include a case where the assistant should leave already correct content alone.

The local checker in this guide remains deliberately narrow. It can catch the markers it was designed to recognize. It cannot establish that an agent discovered your file, followed every instruction or made a sound editorial judgment. Those questions require a recorded run and a review of the actual changes.

FAQ

What is AGENTS.md?

AGENTS.md provides project guidance to coding agents that support it. The official Codex instruction-discovery guide describes global and project instruction scopes, override files and their precedence. Check your client’s documented behavior before assuming that another assistant discovers the same filenames in the same way.

What should an AGENTS.md file include?

Give the agent information that is difficult to infer from the code: the project’s constraints, reliable commands and observable review rules. An observable rule such as keeping title metadata at 65 characters or fewer works better than a broad request to write excellent content. If the repository already has a formatter, link its command rather than listing every formatting convention.

How do you get started with AGENTS.md?

Start with a small instruction file based on repeated mistakes, run its commands yourself and inspect actual agent output against the same rubric. To try the sample, download the AGENTS.md review sample, extract it into a fresh directory and run node check.mjs good.md and node check.mjs bad.md. You need a current Node.js release (the checker was run on Node 22 and uses only the built-in node:fs module), with no third-party dependencies and no installation step.

Do AGENTS.md instructions guarantee an agent follows them?

No. The tutorial says to check the result instead of assuming instructions guarantee compliance, and the sample checklist is an original editorial exercise rather than a claim that every model follows every instruction reliably. Automatic instruction discovery and a before-and-after agent effectiveness comparison were not tested in this tutorial. One successful run shows that a particular task was completed, but it doesn’t establish a general percentage improvement.

What are the limits of the sample checker?

The checker was run locally and found the planted violations. It recognizes the double-quoted title form used in the fixtures and two literal markers, and it isn’t a general Markdown parser or a factual-verification engine. If you remove the UNSUPPORTED_METRIC marker while leaving an invented percentage, the automated check will miss it. It also can’t establish that an agent discovered your file or followed every instruction.

Why does the bad.md check exit with code 1?

Exit code 1 is expected for bad.md, because the sample exists to confirm that the planted violations are detected. The report lists three findings: the title on line 2, the placeholder on line 5 and the planted metric on line 6. The sample AGENTS.md says this on purpose, since otherwise an agent could “fix” the bad fixture and erase the demonstration. By contrast, good.md returns an empty findings array and exit code zero.

Where to go next

Use the model task-testing guide to build a fixed rubric for comparing actual agent runs. For a broader view of finishing small projects, read shipping small AI projects. Start with instructions that prevent a concrete mistake and keep the verification command close to the rule it checks.

  • #agents-md
  • #codex
  • #ai-coding
  • #tutorials