Prompting Claude Code: CLAUDE.md Patterns & Project Instructions

Master CLAUDE.md for Claude Code. Learn the patterns Claude Code actually follows for project-level instructions, coding conventions, architectural constraints, and workflow preferences.

January 14, 2026
Claude CodeCLAUDE.mdAgentic CodingProject ConfigurationAnthropic

Claude Code reads CLAUDE.md at the root of your project and uses it as an instruction set for the entire session. This file is the single highest-leverage thing you can write for Claude Code — a good CLAUDE.md makes Claude Code a reliable teammate; a bad one makes it a liability.

Unlike API system prompts, CLAUDE.md needs to work in a terminal environment where Claude Code reads files, runs commands, and modifies code. The patterns below are tested against real Claude Code usage.

CLAUDE.md Structure

A production CLAUDE.md has five sections:

# Project Name

Brief one-line description of what this project is.

## Commands

| Command | What it does |
|---|---|
| `npm run dev` | Start dev server |
| `npm run lint` | ESLint + Prettier |
| `npm run test` | Vitest |

## Architecture

[High-level architecture: folder structure, data flow, key design decisions]

## Conventions

[Coding conventions, naming patterns, file organization, preferred libraries]

## Constraints

[Hard rules: what NOT to do, deployment restrictions, API limitations]

The Commands Section

Claude Code will run terminal commands. Tell it which ones are safe and what they do:

## Commands

| Command | What it does | Safe to run? |
|---|---|---|
| `npm run dev` | Start dev server on port 3000 | Yes |
| `npm run lint` | ESLint check | Yes |
| `npm run test` | Run test suite | Yes — never skip |
| `npm run build` | Production build | Yes |
| `npm run db:migrate` | Run database migrations | ASK FIRST — affects production schema |
| `npm run deploy` | Deploy to production | NEVER run without explicit instruction |

The Architecture Section

Give Claude Code a mental model of the codebase:

## Architecture

- **Next.js 14 App Router** — pages in `app/`, API routes in `app/api/`
- **Prisma ORM** — schema in `prisma/schema.prisma`, migrations in `prisma/migrations/`
- **tRPC** — API layer: routers in `server/api/routers/`, procedures use Zod validation
- **Auth** — NextAuth.js with GitHub + Google providers, session in `server/auth.ts`
- **State** — React Query for server state, Zustand for client state
- **Styling** — Tailwind CSS, component library in `components/ui/`

Data flow: `Page → tRPC query → Prisma → PostgreSQL`
Mutation flow: `Form → tRPC mutation → Zod validation → Prisma → PostgreSQL → React Query invalidation`

The Conventions Section

This is where you encode tribal knowledge:

## Conventions

### Code style
- Use `const` over `let`; avoid `var`
- Prefer arrow functions for callbacks, named functions for components
- Early returns over nested ifs
- No `any` types — use `unknown` and type guards

### File naming
- Components: `kebab-case.tsx` (e.g., `user-profile.tsx`)
- Utilities: `camelCase.ts` (e.g., `formatDate.ts`)
- Types: `PascalCase.ts` (e.g., `UserTypes.ts`)

### Component patterns
- "use client" directive at top of file when needed
- Props interface exported, named `{ComponentName}Props`
- Use `cn()` utility for conditional classes

### Git
- Branch naming: `feature/description`, `fix/description`, `chore/description`
- Commit messages: conventional commits (`feat:`, `fix:`, `chore:`)
- NEVER commit `.env` files or secrets

The Constraints Section

Hard rules Claude Code must follow:

## Constraints

### NEVER
- Use `any` as a TypeScript type — use `unknown` and type guards
- Import from `@/lib/internal` — this module is deprecated
- Modify `prisma/schema.prisma` without asking — requires migration
- Add new npm dependencies without listing them first for approval
- Commit directly to `main` — always create a branch
- Use `console.log` in production code — use the logger utility

### ALWAYS
- Run `npm run lint` after making changes
- Run `npm run typecheck` if TypeScript files were modified
- Write tests for new features (files in `__tests__/` next to the source)
- Update the PR description if scope changes mid-implementation

### ENVIRONMENT
- Node 20, npm 10
- PostgreSQL 16
- Redis 7 for caching and session store

CLAUDE.md Patterns That Work

Pattern: Give Explicit Decision Criteria

Vague: "Use good error handling."

Effective:

Error handling patterns:
- API routes: return `{ error: string }` with appropriate HTTP status
- tRPC procedures: throw `TRPCError` with code and message
- Client components: use error boundaries; show toast for mutations
- Never swallow errors silently — always log with `logger.error()`

Pattern: Show, Don't Just Tell

Vague: "Write tests."

Effective:

Test patterns (Vitest):
- Unit tests: `describe('functionName', () => { it('behavior', () => { ... }) })`
- Component tests: use `@testing-library/react`, test behavior not implementation
- API tests: use `testcontainers` for Postgres; clean up after each test suite
- Naming: `it('returns X when given Y')` not `it('should work')`

Pattern: Define the Review Checklist

Before considering a task complete, verify:
- [ ] `npm run lint` passes
- [ ] `npm run typecheck` passes
- [ ] `npm run test` passes
- [ ] New code follows existing patterns (check neighboring files)
- [ ] No commented-out code left behind
- [ ] No `console.log` or `debugger` statements
- [ ] Git diff reviewed for accidental changes

Common CLAUDE.md Mistakes

Note:

Mistake: CLAUDE.md as autobiography. Don't write the story of your project. "We started this project in 2022 because..." — Claude Code doesn't need project history. It needs commands, architecture, conventions, constraints.

Note:

Mistake: Contradictory rules. "Always add JSDoc comments" + "Keep files under 200 lines." Pick one or give priority: "Prefer brevity over comments when they conflict."

Note:

Mistake: Outdated CLAUDE.md. The most harmful CLAUDE.md is a stale one. If you rename the lib/ folder to utils/ but CLAUDE.md still says @/lib/, Claude Code will confidently use the wrong path. Review CLAUDE.md monthly.

Note:

Pro Move: Include a CLAUDE.md in every significant subdirectory (monorepo, multi-package projects). Claude Code reads the nearest CLAUDE.md, so packages/api/CLAUDE.md can specify API-specific conventions while root CLAUDE.md covers shared rules.

  • Autonomous Coding Strategies — Once your CLAUDE.md is solid, learn to give Claude Code the right level of autonomy with guardrails and task decomposition.