CLAUDE.md Writing Guide โ Order Optimization & Best Practices
Claude CodeStable content on top, frequently changing content at bottom โ Prompt caching optimization
CLAUDE.md is the most important file for Claude Code to understand your project. It's automatically loaded at the start of every session, and Claude follows the rules written here.
Most Important Principle: Placement Order
Put stable content on top, frequently changing content at the bottom. The reason is prompt caching.
Claude Code caches system prompts, and caching is only valid for the matching portion from the beginning. If the front of the file changes, the entire cache is invalidated. So putting content that never changes (project description, coding rules) at the very top increases cache hit rates, while content that changes every time (current work context) should go at the very bottom so it doesn't affect the cache.
Recommended Order
- Project Description โ name, tech stack, purpose (rarely changes โ cache hit)
- Coding Rules โ conventions, prohibitions, style (additions happen but existing content stays โ cache hit)
- Architecture Decisions โ DB, auth, routing patterns (occasional changes โ occasional cache miss)
- Current Work Context โ ongoing tasks, temp notes (frequent changes โ cache miss, but at bottom so no impact on above cache)
Rule File Separation
Split topic-specific .md files into .claude/rules/ directory to keep the main CLAUDE.md clean. Rule files are auto-loaded alongside CLAUDE.md.
3 Levels of CLAUDE.md
~/.claude/CLAUDE.mdโ personal global rules (all projects)project-root/CLAUDE.mdโ project rules (Git committed, team shared).claude/rules/*.mdโ topic-specific split rules (Git committed)
Prompt Caching and Placement Order
Claude Code includes CLAUDE.md in the system prompt and caches from the beginning. If the front matches the previous session โ cache hit โ faster and cheaper.
Key: Caching is valid only for the matching portion from the beginning. Putting changing content at the top invalidates the entire cache.
3 Levels of CLAUDE.md
| Level | Location | Scope | Git |
|---|---|---|---|
| Personal Global | ~/.claude/CLAUDE.md |
All my projects | Untracked |
| Project | project-root/CLAUDE.md |
Entire project (team shared) | Committed |
| Topic-specific | .claude/rules/*.md |
Project (topic separated) | Committed |
Writing Tips
Be specific โ "Use Rails 8 enum syntax (attribute + enum :status)" beats "write clean code"
Include reasons with prohibitions โ "No jsonb (SQLite unsupported)" is followed better than just "No jsonb"
Include code examples โ adding code blocks alongside instructions dramatically improves accuracy
Don't document what code shows โ file structure, git history, variable names โ Claude can read these directly
How to Use
Create CLAUDE.md at project root โ write in stability order (project desc โ coding rules โ architecture โ current work)
When rules grow, split into topic-specific .md files in .claude/rules/
Set personal global rules in ~/.claude/CLAUDE.md (applies to all projects)
Always place frequently changing content (current tasks, temp notes) at the very bottom
Tags
Source
Anthropic Docs