Skip to content

Rules files to borrow

← Technical edition start · The friendly primer

Part of the technical edition. A rules file is the Markdown file your agent loads at the start of every session: CLAUDE.md for Claude Code, AGENTS.md for Codex CLI and others. Practice 2 has the file names for each tool.

Below are five complete ones to start from, then how to share one file between tools, how to scope rules to part of a repo, and what to leave out. Each one is short on purpose. Claude Code's own guidance is to keep each file under 200 lines, and the best ones are much shorter.

Contents: what goes in one · 1. You · 2. An infra repo · 3. A service · 4. An ML repo · 5. A data pipeline · one file for several tools · rules for part of a repo · what to leave out · keeping it true

What goes in one

Things the agent can't work out quickly from the code, and gets wrong without being told:

  • Commands: how to build, test, lint and run it, exactly, including the one that checks everything.
  • Map: where things live, when it isn't obvious from the folder names.
  • Conventions a linter doesn't enforce: error handling, naming, how migrations are written.
  • Definition of done: what must pass before it says it's finished.
  • Boundaries: what it should leave alone, and what to ask about first.
  • Gotchas: the thing that bit the last three people.

A rules file shapes what the agent tries. It doesn't stop anything. Put the hard limits in permission rules and hooks, and keep a line in the rules file that says why, so the agent doesn't keep trying.

1. Your personal file

Goes in ~/.claude/CLAUDE.md for Claude Code or ~/.codex/AGENTS.md for Codex CLI. It applies to every repo you open, so keep it to how you like to work, not facts about any one project.

# How I work

- Answer first, then the reasoning. Keep it short.
- When you say something works, show the command you ran and its output.
- Cite file:line when you describe code. Say when you're guessing.
- Ask before you start anything that will take more than a few steps; show me a plan.
- One logical change per commit. Commit messages: imperative, under 72 characters,
  with a body explaining why.
- Don't add dependencies, reformat files or rename things I didn't ask about.
- If you're stuck after two attempts at the same thing, stop and tell me what you
  tried.

# My machine

- Shell: bash. Python via uv. Node via the version in each repo's .nvmrc.
- I review everything before it's pushed. Never push unless I ask.

2. An infrastructure repo

For a repo of Terraform, Ansible, Kubernetes manifests or similar. Pair it with the starter settings file, which denies apply and destroy.

# infra

Terraform for our cloud accounts, plus Ansible roles for the VMs.

## Layout
- modules/: reusable Terraform modules. Change these with care: every env uses them.
- envs/dev, envs/staging, envs/prod: one root module each. Same modules, different
  variables.
- ansible/roles, ansible/inventories/{dev,staging}: no prod inventory in this repo.

## Commands
- Check everything: make check  (terraform fmt -check, validate, tflint, ansible-lint)
- Plan one env: make plan ENV=dev
- Ansible dry run: ansible-playbook -i ansible/inventories/dev site.yml --check --diff

## Rules
- You may run fmt, validate, lint and plan for dev. Never apply, destroy, import or
  edit state. Those are blocked in settings; a person or the pipeline runs them.
- After a plan, list every destroy and replace first, with the reason for each.
- Every new resource gets the tags owner and env.
- Renaming a resource: add a moved block so it isn't destroyed and recreated.
- Don't change provider or module versions unless that's the task.
- Ansible tasks must be idempotent. Prefer modules to shell; if shell is the only way,
  add creates, removes or changed_when.
- No secrets in this repo. Secrets come from the vault at runtime.

## Done means
make check passes, the plan for dev shows only what was asked for, and the pull
request description has the plan summary.

3. A service or app repo

# service-a

HTTP API in Go. Postgres for storage, a queue for background jobs.

## Commands
- Everything: make check  (gofmt, go vet, golangci-lint, unit tests)
- Unit tests: go test ./...
- Integration tests (need Docker): make test-integration
- Run locally: make run  (reads .env.local; copy .env.example)

## Layout
- cmd/server: entry point. internal/api: handlers. internal/store: database access.
- internal/jobs: queue workers. migrations/: SQL migrations, numbered.

## Conventions
- Errors: wrap with context, fmt.Errorf("doing x: %w", err). Never ignore an error.
- Handlers don't touch the database directly; they call internal/store.
- Migrations are backward compatible: the previous release must still run against
  the new schema. Never edit a migration that has been merged; add a new one.
- New behavior that users see goes behind a feature flag.

## Tests
- A bug fix starts with a failing test, in its own commit.
- Never change a test's expected value without saying why the old one was wrong.
- Never skip a test to get a green build.

## Ask first
- Adding a dependency. Changing a public API or a response shape. Anything in
  migrations/ that drops or renames.

## Done means
make check passes, and the integration tests pass for anything touching the store.

4. An ML repo

# ranking-model

Training and evaluation for the ranking model. PyTorch, configs in YAML.

## Commands
- Everything: make check  (ruff, mypy, pytest)
- Smoke test a config: python -m train --config configs/smoke.yaml  (two minutes)
- Evaluate a checkpoint: python -m evaluate --run <run-id>

## Layout
- src/data: loading and features. src/model: architecture. src/train.py, evaluate.py.
- configs/: one YAML per experiment. runs/<run-id>/: config.yaml, metrics.json,
  checkpoints, written by train.py. Never edit files in runs/.
- experiments.md: the run log. Add a line for every run: what changed and why.

## Rules
- Quote metrics from runs/<run-id>/metrics.json, with the path. Never from memory.
- Before comparing two runs, list every difference: config, commit, data version, seed.
- Change one thing per run unless I say it's a sweep.
- Every run has a seed, and train.py logs the commit and the data version. Keep it
  that way.
- The evaluation code and the test split are fixed. Don't change either without
  asking; a change there makes every old result incomparable.
- Raw data in data/raw is read-only. Derived data goes in data/derived with the
  script that made it.
- Never print more than five rows of data.

## Compute
- Only the smoke config runs without asking. Anything longer, or on the shared GPUs,
  ask first and say how long you expect it to take.

## Done means
make check passes and the smoke config runs end to end.

5. A data pipeline repo

# events-pipeline

Daily batch jobs that turn raw events into reporting tables. SQL models plus Python
for ingestion.

## Commands
- Everything: make check  (sqlfluff, ruff, pytest, model tests)
- Run one model against dev: make run MODEL=<name> TARGET=dev
- Test one model: make test MODEL=<name> TARGET=dev

## Rules
- You may run against TARGET=dev only. The dev warehouse has sample data. Never
  target prod.
- Every model has tests: unique and not-null on its key, plus accepted values or
  relationships where they apply.
- Changing a model's columns or meaning: list every downstream model and report that
  uses it, first.
- Backfills and full refreshes: never run them. Write the command in the pull request
  for a person to run.
- No customer data in fixtures, test files, comments or examples. Use the fake data
  generator in tests/fixtures.
- Dates and times are UTC everywhere. Say so in column names: event_ts_utc.

## Done means
make check passes, and the changed models and everything downstream build and pass
their tests against dev.

One file for several tools

If your team uses more than one agent, keep the shared rules in AGENTS.md, which Codex CLI, Cursor and many others read, and make CLAUDE.md a short file that imports it:

@AGENTS.md

## Claude Code only
- Use plan mode for anything that touches more than three files.

Claude Code reads AGENTS.md on its own only when there's no CLAUDE.md (version 2.1.277 and later), so the import is what keeps both tools on the same rules once you add a CLAUDE.md. For GitHub Copilot, .github/copilot-instructions.md can say "Follow AGENTS.md in the repo root", or hold a copy.

Rules for part of a repo

Some rules only matter in one corner: the migrations folder, the frontend, the notebooks. Loading them every session wastes context. Each tool has a way to scope them.

  • Claude Code: a file in .claude/rules/ with a paths list in its frontmatter loads only when the agent reads or edits a matching file. A CLAUDE.md in a subfolder also loads only when it works there.
  • Codex CLI: it reads AGENTS.md files from the repo root down to the folder you start it in, and files nearer that folder win. Start it in a subfolder to pick up that subfolder's rules.
  • GitHub Copilot: .github/instructions/<name>.instructions.md with an applyTo glob in its frontmatter.
  • Cursor: rule files in .cursor/rules/ can be scoped to file patterns; see its docs for the fields.

A path-scoped rule for Claude Code, in .claude/rules/migrations.md:

---
paths:
  - "migrations/**/*.sql"
---

# Migrations

- Never edit a merged migration. Add a new one with the next number.
- Every migration must be safe to run while the previous release is live: add
  columns as nullable, backfill separately, drop in a later release.
- Include the rollback as a comment at the top.

Scoped rules are for what's useful, not for what must always hold, because not every place loads them. Claude's Projects cloud threads, for one, skip subfolder CLAUDE.md files and path-scoped rules entirely, and load only the root files and unscoped .claude/rules/ files. Keep anything that must always apply there.

What to leave out

  • Secrets, hostnames you wouldn't post publicly, customer names and anything else you wouldn't put in a public README. Rules files get committed, copied and pasted.
  • What a tool already enforces. If the linter or formatter checks it, the rule is "run make check", not the style guide.
  • What the code says plainly. "This is a Go project" when there's a go.mod.
  • Long explanations. One line per rule. Link to the design doc for the why.
  • Hard limits written as wishes. "Never run terraform apply" belongs in a deny rule. Keep a short line saying it's blocked and who runs it instead.
  • Wishes about the model. "Be careful" and "think hard" change little. Concrete rules and checks change a lot.

Keeping it true

  • Add a line the second time you correct the same thing. That's where most good rules come from.
  • Remove a line when it stops being true. A wrong rule is worse than none, because the agent follows it.
  • Keep it under 200 lines, Anthropic's suggested size for a CLAUDE.md. Every line costs context in every session, and long files get followed less closely.
  • One source for rules shared across repos, and a check on the copies. An @path import from another repo only works where that repo is on disk, and a cloud session usually has only the one it's working in. So keep a copy in each repo, keep the master in one template, and check the copies: the first primer's check-rules.sh compares named sections heading by heading and names the repos that drifted. Run it after every change to the template, or in CI.
  • Review it every month or so, or after a big change to the repo. This prompt helps:

Read CLAUDE.md (or AGENTS.md) and check every line against the repo as it is now. List lines that are out of date, commands that don't work, rules that contradict each other, and anything the last few weeks of commits suggest is missing. Don't edit the file; show me the changes you'd make.


← Back: Worked examples · Next: The technical cheat sheet → · Technical edition start