Claude.md Patterns for Bulletproof AI Coding
Craft claude.md with project description first, Karpathy rules like 'think before coding' and simplicity, tool overrides, git safety, scoped files, verification steps, and priority-ordered instructions under 300 lines to make Claude ship exact implementations without guesswork or bloat.
Karpathy-Inspired Rules to Align Claude with Your Intent
Start every claude.md with a project description at the top so Claude grasps the app's structure, services, dependencies, and runtime before diving in—this prevents deduction from code alone and cuts misalignment. Add explicit 'think before coding': Claude must state assumptions, list multiple interpretations if ambiguous, and confirm your choice, slashing course-corrections by forcing clarification over training-data guesses.
Prioritize simplicity: Instruct Claude to solve in minimal lines (e.g., refactor if >200 lines when 50 suffice), add only requested features with proper error handling, and iterate toward conciseness. This avoids verbose overhead that bloats tokens, delays refactoring, and hinders scaling in large apps.
Enforce surgical changes: Touch only task-tracing code; flag unrelated issues (dead code, formatting) without fixing unless asked, as agents scatter focus on 'improvements.' Every edit must link directly to your request, listing other findings for your triage.
Drive goal execution: For each task, Claude defines verifiable success criteria upfront—like writing passing tests for validation inputs/outputs—then plans, implements, iterates until verified. For UI, pair with tools like Claude Chrome extension or Puppeteer MCP to visually confirm changes, as code alone misleads.
These patterns from Andrej Karpathy's skills repo transform vague tasks into precise, testable outcomes, ensuring behavior matches intent without wild implementations.
Tool Overrides, Safety, and Iterative Refinement
Override defaults: Skip init-generated commands (e.g., npm run dev) Claude already knows; specify custom tools like GitHub CLI over git, PNPM over npm, or non-standard run instructions to leverage your stack without fallbacks.
Update dynamically: After user corrections, Claude applies fixes then logs learnings to a dedicated file, building a knowledge base of pitfalls and preferences for future tasks—treat claude.md as living, not static.
Embed git safety: Ban irreversible commands (force-push, reset --hard, rm -rf) without confirmation; if unsure, always ask. This guards production from accidents like unwanted merges.
Use path-scoped rule files: Create e.g., api-rules.md (first line declares scope) for file-type rules, referenced in root claude.md. This avoids bloat—Claude loads only relevant rules, staying focused without interference.
For monorepos, add scoped claude.md per subfolder for module-specific guidance; root holds global rules only, preventing divergence from irrelevant instructions.
Prioritized Structure and Verification for Peak Performance
Order by priority: Hard rules first (non-negotiable, e.g., safety, scoping), then medium (key principles like simplicity), finally low (references). Burying criticals dilutes impact.
Mandate full verification before completion: Don't just add code—run builds, tests, linting, type checks to confirm functionality. Report only when all pass, using every mechanism for fidelity.
Cap at 300 lines: Beyond this, performance drops; trim ruthlessly for focus.
This setup, refined from community testing and shipping, eliminates agent fights: Claude reasons correctly, changes precisely, verifies rigorously, and adapts—saving hours on real projects.