ai-rules for Claude Code

repository·main·Indexed 42 days ago

https://github.com/sahin/ai-rules

A collection of generic, reusable rules and skills for Claude Code designed to minimize context window usage. It implements a two-tier system separating always-loaded declarative standards in `.claude/rules/` from on-demand implementation guides and skills invoked via slash commands (e.g., `/test`, `/api`, `/refactor`) in `.claude/commands/`.

Tokens
2.8K
Snippets
7
Records
13
Agent score
3%

What's inside ai-rules

  1. Optimize Claude Code context by separating Rules and Skills

    main

    To prevent context window bloat and improve Claude's reasoning capabilities, distinguish between Rules and Skills:

    • Rules (.claude/rules/): Use these for high-level, declarative standards that Claude needs to know for literally every task (e.g., "Use conventional commits" or "Never use git add -A"). Keep these short and avoid code examples.
    • Skills (.claude/commands/): Use these for detailed, implementation-heavy guides that are only relevant during specific tasks (e.g., "How to configure Playwright" or "k6 load testing templates"). These are invoked on-demand using a command (e.g., /test) and can contain extensive code examples and checklists.

    The Goal: Minimize the lines in .claude/rules/ (aim for ~300 lines) to leave maximum space for your actual code and reasoning.

    # Example of how a skill is invoked in a conversation
    /test
  2. Understand the Rules vs. Skills architecture

    main

    The project uses a two-tier context management system to optimize Claude Code's performance and reduce context bloat. Instead of loading all instructions at once, it separates them into:

    1. Always-loaded Rules (.claude/rules/): These are the "what". They contain non-negotiable, high-level standards that apply to every task (e.g., coding standards, security principles, Git conventions). They are small (~276 lines) to ensure they stay in Claude's active context without overwhelming it.

    2. On-demand Skills (.claude/commands/): These are the "how". They contain detailed implementation guides, templates, and checklists. They are only loaded when you explicitly invoke them using a slash command (e.g., /test, /api). This keeps the context relevant to the specific task at hand.

  3. How rules and skills work together in Claude Code

    main

    This project uses a two-tier system to manage Claude Code context efficiently:

    1. Always-Loaded Rules: Short, declarative standards located in .claude/rules/. These load into every conversation and cover non-negotiable standards (e.g., coding-standards.md, git-standards.md). They are designed to be minimal (approx. 276 lines) to save context.
    2. On-Demand Skills: Detailed guides and code examples located in .claude/commands/. These do not consume context until you explicitly invoke them using a /command in the Claude Code interface.

    Design Principle: Rules define what the standard is; Skills define how to implement it.

  4. Avoid duplicating Claude Code native features in rules

    main

    Before writing custom markdown rules to enforce workflows, check if Claude Code provides a native way to handle it. Custom markdown enforcement is brittle and burns context.

    RequirementUse Native Claude Code Feature
    Plan enforcementUse native plan mode
    Task trackingUse TodoWrite
    Permission controlUse settings.json deny rules
    Dynamic rule loadingUse skills (/command)
  5. Understand the difference between Rules and Skills in Claude Code

    main

    To optimize context usage in Claude Code, you must distinguish between instructions that are always active and those that are invoked on demand. Using too many rules can exhaust Claude's context window, leaving less room for actual code reasoning.

    Rules (.claude/rules/)

    • Behavior: Automatically load into every conversation.
    • Usage: Use these for "constitutional law"—short, non-negotiable, foundational standards that must always be in effect (e.g., "use kebab-case for files", "never use git add -A").
    • Constraint: Every line in these files consumes context tokens in every single session.

    Skills (.claude/commands/)

    • Behavior: Load only when you explicitly invoke them using the /command syntax.
    • Usage: Use these for "reference books"—detailed guides, checklists, code examples, configuration templates, or step-by-step workflows (e.g., "how to set up a Playwright Page Object Model").
    • Benefit: They have zero context cost until they are actually needed.
  6. Install Claude Code Rules as a git submodule (recommended)

    main

    To use these rules across multiple projects while keeping them updated, add the repository as a git submodule and symlink the rules and commands into your project's .claude directory. This ensures that the core rules are loaded into every conversation without bloating your context window.

    # Add to your project
    git submodule add https://github.com/sahin/ai-rules.git .claude/ai-rules
    
    # Symlink rules and commands into place
    ln -s ai-rules/.claude/rules .claude/rules
    ln -s ai-rules/.claude/commands .claude/commands
  7. Use on-demand skills via slash commands

    main

    You can trigger detailed, task-specific guidance by typing a slash command in Claude Code. This loads specialized context (templates, patterns, and checklists) only when needed.

    Available skills include:

    CommandPurpose
    /testWriting or debugging tests (Playwright, mocking, E2E)
    /apiBuilding API endpoints (Zod, pagination, OpenAPI)
    /dbDatabase schema or query work (Migrations, indexing)
    /frontendUI development (Component patterns, state, accessibility)
    /deployDeployment or infrastructure (CI/CD, rollback, logging)
    /reviewCode reviews (Checklists, PR requirements, feedback tone)
    /adrArchitecture decisions (ADR templates and lifecycle)
    /perfPerformance work (Core Web Vitals, caching, load testing)
    /refactorRefactoring sessions (Patterns, triggers, step-by-step process)
    /securitySecurity reviews or audits (OWASP, input validation, auth)
  8. Install Claude Code Rules via direct copy

    main

    If you prefer not to use git submodules, you can manually clone the repository and copy the rules and commands directories directly into your project's .claude folder.

    git clone https://github.com/sahin/ai-rules.git
    cp -r ai-rules/.claude/rules /your/project/.claude/rules/
    cp -r ai-rules/.claude/commands /your/project/.claude/commands/
  9. Verify CLAUDE.md references are valid

    main

    Ensure that your CLAUDE.md file does not contain "phantom references" (links to directories that do not exist). Phantom references waste context and cause Claude to hallucinate or waste tool calls. Use this script to verify your references:

    # Check if your CLAUDE.md references things that exist
    grep -oP '\.\S+/' CLAUDE.md | sort -u | while read dir; do
      [ -d "$dir" ] || echo "PHANTOM: $dir does not exist"
    done
  10. Reference: Always-Loaded Rules

    main

    The following rules are automatically loaded into every Claude Code conversation to enforce baseline standards.

    | File | Covers |
    |------|--------|
    | `coding-standards.md` | File naming, size limits, imports, error handling, TypeScript, anti-patterns |
    | `testing-requirements.md` | Coverage targets, test pyramid, AAA pattern, before-delivery checklist |
    | `api-security-standards.md` | REST design, HTTP status codes, input validation, auth, OWASP top 10 |
    | `database-standards.md` | Naming, required columns, constraints, migrations, query performance |
    | `git-standards.md` | Branch naming, conventional commits, PR guidelines |
    | `project-standards.md` | File organization, docs placement, frontend requirements, env config |
  11. Reference the Always-loaded Rules standards

    main

    The following six categories of standards are always present in the context to ensure consistent output. They define the "what" of the project's quality requirements:

    1. Coding Standards: File naming (kebab-case), component naming (PascalCase), function size limits, and TypeScript policies.
    2. Testing Requirements: Coverage targets (e.g., 100% API, 80% UI), test pyramid ratios, and the AAA pattern.
    3. API & Security Standards: RESTful design, HTTP status codes, input validation, and OWASP awareness.
    4. Database Standards: Naming (snake_case), required columns (created_at, updated_at), and migration principles.
    5. Git Standards: Branch naming (feature/, bugfix/), conventional commits, and PR size limits.
    6. Project Standards: File organization, documentation location, and mandatory UI states (loading/error/empty).