Skip to content

Rules

Rules are the persistent, human-authored context each CLI reads automatically at session start and injects into the model’s context — before you’ve typed anything. They live in a file (or set of files) you write and check into the repo: build commands, naming conventions, “we use pnpm not npm,” “the API lives in services/api,” “never edit migrations after they’ve been merged.”

There’s a cross-tool convention here. Codex, opencode, Cursor, and Copilot all read AGENTS.md natively (Copilot also reads CLAUDE.md and GEMINI.md from the repo root). Claude Code reads CLAUDE.md but can import @AGENTS.md for interop. The clean pattern is one AGENTS.md at your repo root, imported into CLAUDE.md — a single source of truth that every major tool will load.

Rules are declarativeyou write them, the agent reads them. That’s the distinction from agent-managed memory (where the agent decides what to remember about a session or a user); memory is a separate primitive, not covered here. This chapter is about the static, version-controlled context you author.

You’ve started a fresh session on the same repo for the fifth time this week. The first three messages of every session are always the same: “we use pnpm, not npm. Tests are in __tests__/, not next to source. Don’t auto-run the dev server, it conflicts with the one I already have open. Use the dev-stack branch as base, not main.”

You’re not telling the agent anything new. You’re telling this session something you already told last session, because sessions don’t remember each other. The agent reads files, runs greps, can find anything in the codebase — but it still doesn’t know the unwritten conventions of the team that owns it. So every session starts at zero on the same five facts.

That’s the gap rules close. Write the facts down once, in a file the CLI reads on every start, and they’re loaded before your first message. The agent walks in already knowing.

Real-world examples of what teams put in CLAUDE.md / AGENTS.md:

  • Stack and tooling — package manager, test runner, linter, build commands. The first thing the agent gets wrong without this.
  • Project layout — where things live (“API code is in services/api, web is in apps/web”). Saves it from grepping the whole tree to find a file you could’ve named.
  • Conventions the linter can’t catch — “use the Logger class, not console.log,” “all DB calls go through db/repo/,” “feature flags read from flags.ts, never inline.”
  • Workflow rules — “never push to main,” “run pnpm validate before declaring done,” “write tests in the same PR as the feature.”
  • Domain context — what your product is, who uses it, what the core nouns mean. Especially useful for non-obvious domains (insurance, logistics, ad-tech).
  • Pointers to deeper docs — “for the auth flow, read docs/auth.md,” so the file stays small but the breadcrumbs are there.

The test: if you’ve corrected the agent on the same thing twice across sessions, it belongs in your rules.

You want to…Reach forNot
State a fact the agent should know on every turn of every sessionRulesSkill
Encode a multi-step procedure the agent activates only when relevantSkillRules
Hard-enforce a rule (block, gate, redirect) deterministicallyHookRules
Apply guidance only when working in a specific path/subdirPath-scoped rules (see below)Bloating root CLAUDE.md
Share knowledge across multiple reposUser-global rules filePer-repo rules

Rules are always loaded, every turn. That makes them powerful — and expensive on context. Anything that doesn’t need to be present every single turn belongs in a skill instead. Anything that needs to enforce (not just inform) belongs in a hook.

Files Claude Code reads on every session:

  • ./CLAUDE.md — project root
  • Nested CLAUDE.md files in subdirectories — load when Claude works in that subdir
  • ~/.claude/CLAUDE.md — your user-wide file (applies to every project)
  • Managed-policy CLAUDE.md — org-controlled, takes precedence
  • .claude/rules/*.md — optional path-scoped rules (new in 2026)

Layering: all levels are additive. Claude reads every applicable file and uses judgment when they conflict; more specific takes precedence.

Path-scoped rules keep CLAUDE.md small. A rule file in .claude/rules/ with paths: frontmatter only loads when Claude touches matching files:

---
paths: ["src/api/**"]
---
Use Zod for all request validation.

Imports: @path/to/file anywhere in CLAUDE.md inlines that file’s content. Use @AGENTS.md to share rules with Codex and opencode from a single file.

Sizing: keep CLAUDE.md under ~200 lines. Move reference material to skills, and path-specific guidance to .claude/rules/.

AspectClaude CodeCodexopencodeCursorCopilot
Default project fileCLAUDE.mdAGENTS.mdAGENTS.md.cursor/rules/*.mdc or AGENTS.md.github/copilot-instructions.md or AGENTS.md
Cross-tool standardVia @AGENTS.md importNative (agents.md)Native (agents.md)Native (AGENTS.md)Native (AGENTS.md / CLAUDE.md / GEMINI.md)
User-global file~/.claude/CLAUDE.md~/.codex/AGENTS.md~/.config/opencode/AGENTS.mdUser Rules (UI; no local file)Personal instructions (github.com)
Path-scoped rules.claude/rules/ with paths:instructions: in opencode.jsonglobs: in .mdc frontmatter.instructions.md with applyTo:
Nested files per subdirConcatenated (additive)Concatenated; deeper files override by appearing laterFirst match wins (no merging)Nested AGENTS.md auto-applied in subtreeNearest AGENTS.md takes precedence
@path importsYes (bare @path)NoNo@RuleName only (manual invocation)No
Managed/org overrideYesYesYesYes (Team Rules)Yes (Business / Enterprise)
  • “Rules” means different things across tools. In Claude Code it’s path-scoped Markdown files in .claude/rules/ (a subset of the rules layer). In opencode there is no separate “Rules” file — AGENTS.md carries that role, supplemented by the instructions field in opencode.json. In Cursor, “Rules” is the umbrella name for the whole layer (.cursor/rules/*.mdc, User Rules, Team Rules, plus AGENTS.md). Copilot calls the same concept “instructions” (copilot-instructions.md, .instructions.md, personal and org instructions). In this chapter, “rules” is the cross-tool umbrella term.
  • AGENTS.md vs CLAUDE.md — same purpose, different filenames. Don’t write conflicting copies of both in one repo; have one import the other.
  • “Memory” is not the same as rules. Rules are human-authored, declarative, version-controlled. Memory (in the strict sense) is agent-managed — the agent itself decides what facts to remember about a user or project across sessions, often without you seeing the write. They solve different problems and live in different places. This chapter covers rules only.