Claude Code 完全学习指南
Anthropic 官方 AI 编程助手深度解析
当前版本: Claude Code 2.0 (2025) | 核心模型: Claude Sonnet 4.5 | 上下文: 1M tokens
📊 Claude Code 核心指标
Claude Code 核心数据
System Prompts
16
可用工具数
400+
上下文窗口
1M
日发布次数
1
内部发布/天
100
PR 数/工程师/天
5
🏗️ Claude Code 整体架构
系统架构全景图
正在加载图表...
🎨 核心设计理念
1. Radical Simplicity - 激进的简洁性
Claude Code 团队奉行”极简主义哲学”:
正在加载图表...
关键原则:
| 原则 | 传统方法 | Claude Code 方法 |
|---|---|---|
| 代码量 | 复杂脚手架,大量业务逻辑 | 最小化外壳,模型主导 |
| 执行环境 | Docker/容器化/云端 | 本地直接执行 |
| 工具策略 | 添加更多功能 | 持续删减优化 |
| 提示词工程 | 详细指令和约束 | 信任模型智能 |
| 开发方式 | 人工编写为主 | AI 自己构建自己 |
2. On-Distribution 技术栈
选择 Claude 已经精通的技术:
正在加载图表...
3. 极致开发速度
正在加载图表...
🔧 核心组件详解
Component 1: Agent 系统 (16个 System Prompts)
正在加载图表...
Agent 层次结构:
- general-purpose - 最常用,处理 80% 的任务
- Agent Architect - 唯一能创建 Agent 的 Agent(元编程)
- Security Review - 最大的 Prompt (10.8KB),最复杂的审查逻辑
- Explore - Plan 模式专用,避免无限嵌套
Component 2: 工具系统 (400+ 工具)
正在加载图表...
Component 3: 权限系统
Claude Code 中最复杂的组件:
正在加载图表...
🔄 工作流程解析
Workflow 1: 标准任务执行流程
正在加载图表...
Workflow 2: Subagent 委派流程
正在加载图表...
Workflow 3: Plan 模式工作流程
正在加载图表...
Plan 模式特点:
- ✅ 只读探索 - 不会修改任何文件
- ✅ 深度理解 - 全面分析代码库架构
- ✅ 策略优先 - 先规划后执行
- ✅ 避免嵌套 - 使用特殊的 Explore Agent
🪝 Hooks 深度指南
Hooks 概述
Hooks 是在 Claude Code 生命周期各个点执行的 Shell 命令,提供确定性控制。
正在加载图表...
Hook 配置格式
基本结构:
{
"hooks": [
{
"event": "PostToolUse",
"matcher": "Edit:**/*.ts",
"command": {
"type": "Bash",
"command": "prettier --write \"$FILE_PATH\""
}
}
]
}
配置字段:
| 字段 | 说明 | 示例 |
|---|---|---|
event | Hook 事件类型 | PreToolUse, PostToolUse |
matcher | 工具名称匹配模式 | Edit:**/*.ts, Bash:* |
command.type | 命令类型 | Bash |
command.command | 要执行的命令 | prettier --write "$FILE_PATH" |
实战示例
示例 1: 自动代码格式化
示例 2: 操作审计日志
示例 3: 文件保护
示例 4: Git 自动提交
{
"hooks": [
{
"event": "Stop",
"matcher": "*",
"command": {
"type": "Bash",
"command": "git diff --quiet || (git add -A && git commit -m '🤖 Auto-commit by Claude Code' && echo '✅ Changes committed')"
}
}
]
}
示例 5: 系统通知
Hook 最佳实践
正在加载图表...
Hook 安全警告
⚠️ 重要安全提醒:
- 🔒 Hooks 执行任意 Shell 命令,具有完全系统访问权限
- 🔒 恶意 Hook 可以窃取数据、删除文件、执行恶意代码
- 🔒 始终审查第三方 Hooks
- 🔒 使用
.claude/settings.local.json存储敏感配置(不要提交到 Git) - 🔒 测试 Hooks 前先备份重要数据
💻 Slash Commands 完全指南
Commands 概述
Slash Commands 将常用提示词保存为 Markdown 文件,快速复用。
正在加载图表...
创建 Slash Commands
基本命令
创建项目命令:
mkdir -p .claude/commands
cat > .claude/commands/optimize.md << 'EOF'
分析这段代码的性能问题,并提供优化建议。
重点关注:
- 时间复杂度
- 空间复杂度
- 可读性
- 最佳实践
EOF
使用: /optimize
创建用户命令:
mkdir -p ~/.claude/commands
cat > ~/.claude/commands/security-review.md << 'EOF'
---
description: 安全漏洞审查
---
检查这段代码的安全漏洞,包括:
- SQL 注入
- XSS 攻击
- CSRF 漏洞
- 敏感信息泄露
- 认证和授权问题
EOF
使用: /security-review
带参数的命令
---
description: 修复 GitHub issue
argument-hint: <issue-number> <priority>
---
修复 GitHub issue #$1
优先级:$2
要求:
- 分析问题根因
- 提供解决方案
- 编写单元测试
- 更新文档
使用: /fix-issue 123 high
命名空间组织
.claude/commands/
├── frontend/
│ ├── component.md # /component (project:frontend)
│ ├── style.md # /style (project:frontend)
│ └── test.md # /test (project:frontend)
├── backend/
│ ├── api.md # /api (project:backend)
│ ├── database.md # /database (project:backend)
│ └── migration.md # /migration (project:backend)
└── docs/
├── readme.md # /readme (project:docs)
└── api-docs.md # /api-docs (project:docs)
使用 Bash 执行
---
description: Git 状态报告
allowed-tools: Bash(git status:*), Bash(git diff:*)
---
当前 Git 状态:
!`git status`
未提交的更改:
!`git diff --stat`
最近的提交:
!`git log -5 --oneline`
使用: /git-status
文件引用
---
description: 代码审查
---
审查以下文件的代码质量:
@src/components/UserProfile.tsx
@src/services/authService.ts
@tests/auth.test.ts
重点检查:
- 代码规范
- 错误处理
- 测试覆盖率
使用: /review
高级命令示例
示例 1: 完整的 Feature 开发
示例 2: 数据库迁移
示例 3: 测试生成器
示例 4: PR 描述生成
Frontmatter 完整配置
SlashCommand 工具
Claude 可以自动调用你的自定义命令:
// Claude 内部使用 SlashCommand 工具
await slashCommand({
command: "/optimize src/utils/heavy-calculation.ts"
});
特性:
- ✅ 自动识别适合的命令
- ✅ 在对话中智能调用
- ✅ 只支持自定义命令(不支持内置命令如 /compact)
🤖 Agents & Subagents 完全指南
Subagents 概述
Subagents 是专业化的 AI 助手,拥有独立的上下文窗口。
正在加载图表...
Subagent 配置格式
基本结构:
配置字段:
| 字段 | 必需 | 说明 | 示例 |
|---|---|---|---|
name | ✅ | 小写标识符,使用连字符 | code-reviewer, test-generator |
description | ✅ | 何时使用此 Agent | Reviews code for quality and best practices |
tools | ❌ | 允许的工具列表,省略则继承全部 | Read, Grep, Glob |
model | ❌ | 模型选择 | sonnet, opus, haiku, inherit |
实战 Subagents 示例
示例 1: 代码审查 Agent
使用:
请使用 code-reviewer agent 审查 src/auth/login.ts
示例 2: 测试生成 Agent
使用:
用 test-generator agent 为 src/utils/payment.ts 生成测试
示例 3: 文档生成 Agent
使用:
让 doc-writer agent 为 src/api/client.ts 生成 API 文档
示例 4: 重构专家 Agent
示例 5: 性能优化 Agent
示例 6: 安全审计 Agent
---
name: security-auditor
description: Performs security vulnerability assessment
tools: Read, Grep, WebSearch
model: opus
---
You are a cybersecurity expert specializing in application security.
Security checklist (OWASP Top 10):
1. ✅ Injection (SQL, NoSQL, Command)
2. ✅ Broken Authentication
3. ✅ Sensitive Data Exposure
4. ✅ XML External Entities (XXE)
5. ✅ Broken Access Control
6. ✅ Security Misconfiguration
7. ✅ Cross-Site Scripting (XSS)
8. ✅ Insecure Deserialization
9. ✅ Using Components with Known Vulnerabilities
10. ✅ Insufficient Logging & Monitoring
Audit process:
1. Code review for vulnerabilities
2. Dependency vulnerability scan
3. Authentication/Authorization review
4. Input validation check
5. Output encoding verification
6. Cryptography review
7. Configuration assessment
Output format:
## Critical Vulnerabilities (P0)
- [CVE/CWE] Description
- Impact: ...
- Remediation: ...
## High Priority (P1)
...
## Medium Priority (P2)
...
## Best Practice Recommendations
...
Search for latest CVEs and security patches.
Subagent 编排模式
正在加载图表...
Subagent 最佳实践
正在加载图表...
🔌 Plugin System
Plugin 概述
Plugins 是打包的命令 + Agents + Hooks + MCP 服务器集合。
正在加载图表...
使用插件:
# 安装插件
/plugin install github:username/plugin-name
# 查看已安装的插件
/plugin list
# 更新插件
/plugin update plugin-name
# 卸载插件
/plugin remove plugin-name
🚀 最佳实践总结
开发工作流建议
正在加载图表...
配置优先级
1. 本地项目配置 (.claude/settings.local.json) - 最高优先级
2. 项目配置 (.claude/settings.json) - 团队共享
3. 用户配置 (~/.claude/settings.json) - 个人偏好
4. 组织配置 (企业部署) - 全局策略
文件组织建议
project/
├── .claude/
│ ├── settings.json # 项目配置
│ ├── settings.local.json # 本地配置(不提交)
│ ├── commands/ # 项目命令
│ │ ├── frontend/
│ │ │ ├── component.md
│ │ │ └── test.md
│ │ ├── backend/
│ │ │ ├── api.md
│ │ │ └── migration.md
│ │ └── common/
│ │ ├── optimize.md
│ │ └── refactor.md
│ ├── agents/ # 项目 Agents
│ │ ├── code-reviewer.md
│ │ ├── test-generator.md
│ │ ├── doc-writer.md
│ │ └── security-auditor.md
│ └── README.md # 使用说明
├── .gitignore # 排除 settings.local.json
└── docs/
└── claude-code-guide.md # 团队指南
.gitignore 配置:
# Claude Code 本地配置(包含敏感信息)
.claude/settings.local.json
# 审计日志(可选)
.claude/audit.log
.claude/audit.json
团队协作建议
正在加载图表...
📚 学习资源
官方资源
社区资源
- 📄 Awesome Claude Code - 精选资源集合
- 📦 Claude Command Suite - 专业命令集
- 🤖 Awesome Subagents - 100+ Subagents
- 🔧 Claude Code Hooks Mastery - Hooks 完全指南
技术深度文章
- How Claude Code is Built - 技术架构深度解析
- Claude Code Plan Mode - Plan 模式详解
- Context Engineering - 上下文工程
🎯 下一步行动
快速开始
-
安装 Claude Code
npm install -g @anthropic-ai/claude-code # 或 brew install claude-code -
初始化项目
cd your-project /init -
创建第一个命令
mkdir -p .claude/commands echo "审查这段代码的质量" > .claude/commands/review.md -
创建第一个 Agent
mkdir -p .claude/agents cat > .claude/agents/reviewer.md << 'EOF' --- name: reviewer description: Code quality reviewer tools: Read, Grep --- You are a code quality expert. Review code for best practices. EOF -
配置第一个 Hook
cat > .claude/settings.json << 'EOF' { "hooks": [ { "event": "PostToolUse", "matcher": "Edit:**/*.ts", "command": { "type": "Bash", "command": "prettier --write \"$FILE_PATH\"" } } ] } EOF
进阶学习路径
正在加载图表...
💡 总结
Claude Code 通过 Radical Simplicity 设计理念,将 AI 能力融入开发工作流:
核心优势:
- ⚡ 极速开发 - 5 PR/工程师/天,2.5-5x 生产力提升
- 🧠 智能编排 - 16 个 System Prompts + 400+ 工具
- 🔧 高度可扩展 - Hooks + Commands + Agents + Plugins
- 🎯 简洁架构 - 本地执行,最小化业务逻辑
三大扩展系统:
- Hooks - 确定性控制,自动化任务
- Slash Commands - 可复用工作流
- Subagents - 专业化 AI 助手
开始使用 Claude Code,让 AI 成为你的超级编程伙伴! 🚀
✨ 准备好用 AI 重新定义开发方式了吗? ✨
开始你的 Claude Code 之旅