CLAUDE.md Best Practices

CLAUDE.md is the instruction file that tells Claude Code how to behave in your project. Get it right and Claude follows your rules. Get it wrong and Claude ignores everything.

The Golden Rule: Keep It Short

Problem: CLAUDE.md files over 100 lines cause Claude to miss important rules. The longer the file, the more likely critical instructions are ignored.

Solution: Keep CLAUDE.md under 50 lines. Move detailed instructions to .claude/skills/ files (loaded on demand, saving tokens).

5 Templates by Use Case

Use CaseLinesSafetyFocus
Beginner14★★Basic safety rules
Autonomous24★★★★Long-running, unattended
Team26★★★Shared repo conventions
Production22★★★★★Zero-tolerance safety
Learning14Explanations, minimal constraints

Minimal Safety (14 lines)

# Project Rules
## Safety
- Never use rm -rf, git reset --hard, or git push --force
- Never commit .env files or API keys
- Always create a new branch before making changes
- Run tests before committing
## Code Style
- Follow existing code conventions
- Keep functions under 50 lines

Autonomous Operation (24 lines)

# Autonomous Operation Rules
## Critical Safety
- NEVER delete files without explicit backup first
- NEVER push to main/master directly
- Stop and report if the same error occurs 3 times in a row
## Work Pattern
- Create a git branch for each task
- Commit after each meaningful change
- Run tests after every code change
## Context Management
- Save work state before context compaction
- Keep TODO items in tasks/todo.md

Common Mistakes

100+ line CLAUDE.md: Claude Code struggles with long instruction files. Split into CLAUDE.md (principles) + Skills (procedures).

Contradictory rules: "Write tests for everything" + "Move fast" without priority. Always state which wins.

Code snippets in CLAUDE.md: Wastes tokens on every turn. Put code in Skills files instead.

CLAUDE.md + Hooks = Enforcement

CLAUDE.md tells Claude the rules. Hooks enforce them at the process level.

# CLAUDE.md says "never rm -rf"
# This hook BLOCKS it:
npx cc-safe-setup --install-example destructive-guard

Auto-generate CLAUDE.md from your project:

npx cc-safe-setup --scan --apply

Detects your tech stack and creates appropriate rules + hooks

Related

Learn more: Production Guide · All Tools