Contents Lesson 2 of 9

11 min read · foundations

Write the rules your agent reads every session

By the end of this lesson you will have a ## My rules section at the end of the starter's AGENTS.md holding five rules you wrote, each one something you could catch the agent breaking, plus a sixth line that sends the agent to PLAN.md — and a fresh session that recites all six back to you when asked.

The idea

An agent begins every session knowing nothing about your project except what is written down for it, in a rules file loaded before your first prompt.

One file, two names

AGENTS.md is the name several agents agree on: Codex CLI reads it, and so does Claude Code when a project has no CLAUDE.md. Claude Code's own name is CLAUDE.md, and when both exist it reads CLAUDE.md only. The starter settles it with a CLAUDE.md that is one line long:

@AGENTS.md

A line starting with @ is an import: Claude Code loads the named file in its place. Codex does not load CLAUDE.md at all; it reads AGENTS.md directly. One file of rules, every agent reads it. Keep your rules in AGENTS.md and leave CLAUDE.md as the pointer.

Rules are advice; permissions are law

The rules file is context. The agent reads it and usually follows it, and nothing forces it to. A rule that must hold no matter what — the key file is never read — also needs something the agent enforces whatever the model decides: in Claude Code a permission rule or a hook. Lesson 1 set up that half, and showed that Codex has no default counterpart for the key file, which makes the rule here the main line of defence there. This lesson is the other half, and it covers everything a permission rule cannot say: how you want the work done.

What a good rule looks like

A rule you cannot check is a wish. Compare:

A wish A rule
Test your changes Run npm test before every commit and show me its last line
Be careful with the API Before any request to EODHD, say what it costs in calls and wait for my yes
Keep it simple No new dependency without asking; name the package and the reason

Every rule on the right is one you can catch being broken. The starter's AGENTS.md already carries seven — data only through the proxy, never touch env files, never a NEXT_PUBLIC_ key, work on fixtures, state the cost before a fetch, done means green, small commits. Read them first: each was written after somebody hit the problem. The block above them, headed "This is NOT the Next.js you know", is addressed to the agent: it sends it to the Next.js guides inside node_modules before it writes code, because this version of Next.js is newer than much of what the agent learned from. Leave it as it is.

Your five go below them, and they are about you: how much you can review at once, when it must stop and ask, what a commit message must say, which files it may not touch, what you want explained before any code. Write them yourself; an agent asked to write your rules writes generic ones.

One thing to leave room for. Lesson 5 lets the agent run four checks without asking: npm test, npm run typecheck, npm run lint and npm run check:leak, the last of which builds the app. If one of your rules makes it ask before running commands or builds, name those four as the exception, or you will be answering the same question after every change.

Keep the file short — Claude Code's guidance is under 200 lines, because a longer file costs context every session and is followed less, and Codex stops reading instruction files past 32 KiB — and never let two rules contradict each other.

Why PLAN.md must be referenced from it

Build Your First Tool opens by writing PLAN.md: what the tool is for, what it is not, what done means. A plan is the standard you hold two hundred generated lines against.

But the agent does not read a file because it exists. It reads the rules file because that is loaded for it, and then whatever the rules tell it to read. A plan nobody points to is a plan the agent never sees, and every session it re-invents your scope. So one line in the rules file does the pointing:

Read PLAN.md before proposing any change. Anything it does not cover is out of scope: ask before doing it.

A plain sentence works in every agent. Writing @PLAN.md instead would import the whole plan into every Claude Code session — context spent on sessions that do not need it — and other agents would see only the literal text.

Try it now

The prompt

In the starter folder, write your five rules in a scratch note first, in your words. Then start the agent and paste this, with your rules in place of the brackets:

Here are five rules I want to add to AGENTS.md under a new heading
"My rules": [your five rules]. For each one, tell me how you would show
me that you followed it. Rewrite any rule you could not show me, and say
why. Do not edit any file yet.

Take the rewrites you agree with, then:

Append my rules under "## My rules" at the end of AGENTS.md, and add this
as the last line of that section: "Read PLAN.md before proposing any
change. Anything it does not cover is out of scope: ask before doing it."
Create `PLAN.md` with five headings: Purpose, Data, Not building,
Done means, Riskiest unknown. Under Purpose put one line: "Practise
working with an agent on this watchlist: ordering it by size of move,
tests, reviews." Leave the other four empty. Then commit both files
with the message "AGENTS.md: my five rules, and the pointer to PLAN.md".

What the agent should do

  • Answer the first prompt with no edits: one line per rule on how it would show you, and a rewrite only where a rule cannot be checked.
  • Append to AGENTS.md without reformatting or reordering the house rules above yours.
  • Create PLAN.md with five headings, your one Purpose line, and nothing invented under the other four — filling those is Build Your First Tool's first lesson. From now on the agent may stop in later lessons and say that PLAN.md does not cover a request: that is your rule working. Answer that the work is in scope (the Purpose line covers it) and let it go on.
  • Ask before the commit, and commit exactly two files.

Check it yourself

git show --stat HEAD
git diff HEAD~1 -- AGENTS.md
  1. The first command lists AGENTS.md and PLAN.md and nothing else.
  2. The second shows only added lines, all at the end of AGENTS.md.
  3. Start a fresh conversation — /clear in Claude Code; in Codex, quit and run codex again, since it reads AGENTS.md when a session starts — then ask: "Which rules do you follow in this repository, and where is the plan?" Your five rules come back, and PLAN.md is named.
  4. Ask for a feature the empty plan does not mention. The agent stops and asks, because your last line says so.

Break it on purpose

Add a line that contradicts a house rule — "always test against live data", against "work on fixtures" — then start a fresh conversation as in step 3 and ask for a small change that touches the data. See which rule it follows, and whether it mentions the conflict. Delete the line. A contradiction never announces itself.

Compare with ours

Our five rules and the plan pointer
## My rules

- Keep a change under 100 changed lines. If it needs more, split it and tell me how.
- When two approaches are both reasonable, show me both in three lines each and wait.
- Commit messages say why, not what: a short first line, present tense, never "update".
- Ask before touching anything in .github, .githooks or scripts.
- Before any code, say in one sentence what the change does and which files it touches.
- Read PLAN.md before proposing any change. Anything it does not cover is out of scope: ask before doing it.

Claude Code adds a Co-Authored-By trailer to commit messages it writes; the rule is about the first line, which is why it says so. The commit is on the branch ref/work-with-an-agent/2 of the starter repository.

Checkpoint

Nothing to commit yourself: the agent made the commit when you said yes. Check it:

git log -1 --format=%s   # AGENTS.md: my five rules, and the pointer to PLAN.md
git status               # nothing to commit

If the agent used another message, that is fine as long as it says why. Then answer this lesson's questions.