Claude Code Rules: How to Configure AI Behavior for Your Project
Claude Code rules give you fine-grained control over how Claude behaves in your project. Unlike CLAUDE.md (which applies globally), rules can be conditional, activating only for specific file types or directories.
What Are Rules?
Rules are markdown files stored in .claude/rules/ that define behavioral constraints and guidelines. They tell Claude what standards to follow, what patterns to use, and what to avoid when working in your codebase.
Rules vs CLAUDE.md vs Skills
- - CLAUDE.md: Always active, project-wide instructions
- - Rules: Conditionally active based on file patterns
- - Skills: Task-specific, portable instruction packages
Creating Rules
Create markdown files in your project's .claude/rules/ directory. Each file can include frontmatter to specify when it should activate.
Always-Active Rules
A rule without frontmatter is always active, similar to CLAUDE.md content but organized in separate files for better maintainability.
Conditional Rules
Add frontmatter with file patterns to activate rules only when Claude is working with matching files. For example, a TypeScript rule that only activates for .ts and .tsx files.
Example Rules
Code Style Rule Define your team's naming conventions, import ordering, comment style, and formatting preferences. This ensures Claude generates code that matches your existing codebase.
Testing Rule Specify your testing framework, assertion style, mock patterns, and coverage requirements. Claude will follow these when writing or modifying tests.
Security Rule Define security requirements like input validation, SQL parameterization, authentication checks, and data sanitization. Activates for all backend code.
Best Practices
- Keep rules focused and single-purpose
- Use conditional activation to avoid context pollution
- Include both positive (do this) and negative (avoid this) guidance
- Reference your rules in CLAUDE.md for discoverability
- Version control your rules with your project
Conclusion
Rules are essential for teams that want consistent AI-generated code. They bridge the gap between general-purpose Claude behavior and your specific team standards.