Claude Code Best Practices Guide
Overview
Claude Code is an agentic coding environment that enables an AI to read files, execute commands, and autonomously implement solutions rather than simply reviewing code. To maximize its effectiveness, users must manage the context window—which stores all messages, file reads, and command outputs—as performance degrades when the window fills.
Implementing Autonomous Verification
To prevent the user from becoming the sole verification loop, Claude Code should be provided with deterministic signals to determine when a task is complete.
Verification Strategies
- Verification Criteria: Instead of vague requests, provide specific test cases (e.g., "write a validateEmail function; user@example.com is true, invalid is false") and instruct Claude to run them.
- Visual Verification: For UI changes, provide a design screenshot and instruct Claude to take a resulting screenshot and list differences.
- Root Cause Analysis: When fixing builds, provide the specific error and require that the build succeeds without suppressing the error.
Gating Mechanisms
Depending on the required level of autonomy, verification can be implemented via:
- Single Prompts: Requesting the check and iteration in one message.
- Goal Conditions (
/goal): Using a separate evaluator to re-check conditions after every turn. - Stop Hooks: Scripts that block a turn from ending until a specific check passes.
- Verification Subagents: Using a fresh model to refute the results of the primary agent.
Workflow: Explore, Plan, and Code
Jumping directly into coding can lead to solving the wrong problem. Anthropic recommends a four-phase workflow using plan mode to separate exploration from execution:
- Explore: Understand the codebase and the problem.
- Plan: Define the implementation strategy.
- Code: Execute the plan.
- Verify: Ensure the solution meets the criteria.
Prompting and Context Optimization
Providing Specific Context
Precision in prompting reduces ambiguity and prevents errors. Effective strategies include:
- Scoping Tasks: Specifying the exact file, scenario, and testing preferences (e.g., "avoid mocks").
- Directing Sources: Pointing Claude to specific git histories or files to answer architectural questions.
- Referencing Patterns: Directing Claude to existing codebase examples (e.g., "HotDogWidget.php") to ensure consistency.
- Describing Symptoms: Providing the error, likely location, and a definition of "fixed."
Rich Content Integration
@References: Use@to reference files directly so Claude reads them before responding.- Direct Inputs: Paste images, provide URLs for documentation, or pipe data using
cat error.log | claude. - Autonomous Fetching: Instruct Claude to use Bash commands or MCP tools to pull necessary context.
Environment Configuration
The CLAUDE.md File
CLAUDE.md is a persistent context file read at the start of every session. It should be kept concise to avoid bloating the context window.
- What to include: Non-obvious Bash commands, custom code style rules, preferred test runners, repository etiquette, and project-specific architectural decisions.
- What to exclude: Standard language conventions, detailed API documentation (link instead), and information that changes frequently.
Permissions and Automation
- Auto Mode: Uses a classifier model to block only risky actions (e.g., scope escalation), reducing the need for manual approvals.
- Permission Allowlists: Specifically permit safe tools like
npm run lint. - Sandboxing: OS-level isolation to restrict filesystem and network access.
Extensions and Tooling
- CLI Tools: Installing tools like the GitHub CLI (
gh) allows Claude to manage issues and PRs more efficiently than via API. - MCP Servers: Connect Claude to issue trackers, databases, and Figma designs.
- Hooks: Deterministic scripts that run at specific points (e.g., running
eslintafter every edit). - Skills: Project-specific knowledge stored in
.claude/skills/that can be invoked via/skill-name. - Subagents: Independent contexts used for heavy research or adversarial reviews to avoid cluttering the main session.
- Plugins: Bundled units of skills, hooks, and MCP servers, including code intelligence plugins for typed languages.
Session Management
Context Maintenance
Because the context window is the primary constraint, aggressive management is required:
/clear: Reset context between unrelated tasks to prevent "kitchen sink sessions."/compact: Manually trigger summarization of the conversation history./btw: Use for side questions that should not be saved to conversation history.- Course Correction: If Claude fails twice on the same issue,
/clearthe session and start with a more specific prompt.
State Control
- Rewind and Checkpoints: Use
Esc + Escor/rewindto restore previous code states or conversation history. - Resuming: Use
claude --continueorclaude --resumeto pick up previous sessions.
Scaling and Automation
Non-Interactive Mode
Using claude -p "prompt" allows integration into CI pipelines and pre-commit hooks, with output available in plain text, JSON, or streaming JSON.
Parallel Execution
- Worktrees: Isolated git checkouts for separate CLI sessions.
- Agent Teams: Automated coordination of multiple sessions with a team lead.
- Writer/Reviewer Pattern: Using separate sessions for implementation and review to eliminate bias.
Fan-out Patterns
For large migrations, users can distribute work by identifying files, generating a list of tasks, and running parallel non-interactive invocations.
Common Failure Patterns to Avoid
- The Kitchen Sink Session: Mixing unrelated tasks in one session; fix by using
/clear. - Over-correcting: Repeatedly correcting the same error; fix by restarting the session with a better prompt.
- Over-specified CLAUDE.md: Too many rules causing Claude to ignore instructions; fix by pruning.
- Trust-then-Verify Gap: Shipping plausible but untested code; fix by requiring deterministic verification.
- Infinite Exploration: Unscoped research that fills the context; fix by using subagents.
Sources
Related
- Project
- Dispatch
- Dispatch
- Dispatch
- Dispatch