Skip to content
mentoor

Topic 1 of 5 · 4 min read

What belongs in CLAUDE.md or AGENTS.md

Goal: Write a project memory file that gives an agent the commands, non-obvious conventions and boundaries it needs in under a page.

A project memory file holds only what an agent cannot work out by reading your code: the exact commands, the conventions that look arbitrary, the boundaries, and where things live. Everything else is noise it must read on every run.

Check yourself · 1 of 3

Which line is the best candidate for a project memory file?

Choose one answer

Why it matters

An agent such as Claude Code, Codex, Gemini CLI or Copilot starts each session knowing nothing about your project. A file like CLAUDE.md or AGENTS.md is loaded into its context at the start, so it is the cheapest place to fix repeated mistakes. It is also read every single time, and the Claude Code docs say longer files use more context and reduce how well the rules are followed. They suggest keeping each file under 200 lines. Short and exact beats long and complete.

What earns a line

  • Commands, exact and runnable. The test command for one file, the typecheck, the lint, the dev server. An agent that guesses npm test in a pnpm repo wastes a turn.
  • Conventions you cannot infer. "Money is stored as integer cents." "All dates are UTC; format only in the UI." The agent can see the code, but not the reason or the rule.
  • Boundaries. Files it must not edit (generated code, migrations already applied), actions that need a human ("never run the deploy script").
  • Pointers. "API handlers live in src/routes/; each has a *.test.ts beside it." A map of two or three lines saves a long search.
  • Gotchas that burned you before. A flaky test, a required env var, the order of two steps.

Write each as a short imperative. Prefer "Run pnpm test path/to/file for one file" over a paragraph of advice.

Before and after

Weak:

# Project
This is a modern, scalable web app. Please write clean, high-quality,
well-tested code and follow best practices.

Better:

# Orders API
- Test one file: `pnpm vitest run src/orders/refund.test.ts`
- Typecheck: `pnpm tsc --noEmit` (run before saying a task is done)
- Money is integer cents everywhere. Never use floats for prices.
- Do not edit `src/generated/`; run `pnpm codegen` instead.
- Never run `scripts/deploy.sh`. Ask the user.
- Handlers: `src/routes/`. Shared errors: `src/lib/errors.ts`.

The first version could be pasted into any repo and changes nothing. The second changes what the agent does on the first task.

What goes wrong

  • Generic advice. "Write clean code" gives the agent nothing to act on.
  • A wall of text. A 600-line file buries the three rules that matter, so the agent follows some and drops others without telling you.
  • Guessed commands. A command you never ran fails, and the agent trusts it anyway.
  • No reason for odd rules. "Never use Date.now()" is followed more reliably as "Never use Date.now(); use the injected clock so tests are deterministic."

Examples

A first-draft file for a small service

# Billing service
- Install: `pnpm install`. Dev: `pnpm dev` (port 4010).
- One test file: `pnpm vitest run <path>`. All: `pnpm test`.
- Amounts are integer cents. Currency is a separate field.
- Migrations in `db/migrations/` are append-only. Add a new file, never edit an old one.
- Webhook handlers must be idempotent; see `src/webhooks/stripe.ts` for the pattern.

Six lines, each of which prevents a real mistake.

Turning a repeated correction into a rule

You have told the agent three times: "the tests need TZ=UTC". Add one line, Run tests with TZ=UTC pnpm test, and the correction is made once for every future session.

Key points

  • Put in the file only what the agent cannot infer from reading the code: commands, hidden conventions, boundaries and pointers.
  • Write exact commands you have actually run, including how to run a single test file.
  • State each rule as a short imperative with the reason for any rule that looks arbitrary.
  • Keep the file short; the Claude Code docs suggest under 200 lines per file because longer files reduce adherence.
  • Generic advice such as "write clean code" changes nothing and should be left out.

Common mistakes

  • Writing vague quality advice instead of concrete commands and rules.
  • Pasting commands from memory without running them.
  • Listing every folder instead of the two or three the agent keeps getting wrong.
  • Stating a rule with no reason, so the agent drops it when it seems inconvenient.

Check yourself · 2 of 3

The Claude Code docs suggest a target size for each CLAUDE.md file. Why?

Choose one answer

Check yourself · 3 of 3

An agent keeps adding prices as floats in a billing repo. What is the best memory-file fix?

Choose one answer

Practice items, not counted towards your mastery.

Start here