Getting Started
Install & Launch
npm install -g @anthropic-ai/claude-code claude # start interactive session claude --version # check version claude update # update to latest
Authentication
claude login # browser OAuth export ANTHROPIC_API_KEY="sk-ant-..." claude logout # clear session
Slash Commands
Session Commands
/helpShow available commands
/clearClear conversation history
/compactCondense context to save tokens
/costShow token usage and cost
/statusShow session info and model
/newStart a new conversation
/configOpen or view configuration
/doctorDiagnose configuration issues
/initCreate a CLAUDE.md for the project
/loginAuthenticate with Anthropic
/logoutClear authentication
Workflow Commands
/bugFile a bug report
/reviewRequest a code review
/prCreate or update a pull request
/commitCommit staged changes with a message
Keyboard Shortcuts
Ctrl+CCancel current generation
Ctrl+DExit Claude Code (EOF)
EscapeCancel current input / edit
TabAutocomplete file paths and commands
Up/DownNavigate input history
CLI Flags
Common Flags
-p, --printNon-interactive (headless) mode
--modelSet model: opus, sonnet, haiku
--output-formatOutput as text, json, stream-json
--allowedToolsRestrict tools: Edit,Read,Bash
--max-turns NLimit conversation turns
--debugEnable debug logging
-n, --nameName the session
Headless / Scripting
claude -p "explain this error" < log.txt claude -p "list todos" --output-format json echo "fix typos" | claude -p
Configuration Files
CLAUDE.md Hierarchy
./CLAUDE.mdProject-level instructions (checked into repo)
./.claude/settings.jsonProject settings (permissions, hooks)
~/.claude/CLAUDE.mdUser-global instructions (all projects)
~/.claude/settings.jsonUser-global settings
~/.claude/projects/*/CLAUDE.mdPer-project user instructions (not in repo)
Environment Variables
ANTHROPIC_API_KEYAPI key for direct authentication
CLAUDE_MODELDefault model override
CLAUDE_CONFIG_DIRCustom config directory path
Permissions
settings.json
{ "permissions": { "allow": ["Read", "Glob", "Grep"], "deny": ["Bash(rm *)"] } }
Permission Modes
defaultAsk before risky tools
--dangerously-skip-permissionsAllow all tools (CI/scripts only)
--allowedToolsCLI flag to restrict tool set
MCP Servers
What Are MCP Servers?
Model Context Protocol servers extend Claude Code with custom tools — databases, APIs, services. Managed via CLI or `.mcp.json`.
CLI Management
claude mcp add server-name -- cmd arg1 arg2 claude mcp list claude mcp remove server-name
.mcp.json Config
{ "mcpServers": { "my-db": { "command": "python", "args": ["-m", "mcp_server_db"], "env": { "DB_URL": "${DATABASE_URL}" } } } }
Scope
.mcp.jsonProject-level MCP servers
~/.claude/mcp.jsonUser-global MCP servers
claude mcp add -s userAdd to user scope
claude mcp add -s projectAdd to project scope
Hooks
Hook Events
PreToolUseRuns before a tool is executed
PostToolUseRuns after a tool completes
NotificationRuns when Claude sends a notification
StopRuns when Claude finishes a response
settings.json Example
{ "hooks": { "PreToolUse": [{ "matcher": "Bash", "hooks": [{ "type": "command", "command": "check-command.sh $INPUT" }] }] } }
Hook Behavior
exit 0Allow the tool to proceed
exit 2Block the tool from running
stdoutJSON feedback to Claude
SDK & Automation
Headless Scripting
# Single prompt, get JSON output claude -p "summarize main.py" \ --output-format json # Pipe input git diff | claude -p "review this diff"
CI / GitHub Actions
- uses: anthropics/claude-code-action@v1 with: claude_args: > --max-turns 5 --model claude-sonnet-4-6
Tips
Use `--max-turns` to cap cost in automation. Use `--output-format json` to parse results programmatically. Combine with `--allowedTools` for safety.
Models
Model Selection
claude --model opus # most capable claude --model sonnet # balanced (default) claude --model haiku # fastest, cheapest
Model Shortcuts
opusClaude Opus — highest capability
sonnetClaude Sonnet — default, balanced
haikuClaude Haiku — fast and economical
--model full-namee.g. claude-sonnet-4-6
Best Practices
Writing CLAUDE.md
Put project conventions, tech stack, build commands, and test instructions in CLAUDE.md. Keep it concise — Claude reads it every session. Use `/init` to scaffold one.
Cost Management
/costCheck running token usage
/compactCompress context when it grows large
--max-turnsCap turns in automation
sonnet / haikuUse cheaper models for simple tasks
Effective Prompting
Be specific"Fix the null check in auth.ts:42" > "fix bug"
Give contextReference files, functions, error messages
Use @fileReference files directly: @src/main.ts
Use imagesDrag & drop screenshots for visual context
IterateFollow up to refine; use /clear to reset