Path-scoped rules with .claude/rules/
How to keep instructions out of context until they are relevant.
4 min read · Lesson 4 of 11 in this domain
A root CLAUDE.md loads on every single session, so anything you put there costs context in every unrelated conversation. Rules files solve that. Dropping a markdown file into .claude/rules/ gives you the same instruction mechanism, but adding a paths glob in its frontmatter makes it conditional: the rule enters context only when Claude works with a file that matches. It is the difference between telling someone your API conventions every morning and handing them the note when they open the API folder.
- Put markdown files in
.claude/rules/. All.mdfiles are discovered recursively, so you can organise into subdirectories. - A rule without a
pathsfield loads unconditionally at launch, at the same priority as.claude/CLAUDE.md. - A rule with
pathsfrontmatter loads only when Claude works with files matching those globs. This is the mechanism for scoping instructions by file type or directory. - Path-scoped rules trigger when Claude reads a matching file, not on every tool use.
- User-level rules live in
~/.claude/rules/and load before project rules, giving project rules higher priority. - Rules vs skills: rules load every session (or on path match); skills load only when invoked or judged relevant.
---
paths:
- "src/api/**/*.ts"
- "lib/**/*.{ts,tsx}"
---
# API Development Rules
- All API endpoints must include input validation
- Use the standard error response format
"Put a conditional sentence in the root CLAUDE.md" — that loads on every session regardless, costing context in every unrelated conversation.
How do you load a rule only for TypeScript files under src/api/?
Path-scoped rules are the purpose-built mechanism; a root CLAUDE.md loads every session regardless.
A rules file has no paths field. When does it load?
Omitting paths is the normal way to write an always-on rule.
Practise this domain with 20%%-weighted questions in the study app.
Open in study appSource: Claude Code Docs — Memory · Independent study aid, not affiliated with or endorsed by Anthropic.