claude-codeclaude-mdai-codingguidebest-practices

How to Write an Effective CLAUDE.md File: The Complete Guide

Learn how to write an effective CLAUDE.md file for Claude Code. Examples, templates, and best practices for AI-assisted development.

Hel Rabelo-2026-02-11-12 min read

What is CLAUDE.md?

CLAUDE.md is the instruction file that Claude Code reads to understand your project. Think of it as onboarding documentation for your AI coding assistant. When Claude Code starts a session, it reads your CLAUDE.md files to learn about your codebase, your preferences, and the rules it should follow.

Without a CLAUDE.md, Claude Code starts every session from scratch. It does not know your framework conventions, your team's coding standards, or the sharp edges in your codebase. With a well-written CLAUDE.md, it picks up where your documentation leaves off and writes code that fits your project from the first prompt.

Where CLAUDE.md Lives: The 5-Layer Hierarchy

Claude Code reads CLAUDE.md files from multiple locations, each with a different scope. Understanding this hierarchy is essential for writing effective instructions.

Layer 1: Global (~/.claude/CLAUDE.md)

Your global CLAUDE.md applies to every project Claude Code works on. This is where you put your personal preferences, universal rules, and identity information.

What to include:

  • Your role and professional context
  • Universal coding style preferences (tabs vs spaces, semicolons, etc.)
  • Tools and frameworks you always use
  • Things Claude should never do across any project
  • Example:

  • Prefer TypeScript over JavaScript in all new files
  • Use conventional commits: feat:, fix:, chore:, docs:
  • Never commit .env files or secrets
  • Always check if node_modules exists before suggesting npm install
  • Layer 2: Workspace (parent directory CLAUDE.md)

    If you organize projects under a common parent directory, a CLAUDE.md there applies to all child projects. This is useful for monorepos or workspaces where multiple projects share conventions.

    Use case: You have a ~/code/work/ directory with multiple client projects that all use the same tech stack. A CLAUDE.md at ~/code/work/ can define the shared conventions.

    Layer 3: Project Root (repo CLAUDE.md)

    The most common location. A CLAUDE.md at the root of your repository defines project-specific instructions. This is checked into version control and shared with your team.

    What to include:

  • Project architecture overview
  • Build and test commands
  • Directory structure
  • Key dependencies and their versions
  • Known gotchas and workarounds
  • PR and commit conventions
  • Layer 4: Per-Directory Overrides

    You can place CLAUDE.md files in subdirectories to override or supplement project-level instructions for specific parts of the codebase.

    Use case: Your src/api/ directory has different conventions than src/frontend/. A CLAUDE.md in each directory can specify the relevant patterns.

    Layer 5: Memory Files (~/.claude/projects/)

    Claude Code also reads memory files that persist across sessions. These are typically auto-generated but can be manually edited. They store patterns Claude has learned about your workflow.

    What to Include: The Essential Sections

    A good CLAUDE.md balances completeness with conciseness. Every token in your CLAUDE.md counts against your context window, so include what matters and skip what doesn't.

    Project Architecture

    Give Claude the 30-second elevator pitch of your project. What is it? What technology does it use? How is it structured?

    Example:

  • This is a Next.js 14 App Router project with Tailwind CSS and Supabase
  • The app is a SaaS dashboard for managing subscriptions
  • API routes live in src/app/api/
  • Database queries use Supabase client in src/lib/supabase.ts
  • Authentication uses Supabase Auth with middleware in middleware.ts
  • Build and Test Commands

    Claude needs to know how to validate its work. List the exact commands.

    Example:

  • Build: npm run build
  • Type check: npx tsc --noEmit
  • Test: npm run test
  • Lint: npm run lint
  • Dev server: npm run dev
  • Coding Conventions

    Be specific. "Write clean code" is useless. "Use named exports, not default exports" is actionable.

    Examples of good conventions:

  • Use named exports for all components
  • Prefix custom hooks with use
  • Always handle loading and error states in data fetching components
  • Use Zod for runtime validation of API inputs
  • Prefer server components; use 'use client' only when necessary
  • Name files in kebab-case, components in PascalCase
  • Known Gotchas

    Every codebase has sharp edges. Document them so Claude does not cut itself.

    Examples:

  • The Supabase client in src/lib/supabase.ts must not be imported in server components; use src/lib/supabase-server.ts instead
  • The pricing page uses a custom Stripe checkout flow; do not use the default Stripe checkout session
  • The database has a legacy users_v1 table that is still referenced by the mobile app; do not drop or rename it
  • Safety Rules

    Define what Claude should never do. These are your guardrails.

    Examples:

  • Never run git push without explicit permission
  • Never delete files without moving them to /tmp first
  • Never modify database migration files that have already been applied
  • Never commit changes to .env or files containing API keys
  • Always run the build command before committing
  • Common Mistakes

    Too Vague

    A CLAUDE.md that says "write good code" or "follow best practices" provides no value. Claude already tries to write good code. Your CLAUDE.md should tell it what "good" means in your specific context.

    Bad: Use best practices for error handling

    Good: Wrap all async operations in try/catch. Log errors to src/lib/logger.ts. Return structured error responses with { error: string, code: number } shape.

    Too Long

    Every line in your CLAUDE.md consumes tokens from your context window. A 5,000-word CLAUDE.md eats into the space available for your actual conversation. Be concise.

    Rule of thumb: If your CLAUDE.md is longer than 200 lines, you are probably including information that belongs in your actual documentation, not in your AI instructions.

    Outdated Instructions

    A CLAUDE.md that references a framework version you upgraded three months ago or a directory structure that no longer exists causes Claude to write code that does not work. Review your CLAUDE.md when you make significant changes to your project.

    Missing Safety Rules

    The most common regret people have with Claude Code is not telling it what NOT to do. Add explicit safety rules about destructive operations, secret files, and irreversible actions.

    Templates for Different Project Types

    Next.js App Router

  • Framework: Next.js 14+ with App Router
  • Styling: Tailwind CSS
  • Data: Server components fetch data directly; client components use SWR or React Query
  • Build: npm run build
  • Key patterns: Use loading.tsx and error.tsx for each route segment. Prefer server components. Use route handlers in app/api/ instead of pages/api/.
  • Python / Django

  • Framework: Django 5+ with DRF
  • Database: PostgreSQL
  • Virtual env: Always activate .venv before running commands
  • Test: python manage.py test
  • Key patterns: Use class-based views for CRUD, function-based views for custom logic. Serializers validate all input. Migrations must be reversible.
  • React Native / Expo

  • Framework: Expo SDK 50+ with Expo Router
  • Navigation: File-based routing
  • Build: npx expo build
  • Key patterns: Use NativeWind for styling. Test on both iOS and Android simulators. Handle deep links in app/_layout.tsx.
  • Monorepo

  • Structure: Turborepo with apps/ and packages/
  • Build: turbo run build
  • Key patterns: Shared packages import from @repo/package-name. Each app has its own CLAUDE.md. Root CLAUDE.md defines workspace conventions.
  • Managing CLAUDE.md at Scale

    The real challenge with CLAUDE.md is not writing one file. It is maintaining context files across multiple projects, especially if you use multiple AI tools.

    If you work on 5+ projects and use Claude Code alongside Cursor or GitHub Copilot, you end up maintaining 10-15 config files across different formats. They drift out of sync. Some get stale. You lose track of which project has which instructions.

    This is exactly the problem [TokenCentric](https://tokencentric.app) solves. It scans all your projects, shows every AI context file in one dashboard, and lets you edit them with real-time token counting so you know exactly how much context space each file consumes.

    Conclusion

    A well-written CLAUDE.md is the single highest-leverage thing you can do to improve your Claude Code experience. It transforms Claude from a generic coding assistant into a teammate who understands your project.

    Start with the basics: architecture, commands, conventions, gotchas, and safety rules. Keep it concise. Review it regularly. And if you manage multiple projects, consider using [TokenCentric](https://tokencentric.app) to keep everything organized.

    Ready to try TokenCentric?

    Free, open-source AI config file manager for your projects.

    Download TokenCentric