Claude Code/
Lesson

A good CLAUDE.md is like a great onboarding document: it tells the new team member exactly what they need to know, nothing more. Here is a systematic approach to writing one.

What to include

1. Tech stack summary

Tell Claude what technologies you use and how they fit together.

markdown
## Stack
- Runtime: Node.js 20 with TypeScript 5.4
- Framework: Astro 5 with React 18 for interactive components
- Styling: TailwindCSS 3.4 with custom design tokens
- Database: Cloudflare D1 (SQLite) via Drizzle ORM
- Deployment: Cloudflare Pages (frontend) + Workers (API)

This prevents Claude from suggesting incompatible libraries or outdated patterns.

2. Commands

List the commands Claude needs to run. This is one of the most valuable sections because Claude can execute these directly.

markdown
## Commands
| Command | Purpose |
|---------|---------|
| npm start | Start dev server + API concurrently |
| npm test | Run Vitest test suite |
| npm run lint | ESLint + Prettier check |
| npm run db:migrate | Apply database migrations |
| npm run build | Production build |

3. Coding conventions

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

markdown
## Conventions
- Use functional components with hooks, never class components
- All API responses follow { data, error, meta } envelope format
- File names use kebab-case: user-profile.tsx, not UserProfile.tsx
- Database queries go through the ORM, never write raw SQL
- All user-facing strings must use the i18n translation function t()

4. Project structure overview

Help Claude understand where things live.

markdown
## Structure
- src/pages/ - Astro page routes (.astro files)
- src/components/ - React components (interactive, use client:load)
- src/services/api.ts - API client, all backend calls go through here
- worker/ - Cloudflare Worker backend (separate from frontend)
- migrations/ - SQL migration files for D1

5. Do/Don't rules

These are the most powerful lines in your CLAUDE.md. When Claude makes a specific mistake, add a rule to prevent it.

Good to know
Do/Don't rules are the highest-ROI lines in your CLAUDE.md. Each one prevents a specific mistake that AI would otherwise repeat in every conversation. If you only have time to write five lines, make them all Do/Don't rules.
markdown
## Rules
- DO: Use Zod for all input validation
- DO: Add error handling to every async function
- DON'T: Use any - always provide explicit TypeScript types
- DON'T: Import from relative paths across module boundaries, use path aliases
- DON'T: Add console.log statements, use the logger utility
02

What to skip

Do not explain technologies Claude already knows

Claude knows what JavaScript is. Claude knows how React hooks work. Claude knows SQLWhat is sql?A language for querying and managing data in relational databases, letting you insert, read, update, and delete rows across tables. syntax. Explaining these basics wastes context tokens.

markdown
# BAD - wastes tokens
JavaScript is a programming language that runs in browsers.
React uses a virtual DOM to efficiently update the UI.

# GOOD - project-specific info only
We use React 18 with the new use() hook for data fetching.

Do not duplicate your README

Your README explains the project to humans. CLAUDE.md gives Claude actionable instructions. They serve different purposes.

Do not include information that changes frequently

If something changes every week, it will be outdated in your CLAUDE.md more often than it is current. Focus on stable conventions.

AI pitfall
AI tends to generate code using the most popular library for a task, not the one your project actually uses. Without a CLAUDE.md, ask Claude to add form validation and it'll probably reach for Yup or Joi, even if your project uses Zod. The tech stack section prevents this.

Never include secrets

CLAUDE.md is committed to version control. APIWhat is api?A set of rules that lets one program talk to another, usually over the internet, by sending requests and getting responses. keys, database passwords, and tokens do not belong here. Use environment variables and reference them by name.

markdown
# BAD
API_KEY=sk-abc123def456

# GOOD
API keys are in .env (not committed). Required vars: API_KEY, DB_URL, REDIS_URL
03

The feedback loop

The most effective CLAUDE.md files are built iteratively, not written in one sitting. Here is the process:

  1. Start with the basics. Tech stack, commands, and a few conventions.
  2. Work with Claude normally. Notice when it makes mistakes or asks questions it should not need to ask.
  3. Add a rule. Every time Claude gets something wrong, add a specific instruction to CLAUDE.md.
  4. Test. Start a fresh conversation and verify Claude follows the new rule.
  5. Repeat. Over days and weeks, your CLAUDE.md becomes increasingly precise.

This iterative approach works because you discover the rules you actually need through real usage, not by guessing upfront.

CLAUDE.md sectionWhat to writeExample
StackTechnologies and versions"React 18, TypeScript 5.4, Vite, Vitest"
CommandsWhat Claude needs to run"npm test, run Vitest"
ConventionsSpecific, actionable rules"Named exports only, never default"
StructureWhere things live"src/services/, business logic"
Do/Don'tMistake prevention"DON'T use any, use unknown"
Edge case
If your CLAUDE.md contradicts your codebase (e.g., it says "use Vitest" but your package.json has Jest), Claude will follow the CLAUDE.md instruction and generate incorrect code. Keep your CLAUDE.md in sync with reality.
04

Testing your CLAUDE.md

The simplest test: start a brand new Claude conversation in your project and give it a task without any extra context. Does it follow your conventions? Does it run the right commands? Does it put files in the right places?

If Claude makes a mistake that your CLAUDE.md should have prevented, either:

  • The instruction is missing, add it
  • The instruction is vague, make it more specific
  • The instruction is buried, move it higher in the file