Developer Productivity

How to Write a CLAUDE.md File That Actually Works

Best practices for structuring your Claude Code context file — from single projects to monorepos to API integrations. Based on official Anthropic docs, real-world usage, and lessons from our own codebase.

Nicolas
NicolasFounder & CEO
March 9, 202612 min read

Most CLAUDE.md files fall into one of two extremes: completely empty, or bloated with hundreds of lines that Claude never reads. After months of using Claude Code to build TurboDocx — across several repos spanning TypeScript backends, React frontends, a documentation site, open-source packages, and multiple SDKs — we've learned exactly what makes Claude Code productive and what wastes context tokens.

This guide distills the official Anthropic recommendations, insights from the developer community, and our hands-on experience into actionable best practices. Whether you're configuring Claude Code for a solo project or an enterprise monorepo with API integrations, you'll walk away knowing exactly what to put in your CLAUDE.md — and what to leave out.

The Golden Rule: Keep It Under 200 Lines

Anthropic's official best practices documentation is clear: your CLAUDE.md should be concise. The recommended target is under 200 lines. Some teams go as low as 60 lines. The absolute ceiling is roughly 300 lines before Claude starts losing signal in the noise.

Why? CLAUDE.md is loaded into context every single session. Every line consumes context window budget that could be spent on your actual code. Think of it like onboarding a new developer — you wouldn't hand them a 50-page document on their first day. You'd give them the essentials and point them to detailed docs when needed.

The Test for Every Line

Ask yourself: “If I remove this line, will Claude make mistakes?” If the answer is no, cut it. Every line must earn its place.

What Belongs (and What Doesn't)

The most common mistake is treating CLAUDE.md like documentation. It's not — it's a context injection file. Here's the definitive breakdown:

Include

ContentWhyExample
Project context (1-2 lines)Orients Claude immediately“SaaS document automation platform — Next.js, Express, MySQL”
Essential commandsClaude uses these exact stringsnpm run dev, npm run test, npm run build
Architecture / directory mapTells Claude where code livessrc/routes/ — API endpoints, src/models/ — DB models
Non-default conventionsOnly rules Claude wouldn't guess“Use Zustand stores, never Redux”
Common gotchasPrevents repeated mistakes“Always run sync-pricing before build”
Workflow rulesCan't be inferred from code“Branch naming: feat/JIRA-123-description”

Exclude

ContentWhy
Standard language conventionsClaude already knows TypeScript patterns
Detailed API documentationLink to docs instead — use @imports
Code style enforced by lintersDon't send an LLM to do a linter's job
File-by-file codebase descriptionsClaude can read files itself
“Write clean code” instructionsSelf-evident — wastes tokens
Frequently changing informationGoes stale, causes confusion

The WHY / WHAT / HOW Structure

The most effective CLAUDE.md files follow a clear information hierarchy. We call it the WHY/WHAT/HOW pattern — it mirrors how you'd brief a senior engineer joining your team:

# Project Name (WHAT — 1-2 lines)
SaaS document automation platform. TypeScript monorepo:
backend (Express), frontend (React/Gatsby), docs (Docusaurus).

## Commands (HOW — essential commands only)
```bash
npm run dev       # Start dev server
npm run test      # Run Jest tests
npm run build     # Production build
```

## Project Structure (WHAT — directory map)
```
src/routes/       # API endpoints
src/models/       # DB models (Objection.js)
src/handlers/     # Business logic
```

## Conventions (HOW — only non-default patterns)
- Use Zustand for state management, never Redux
- All API responses use `{ success, data, error }` shape
- Database migrations go in src/database/migrations/

## Reference Documents (progressive disclosure)
### API Architecture — `@docs/api-architecture.md`
**Read when:** Adding or modifying API endpoints

Notice the pattern: context first, then commands, then structure, then conventions, then references. Each section is scannable — Claude can extract exactly what it needs without reading every word. This is the same approach we use for our TurboDocx API & SDK projects where Claude Code helps us build and maintain the API layer.

Progressive Disclosure: @imports, Rules, and Skills

The single most powerful CLAUDE.md technique is progressive disclosure — keeping task-specific knowledge out of CLAUDE.md and loading it only when needed. Claude Code gives you several mechanisms for this:

@imports

Use @path/to/file.md syntax in CLAUDE.md to pull in external files on demand. Supports recursive imports up to 5 levels deep.

Best for: READMEs, detailed SOPs, API architecture docs

.claude/rules/

Markdown files in .claude/rules/ are auto-loaded alongside CLAUDE.md with the same priority.

Best for: Team-wide coding rules, review checklists

.claude/skills/

Skill files are loaded on demand based on relevance. They have metadata (name, description) for discovery without loading the full content.

Best for: Domain knowledge, specialized workflows, complex procedures

docs/ Directory

Reference detailed SOPs, architecture docs, and guides in CLAUDE.md with a “Read when” trigger so Claude knows when to look.

Best for: Long-form documentation, style guides, migration playbooks

## Reference Documents
### Content SEO SOP — `@docs/CONTENT-SEO-SOP.md`
**Read when:** Creating or editing ANY content page
Covers metadata requirements, JSON-LD schemas, internal linking minimums

### API Architecture — `@docs/api-architecture.md`
**Read when:** Adding or modifying API endpoints
Defines route patterns, request/response shapes, auth middleware

This pattern tells Claude when to read each document and what it contains, without loading everything into context every session. At TurboDocx, our root CLAUDE.md references six detailed SOPs — content creation, SEO architecture, deployment, and more — but Claude only loads them when the task requires it. It's the same principle behind how we manage our open-source projects: keep the surface area small, go deep only when needed.

CLAUDE.md for API Integration Projects

If you're building an application that integrates with external APIs — whether it's adding document signing via JavaScript, connecting to payment processors, or consuming AI services — your CLAUDE.md needs specific API context:

## API Integrations
- TurboDocx API: REST, Bearer token auth, base URL in TURBODOCX_API_URL
  - SDK: @turbodocx/sdk (TypeScript) — always prefer SDK over raw fetch
  - Docs: https://docs.turbodocx.com/API
  - Webhooks: POST to /webhooks/turbodocx, verify X-Signature header

## Error Handling Conventions
- All external API calls wrapped in try/catch
- Retry transient errors (429, 503) with exponential backoff
- Log API errors to structured logger, never swallow silently
- Return standardized error shape: { code, message, retryable }

The key insight: document the conventions around API usage, not the API itself. Claude can read API docs — what it can't infer is your team's patterns for authentication, error handling, retry logic, and webhook processing. For a deeper dive into API integration patterns, see our API integration best practices guide.

If you're working with the TurboDocx API for document generation, your CLAUDE.md might reference the SDK package, the base URL environment variable, and your webhook verification pattern. The full API reference stays in external docs — CLAUDE.md just points Claude in the right direction.

File Placement and Hierarchy

Claude Code loads CLAUDE.md files from multiple locations with a clear hierarchy. Understanding this lets you layer context appropriately:

LocationScopeCommit?
~/.claude/CLAUDE.mdAll sessions, all projectsNo (personal)
./CLAUDE.mdProject rootYes
./CLAUDE.local.mdPersonal project overridesNo (.gitignore)
parent/CLAUDE.mdInherited by child directoriesYes
child/CLAUDE.mdLoaded when working in that dirYes
.claude/rules/*.mdAuto-loaded alongside CLAUDE.mdYes

In a monorepo, put shared conventions in the root CLAUDE.md and service-specific context in child directories. For example, our TurboDocx backend has its own CLAUDE.md covering database migrations and API patterns, while the frontend directory has one covering component conventions and state management.

Maintaining Your CLAUDE.md

A CLAUDE.md file isn't set-and-forget. Treat it like code: review it when things go wrong, prune regularly, and commit changes to version control.

  • Run /insights weekly to surface patterns from your sessions and add rules
  • If Claude repeatedly ignores a rule, the file is too long — prune it
  • If Claude asks questions already answered in CLAUDE.md, the phrasing is ambiguous — rewrite it
  • Use IMPORTANT or YOU MUST prefixes for critical rules to improve adherence
  • When Claude makes a mistake, tell it to update the CLAUDE.md so it doesn't repeat the error

That last point is the most underrated trick. When Claude gets something wrong — uses the wrong import path, breaks a naming convention, misunderstands your project structure — don't just fix it and move on. Tell Claude to add the correction to CLAUDE.md itself. Over time, your CLAUDE.md becomes a living record of your codebase's quirks and preferences, written by the same tool that consumes it. Claude effectively becomes self-aware of your patterns, your style, and the traps it fell into before. The file evolves from a static brief into a feedback loop.

Quick Reference Checklist

Do

  • Keep under 200 lines
  • Start with 1-2 line project context
  • List essential commands (dev, test, build)
  • Document non-default conventions only
  • Use @imports for detailed docs
  • Include a directory structure overview
  • Add "Read when" triggers for referenced docs
  • Use IMPORTANT for critical rules
  • Review and prune regularly

Don't

  • Exceed 300 lines total
  • Duplicate what linters enforce
  • Include full API documentation
  • Write file-by-file descriptions
  • Add obvious instructions ("write clean code")
  • Store frequently changing information
  • Put task-specific instructions in CLAUDE.md
  • Forget to commit changes to git

Starter Template

# Project Name

One-line description of what this project does and its tech stack.

## Commands
```bash
npm run dev       # Development server
npm run test      # Run tests
npm run build     # Production build
npm run lint      # Lint code
```

## Project Structure
```
src/              # Source code
  routes/         # API routes
  models/         # Data models
  handlers/       # Business logic
tests/            # Test files
docs/             # Documentation
```

## Conventions
- [List only non-obvious, non-default patterns]
- [Things Claude would get wrong without being told]

## Reference Documents
### [Doc Name] — `@docs/filename.md`
**Read when:** [trigger condition]
[2-3 line summary]

Related Resources

Build Faster with Claude Code + TurboDocx API

Use Claude Code with a well-crafted CLAUDE.md to integrate document generation and e-signatures into your application in minutes, not days.

Nicolas
NicolasFounder & CEO