
Why a Shorter CLAUDE.md Works Better: Only Write What Claude Can't See
- كاتب موثّق
- Claude Code 2.x
محتويات المقال
AppError from src/lib/errors.ts, never raw strings", sits in the middle next to a folder tree that's two refactors out of date. Someone rewrites it as "IMPORTANT: NEVER THROW RAW STRINGS". Claude starts wrapping errors in test helpers too, where the team wanted plain assertions.
The common misconception
Onboarding docs work that way for people: a new hire with a thorough guide makes fewer mistakes. Claude Code's own /initgenerates a CLAUDE.md from the codebase, which looks like an invitation to describe it, though the docs add that from there "the work is mostly removing lines".Older models sometimes did need things spelled out, and loud emphasis once helped. When Claude misses a rule, adding text is the fastest fix at hand.
What actually happens
Every line is in every session
Claude already reads the repo
/doctor checkup proposes trims along the same lines: directory layouts, dependency lists and architecture overviews go; pitfalls, rationale and conventions that differ from tool defaults stay.
The two-question test
How to confirm work: the exact test, typecheck and lint commands, and how to run one test file Places where the code misleads: an old pattern that new code must not copy, a directory that looks unused but isn't Decisions that aren't in the repo, such as which error helper the team settled on Anything you've had to correct twice
Emphasis isn't the fix
The same file, before and after
# ArchitectureNext.js app. Pages in src/app/, components in src/components/,API routes in src/app/api/, helpers in src/lib/.Stack: React, TypeScript, Tailwind, Prisma, Vitest.# Folder structuresrc/app/ src/components/ src/lib/ prisma/ tests/ ...# Rules- IMPORTANT: ALWAYS write clean, readable code- Use TypeScript for all new files- NEVER THROW RAW STRINGS!!! Use the error helpers.# Release process1. Pull main. 2. Bump the version. 3. ... (38 more steps)# Commands- Install: pnpm install- One test file: pnpm vitest run path/to/file.test.ts- Typecheck: pnpm tsc --noEmit# Conventions- Throw AppError from src/lib/errors.ts, never raw strings- src/legacy/ is still live; don't copy its patterns# Gotchas- Integration tests need DATABASE_URL (see .env.example)- After editing prisma/schema.prisma, run pnpm db:generateWhere the rest goes
.claude/rules/ with a paths: filter. It loads only when Claude reads, writes or edits a matching file:
---paths: - "src/app/api/**/*.ts"---- Validate request bodies with the zod schemas in src/lib/schemas/- Every new endpoint needs an entry in openapi.yaml.claude/rules/ with paths: | ||
CLAUDE.local.md, gitignored |
/clear, /compact or restart.
How to apply it in your project
Run `/doctor` Let the checkup propose cuts for content Claude can derive from the codebase. Review each one; you decide what goes. Apply the two-question test to every line Could Claude find it in the code, and would it look? If yes to both, delete it. Keep commands, traps and decisions. Move area rules and procedures out Put rules for one directory in .claude/rules/withpaths:, and long procedures in a skill.Turn the "never" lines into hooks If a line has emphasis because Claude must never break it, enforce it with a hook or a deny rule and drop the caps. Audit it like code Review CLAUDE.md changes in pull requests, and run /doctor prompt-auditnow and then to catch stale or contradicting lines. Start a fresh session to load the new version.
Key takeaways
CLAUDE.md loads into every session, so every line costs attention on every task, relevant or not. Aim for under 200 lines; longer files consume more context and reduce adherence. Don't describe the architecture or folder layout; Claude reads the repo. Keep commands, misleading spots, decisions that aren't in the repo and repeated corrections. Move area rules to .claude/rules/, procedures to skills and must-never rules to hooks.
التعليقات (0)