Claude Tips mascot
Claude Tips & Tricks
Claude Code intermediate

Use .claude/rules/ for Conditional Instructions

Place rule files in .claude/rules/ with glob patterns so they only load when Claude is working on matching files.

Instead of stuffing everything into one CLAUDE.md, use .claude/rules/ to load instructions conditionally based on which files Claude is working with.

Setup

.claude/rules/
  typescript.md    # Loaded when editing *.ts, *.tsx
  python.md        # Loaded when editing *.py
  testing.md       # Loaded when editing *test*, *spec*
  security.md      # Loaded when editing auth/*, crypto/*

Each rule file uses YAML frontmatter with a glob pattern:

---
globs: ["*.ts", "*.tsx"]
---

- Use `type` instead of `interface` for object types
- Prefer `z.infer<typeof schema>` over manual type definitions
- All API responses must use the `ApiResponse<T>` wrapper

Benefits

  • Lean context: Only relevant rules load, saving tokens
  • No conflicts: Python rules don’t pollute TypeScript sessions
  • Easy maintenance: Each concern lives in its own file
  • Team-friendly: Different team members can own different rule files

vs. Subdirectory CLAUDE.md

Feature.claude/rules/Subdirectory CLAUDE.md
Matches byFile glob patternWorking directory
Best forLanguage/file type rulesModule-specific context
GranularityFine (per file type)Coarse (per directory)

Tip

Keep each rule file focused and short. The same 200-line attention limit applies. Splitting into many small files is better than a few large ones.

Paste into Claude Code
Set up .claude/rules/ with separate rule files for TypeScript, Python, and test files that only load when working on those file types.