Aller au contenu principal

Claude Code Best Practices (Boris Cherny)

Verification: The Quality Multiplier

"Give Claude a way to verify its work. If Claude has that feedback loop, it will 2-3x the quality of the final result." -- Boris Cherny

Always give Claude a way to validate its work:

ComplexityMethodExample
SimpleBash commandnpm run lint, npm run typecheck
ModerateTest suitenpm test, pytest, go test
ComplexBrowser/SimulatorPlaywright, Chrome DevTools, mobile emulator

Integration: PostToolUse hooks (auto-format, type-check, lint), PreToolUse on commit (mandatory tests), QA agents (/qa:qa-audit).

"I use Opus with adaptive thinking for everything." -- Boris Cherny

ContextModelRationale
Complex tasksOpus 4.7Most advanced reasoning, adaptive thinking, 1M context, xhigh effort
Audits and analysesSonnetGood speed/quality balance
Simple tasksHaikuFast for trivial operations

Advanced Prompting

AvoidPrefer
"Fix this bug""Fix the null pointer in getUserById when user doesn't exist"
"Make it better""Reduce the time complexity from O(n^2) to O(n log n)"
"Add error handling""Add try/catch for network errors with retry logic (3 attempts, exponential backoff)"

Techniques: "Grill me on these changes", "Prove to me this works", "Knowing everything you know now, implement the elegant solution".

See docs/guides/PROMPTING-GUIDE.md for the complete guide.

Effort Levels

Match the level of reasoning to the task.

TaskEffortWhy
Exploring code, reading fileslowNo need for deep reasoning
Implementing a standard featuremediumSpeed/quality balance
Designing an architecture, audit, complex debughighDeep reasoning required
Critical system architecture, advanced security auditxhighMaximum reasoning (Opus 4.7 required)

Command: /effort low, /effort medium, /effort high, /effort xhigh (interactive slider).

Automatic Memory (CLI 2.1.76+)

Claude Code automatically remembers preferences, decisions, and project context in ~/.claude/memory/.

Memorize (auto)CLAUDE.md (git)Rules (auto-activated)
Personal preferencesProject conventionsPer-language rules
Architecture decisionsMandatory workflowCode patterns
Team contextDocumentation referencesVerification checklist

Do not duplicate: if it is in CLAUDE.md, no need to memorize it. Use "remember that..." to force an explicit memorization.

Parallel Sessions

"The single biggest productivity unlock." -- Boris Cherny

Use git worktrees for 5+ Claude Code sessions in parallel. See the git-worktrees skill for details.

Context Management

SituationActionWhen
Long session, intact context/compactBetween phases (Explore → Plan → TDD)
Total topic change/clearNew unrelated task
Normal sessionLet it beAuto-compaction if needed

Quick Recovery

If a refactoring breaks everything: /rewind (or /undo, equivalent alias) returns to the last stable state. Faster than git stash or git checkout. Checkpoints saved automatically before each modification.

Session Resume

/recap generates a summary of the current session — decisions made, files modified, work state. Useful to resume a session after a break or a /compact.

SituationAction
Return after a break/recap to recover the context
After /compact/recap to verify what was kept
Onboarding on an existing sessionclaude --resume <id> then /recap

Configurable via /config (enable/disable automatic recap on resume).

Token Optimization

Prompt Caching 1h (CLI 2.1.108+)

The ENABLE_PROMPT_CACHING_1H variable enables a 1-hour prompt cache instead of 5 minutes. Significantly reduces costs for long sessions.

Enable in .claude/settings.local.json:

{
"env": {
"ENABLE_PROMPT_CACHING_1H": "1"
}
}

Compatible with API key, Bedrock, Vertex, and Foundry. Alternative: FORCE_PROMPT_CACHING_5M to force the 5-minute TTL (useful if telemetry is disabled).

RTK (optional)

Reduce token consumption by 60-90% with RTK.

Installation: brew install rtk. The foundation includes a PreToolUse hook that automatically rewrites commands. Disabled by default, enable with ENABLE_RTK=1 in the env section of .claude/settings.json or .claude/settings.local.json.

rtk gain to see the savings. rtk discover to find unoptimized commands.

Quick Command

/work:work-commit-push-pr "description" -- commit + push + PR in a single command.