Claude Code CLI Cheatsheet - Anthropic AI Coding Agent Full Reference

Claude Code targets developers who want the model to read, edit, and run commands inside a repo from the terminal, plus automation engineers wiring it into CI. As Anthropic official agentic coding tool it differs from chat-style assistants, and drives pipelines headlessly via -p + --output-format json or iterative multi-turn REPL sessions. This sheet surfaces the key commands for install/auth, session resume, permission modes, and MCP extensibility so you can start on real projects immediately.

AI CLI·53 commands·Last updated 2026-08-05
claude-codeclaudeanthropicaicliagentCoding Agent

Typical Use Case

Claude Code is most useful in two situations: daily coding where the AI directly edits repo code (refactoring, bug fixes, tests, new features) via the interactive REPL, and CI/script automation where -p print mode with --output-format json handles one-shot reviews, docs, or codegen. It shines as a pair engineer when build/test/lint live in CLAUDE.md and context comes via @file — far fewer wasted turns. When you do not want the model touching files, use /plan read-only planning first, then release permissions.

Install & Auth 7

npm install -g @anthropic-ai/claude-code
Install Claude Code globally via npm
claude --version
Print the installed version
npm update -g @anthropic-ai/claude-code
Upgrade to the latest version
claude update
Update to the latest version from within the CLI
claude auth login
Interactive sign-in to your Anthropic account
claude auth status
Show current authentication status
claude auth logout
Sign out

Launch & Run 6

claude
Start an interactive REPL in the current directory
claude "Refactor the user-auth module to use the new session API"
Start a session with an initial prompt
cat error.log | claude -p "Explain this error and suggest a fix"
Pipe content in and process it in print mode
claude -c
Continue the most recent conversation (--continue)
claude -r "abc123" "Finish the PR"
Resume a specific conversation by session ID (--resume)
claude -c -p "Check for type errors"
Continue the recent conversation in print mode for automation

CLI Flags 13

claude -p -m claude-sonnet-4 "Write a Python function to parse CSV"
Set the model and run in print mode
claude -p --output-format json "List all TODO comments in this project"
Output JSON for scripting and parsing
claude -p --max-turns 5 "Refactor the auth module"
Cap agentic turns to prevent runaway operations
claude --max-budget-usd 2 "Implement the login page"
Cap API spend in -p mode
claude --add-dir ../frontend ../backend
Grant file access to extra working directories
claude --system-prompt "You are a security auditor"
Replace the entire system prompt
claude --append-system-prompt "Use TypeScript strict mode"
Append instructions to the default system prompt
claude --permission-mode plan
Start in plan mode (read-only, no file changes)
claude --dangerously-skip-permissions
Skip all permission prompts (trusted environments only, use with caution)
claude -w feat/login
Start in an isolated git worktree (--worktree)
claude --effort high "Design the cache layer architecture"
Set reasoning effort: low/medium/high/xhigh/max
claude --bg "Run the test suite"
Start as a background agent and return immediately
claude --verbose
Show full turn-by-turn tool output

Session Slash Commands 7

/clear
Clear conversation history for a fresh task
/compact
Summarize the conversation to free context window space
/context
Visualize context window usage and optimization tips
/resume
Reopen a past session by name or from a picker
/branch
Fork the conversation to try a different direction
/rewind
Roll code and conversation back to a checkpoint
/exit
Quit the REPL

Project & Dev Slash Commands 10

/init
Generate a starter CLAUDE.md for the project
/memory
Edit CLAUDE.md files and manage auto-memory
/mcp
Manage MCP server connections interactively
/agents
Configure subagent settings
/permissions
Set allow/ask/deny rules for tools
/hooks
View hook configurations
/plan
Enter plan mode before a large change
/model
Switch the active model
/diff
Open the interactive diff viewer
/code-review --fix
Review the current diff (optionally auto-fix)

MCP Server Management 7

claude mcp add --transport http <name> <url>
Add an HTTP MCP server (recommended transport)
claude mcp add <name> -- <args...>
Add a stdio MCP server; command follows --
claude mcp add-json '<json>'
Add a complex MCP server from JSON config
claude mcp list
List configured MCP servers
claude mcp get <name>
Show a server configuration
claude mcp remove <name>
Remove an MCP server
claude mcp add-from-claude-desktop
Import MCP servers from Claude Desktop

Prompt File References 3

Review @./src/components/Button.tsx
Reference a single file with @ in the prompt
Compare @src/old.js with @src/new.js
Reference multiple files to compare
Explain the structure of @./src
Reference a whole directory for context

Command Examples

Fix a failing test in print mode

claude -p --output-format json "运行测试,根据失败信息定位根因并给出修复方案" cat log.txt | claude -p "分析这个错误的根因并给出修复步骤"

-p 只输出结果后退出,加 --output-format json 便于脚本直接解析,是自动化调用的事实标准。

Output

{"status":"root cause identified","cause":"user-auth 模块未处理空 token","fix":"在 session 校验前补充 null 检查"}

Resume last session to continue a refactor

claude -c echo "错误日志" | claude -c -p "检查类型错误"

用 -c(最近会话)或 -r <session-id>(指定会话)在终端重启后即可无痛续接。

Output

(进入 REPL,恢复上文中的上下文与已做改动,可继续对话或改文件)

Plan before a large change

claude --permission-mode plan "重构支付模块,先给出改动清单和风险点"

plan 模式下模型只做读与分析,确认方案后再按默认权限执行,能显著降低返工。

Output

(仅输出规划报告,不修改任何文件)

Add a Postgres MCP server

claude mcp add --transport http my-pg http://localhost:8080/mcp claude mcp list

接入后可在会话里让 Claude 直接查询数据库并基于结果写代码,省去手动复制查询结果。

Output

✔ Added MCP server "my-pg" (HTTP)

Common Pitfalls

  • --dangerously-skip-permissions skips all prompts so the model can edit files and run commands directly — only in trusted/throwaway environments.
  • --max-budget-usd only caps -p print mode; an interactive REPL needs another budget mechanism, so do not rely on it to stop long sessions.
  • Changes are diffed before applying, but editing CLAUDE.md/AGENTS.md or dependencies changes future behavior — review those memory files before big changes.
  • stdio MCP servers are spawned by the claude subprocess; if one dies it can slow the agent, so check /mcp on stalls.

Tips

  • For scripts and CI prefer `claude -p --output-format json`: print mode + JSON is the de-facto standard for automation.
  • Cap long tasks or bounded budgets with both `--max-turns` and `--max-budget-usd` to avoid runaway API spend.
  • Give Claude context with `@file` / `@dir` references — more accurate and maintainable than pasting file contents into the prompt.
  • Before big changes, enter read-only `/plan` mode to confirm the approach, then grant permissions to execute — far less rework.

FAQ

What is the difference between Claude Code -p (print mode) and the interactive REPL?

The interactive REPL suits exploratory development with multi-turn conversations and file edits; -p print mode suits scripts and pipelines — it runs once, prints the result, and exits, and supports --output-format json for programmatic parsing.

How do I let Claude Code auto-approve all actions without confirmation prompts?

Use --dangerously-skip-permissions to skip all prompts, or set --permission-mode to acceptEdits / bypassPermissions. Only use these in trusted environments, since they let the agent modify files and run commands directly.

How does Claude Code connect to MCP servers to extend its capabilities?

Manage connections interactively via the /mcp slash command, or add external MCP servers with claude mcp add <name> <command> (e.g. database, filesystem, API). Once connected, the agent can call those tools during a session.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Aug 5, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us