Sub topic 12
Checking that the agent follows your instructions
This chapter assumes you can write an AGENTS.md file, know how nested files and precedence work, and have used a coding agent on a real repository. You also need to be comfortable running build and test commands from a terminal.
Writing instructions does not prove they work. An agent can ignore a rule, misread it, or never load the file at all. This chapter shows how to test an AGENTS.md the way you would test code: pick a behavior, run a task, observe the result, and fix the cause.
Why instructions fail silently
An agent that ignores your file does not raise an error. It produces plausible output that does not follow your rules. You find out in code review, or you never find out. Four causes account for most failures:
- The file was never loaded. The tool did not look in that directory, the filename differs from what the tool expects, or the agent was started from another working directory.
- The file was loaded but is ambiguous. A rule such as "test properly" gives the agent nothing to act on.
- A higher-priority instruction overrode it. An explicit request in the user's prompt, or a nearer AGENTS.md, won over the rule you were testing.
- The rule is buried. In a long file, important rules compete with filler, and the agent may weigh them less.
Verification separates these causes, so you fix the right one instead of rewording the file at random.
Step 1: Confirm the file is loaded
Check loading before you check behavior. If the file is not loaded, no amount of rewriting helps.
A reliable method is a probe question. Ask the agent something that only the file can answer:
What command should I run to execute a single test file in this repository?
Answer from the repository instructions you have loaded, and name the file you took it from.With the example file from earlier chapters, a correct answer names npm test -- path/to/file.test.ts and cites AGENTS.md. If the agent gives a generic answer such as "run your test runner", the file is probably not in context.
Many tools also show which instruction files they loaded. Look for a startup message, a status command, or a context listing in your tool's documentation. Prefer that over asking the agent, because the agent's description of its own context can be wrong.
If the file is not loaded, check these in order:
- The filename is exactly
AGENTS.md, with that capitalization. Case-sensitive file systems treatagents.mdas a different file. - The file is in the repository root, or in a directory that is an ancestor of where the agent is working.
- You started the agent from inside the repository.
- Your tool reads AGENTS.md at all. If it reads only a tool-specific file such as CLAUDE.md, use the symlink or import approach from the compatibility chapter.
Step 2: Write checkable behaviors
Turn each important rule into a test case: a task prompt plus an observable outcome. The outcome must be something you can check without judgment calls.
| Rule in AGENTS.md | Test task | Observable outcome |
|---|---|---|
| Run `npm run lint && npm run typecheck` before finishing | "Add a `getTotal` function to `src/services/invoice.ts`." | The agent's transcript shows both commands ran, and they passed |
| Use named exports, no default exports | "Add a new service `refund.ts`." | `grep -n "export default" src/services/refund.ts` finds nothing |
| Never edit an existing migration | "Add a `status` column to invoices." | `git diff --name-only` shows only new files under `migrations/` |
| Add tests for every behavior change | "Make invoices round to two decimals." | The diff includes a changed or new `*.test.ts` file |
Notice that each outcome is either a command you can run or a line you can find in the transcript. A rule whose outcome you can only judge by feel, such as "code is clean", cannot be tested, which is a sign the rule needs rewriting (see the chapter on writing concrete instructions).
Include a conflict case
Most real failures happen where rules meet. Add tests for the boundaries:
- Nested scope. In a monorepo, ask for a change under
packages/web/and check that the agent used the web package's test command, not the root one. - Prompt override. Tell the agent in the prompt to skip tests, and confirm it follows the prompt. This is expected behavior, and you want to know the agent does it, because it shows where your file's authority ends.
- Rule you added recently. New rules are the ones most likely to be wrong or unloaded.
Step 3: Run the tasks and collect evidence
Run each task in a clean state so earlier work does not influence the result.
- Create a throwaway branch.
- Start a fresh agent session. A long session carries earlier context that may mask or contradict the file.
- Give the test prompt, and nothing else about the rule.
- Save the transcript and the diff.
- Discard the branch.
The script below automates the checking half. It assumes you have already run the agent on a branch and want to check the result against the rules in the table above.
#!/usr/bin/env bash
# check-agent-output.sh: verify an agent's changes against AGENTS.md rules.
# Run from the repository root, on the branch where the agent worked.
set -u
base="${1:-main}"
fail=0
check() {
# check "description" command...
local desc="$1"; shift
if "$@" >/dev/null 2>&1; then
echo "PASS: $desc"
else
echo "FAIL: $desc"
fail=1
fi
}
changed_files() { git diff --name-only "$base"...HEAD; }
no_default_exports() {
! changed_files | grep -E '\.(ts|tsx)$' \
| xargs -r grep -l 'export default' 2>/dev/null | grep -q .
}
no_migration_edits() {
# Added files are fine; modified or deleted files are not.
! git diff --name-status "$base"...HEAD -- migrations/ | grep -Eq '^(M|D)'
}
touches_tests() {
changed_files | grep -Eq '\.test\.ts$'
}
check "no default exports in changed files" no_default_exports
check "no existing migration modified" no_migration_edits
check "tests added or updated" touches_tests
check "lint and typecheck pass" npm run lint --silent
check "tests pass" npm test --silent
exit "$fail"Run it with bash check-agent-output.sh main. The script checks outcomes in the repository, so it works regardless of which tool produced the change. It cannot check process, such as whether the agent ran lint itself before finishing. For that, read the transcript.
Step 4: Diagnose a failure
When a test fails, decide which cause applies before editing anything. The flow below separates loading problems from wording problems.
Apply one change at a time and rerun the same test. If you change three things at once, you will not know which one helped.
Common fixes
- Rule ignored in a long file. Remove low-value lines. Every line competes for attention, so a shorter file raises the weight of the rest.
- Command run, but with the wrong scope. Name the scope in the rule. "Run tests" becomes "Run tests for this package only:
npm test --workspace packages/web". - Agent runs the command but ignores failures. Add the expectation: "Fix any failures you introduced."
- Agent follows the rule only sometimes. Check for contradictions between the root file and a nested file. Two files that disagree produce inconsistent behavior.
Step 5: Keep the checks
A test you run once only tells you about today. Keep your cases in the repository, for example in a docs/agent-checks.md file, with the prompt and the expected outcome for each. Rerun them when:
- you change AGENTS.md,
- a build or test command changes,
- you switch to a different agent tool or a new model version,
- a pull request shows the agent breaking a rule that used to work.
Model and tool updates matter because they change how instructions are weighted. A file that worked last quarter can regress without any edit on your side.
Instructions versus enforcement
AGENTS.md is guidance. It is not a security boundary or a build gate. Anything that must hold every time belongs in automation that fails loudly:
- A CI job that fails when an existing migration file is modified.
- A lint rule that bans default exports.
- Branch protection that requires passing tests.
Keep the matching line in AGENTS.md anyway. The instruction helps the agent get it right the first time, and the automation catches the cases where it does not. The two layers do different jobs.
Summary of the workflow
- Confirm the file loads with a probe question and the tool's own context listing.
- Convert key rules into tasks with observable outcomes, including nested-scope and override cases.
- Run each task in a fresh session on a throwaway branch.
- Check outcomes with commands, and read the transcript for process.
- Diagnose by cause: loading, wording, conflict, or file length. Change one thing at a time.
- Store the checks and rerun them after changes to the file, the tooling, or the model.
- Back critical rules with CI or hooks.
Exercise
Your root AGENTS.md says "Use named exports; do not add default exports." You ask the agent to add src/services/refund.ts, and the result contains export default. The agent answered a probe question about the test command correctly from the file. What is your next diagnostic step, and why?
Show answer
The probe answer shows the file is loaded, so skip the loading fixes. Next check whether the rule is concrete and whether something conflicts with it. The rule is concrete, so look for a conflict: a nearer AGENTS.md in src/services/ or a package that says otherwise, or wording in your prompt that implied a default export. If there is no conflict, rerun the test a few times in fresh sessions to see whether the failure is consistent. If it is intermittent, shorten the file and also enforce the rule with a lint rule such as one banning default exports.
Exercise
Write one test case for the rule "Never edit an existing migration". Give the task prompt and an observable outcome that a command can check.
Show answer
Prompt: "Add a status column to the invoices table." Outcome: run git diff --name-status main...HEAD -- migrations/. Every line must start with A (added). Any line starting with M or D means an existing migration was modified or deleted, and the test fails.
Exercise
Why should you start a fresh session for each verification task instead of running all of them in one long session?
Show answer
A long session carries earlier prompts, corrections, and outputs. These can make the agent follow a rule it would otherwise miss, or contradict the file. A fresh session tests what the file alone produces, which is what a new contributor's session will see.