Working Code
Create a CLAUDE.md file at your project root:
> Analyze this project and create a CLAUDE.md file.
> Include the project structure, tech stack, and development conventions.
Claude analyzes your project and generates CLAUDE.md. Now start a new session — Claude automatically reads this file:
claude
> How should I add a new API endpoint to this project?
Claude guides you according to the conventions defined in CLAUDE.md.
Try It Yourself
Open the CLAUDE.md that Claude generated and edit it yourself. A good CLAUDE.md structure:
# Project Name
## Tech Stack
- Framework: Next.js 14
- Language: TypeScript (strict mode)
- DB: PostgreSQL + Prisma
- Testing: Jest + React Testing Library
## Dev Commands
- `npm run dev` — development server
- `npm run build` — build
- `npm test` — run tests
- `npm run lint` — linting
## Coding Conventions
- Commit messages: Conventional Commits
- Function names: camelCase
- Components: PascalCase
- File names: kebab-case
- Error handling: use custom error classes (src/errors/)
## Project Structure
- src/app/ — page routing
- src/components/ — reusable components
- src/lib/ — utilities, DB client
- src/api/ — API route handlers
## Important
- Never commit .env files
- DB migrations must go through PR review first
- External API keys: environment variables only
Add your team's actual rules here.
"Why?" — CLAUDE.md Is AI's Memory
Claude Code starts fresh every session. It doesn't remember previous conversations. CLAUDE.md solves this. Since it's automatically read at session start, you don't need to repeat the same rules every time.
CLAUDE.md Hierarchy
| Location | Scope | Purpose |
| ----------------------------- | --------------- | ------------------------------------------------------ |
| ~/.claude/CLAUDE.md | All projects | Personal preferences (editor settings, language, etc.) |
| Project root /CLAUDE.md | Current project | Project rules, tech stack |
| Subdirectory /src/CLAUDE.md | That directory | Directory-specific rules |
Claude reads all CLAUDE.md files from the current working directory up to the root. More specific rules take priority over higher-level ones.
Principles for Good CLAUDE.md
- Be specific — instead of "write clean code," say "functions must not exceed 20 lines"
- Include examples — concrete code examples work better than abstract rules
- Keep it updated — update CLAUDE.md when the project changes
Deep Dive
What shouldn't go in CLAUDE.md?
Don't put secrets (API keys, passwords) in CLAUDE.md. This file is usually committed to Git. Keep secrets in .env files and just write "see .env.example for environment variable setup" in CLAUDE.md.
How is this different from the /init command?
/init is a command that has Claude analyze your project and auto-generate CLAUDE.md. It's useful for getting started, but you should always review and edit the generated content. Auto-generation is a starting point, not the final result.
- If your project doesn't have a CLAUDE.md, generate one with
/init. - Review the generated content and add any missing rules.
- Start a new session to verify CLAUDE.md is applied — try making a request that violates a project rule and see if Claude pushes back or mentions the rule.
Q1. What is the main role of the CLAUDE.md file?
- A) A configuration file for installing Claude Code
- B) Memory that automatically communicates project rules and context to Claude
- C) An authentication file for the Claude API
- D) A Git commit message template