Skip to content

Claude Code Cheat Sheet

import { Aside, Card, CardGrid } from ‘@astrojs/starlight/components’;

CommandPurposeExample
claudeStart interactive REPLclaude
claude -p "prompt"One-shot modeclaude -p "explain this error"
claude --model sonnetStart with specific modelclaude --model opus
claude --versionCheck installed versionclaude --version
cat file | claude -p "..."Pipe mode — feed file contentcat log.txt | claude -p "summarize"
claude configManage configurationclaude config
CommandWhat It DoesWhen to Use
/helpList all available commandsForgot syntax? Start here
/initCreate CLAUDE.md for projectFirst time in a new project
/compactCompress conversation historyContext at 50-70% — frees 30-70% tokens
/clearErase ALL context, start freshSwitching to unrelated project
/costShow token usage and estimated costEvery 20-30 minutes to track spending
/exit or Ctrl+CExit sessionDone working
ScenarioModeCommand
Multi-turn debuggingREPLclaude
Quick questionOne-shotclaude -p "..."
Review a diffPipegit diff | claude -p "review"
Process a log filePipecat errors.log | claude -p "analyze"
CI/CD integrationOne-shotScript with -p flag
Learning / exploringREPLclaude
ModelCostBest For
Haiku$Formatting, typos, simple edits, quick answers
Sonnet$$Features, debugging, code review, documentation
Opus$$$Architecture, complex debugging, security, novel problems

Every project CLAUDE.md should include these sections:

# Project: [Name]
## 1. Project Overview
Tech stack: Node.js 20, Express, PostgreSQL, Redis
Architecture: Monorepo with packages/ directory
## 2. Architecture Rules
- Routes → Services → Repositories → Database
- All business logic in services, never in routes
- Shared types in packages/shared/
## 3. Coding Conventions
- Files: kebab-case (user-service.ts)
- Classes: PascalCase (UserService)
- Functions: camelCase (getUserById)
- Error handling: Promise<Result<T, AppError>>
- Imports: Absolute via @/ prefix
## 4. Commands
npm run dev # Start dev server (port 3000)
npm test # Run all tests
npm run lint # ESLint + Prettier check
npm run db:migrate # Run database migrations
## 5. Constraints (DO NOT)
- Do NOT use `any` type — always define proper types
- Do NOT install dependencies without asking first
- Do NOT put business logic in route handlers
- Do NOT commit .env files
- Do NOT modify database schema without migration
## 6. Context (Tribal Knowledge)
- Redis keys MUST include version prefix: v1:user:123
- JWT expires in 15 min, refresh token in 7 days
- Legacy /api/v1 routes maintained for mobile app compatibility
- Payment gateway sandbox mode uses TEST_ prefix keys
SizeTokensQuality
300-500 words~400-650Minimal — misses edge cases
500-800 words~650-1000Sweet spot for most projects
800-1200 words~1000-1500Comprehensive — large projects
1200+ words1500+Too large — diminishing returns
LocationScopePriority
~/.claude/CLAUDE.mdGlobal (all projects)Lowest
./CLAUDE.mdProject rootMain — use this
./src/CLAUDE.mdDirectory-levelHighest (overrides root)

Terminal window
# Secrets — NEVER commit
.env
.env.local
.env.*.local
# Dependencies
node_modules/
vendor/
.venv/
# Build output
dist/
build/
.next/
# OS files
.DS_Store
Thumbs.db
Terminal window
# .env.example — committed to git, safe for Claude to read
DATABASE_URL=postgresql://user:password@localhost:5432/dbname
REDIS_URL=redis://:password@localhost:6379
API_KEY=your_api_key_here
STRIPE_KEY=sk_test_placeholder
# .env — real secrets, gitignored, NEVER read by Claude
# DATABASE_URL=postgresql://realuser:s3cr3t@prod.db:5432/prod
Command CategoryAction
ls, pwd, cat, head, tailApprove freely — read-only
grep, findApprove freely — search only
git status, git log, git diffApprove freely — read-only git
git commit -m "..."Review message, then approve
npm install <package>Verify package name first
File writes in src/Verify path and content
rm, git push --forceAlways deny unless intentional
curl, wgetAlways deny — data exfiltration risk
Operations on ~/.ssh/, ~/.aws/Always deny — sensitive directories
Terminal window
# Install
brew install gitleaks
# Add pre-commit hook
cat > .git/hooks/pre-commit << 'HOOK'
#!/bin/bash
gitleaks protect --staged --verbose
if [ $? -ne 0 ]; then
echo "SECRETS DETECTED! Commit blocked."
exit 1
fi
echo "No secrets detected."
exit 0
HOOK
chmod +x .git/hooks/pre-commit

Content TypeTokens per 1000 chars
English prose~250
Source code~400
JSON data~400
Small file (<5KB)1,000-2,000 total
Medium file (5-20KB)2,000-5,000 total
Large file (>50KB)10,000+ total
LevelQualityAction
0-50%ExcellentContinue working
50-70%GoodPlan to /compact soon
70-85%DegradingRun /compact now
85-95%PoorMust compact or clear
95%+Unreliable/clear and restart
ComboWorkflowWhen
/cost/compactCheck → compressRoutine maintenance (every 30 min)
/clear/initReset → configureStarting new project
/compact → work → /costCompress → continue → verifyLong sessions

Terminal window
cd my-project
claude # Start session
/init # Create CLAUDE.md
# Edit CLAUDE.md with 6 sections above
/cost # Check baseline usage
Terminal window
# Every 20-30 minutes:
/cost # Check context level
# At 50-70% context:
/compact # Free space, keep task context
# Switching to unrelated task:
/clear # Full reset
Terminal window
git diff main | claude -p "Review this diff for bugs, security issues, and style"
Terminal window
# Step 1: Overview first
"Show me the directory structure of src/"
# Step 2: Targeted reading
"Show me function signatures in src/auth/"
# Step 3: Deep dive only where needed
"Read src/auth/login.ts — I need to understand the token flow"