Claude Code Guide
npm install -g @anthropic-ai/claude-code # or: npx @anthropic-ai/claude-code # Requires: ANTHROPIC_API_KEY environment variable
Claude Code is Anthropic's terminal-based AI coding agent. Unlike IDE plugins, Claude Code operates at the filesystem level — it reads files, writes changes, runs commands, and commits to git. Think of it as an AI pair programmer that works in any editor or CI pipeline.
Start a session with `claude` in your project root. Claude reads your CLAUDE.md file for project context (similar to .cursorrules). It can search files with glob patterns, run bash commands, install packages, fix lint errors, and create git commits — all through natural language conversation.
Claude Code excels at cross-cutting refactors (renaming a type across 20 files), initial project scaffolding ("create a Next.js blog with MDX support"), and debugging ("the tests are failing, diagnose and fix"). Use `/help` for available slash commands and `/cost` to track token usage.
Setup
cd ~/my-project export ANTHROPIC_API_KEY=sk-ant-... claude # One-shot command (no interactive session): claude -p "Refactor the User model to use email instead of username" # Pipe a prompt: echo "Add input validation to all API routes" | claude
Basic Workflows
# Claude Code handles file ops naturally: "Read src/app.ts and explain the routing setup" "Create a new Prisma schema for a blog with User, Post, Comment" "Add error handling to all async route handlers" "Rename the `name` field to `fullName` across all files" "Delete all console.log statements from the codebase"
# Claude Code auto-commits before and after changes # Workflow: "Fix the login bug" # Claude: reads files, finds bug, creates changes # Claude: git commit -m "fix: resolve login redirect loop" # Roll back if needed: git reset --soft HEAD~1 git restore --staged . git checkout .
# DO: Be specific about files "Read src/lib/auth.ts and find the security vulnerability" # NOT: "find bugs" (too vague) # DO: Provide a pattern to follow "Create a new API route following the pattern in src/app/api/users/route.ts" # DO: Specify constraints upfront "Write this using only built-in Node.js APIs, no dependencies" # DO: Break complex tasks # Instead of "build a full e-commerce site": # Step 1: "Create the Product schema" # Step 2: "Add CRUD endpoints for products" # Step 3: "Add the product listing page"
CLAUDE.md
# CLAUDE.md — place at project root # Claude reads this at the start of every session ## Tech Stack - Runtime: Node.js 22, TypeScript 5.5 - Framework: Next.js 14 (App Router) - Database: PostgreSQL via Prisma - Auth: NextAuth v5 - Testing: Vitest + Playwright ## Conventions - Use server actions for mutations, not REST - Components are PascalCase, utils are camelCase - Every public function needs JSDoc - No `any` types — use `unknown` and narrow ## Project Structure - src/app/ — Next.js App Router pages - src/components/ — Shared UI components - src/lib/ — Business logic and utilities - src/db/ — Prisma schema and queries - tests/ — Vitest test files
Slash Commands
/help # Show all commands /cost # Show token usage this session /compact # Summarize conversation to save context /clear # Clear conversation history /init # Create CLAUDE.md template /review # Review uncommitted changes /retry # Regenerate last response # Example: /cost # Shows: Tokens used: 45,231 / Estimated cost: $0.68
CI Mode
# Run Claude Code in CI without TTY: export CLAUDE_CODE_HEADLESS=true # Fix lint errors across the project: claude -p "Fix all ESLint errors in src/" # Generate tests: claude -p "Add unit tests for all functions in src/utils/" # Update dependencies: claude -p "Migrate from Moment.js to date-fns across the project" # Use with output file: claude -p "Explain the architecture" > architecture.md