Project Initialization: CLAUDE.md and Project Context
You ask Claude to add a new API endpoint and it generates Express-style code — but your project uses Hono. You ask it to write tests and it reaches for Jest, when your repo uses Vitest with a custom configuration. Every session starts with you correcting Claude about your stack, your conventions, and your file structure. A well-crafted CLAUDE.md file eliminates this pattern entirely.
This guide shows you how to create and maintain the memory files that teach Claude about your specific project, so it writes code that fits your codebase from the first prompt.
What You’ll Walk Away With
Section titled “What You’ll Walk Away With”- A
CLAUDE.mdfile tailored to your project’s architecture and conventions - Modular rules in
.claude/rules/for topic-specific instructions - Understanding of the full memory hierarchy (managed, user, project, local)
- Auto memory configured to learn your preferences over time
The Memory Hierarchy
Section titled “The Memory Hierarchy”Claude Code loads instructions from multiple locations. Understanding the hierarchy helps you put the right instructions in the right place.
| Memory Type | Location | Who Sees It | Loaded When |
|---|---|---|---|
| Managed policy | /Library/Application Support/ClaudeCode/CLAUDE.md | All users | Always |
| User memory | ~/.claude/CLAUDE.md | Just you, all projects | Always |
| Project memory | ./CLAUDE.md or ./.claude/CLAUDE.md | All team members | Always (root) |
| Project rules | ./.claude/rules/*.md | All team members | Always |
| Local memory | ./CLAUDE.local.md | Just you, this project | Always |
| Auto memory | ~/.claude/projects/<project>/memory/ | Just you, per project | First 200 lines |
| Child CLAUDE.md | ./subdir/CLAUDE.md | All team members | On demand |
More specific instructions take precedence over broader ones. A project-level rule overrides a user-level preference. CLAUDE.local.md is automatically gitignored, making it safe for personal preferences.
Bootstrap with /init
Section titled “Bootstrap with /init”The fastest way to start is the /init command inside a Claude Code session:
cd /path/to/your/projectclaudeThen inside the session:
/initClaude will analyze your project structure, detect your tech stack, and generate a CLAUDE.md with:
- Key commands (build, test, lint, format)
- Detected technologies and frameworks
- Directory structure overview
- Basic coding conventions
Anatomy of a Great CLAUDE.md
Section titled “Anatomy of a Great CLAUDE.md”Here is a production-quality template. Copy the structure and fill in your project specifics:
# Project: [Your Project Name]
## Key Commands- `npm run dev` - Start development server (port 3000)- `npm run build` - Production build- `npm run test` - Run Vitest tests- `npm run test:watch` - Watch mode- `npm run lint` - ESLint check- `npm run format` - Format with Prettier- `npm run type-check` - TypeScript checking
## Architecture- **Framework**: Next.js 14 with App Router- **Language**: TypeScript (strict mode)- **Styling**: Tailwind CSS with shadcn/ui components- **Database**: PostgreSQL with Drizzle ORM- **Auth**: NextAuth.js v5 with Google/GitHub providers- **Testing**: Vitest + React Testing Library
## Coding Conventions- Use functional components with TypeScript interfaces (not types) for props- Prefer named exports over default exports- Use `async/await` over `.then()` chains- Error handling: always catch and log, never silently swallow errors- Imports: external deps first, then internal absolute (`~/`), then relative
## File Organization- `src/app/` - Next.js App Router pages and layouts- `src/components/` - Reusable UI components- `src/lib/` - Utility functions and shared logic- `src/lib/db/` - Database schema and queries (Drizzle)- `src/server/` - Server-only code (API handlers, auth)
## Important Patterns- All API routes return `NextResponse.json()` with explicit status codes- Database queries go through `src/lib/db/queries.ts`, not inline- Components follow the pattern: interfaces first, hooks, handlers, render- Never use `any` - use `unknown` and narrow the typeModular Rules with .claude/rules/
Section titled “Modular Rules with .claude/rules/”For larger projects, splitting instructions into focused files is cleaner than one massive CLAUDE.md. Place markdown files in .claude/rules/:
your-project/├── .claude/│ ├── CLAUDE.md # Main project instructions│ └── rules/│ ├── code-style.md # Code style guidelines│ ├── testing.md # Testing conventions│ ├── api-design.md # API endpoint patterns│ └── security.md # Security requirementsAll .md files in .claude/rules/ are automatically loaded as project memory.
Path-Specific Rules
Section titled “Path-Specific Rules”Rules can be scoped to specific files using YAML frontmatter:
---paths: - "src/api/**/*.ts" - "src/server/**/*.ts"---
# API Development Rules
- All API endpoints must validate input with Zod schemas- Return standardized error responses with { error: string, code: number }- Include rate limiting middleware on public endpoints- Log all 5xx errors to our monitoring serviceRules without a paths field apply to all files. Rules with paths only load when Claude works with matching files.
Organizing Rules by Concern
Section titled “Organizing Rules by Concern”.claude/rules/├── frontend/│ ├── react.md # React component patterns│ └── styling.md # Tailwind conventions├── backend/│ ├── api.md # API design patterns│ └── database.md # Query patterns└── general.md # Universal rulesAll .md files are discovered recursively, so subdirectories work fine.
CLAUDE.md Imports
Section titled “CLAUDE.md Imports”You can import external files into your CLAUDE.md using @path/to/file syntax:
See @README.md for project overview and @package.json for available commands.
# Additional References- API docs: @docs/api-reference.md- Git workflow: @docs/git-workflow.mdBoth relative and absolute paths work. Relative paths resolve from the file containing the import. Imports can be nested up to 5 levels deep.
User-Level Memory
Section titled “User-Level Memory”For preferences that apply across all your projects, edit ~/.claude/CLAUDE.md:
# Personal Preferences
- I prefer detailed explanations with code examples- Always show the full file path when creating new files- When writing tests, include both happy path and error cases- Use British English in comments and documentationYou can also create user-level rules in ~/.claude/rules/:
~/.claude/rules/├── preferences.md # Your coding preferences└── workflows.md # Your preferred workflowsAuto Memory
Section titled “Auto Memory”Claude Code can automatically save learnings across sessions. Auto memory records project patterns, debugging insights, and your preferences without you manually editing files.
To opt in (if not yet enabled by default):
export CLAUDE_CODE_DISABLE_AUTO_MEMORY=0Auto memory is stored at ~/.claude/projects/<project>/memory/MEMORY.md. The first 200 lines are loaded into every session. Detailed notes go into topic files that Claude reads on demand.
You can ask Claude to remember things explicitly:
Remember that we use pnpm, not npm, in this project.Save to memory that the API tests require a local Redis instance running on port 6379.Use /memory to open the memory file selector and edit any memory file directly.
CLAUDE.local.md for Personal Overrides
Section titled “CLAUDE.local.md for Personal Overrides”For project-specific preferences that should not be committed to git, use CLAUDE.local.md in the project root:
# My local preferences
- My local API runs on port 8080, not the default 3000- Use my test database: postgresql://localhost:5433/myapp_test- When running E2E tests, use --headed flag so I can watchThis file is automatically gitignored. It is loaded alongside the project CLAUDE.md but only affects your local sessions.
When This Breaks
Section titled “When This Breaks”Claude ignores your CLAUDE.md instructions — Make sure the file is in the project root or at .claude/CLAUDE.md. Claude loads memory files relative to the directory where you started the session. If you run claude from a subdirectory, it walks up the tree but your file must be discoverable.
Instructions conflict between files — More specific scopes win. Project rules override user rules, and local rules override project rules. If you have conflicting instructions, the closest scope to the working directory takes precedence.
CLAUDE.md is too long and Claude seems to lose track — Keep the main CLAUDE.md concise (under 200 lines) with the most critical information. Move detailed guidelines into .claude/rules/ files. Claude loads rules files alongside the main memory but can manage more content when it is organized by topic.
Auto memory files getting stale — Use /memory to open and edit memory files. Delete outdated entries. Claude may also update memory automatically as patterns change.
What’s Next
Section titled “What’s Next”With your project context established, learn how to turn product requirements into structured implementation plans.