CLAUDE-MD · context-engineering
Rules directory: path-scoped instructions
A .claude/rules/ layout that moves per-area instructions out of CLAUDE.md into path-scoped rule files, so API rules load only when the agent touches API files and the root file stays under the 200-line bar.
✓ verified 2026-07-18 · stack: claude-code · source
CLAUDE.md files over roughly 200 lines consume more context and reduce
adherence; the documented fix is .claude/rules/: topic files, discovered
recursively, with an optional paths frontmatter that loads a rule only
when Claude works with matching files
(memory). This is the layout for
a service repo, with one unconditional rule and two path-scoped ones.
your-service/├── CLAUDE.md # under 200 lines: commands, layout, hard rules└── .claude/ └── rules/ ├── security.md # unconditional: loads every session ├── api.md # loads when touching src/api/** └── testing.md # loads when touching tests---paths: - "src/api/**/*.{ts,tsx}"---
# API rules
- Every endpoint validates input at the boundary; internal layers assume validated data.- Errors use the standard problem+json envelope from src/api/errors.ts; never invent an error shape.- New routes get an OpenAPI comment block; the spec is generated, so a missing block fails CI.---paths: - "tests/**/*.test.ts" - "src/**/*.spec.ts"---
# Testing rules
- Tests are deterministic: no network, no wall clock, seeded randomness.- One behaviour per test; the name states the behaviour, not the method.- Never delete or weaken a failing test to make a suite pass; flag it.# Security rules (always loaded)
- Secrets never appear in code, logs, or test fixtures; they arrive via the environment.- New dependencies need a one-line justification in the PR description.What it does
- Path-scoped rules load on read, not on every tool use: the API rules
enter context when Claude reads a matching file, so a session working in
the billing module never pays for them. Rules without a
pathsfield load at launch with the same priority as.claude/CLAUDE.md. - Glob semantics are the documented set:
**/*.tsfor extensions anywhere,src/**/*for a subtree, brace expansion (src/**/*.{ts,tsx}) for multiple extensions in one pattern. - One topic per file with a descriptive filename is the documented
convention, and it is also what keeps rules reviewable: a diff to
testing.mdis legible in a way a diff to line 340 of CLAUDE.md is not. - Symlinks are supported and resolved, so a platform team can maintain
shared rules once and link them into every repo
(
ln -s ~/company-standards/security.md .claude/rules/security.md). - The root CLAUDE.md keeps what belongs in every session: build commands, layout, and the handful of always-true hard rules. The CLAUDE.md architecture pattern carries the full sizing argument.
Failure modes
- Rules are context, not enforcement. The docs are explicit: Claude treats memory as context, and blocking an action regardless of what Claude decides is a PreToolUse hook’s job. Anything non-negotiable in these files should have a hook or CI twin; see hooks as hard gates.
- An invalid glob matches nothing. A pattern with an unreadable
[is invalid and silently matches nothing (other patterns in the rule keep working); escape a literal bracket as\[. On Claude Code before v2.1.207 one invalid pattern broke Read for evaluated files, so keep versions current if you use bracketed paths. - Contradictions across files are picked arbitrarily. A rule in
api.mdthat fights a line in CLAUDE.md does not lose deterministically; it makes behaviour random. Review the set together, not file by file. - Path-scoped is not secret. Rules load into context like any other instruction; they are for relevance, not access control.
Verified against the .claude/rules/ discovery, paths frontmatter and glob table, load semantics, size guidance, and symlink support at code.claude.com/docs/en/memory, 2026-07-18.