Skip to main content

Context Architecture: CLAUDE.md, AGENTS.md and Rules

Why This Matters

Every session starts with an empty context window. Without written context the agent guesses your commands, your folder conventions and your rules, and it guesses differently each time. Context files turn those guesses into facts that load automatically.

CLAUDE.md: the onboarding document

CLAUDE.md in the root of your repository is loaded at the start of every Claude Code session. Write it like onboarding notes for a new colleague: short, concrete and checked into git so the whole team shares it. Running /init in a new project gives you a first draft to edit.

CLAUDE.md for TestRun Board in an editor, next to the project tree with .claude/settings.json, skills, agents and .mcp.json

A good CLAUDE.md answers four questions:

  1. What is this? One or two sentences and the stack.
  2. How do I run it? Exact commands for dev, test, e2e and the full check.
  3. How is it organised? Where business logic lives, what talks to what.
  4. What are the rules? The few things that must always or never happen.

Keep it short (the official guidance is to stay well under a few hundred lines). Every line costs context in every session, and long files get followed less reliably.

The hierarchy: user, project, local

Claude Code combines several files:

LevelFileUse it for
User~/.claude/CLAUDE.mdYour personal preferences in every project
Project./CLAUDE.md (or ./.claude/CLAUDE.md)Team conventions, committed to git
Local./CLAUDE.local.mdYour personal notes for this project, not committed
Subfoldersrc/api/CLAUDE.mdLoaded when the agent works in that folder

Files in parent folders load at start; files in subfolders load when the agent works there. You can also import other files with @path/to/file.md, for example @docs/architecture.md.

AGENTS.md: one file for every harness

AGENTS.md is an open convention that other agents (OpenCode, Codex and more) read. If your team uses more than one harness, keep the shared instructions in AGENTS.md and make CLAUDE.md import it:

# CLAUDE.md
@AGENTS.md

## Claude Code specific
- Use the add-api-endpoint skill for changes in src/lib/api.ts

Rules files for specific areas

When CLAUDE.md grows, split it. Markdown files in .claude/rules/ are loaded as extra instructions, and a paths field in the frontmatter can scope a rule to matching files:

---
paths:
  - "tests/**/*.ts"
  - "tests/**/*.tsx"
---
# Test rules
- Use getByRole/getByLabel locators, never CSS classes
- One behaviour per test, named after the behaviour

Path scoping has had parser quirks between versions, so check with /memory that your rule is actually loaded.

In the example project, AGENTS.md holds the shared instructions, CLAUDE.md imports it and adds the Claude Code specifics, and .claude/rules/testing.md holds the test conventions.

Hands-on

  1. Run /init (or write CLAUDE.md by hand) and cut it down to the four questions above.
  2. Add your real check command and two rules your team always repeats in code review.
  3. Start a fresh session and ask: “What must you do before you say a task is done?” The answer should come from your file.
  4. Move test conventions into .claude/rules/testing.md and confirm with /memory.

Walkthrough in TestRun Board

  1. Open AGENTS.md: commands, architecture, rules, definition of done, communication and writing conventions. Then CLAUDE.md: one line @AGENTS.md plus a few Claude Code specifics.
  2. Start a session and run /memory. You should see CLAUDE.md with the imported AGENTS.md. Then ask the agent to read tests/runs-page.test.tsx and run /memory again: .claude/rules/testing.md is path-scoped, so it only loads once the agent works with test files.
  3. Experiment: comment out the @AGENTS.md line in CLAUDE.md, start a new session and ask “What must you do before you say a task is done?”. Then restore the line, start a new session and ask again. The second answer should quote the definition of done.
  4. Ask the agent to write a component test for the empty state. With the rules file loaded, it should use getByRole/getByText and server.use(...) to override the API, as the rules ask.
  5. Notice what is not in AGENTS.md: no long explanations of React or Vitest. The agent knows those; it needs the project-specific facts.

Key Takeaways

  • Context files replace guessing with facts that load every session.
  • Keep CLAUDE.md short: what, how to run, how it is organised, the rules.
  • Use the hierarchy: personal in ~/.claude, team in the repo, private in CLAUDE.local.md.
  • Share instructions across harnesses with AGENTS.md and an @AGENTS.md import.