Skip to content
◀ Blog

How to write a CLAUDE.md file the agent follows

Keep CLAUDE.md under 200 lines, list the exact commands, import AGENTS.md for other tools, and move rules that must always hold into hooks and settings.

Riley Hilliard9 min read

agentsclaude-code

A CLAUDE.md file works when it is short, lists the exact commands that build and test the project, and leaves enforcement to hooks and tool settings. Most advice on how to write a CLAUDE.md file stops at “add your conventions,” and that is how these files grow to hundreds of lines that the agent half reads. Claude Code’s own memory docs say the file is treated “as context, not enforced configuration,” and that longer files “reduce adherence.” So anything that must happen every time belongs somewhere the agent can’t skip.

The examples below are steamfield.io’s real files. The repo has a 10-line CLAUDE.md and a 160-line AGENTS.md, and the rules that can’t be broken live in .claude/settings.json and a handful of shell hooks.

What should you write in a CLAUDE.md file?

Write down the facts an agent needs in every session and can’t discover quickly by reading code. The Claude Code docs put it as “what you’d otherwise re-explain”: build commands, project layout, conventions, and “always do X” rules. Add an entry when the agent makes the same mistake twice or when you type the same correction you typed last session.

Our AGENTS.md has these sections in this order, with others (Agent harness, Debugging, CI, Deploy) between them:

Section What it holds
Overview A few sentences: what the project is and its main technologies
Commands A table of every supported command, including how to run one test file
Architecture map A short tree of directories that exist, with one line each
Conventions Patterns an agent wouldn’t guess, such as where product facts live
Guardrails What the hooks block, and that a block means ask the user
Testing Test layers, runners, what is real and what is faked
Build workarounds Pins and hacks, each with when it can be removed
Deferred What is out of scope on purpose
Pending user actions Steps only a person can do, and how to tell they’re done
Deeper context Links to docs the agent reads only when a task needs them

The last four sections do the most work on a project that’s more than a week old. Without Deferred, an agent that notices there are no tag pages on the blog has no way to tell a choice from an oversight, so it may start building them. Without Pending user actions, it can flag the same placeholder contact address as a problem in every session. Our entry for that one reads “[email protected] in src/config.ts is a placeholder. Done when mail to it arrives.” The “done when” clause tells the agent, and you, how to close the item.

Leave out anything a linter or formatter already enforces, long procedures (put those in a skill or a doc), and guidance that only matters in one directory. Claude Code loads path-scoped rules from .claude/rules/ only when the agent touches matching files, which keeps them out of every other session.

Should you use AGENTS.md or CLAUDE.md?

Use AGENTS.md for the content and a CLAUDE.md that imports it. AGENTS.md is the file other coding agents (Codex, Cursor, OpenCode) look for, so project knowledge written there reaches all of them. Claude Code v2.1.277 and later read AGENTS.md directly, but only when there is no CLAUDE.md in the working directory or above it. Once you have a CLAUDE.md, Claude reads that and ignores AGENTS.md unless the CLAUDE.md imports it.

Our CLAUDE.md starts with the import on its own line:

@AGENTS.md

## Claude Code

`AGENTS.md` (imported above) is the project's instruction file for every coding
agent. Put project knowledge there, not here. This section is only for things
specific to Claude Code.

Below that are four bullets about Claude-only features: where hook behavior is documented, what .claude/rules/ is for, and that personal settings go in the gitignored settings.local.json. The import keeps working in sessions where Claude Code can’t read AGENTS.md on its own, and the docs say it never causes the file to load twice.

Don’t write “read AGENTS.md” as a sentence instead of the import. Claude then sees the file only if it decides to open it.

How long should CLAUDE.md be?

Keep it under about 200 lines. That number comes from Claude Code’s docs, which recommend under 200 lines per file and note that imports “don’t reduce its context cost,” since an imported file loads alongside the one that imports it. Splitting a 500-line file into five imported files changes nothing for the agent.

If your CLAUDE.md is too long, cut in this order:

  1. Anything only one area needs moves to a doc, linked from a Deeper context table with a one-line “read this when” column, or to a path-scoped rule.
  2. Anything a tool enforces comes out. A formatter config beats a paragraph about quote style.
  3. Restated framework docs come out. The agent knows how Astro routing works; it needs to know your exceptions.
  4. Contradictions get resolved. The docs warn that when two instructions conflict, Claude “may pick one arbitrarily.”

Our AGENTS.md states its own budget on line 3: “Keep this file under about 200 lines: it is loaded at the start of every session.” Putting the limit in the file means the next person, or agent, who edits it sees the rule.

Which commands should the file list?

List every command a contributor runs, copied from the Makefile or package scripts and run once to confirm it works. That includes install, dev server, lint, format, typecheck, test, build, and how to run a single test file. Don’t skip the single-file command: without it, the agent’s only way to check one change is the whole suite.

Our Commands table has 17 rows. A few of them:

| `make check` | `astro check`: type-check and validate content |
| `make test` | Vitest unit and component tests |
| `bunx vitest run src/lib/posts.test.ts` | One test file |
| `make deploy` | Production deploy (needs Cloudflare credentials; never run by agents) |

The table opens with a rule: “They are the only supported way to build, test, and run the project; if a command here is wrong, fix it here in the same change.” That stops the agent from inventing npx astro build with different flags, and it makes a broken command a documentation bug the agent fixes on the spot.

List the commands the agent must not run, too, with the reason. Our deploy rows say they need Cloudflare credentials and that agents never run them, and the Guardrails section repeats “Never deploy.”

What belongs in hooks instead of instructions?

Anything that must hold every time goes in a hook or a permission rule, because CLAUDE.md can only ask. (We cover the hooks themselves in how to block dangerous commands in Claude Code.) The Claude Code docs say it directly: CLAUDE.md content is “delivered as a user message after the system prompt,” and there is “no guarantee of strict compliance.” Claude Code applies permission rules and runs hooks itself, so the model can’t skip them.

Here is how the steamfield.io repo splits it:

Need Where it lives
Never force push or git reset --hard permissions.deny and a PreToolUse hook on Bash
Never write real credentials or .env A PreToolUse hook on Edit and Write
Fix lint errors in the file just written A PostToolUse hook that returns the diagnostics
Leave edited files formatted A Stop hook that runs Prettier on this session’s files
Run tests before pushing A lefthook pre-push hook

The instruction file still has a job here: it tells the agent what to do when a hook fires. Our Guardrails section says “A block means ask the user; never rephrase a command or use another tool to bypass it.” A hook can deny git push --force, but only the instructions can tell the agent not to try git push -f next. The hook’s deny message repeats it (“Never work around this block”) so the rule shows up at the moment it applies.

This layout came from Stacks, the project starter we build, which scaffolded this site. Its agent-ops fragment writes the AGENTS.md skeleton, the CLAUDE.md with the import, and the guard hooks, and stacks verify checks that CLAUDE.md still has @AGENTS.md on a line of its own.

Stacks / local terminal
LOCALREADY

Choose a command to watch it run, or type your own below.

STEAMFIELD / LOCAL SESSION
Explore the starter behind this site.
This is a simulation. No files or commands leave your browser.
Type help, or choose a command below.

Enter runs · ↑ ↓ history · Esc / Ctrl+C stops · Tab moves between controls. This is a local simulation; no commands are executed.

Local demo ready.

Fig. 1 A simulated project session with an abridged brief and recorded verification output. Source: this site's scaffold preview and historical verification

How do you keep the file from going stale?

Give every temporary entry a condition for removing it. Our Build workarounds table has a “Remove when” column, so a pin isn’t permanent by accident:

| `package.json` `typescript: ^6` | `@astrojs/check` 0.9.12 declares a peer range of `^5 \|\| ^6`; TypeScript 7 fails it. | `@astrojs/check` supports TypeScript 7 |

When someone upgrades @astrojs/check, the row tells them what to try. Without it, the pin outlives its reason and the next agent copies it into new code.

Three other habits help:

  • Fix the file in the same change that makes it wrong. Our Commands rule (“fix it here in the same change”) is the general version of this.
  • Check it mechanically. Stacks writes the skeleton with stacks:fill markers, and stacks verify fails while any remain or while the ## Commands heading is missing. Claude Code also ships /doctor prompt-audit, which looks for references to files or commands that no longer exist and for instructions that contradict each other.
  • Write it from the project as built. Stacks’ instructions say to fill AGENTS.md “last, once verify is nearly green,” so the Commands table lists commands that have already run.