Claude.md Patterns That Stop Agent Course Corrections
Structure claude.md with project description first, Karpathy patterns (think-before-coding, simplicity first, surgical changes, goal-driven execution), scoped rules, tool overrides, git safety, verification steps, and priority-ordered instructions under 300 lines to align Claude Code precisely on tasks.
Karpathy Patterns Align Claude on Tasks Without Guessing
Instruct Claude to think before coding: Explicitly state assumptions, present multiple interpretations if ambiguous, and confirm intent before implementing. This cuts course corrections by making Claude ask clarifying questions instead of guessing from training data patterns.
Prioritize simplicity first: Solve problems in under 200 lines (refactor if >50 needed), add no extra features, ensure error handling. Rewrite verbose solutions to avoid token waste, delays, and refactoring issues—critical for large apps.
Enforce surgical changes: Touch only code directly tied to the task. Flag unrelated issues (dead code, formatting) without fixing; trace every edit back to user request. Prevents divided attention and unwanted refactors.
Drive goal-driven execution: Define verifiable success criteria per task (e.g., add tests for validation inputs/outputs, iterate until passing). For UI, use Claude Chrome extension or Puppeteer MCP to visually verify changes, as code alone can't judge visuals.
These patterns from Andrej Karpathy's skills repo ensure Claude plans, verifies, and implements exactly what's needed, turning vague tasks into reliable outputs.
Scoped Rules, Tool Overrides, and Git Safety for Project Scale
Override default tools: List only non-standard CLI tools (e.g., GitHub CLI over git, PNPM run if not npm) and custom run commands. Skip built-in knowledge like dev/build servers to save lines.
Add git commit safety: Never run irreversible commands (force push, reset head, merge, rm -rf) without confirmation. Ask if unsure—prevents production damage.
Use path-scoped rule files: Create dedicated files (e.g., for APIs) with scope declared first line; reference in root claude.md. Loads only relevant rules, avoids bloat/distraction.
For monorepos, place scoped claude.md in each subfolder for module-specific guidance; keep root global for broad rules only. Focused context boosts performance over bloated single file.
Update claude.md iteratively: After user corrections, apply fixes and log learnings to a knowledge base file for future reference.
Priority Ordering and Verification for Peak Performance
Place project description first: Summarize app structure, services, dependencies, run flow at top so Claude grasps context immediately, not from code inference.
Mandate full verification before completion: Don't just check feature existence—run builds, tests, linting, type checks to confirm function. Report only when all pass.
Order by priority: Hard rules (non-negotiable) first, medium (important, somewhat flexible) next, low (references/conveniences) last. Keeps decision-making sharp.
Enforce 300-line limit: Beyond this, performance degrades—trim ruthlessly for focus.
Combined, these make Claude Code ship correct implementations on first try, saving hours vs. constant fights.