Maybe you know this:
Claude does something weird, suddenly creates 10 new files when you just wanted to fix a bug, uses JavaScript even though your entire project is TypeScript, or spends 5 minutes or more understanding your project before each task.
The solution is to create one or more CLAUDE.md files.
In this blog article, I'll show you how to create a perfect CLAUDE.md file including templates, examples, and pro tips. So you never have to say (or, realistically, less often) "Claude, no! What the hell are you doing?" anymore.
1. What is a CLAUDE.md File?
CLAUDE.md is a "here are my rules" file for Claude Code. You tell it once how it should work for you, and it remembers for all future sessions.
Important to understand:
A CLAUDE.md isn't static documentation, but a living documentation of Claude's mistakes. Every time Claude does something wrong, that correction goes directly into my CLAUDE.md (so Claude gets smarter with each task).
After struggling with Claude for weeks, I analyzed my sessions. And with a proper CLAUDE.md, things just work much better.
2. Global, Project, and Folder-Specific CLAUDE.md
There are the following types of CLAUDE.md files:
- Global CLAUDE.md (
~/.claude/CLAUDE.md): Your personal settings that apply to all projects - Project-specific (in project root): Specific instructions for a single project
- Folder-specific CLAUDE.md (in project root): Specific instructions for a single folder
You don't need to reference all three file types in prompts ā Claude Code automatically checks with every prompt whether global, project-specific, or folder-specific CLAUDE.md files exist and loads them into the context window accordingly.
Claude loads CLAUDE.md files hierarchically and combines them intelligently. The most specific (deepest nested) file wins in conflicts. This means:
# ~/.claude/CLAUDE.md (Global)
- Always use TypeScript
- Write tests with Jest
# ~/project/CLAUDE.md (Project)
- Write tests with Vitest # Overrides Jest
# Result for Claude:
- Always use TypeScript (from global)
- Write tests with Vitest (from project)You should still avoid conflicts between file types or clearly point out conflicts to prevent unexpected behavior.
3. Structure and Layout
CLAUDE.md is a normal Markdown file. Nothing magical about it. Claude interprets it as additional context for each request.
The trick is finding the right balance between "too little info" and "token waste". In that regard, I keep the formatting as simple as possible. I mostly use only:
- Headings with
#for structure - Bullet points with
-for lists - Code blocks with
```for examples - Bold with
**text**for important things
What you should avoid are tables (too many tokens), images (get ignored), links (mostly useless).
4. Creating the CLAUDE.md Step by Step
Let's get practical. I'll show you how to create a solid CLAUDE.md in 10 minutes that actually makes a difference.
4.1 Setting Up Global CLAUDE.md in ~/.claude/
First, the global file. This should contain your personal preferences ā things you always want, regardless of which project you're working on:
# Terminal
mkdir -p ~/.claude
touch ~/.claude/CLAUDE.mdHere's an example for a global CLAUDE.md:
# Global Developer Preferences
## General Rules
- Prefer TypeScript over JavaScript
- Write code in English, comments in German
- Use modern ES6+ syntax
- No console.log() in production code
## Code Quality
- Write clean, readable code
- Use meaningful variable names
- Keep functions small (max 20 lines)
- Follow DRY principle
## Error Handling
- Always try-catch with async/await
- Never swallow errors
- Meaningful error messages
## Git
- Commit messages in English
- Use Conventional Commits format
- Never push directly to main4.2 Defining Project-Specific Instructions
Now the project-specific CLAUDE.md. This should contain everything Claude needs to know about your specific project.
To do this, simply give Claude Code the following command:
Create a CLAUDE.md for this project. Analyze the project comprehensively so you don't forget anything. Use the following template:"And copy in the following template (simply copy & paste):
# [Project Name]
[One-sentence description of the project]
## Tech Stack
- **Framework**: [e.g., Next.js, React, Vue]
- **Language**: [e.g., TypeScript, Python]
- **Database**: [e.g., PostgreSQL, MongoDB]
- **Styling**: [e.g., Tailwind, CSS Modules]
- **Testing**: [e.g., Jest, Vitest, Pytest]
## Project Structure
```
src/
āāā [important folder]/ # [Description]
āāā [another folder]/ # [Description]
āāā [config files] # [Description]
```
## Development
```bash
[install command] # Install dependencies
[dev command] # Development server
[build command] # Production build
[test command] # Run tests
```
## Code Standards
- [Standard 1, e.g., "TypeScript strict mode"]
- [Standard 2, e.g., "Follow ESLint rules"]
- [Standard 3, e.g., "No any types"]
- [Standard 4, e.g., "Tests for new features"]
## Project-Specific Rules
- [Rule 1, e.g., "API calls only via services/"]
- [Rule 2, e.g., "Styling only with Tailwind"]
- [Rule 3, e.g., "Components must be typed"]
## Important Notes
- [Note 1, e.g., "Production secrets in .env.production"]
- [Note 2, e.g., "No direct DB queries"]
- [Note 3, e.g., "Mind rate limiting"]
## Common Mistakes to Avoid
- DON'T: [Common mistake]
- DON'T: [Another mistake]
- ALWAYS: [Best practice]
- ALWAYS: [Another best practice]And here's what it can look like in the end:
# AI Blog Project
Next.js 15 Blog with TypeScript, focused on AI and SEO topics.
## Tech Stack
- **Framework**: Next.js 15.3.3 with App Router
- **Language**: TypeScript 5.x (strict mode)
- **Styling**: Tailwind CSS v4 with PostCSS
- **UI**: Radix UI + shadcn/ui components
- **Database**: PostgreSQL with Prisma ORM
## Project Structure
```
src/
āāā app/ # Next.js App Router
ā āāā blog/ # Blog posts (IMPORTANT: No /blog/ URL!)
āāā components/ # React Components
ā āāā ui/ # shadcn/ui Components
āāā lib/ # Utilities and Services
āāā types/ # TypeScript Types
```
## Important Commands
```bash
npm run dev # Development server
npm run build # Production build
npm run lint # Run ESLint
npm run typecheck # TypeScript check
```
## Blog System Rules
- Blog posts in src/app/blog/[slug]/page.tsx
- URLs WITHOUT /blog/ prefix (Rewrite in next.config.ts)
- Always use BlogPostConfig for metadata
- CodeBlock component for all code examples
- Define FAQ items outside the component
## Code Standards
- Components in PascalCase
- Utilities in camelCase
- Tailwind instead of CSS modules
- No new components without checking ui/
- German content, English code
## Common Mistakes to Avoid
- DON'T: Create new README.md
- DON'T: Leave console.log in code
- DON'T: New UI components from scratch
- ALWAYS: Edit existing files
- ALWAYS: TypeScript strict mode
## Git Workflow
- Feature branches from main
- PR before merge
- Squash commits on merge5. Avoiding Common Beginner Mistakes
Here are mistakes you should avoid:
- Being too vague: "Write good code" doesn't help. Be specific: "Use async/await instead of Promises"
- Putting in too much: 1000+ lines CLAUDE.md? Claude ignores half of it. Keep CLAUDE.md compact.
- File in wrong location Check with
ls -la ~/.claude/and in project root - Not updating: Project changes, CLAUDE.md stays the same = chaos
- Contradictory instructions "Use tabs" globally and "Use spaces" locally = chaos (Claude can often detect conflicts, but not always)
- Wrong Markdown syntax No tabs, only spaces. Markdown must be valid.
- Wanting to explain everything: Claude knows the basics. You don't need to explain what React is.
- Copy-paste without adapting: My template is a starting point, not the solution.
6. Adapting to Your Workflow
The template is just the beginning. What works for me doesn't have to work for you. Here's my workflow for adapting:
- Start with the basic template
- Work normally with Claude for a week or a few days
- Note every "Claude does X wrong" moment
- Add these as rules to CLAUDE.md
- Test if it's better
- Repeat
After 2-3 weeks, you'll have a CLAUDE.md that perfectly fits your style.
7. Pro Tips: Advanced CLAUDE.md Configuration
Time for the advanced tricks that make the difference between "pretty good" and "holy shit this is good".
7.1 Formulating Context-Specific Instructions
Instead of general instructions, make them situation-specific:
## Context Rules
### For Bug Fixes
- First understand, then fix
- Analyze root cause
- Write regression test
- Update changelog
### For New Features
- First define Types/Interfaces
- API-First Design
- Write tests in parallel
- Documentation comments
### For Refactoring
- Don't change functionality
- Tests must stay green
- Proceed step by step
- Measure performance7.2 Defining Code Standards and Best Practices
Be ultra-specific with code standards. Claude does exactly what you say ā no more, no less:
## Code Standards (Detailed)
### Naming Conventions
- Components: PascalCase (UserProfile, NavBar)
- Hooks: camelCase with 'use' prefix (useAuth, useFetch)
- Utils: camelCase (formatDate, parseJson)
- Constants: SCREAMING_SNAKE_CASE (API_URL, MAX_RETRY)
- Types/Interfaces: PascalCase with Suffix (UserType, ApiResponse)
### Async Code
```typescript
// ALWAYS like this:
try {
const data = await fetchData()
return processData(data)
} catch (error) {
logger.error('Fetch failed:', error)
throw new AppError('Data fetch failed', error)
}
// NEVER like this:
fetchData().then(data => processData(data))
```
### Imports
- Absolute imports for src/ ('@/components/Button')
- Grouped imports (React, Third-party, Local)
- Prefer named exports7.3 Optimizing Project Structure and File Paths
Claude tends to create files in the wrong place. Be explicit:
## File Locations (IMPORTANT!)
### Where new files go:
- React Components ā src/components/[name]/[Name].tsx
- API Routes ā src/app/api/[route]/route.ts
- Utilities ā src/lib/utils/[name].ts
- Types ā src/types/[name].types.ts
- Hooks ā src/hooks/use[Name].ts
- Tests ā [same-folder]/[name].test.ts
### These folders NOT to touch:
- node_modules/ (obvious, but Claude does it anyway)
- .next/ (Build output)
- dist/ (Build output)
- coverage/ (Test output)
### When in doubt:
ALWAYS ask me before creating new files!7.4 Performance Optimization for Large CLAUDE.md Files
Large CLAUDE.md files can slow Claude down. Here it can help to outsource the instructions from CLAUDE.md to separate docs:
# Project Core (Max 100 Lines)
## Must-Know (Top Priority)
- [Only the most important things]
- [Max 10 points]
## Quick Reference
```
npm run dev
npm run build
npm run test
```
## Details see:
- Architecture: docs/ARCHITECTURE.md
- API: docs/API.md
- Testing: docs/TESTING.mdOutsource details to separate docs and reference them. Claude can ask for more details when needed.
7.5 Version Control and Team Synchronization
What you can do when working in a team:
- CLAUDE.md in the repo: Yes, commit it. Everyone on the team benefits.
- CLAUDE.personal.md in .gitignore: For personal preferences
- Changelog at the end: Document changes
# Team Project
[... Project Config ...]
---
## Changelog
- 2025-01-25: TypeScript strict mode enabled (FH)
- 2025-01-20: Tailwind v4 Update (MK)
- 2025-01-15: Initial version (FH)




