The Complete CLAUDE.md Guide: Write Better Claude Code Configurations in 2026
TL;DR: Your CLAUDE.md file is the single most impactful configuration in Claude Code. A well-written one under 300 lines can double output quality by providing project context, coding conventions, and critical rules. This guide covers the /init command, template structure, path-scoped rules, and local overrides โ with a full template you can copy.
Why CLAUDE.md Matters
Claude Code is a blank slate at the start of every session. It has no memory of your project's architecture, your coding conventions, or your team's preferred patterns. The CLAUDE.md file is the bridge โ it's loaded at the beginning of every conversation, providing persistent context that Claude cannot infer from code alone.
A project without a CLAUDE.md will get correct code. A project with a well-written CLAUDE.md will get code that looks like your code โ matching your conventions, avoiding your known pitfalls, and building on your architectural decisions rather than guessing them.
Generating Your First CLAUDE.md: The /init Command
The fastest way to start is with the built-in /init command:
class="language-bash"># From your project root, inside Claude Code
/init
Claude analyzes your codebase โ detecting your build system, test framework, languages, and project structure โ and generates an initial CLAUDE.md. This provides a solid starting point that you can then refine with project-specific rules and conventions.
The Template
Below is the structure we recommend for any CLAUDE.md file. Keep it under 300 lines to minimize token overhead while maximizing useful context.
class="language-markdown"># Project: [Project Name]
## Critical Rules
- [Non-negotiable rules that must always be followed]
- Example: All new features must include both unit and integration tests
- Example: API responses must follow JSend specification
## Project Context
- Type and goals: [One-line description of the project's purpose]
- Tech stack:
- Frontend: React 18, TypeScript, Tailwind CSS, Next.js
- Backend: Node.js 20, Express, PostgreSQL, Prisma
- Testing: Jest, React Testing Library, Playwright
- Architecture: [High-level architectural decisions and folder organization]
## Coding Conventions
- Code style: ESLint and Prettier rules enforced
- Naming: camelCase for variables/functions, PascalCase for components
- Components: Functional React components only, prefer server components
- Error handling: try-catch for async operations, log to Sentry
- Comments: JSDoc for all public functions and components
## UI and Design
- Design system: Follow ShadCN patterns, use tokens from styles/tokens.ts
- Accessibility: WCAG 2.1 AA standards for all new UI
## Testing
- Strategy: Unit tests for logic, integration for components, E2E for critical flows
- Commands: npm run test, npm run test:e2e
## Build and Deploy
- Build: npm run build
- Deployment: [Brief deployment process description]
## Key Commands
- npm run dev (local dev server)
- npm run lint (run linter)
- Git: Feature branches from main, PRs required for merging
Advanced Configuration: Path-Scoped Rules
For larger projects, a single CLAUDE.md can become bloated. Claude Code supports path-scoped rules โ separate rule files in .claude/rules/ that only load when editing matching files:
class="language-bash">.claude/
โโโ rules/
โ โโโ python.md # Loads when editing .py files
โ โโโ frontend.md # Loads when editing React/TypeScript
โ โโโ database.md # Loads when editing SQL/migrations
โโโ skip.txt # Files to never edit (.env, secrets)
Path-scoped rules are powerful because they cost zero tokens when out of scope. Your global CLAUDE.md stays lean, while specialized knowledge loads exactly when needed.
Local Overrides: CLAUDE.local.md
For personal preferences that shouldn't be in version control โ editor settings, personal shortcuts, local paths โ use CLAUDE.local.md:
class="language-markdown"># CLAUDE.local.md โ Local overrides (gitignored)
## Personal Preferences
- Always generate verbose logging for the auth module
- Skip the slow integration tests locally (CI handles them)
- Use localhost:3000 for frontend dev server
Add CLAUDE.local.md to your .gitignore. It loads alongside your main CLAUDE.md without polluting the team's shared configuration.
Common Mistakes
- Too long. A 800-line CLAUDE.md costs $0.02-0.05 per turn in baseline tokens. Over a month of heavy use, that's real money. Stay under 300 lines.
- Outdated rules. CLAUDE.md is a living document. When you change your test framework or lint rules, update the file. Stale rules are worse than no rules.
- Stating the obvious. You don't need to tell Claude "use consistent indentation" or "write clean code" โ it already knows. Focus on project-specific rules that it can't infer from the codebase.
- No critical rules section. Claude pays special attention to the first section. Lead with your non-negotiable rules, not your project description.
- Ignoring /init. Even if you plan to rewrite the entire file, run
/initfirst. It often catches patterns you wouldn't think to document.
The Impact
On toolbrain.net's repository, a 250-line CLAUDE.md reduced irrelevant code generation by roughly 40% โ Claude stopped suggesting React for Python tasks, stopped writing tests in Jest when the project uses Vitest, and stopped asking about deployment steps that the CI handles automatically.
The return on investment is extraordinary: one hour of writing a good CLAUDE.md saves days of correcting Claude's assumptions over the life of a project.
For more Claude Code optimization tips, see our cost optimization guide. For comparisons with other coding agents, check the OpenCode review.
Frequently Asked Questions
Where do I put CLAUDE.md?
In your project's root directory. Claude Code loads it automatically when you start a session in that directory or any subdirectory.
Can I have multiple CLAUDE.md files?
Yes. Claude loads CLAUDE.md from parent and child directories hierarchically. Use path-scoped rules in .claude/rules/ for fine-grained control over which rules apply to which files.
Does CLAUDE.md affect API costs?
Yes. Every line adds to the prompt tokens at the start of each turn. A 300-line file adds roughly 2,000 tokens per turn. Over 500 turns/month, that's 1M tokens in overhead โ about $3-5 depending on the model. Keep it lean.
How often should I update CLAUDE.md?
Update it when your tech stack changes, when you discover a new "gotcha" in a code review, or when you change workflows. Review it quarterly for outdated rules.
โ Back to all posts