claude-almanac
Core

Rules

Modular, topic-specific instruction files in .claude/rules/ for organizing project guidelines.

Claude Code Rules

Modular, topic-specific instruction files for organizing project guidelines.

Overview

Rules are markdown files in .claude/rules/ that extend the CLAUDE.md memory system. Instead of one large file, you can organize instructions into focused, maintainable modules.

Rules vs CLAUDE.md

AspectCLAUDE.mdRules
StructureSingle monolithic fileMultiple focused files
Use caseProject overview, main conventionsTopic-specific guidelines
ScalabilityGets unwieldy in large projectsBetter for team collaboration
LoadingAlways loadedCan be path-conditional

Key principle: Rules complement CLAUDE.md, they don't replace it.


File Locations

Project Rules (Team-Shared)

your-project/
└── .claude/
    └── rules/
        ├── code-style.md
        ├── testing.md
        └── frontend/
            └── react.md
  • Location: ./.claude/rules/*.md
  • Scope: Current project only
  • Sharing: Via git (commit to source control)
  • Discovery: Recursive - all .md files in subdirectories

User Rules (Personal)

~/.claude/
└── rules/
    ├── preferences.md
    └── workflows.md
  • Location: ~/.claude/rules/*.md
  • Scope: All your projects
  • Sharing: Personal only
  • Use case: Personal coding preferences

Precedence Hierarchy

From highest to lowest priority:

PrioritySourceNotes
1Managed policy CLAUDE.mdOrganization-controlled
2Project rules (.claude/rules/)Team standards
3Project CLAUDE.mdShared instructions
4User rules (~/.claude/rules/)Personal fallback
5User CLAUDE.mdPersonal global

Project rules override user rules, allowing teams to enforce standards.


File Format

Basic Structure

# Rule Title

Your markdown instructions here...

With Path Conditions

---
paths:
  - "src/api/**/*.ts"
---

# API Development Rules

Instructions that only apply to API files...

Frontmatter Options

FieldTypeRequiredPurpose
pathsArrayNoGlob patterns for conditional loading

Rules without paths are loaded unconditionally for all files.


Path-Conditional Rules

Scope rules to specific file types using glob patterns.

Basic Example

---
paths:
  - "src/components/**/*.tsx"
---

# React Component Guidelines

- Use functional components with hooks
- Keep components under 300 lines

This rule only loads when working on .tsx files in src/components/.

Glob Pattern Reference

PatternMatches
**/*.tsAll .ts files in any directory
src/**/*All files under src/
*.mdMarkdown files in root only
src/**/*.{ts,tsx}Both .ts and .tsx files
{src,lib}/**/*.tsFiles in either src/ or lib/

Multiple Patterns

---
paths:
  - "src/**/*.ts"
  - "lib/**/*.ts"
  - "tests/**/*.test.ts"
---

# TypeScript Guidelines

Rule loads if current file matches any pattern.


How Rules Are Loaded

  1. Session start: Claude discovers all .md files in .claude/rules/ recursively
  2. Path evaluation: Rules with paths are checked against current file
  3. Context merge: All applicable rules combined with CLAUDE.md
  4. Injection: Combined instructions provided to Claude

View Loaded Rules

/memory    # Shows all loaded memory files including rules

Directory Organization

.claude/
├── CLAUDE.md                    # Main project overview
└── rules/
    ├── code-style.md            # Coding style and formatting
    ├── testing.md               # Testing conventions
    ├── security.md              # Security requirements
    ├── frontend/
    │   ├── react.md             # React-specific rules
    │   ├── styling.md           # CSS conventions
    │   └── accessibility.md     # A11y requirements
    └── backend/
        ├── api.md               # API design standards
        ├── database.md          # Database conventions
        └── authentication.md    # Auth rules

Naming Conventions

  • Descriptive names: react-hooks.md not frontend.md
  • One topic per file: Keep rules focused
  • Lowercase with hyphens: api-design.md
  • Numbered prefixes (optional): 00-base.md, 10-api.md if order matters

Rule Examples

Frontend Rules

.claude/rules/frontend/react.md

---
paths:
  - "src/components/**/*.tsx"
  - "src/pages/**/*.tsx"
---

# React Component Guidelines

## Structure
- Use functional components with hooks
- Colocate styles using CSS modules
- Keep components under 300 lines

## Hooks
- Never call hooks conditionally
- Extract custom hooks for reusable logic
- Use useCallback for functions passed to children

## Props
- Use TypeScript interfaces for props
- Destructure props in function signature

Backend/API Rules

.claude/rules/backend/api.md

---
paths:
  - "src/api/**/*.ts"
  - "src/routes/**/*.ts"
---

# API Design Standards

## Endpoints
- Use RESTful naming: `/api/v1/{resource}/{id}`
- Version all endpoints

## Error Format
```json
{
  "error": "error_code",
  "message": "Human readable message",
  "details": {}
}

Validation

  • Validate all inputs with zod/joi
  • Return 400 with validation errors

### Testing Rules

**`.claude/rules/testing.md`**

```markdown
# Testing Conventions

## Unit Tests
- Minimum 80% code coverage
- File naming: `[feature].test.ts`
- Structure: Arrange-Act-Assert

## Integration Tests
- Location: `tests/integration/`
- Clean up database after each test

## Test Data
- Use fixtures in `tests/fixtures/`
- Use factories for test objects

Security Rules

.claude/rules/security.md

# Security Requirements

## Authentication
- Require auth for all protected endpoints
- Use JWT with 24-hour expiration
- Implement refresh token rotation

## Data Protection
- Never log passwords, tokens, PII
- Encrypt data at rest and in transit
- Use parameterized queries (prevent SQL injection)

## Dependencies
- Run `npm audit` before deployment
- Pin dependency versions

Language-Specific Rules

.claude/rules/languages/python.md

---
paths:
  - "scripts/**/*.py"
  - "tools/**/*.py"
---

# Python Guidelines

## Style
- Follow PEP 8
- Use type hints for all functions
- Line length: 88 characters (Black)

## Structure
- Use dataclasses for simple data
- Use pathlib.Path instead of os.path
- Use f-strings for formatting

Commands

CommandPurpose
/memoryView all loaded memory (CLAUDE.md + rules)
/initCreate initial CLAUDE.md

There are no dedicated /rules commands. Rules are managed by creating/editing files directly.

Creating Rules

# Create rules directory
mkdir -p .claude/rules

# Create a rule file
touch .claude/rules/testing.md

# Create organized subdirectories
mkdir -p .claude/rules/backend
touch .claude/rules/backend/database.md

Sharing Rules Across Projects

# Symlink a shared rules directory
ln -s ~/shared-claude-rules .claude/rules/shared

# Symlink individual files
ln -s ~/company-standards/security.md .claude/rules/security.md

Symlinks are resolved and contents loaded normally.


Best Practices

Do

  • One topic per file: Keep rules focused and maintainable
  • Use descriptive filenames: Clearly indicate what the rule covers
  • Organize with subdirectories: Group by domain (frontend/, backend/)
  • Use path conditions sparingly: Only when rules truly apply to specific files
  • Commit to git: Share project rules with team

Don't

  • Don't duplicate CLAUDE.md: Rules complement, not replace
  • Don't create huge rule files: Keep them focused
  • Don't use unsupported frontmatter: Only paths is documented
  • Don't create thousands of files: Discovery has overhead

Limitations

LimitationDetails
No explicit orderingFiles load in filesystem order (likely alphabetical)
Glob patterns onlyNo regex support for paths
No conflict resolutionContradicting rules both load; Claude reconciles
Context usageEach rule consumes tokens; monitor with /context
Import depthMax 5 hops for nested imports from CLAUDE.md

FeatureRulesSkillsHooks
PurposeGuidelines/memoryRunnable workflowsAutomated actions
InvocationAutomatic/skill-nameOn events
Location.claude/rules/.claude/skills/.claude/settings.json
LoadingAlways (or path-matched)On invocationOn trigger

Quick Start

  1. Create rules directory:

    mkdir -p .claude/rules
  2. Add your first rule:

    cat > .claude/rules/testing.md << 'EOF'
    # Testing Conventions
    
    - Use Jest for unit tests
    - Minimum 80% coverage
    - Colocate tests with source files
    EOF
  3. Add a path-conditional rule:

    cat > .claude/rules/api.md << 'EOF'
    ---
    paths:
      - "src/api/**/*.ts"
    ---
    
    # API Standards
    
    - Use RESTful conventions
    - Validate all inputs
    - Return consistent error format
    EOF
  4. Verify rules are loaded:

    /memory

Sources

On this page