概述
Claude Code 是 Anthropic 推出的一款快速迭代的终端原生 AI 编码工具。与传统的 IDE 补全插件不同,它就像坐在你终端里的结对编程伙伴:可以读取整个代码库、理解上下文、自主编辑多个文件、运行命令、执行测试,并以 Agent 身份完成复杂的跨文件任务。从 2 月到 3 月底,Anthropic 接连发布了 73 项产品功能——更新节奏惊人——使其成为许多团队的主力开发工具。
核心特性:终端原生、Agent 自主编码、深度代码库理解,以及通过 MCP 协议扩展能力。
安装
Claude Code 通过 npm 全局安装,需要 Node.js(推荐 18+)。在终端运行以下命令进行全局安装,然后用 --version 验证。
# Install Claude Code globally
npm install -g @anthropic-ai/claude-code
# Verify the installation
claude --version
# Output looks like: 2.3.1 (Claude Code)安装特定版本(例如企业锁定的版本):npm install -g @anthropic-ai/[email protected]
基本用法
安装完成后,在任意项目目录运行 claude 命令即可启动交互式会话。首次使用需要登录 Anthropic 账户或配置 API key。启动后,只需用自然语言描述你的需求,Claude 就会自主分析、编辑文件并运行命令。
# Enter the project directory
cd my-project
# Start a Claude Code interactive session
claude
# Run a one-off task (non-interactive mode)
claude -p "Add type annotations to every function"
# Resume the previous session
claude --continue在会话中输入 /help 查看所有命令,/clear 重置上下文,按两次 Ctrl+C 退出。
技巧
Claude Code 的强大之处在于其 Agent 工作流。一份精心编写的 CLAUDE.md 项目记忆文件能帮助它理解你的项目约定;MCP 协议让它连接数据库、浏览器、外部 API 等工具;/undo 可安全回滚不满意的改动。建议在 Git 仓库中使用——每次改动都会自动提交,便于追踪和回退。
# Create a memory file in the project root so Claude remembers your conventions
echo "# Project conventions
- Use TypeScript strict mode
- Functions must have JSDoc" > CLAUDE.md
# Specify a model
claude --model claude-sonnet-4
# View session history
claude --history提示:将复杂任务拆分为更小的步骤效果更好;在重要改动前运行 git commit 保存当前状态。
Configuration
Claude Code is configured through CLI flags, environment variables, and the CLAUDE.md project memory file. Set ANTHROPIC_API_KEY to authenticate non-interactively, use --model to pick a model, and manage MCP servers in ~/.claude.json or a project-level .mcp.json. A CLAUDE.md file in the project root is auto-loaded as persistent project context that the agent follows on every task.
# Authenticate via environment variable
export ANTHROPIC_API_KEY="sk-ant-..."
# Pick a model for the session
claude --model claude-sonnet-4
# Project memory file (loaded automatically)
# ./CLAUDE.md
# - Use TypeScript strict mode
# - Functions must have JSDoc
# MCP config (~/.claude.json or .mcp.json)
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/path"]
}
}
}
# Open the in-session settings panel
/configKeep CLAUDE.md concise—treat it like a README of conventions the agent must follow on every task.
FAQ
Common questions cover API access, cost control, model selection, MCP setup, and how CLAUDE.md shapes behavior. Claude Code bills through your Anthropic account or API key, so costs scale with token usage; picking a cheaper model for routine edits and reserving the strongest model for planning helps control spend. MCP is optional but unlocks databases, browsers, and external APIs.
Q: Do I need a paid Anthropic plan?
A: Claude Code works with an Anthropic Console API key (pay-as-you-go) or a
supported Claude subscription; check the official pricing page for current tiers.
Q: How do I reduce token costs?
A: Use a smaller/cheaper model for simple edits, keep CLAUDE.md lean, run
/clear between unrelated tasks, and let the agent work in focused steps.
Q: Can I use my own API key?
A: Yes—set ANTHROPIC_API_KEY or sign in interactively on first launch.
Q: How does CLAUDE.md work?
A: It is auto-loaded from the project root (and parent dirs) as persistent
context, so put coding conventions and project notes there.
Q: How do I add external tools?
A: Configure MCP servers in ~/.claude.json or .mcp.json; the agent can then
call databases, browsers, filesystems, and more.Run /help inside a session to see every available command and slash option.
Ready to try Claude Code?
Visit the official site for the latest version and full documentation.
Visit Claude Code