User Tools

Site Tools


ai:agentic-coding:claude-code-how-to-use

Claude Code Best Practices Guide

πŸš€ Getting Started

Installation & Setup

  • Install via npm:
    npm install -g @anthropic-ai/claude-code
 
  • Run in your project folder:
    claude
 
  • Run `/init` first

β†’ Generates a starter `CLAUDE.md` based on your project structure

  β†’ Detects build systems, test frameworks, and code patterns  

πŸ“„ CLAUDE.md β€” Your Most Important File

`CLAUDE.md` is a special file that Claude reads at the start of every conversation.

Include:

  • Bash commands
  • Code style
  • Workflow rules

This gives Claude persistent context it cannot infer from code alone.

Tips for a good CLAUDE.md

  • Document what Claude gets wrong (not just what to do)
  • Avoid referencing full docs inline

β†’ Use: β€œFor complex usage, see path/to/docs.md”

  • Keep rules actionable

β†’ Instead of:

    "never use --foo-bar flag"  
  β†’ Write:  
    "never use --foo-bar; prefer --baz instead"

🧠 Context Window Management β€” #1 Priority

Claude's context window includes:

  • Messages
  • Files read
  • Command outputs

If it gets full β†’ Claude may forget earlier instructions.

Practical tips

  • Use `/clear` between unrelated tasks
  • Pipe data instead of reopening files:
    cat error.log | claude
 
  • Provide URLs instead of pasting large docs
  • Resume sessions:
    claude --continue
    claude --resume
 

πŸ“‹ Always Plan Before You Code

Planning reduces decision complexity.

Without planning β†’ success rate β‰ˆ 33%

Best practices

  • Use Plan Mode (press `Shift + Tab` twice)
  • Plans persist across context resets
  • For large features:

β†’ Ask Claude to write a spec first

  β†’ Say: **"don't implement yet"**

πŸ” Give Claude a Feedback Loop

Include:

  • Test commands
  • Linter checks
  • Expected outputs

Claude will:

  • Run tests
  • Detect failures
  • Fix issues automatically

Example prompt

Refactor the auth middleware to use JWT.
Run the existing test suite after.
Fix any failures before calling it done.

πŸͺ Use Hooks for Non-Negotiable Rules

  • `CLAUDE.md` β†’ advisory (~80% compliance)
  • Hooks β†’ deterministic (100%)

Use hooks for

  • Formatting
  • Linting
  • Security checks

Example

Add `PostToolUse` hooks in:

.claude/settings.json

πŸ€– Sub-Agents & Parallel Work

  • Sub-agents run in separate context windows
  • Return compressed summaries

Benefits

  • Keeps main session clean
  • Improves focus
  • Adds safety

Example roles

  • Code review agent β†’ reads files only
  • Documentation agent β†’ no shell commands

βš™οΈ Keep Things Simple

  • Simple control loops > complex systems
  • Avoid over-engineering
  • Only implement what is requested

Avoid

  • Unnecessary features
  • Extra refactoring
  • β€œNice-to-have” improvements

πŸ› οΈ Useful Commands & Shortcuts

Command Description
`/init` Generate starter CLAUDE.md
`/clear` Reset context window
`/config` Set output style
`!git status` Run shell command
`Shift + Tab (x2)` Enter Plan Mode
`claude –continue` Resume last session

πŸ”‘ Key Mindset

Successful users:

  • Manage context carefully
  • Plan before coding
  • Keep systems simple
  • Use hooks for quality control
  • Continuously improve `CLAUDE.md`

Key idea: The difference between frustration and productivity is not the tool β€” it's how you use it.


πŸ“š Official Documentation

ai/agentic-coding/claude-code-how-to-use.txt Β· Last modified: by phong2018