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 层次结构:

  1. general-purpose - 最常用,处理 80% 的任务
  2. Agent Architect - 唯一能创建 Agent 的 Agent(元编程)
  3. Security Review - 最大的 Prompt (10.8KB),最复杂的审查逻辑
  4. 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\""
      }
    }
  ]
}

配置字段:

字段说明示例
eventHook 事件类型PreToolUse, PostToolUse
matcher工具名称匹配模式Edit:**/*.ts, Bash:*
command.type命令类型Bash
command.command要执行的命令prettier --write "$FILE_PATH"

实战示例

示例 1: 自动代码格式化

{
"hooks": [
  {
    "event": "PostToolUse",
    "matcher": "Edit:**/*.ts",
点击展开查看完整代码28

示例 2: 操作审计日志

{
"hooks": [
  {
    "event": "PreToolUse",
    "matcher": "*",
点击展开查看完整代码20

示例 3: 文件保护

{
"hooks": [
  {
    "event": "PreToolUse",
    "matcher": "Edit:**/production/**",
点击展开查看完整代码20

示例 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: 系统通知

{
"hooks": [
  {
    "event": "Notification",
    "matcher": "*",
点击展开查看完整代码20

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 开发

---
description: 创建新功能
argument-hint: <feature-name>
allowed-tools: Bash(git:*), Write, Edit, Read
model: sonnet
点击展开查看完整代码25

示例 2: 数据库迁移

---
description: 生成数据库迁移
argument-hint: <migration-name>
allowed-tools: Bash(ls:*), Bash(cat:*), Write
---
点击展开查看完整代码20

示例 3: 测试生成器

---
description: 为文件生成测试
allowed-tools: Read
model: sonnet
---
点击展开查看完整代码21

示例 4: PR 描述生成

---
description: 生成 PR 描述
allowed-tools: Bash(git:*)
disable-model-invocation: false
---
点击展开查看完整代码35

Frontmatter 完整配置

---
# 命令描述(显示在 /help 中)
description: 命令的简短描述

# 参数提示
点击展开查看完整代码18

SlashCommand 工具

Claude 可以自动调用你的自定义命令:

// Claude 内部使用 SlashCommand 工具
await slashCommand({
  command: "/optimize src/utils/heavy-calculation.ts"
});

特性:

  • ✅ 自动识别适合的命令
  • ✅ 在对话中智能调用
  • ✅ 只支持自定义命令(不支持内置命令如 /compact)

🤖 Agents & Subagents 完全指南

Subagents 概述

Subagents 是专业化的 AI 助手,拥有独立的上下文窗口。

正在加载图表...

Subagent 配置格式

基本结构:

---
name: agent-identifier
description: When to use this agent (used for automatic matching)
tools: Tool1, Tool2, Tool3
model: sonnet
点击展开查看完整代码21

配置字段:

字段必需说明示例
name小写标识符,使用连字符code-reviewer, test-generator
description何时使用此 AgentReviews code for quality and best practices
tools允许的工具列表,省略则继承全部Read, Grep, Glob
model模型选择sonnet, opus, haiku, inherit

实战 Subagents 示例

示例 1: 代码审查 Agent

---
name: code-reviewer
description: Reviews code for quality, security, and best practices
tools: Read, Grep, Glob
model: sonnet
点击展开查看完整代码28

使用:

请使用 code-reviewer agent 审查 src/auth/login.ts

示例 2: 测试生成 Agent

---
name: test-generator
description: Generates comprehensive unit and integration tests
tools: Read, Write, Glob
model: sonnet
点击展开查看完整代码42

使用:

用 test-generator agent 为 src/utils/payment.ts 生成测试

示例 3: 文档生成 Agent

---
name: doc-writer
description: Creates comprehensive technical documentation
tools: Read, Write, Grep, Glob
model: sonnet
点击展开查看完整代码46

使用:

让 doc-writer agent 为 src/api/client.ts 生成 API 文档

示例 4: 重构专家 Agent

---
name: refactor-expert
description: Refactors code for better architecture and maintainability
tools: Read, Edit, Grep, Glob
model: opus
点击展开查看完整代码42

示例 5: 性能优化 Agent

---
name: performance-optimizer
description: Analyzes and optimizes code performance
tools: Read, Edit, Bash
model: sonnet
点击展开查看完整代码40

示例 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

团队协作建议

正在加载图表...

📚 学习资源

官方资源

社区资源

技术深度文章


🎯 下一步行动

快速开始

  1. 安装 Claude Code

    npm install -g @anthropic-ai/claude-code
    # 或
    brew install claude-code
  2. 初始化项目

    cd your-project
    /init
  3. 创建第一个命令

    mkdir -p .claude/commands
    echo "审查这段代码的质量" > .claude/commands/review.md
  4. 创建第一个 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
  5. 配置第一个 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
  • 🎯 简洁架构 - 本地执行,最小化业务逻辑

三大扩展系统:

  1. Hooks - 确定性控制,自动化任务
  2. Slash Commands - 可复用工作流
  3. Subagents - 专业化 AI 助手

开始使用 Claude Code,让 AI 成为你的超级编程伙伴! 🚀


准备好用 AI 重新定义开发方式了吗?

开始你的 Claude Code 之旅