Claude Code Skills 完全指南:用 SKILL.md 打造自定义工作流(附实战模板)
手把手教你创建 Claude Code Skills,包含 SKILL.md 编写方法、3 个即用模板、社区热门 Skills 推荐,以及 Skills 与 CLAUDE.md、Hooks 的协同使用技巧。
Claude CodeSkillsAutomationWorkflow
2341  字
2026-02-28 02:00 +0000

Claude Code 是目前最强大的 AI 编程工具之一。但开箱即用时,它是通用的——不了解你团队的代码审查清单,不清楚你的 API 文档格式,也不知道你的 commit 信息规范。
每次从头解释同样的工作流,都是在浪费 token 和时间。
Claude Code Skills 就是为此而生的。Skill 是一组可复用的指令——以简单的 Markdown 文件封装——教会 Claude 如何按照你的方式执行特定任务。创建完成后,Claude 会自动检测何时需要使用某个 Skill,无需你手动干预。
本指南涵盖所有内容:什么是 Skills、如何创建、三个可直接复制使用的实战模板、不断壮大的 Skills 生态,以及 Skills 如何与 CLAUDE.md 和 Hooks 协同工作。
什么是 Claude Code Skills?
一个 Skill 就是一个包含 SKILL.md 文件的文件夹,由两部分组成:
- YAML 前置数据(在
---标记之间)——告诉 Claude 何时使用该 Skill - Markdown 内容——Claude 在触发 Skill 时遵循的具体指令
最简单的 Skill 长这样:
---
name: daily-standup
description: Generate a daily standup summary from git activity
---
Summarize today's git commits into a standup format:
1. What I did yesterday
2. What I'm doing today
3. Any blockers
就这么简单。不需要写代码,不需要配置文件,不需要构建步骤。一个 Markdown 文件就能教会 Claude 一项新能力。
Skills 遵循 Agent Skills 开放标准,这意味着它们可以跨多个 AI 工具使用——不仅限于 Claude Code。不过 Claude Code 扩展了该标准,增加了调用控制、子代理执行和动态上下文注入等功能。
Skills 是如何被触发的
Claude Code 采用渐进式加载模式来管理 Skills:
- 启动时:仅加载前置数据(名称 + 描述)——每个 Skill 大约消耗 100 token
- 对话过程中:Claude 读取描述来判断哪些 Skills 与当前内容相关
- 激活时:当 Claude 判断需要某个 Skill 时,才加载其完整内容
这意味着你可以安装几十个 Skills 而不会撑爆上下文窗口。Claude 只在需要时才加载所需内容。
你也可以使用 Skill 名称作为斜杠命令来手动触发:
/daily-standup
两种方式都有效。Claude 可以从对话中自动判断相关性,你也可以在明确知道需求时直接调用。
Skills 与 Slash Commands 的区别
如果你一直在用 Claude Code,可能已经熟悉自定义斜杠命令(.claude/commands/ 中的文件)。以下是二者的对比:
| 特性 | Slash Commands(旧版) | Skills |
|---|---|---|
| 位置 | .claude/commands/review.md | .claude/skills/review/SKILL.md |
| 调用方式 | 手动(/review) | 自动触发 + 手动(/review) |
| 支持附属文件 | 否 | 是(模板、脚本、示例) |
| 前置数据 | 可选 | 支持(调用控制、工具、模型) |
| 状态 | 仍然可用 | 推荐使用 |
核心区别:Slash Commands 每次都需要你手动输入 /command-name。而 Skills 在 Claude 检测到相关对话上下文时可以自动触发——无需手动操作。
自定义斜杠命令仍然有效。.claude/commands/review.md 和 .claude/skills/review/SKILL.md 都会创建 /review 命令且行为一致。但 Skills 增加了可选功能:附属文件目录、调用控制的前置数据,以及自动触发。如果 Skill 和命令同名,Skill 优先。
如果从零开始,请使用 Skills。如果已有旧命令,它们会继续工作——方便时再迁移。
什么时候该用 Skills
Skills 在特定场景下效果最好。以下是使用它们的判断标准:
适合用 Skills 的场景:
- 重复性工作流 —— 代码审查、PR 创建、文档生成、部署清单。任何你每次都用同一方式完成的任务。
- 领域专业知识 —— 行业特定知识、公司规范、框架特定模式。Claude 默认不知道的内容。
- 固定输出格式 —— 站会报告、变更日志、API 文档、测试计划。输出结构很重要的任务。
- 团队规范 —— 编码约定、架构模式、命名规则。需要每个团队成员一致遵循的内容。
不适合用 Skills 的场景:
- 静态项目上下文 —— 使用 CLAUDE.md 代替。技术栈、项目结构和通用约定应放在 CLAUDE.md 中,而非 Skill 里。
- 必须执行的操作 —— 使用 Hooks 代替。如果某个步骤必须始终运行(如编辑文件后自动 lint),Hook 可以保证执行。Skills 是建议性的——Claude 自行决定是否使用。
- 一次性任务 —— 直接在提示中描述即可。Skills 是为你会反复使用的模式设计的。
创建你的第一个 Skill
我们来一步步构建一个 Skill。创建一个执行团队代码审查清单的代码审查 Skill。
第 1 步:创建 Skill 目录
Skills 可以放在两个位置:
| 级别 | 路径 | 作用范围 |
|---|---|---|
| 个人 | ~/.claude/skills/<skill-name>/SKILL.md | 你的所有项目 |
| 项目 | .claude/skills/<skill-name>/SKILL.md | 仅限当前项目 |
创建适用于所有项目的代码审查 Skill:
mkdir -p ~/.claude/skills/code-review
创建项目专属 Skill:
mkdir -p .claude/skills/code-review
第 2 步:编写 SKILL.md
创建 ~/.claude/skills/code-review/SKILL.md:
---
name: code-review
description: Perform a thorough code review following the team checklist. Use when reviewing code changes, pull requests, or when the user asks for a code review.
---
# Code Review Checklist
When reviewing code, evaluate every item in this checklist and provide a structured report.
## Review Process
1. **Read the diff** — Understand what changed and why
2. **Check each category** — Go through every section below
3. **Rate severity** — Flag issues as Critical, Warning, or Suggestion
4. **Summarize** — Provide an overall assessment
## Checklist Categories
### Correctness
- Does the logic handle edge cases?
- Are error paths handled properly?
- Could any input cause unexpected behavior?
### Security
- Is user input validated and sanitized?
- Are there any SQL injection, XSS, or CSRF risks?
- Are secrets hardcoded anywhere?
### Performance
- Are there unnecessary database queries or API calls?
- Could any loop cause O(n^2) or worse performance?
- Are large datasets paginated?
### Maintainability
- Are functions and variables named clearly?
- Is the code DRY (Don't Repeat Yourself)?
- Would a new team member understand this code?
### Testing
- Are there tests for the new code?
- Do tests cover edge cases and error paths?
- Are tests deterministic (no flaky tests)?
## Output Format
Structure your review as:
### Summary
[One-paragraph overall assessment]
### Critical Issues
[Must fix before merging]
### Warnings
[Should fix, but not blocking]
### Suggestions
[Nice to have improvements]
### Approved?
[YES / NO / YES WITH CONDITIONS]
第 3 步:测试 Skill
打开 Claude Code,用两种方式测试:
自动触发 —— 提出匹配描述的问题:
Review the changes in src/auth/login.ts
Claude 应该识别这是代码审查请求,并自动应用 Skill 中的清单。
手动调用 —— 使用斜杠命令:
/code-review src/auth/login.ts
两种方式都应该生成遵循你清单格式的结构化审查。
第 4 步:持续迭代
第一个版本不会完美。观察 Claude 如何使用该 Skill,然后优化:
- 如果 Claude 遗漏了清单项目,让它们更明确
- 如果输出格式不太对,添加一个示例
- 如果 Skill 不该触发时触发了,缩小描述范围
- 如果该触发时没触发,扩大描述范围
SKILL.md 详细解析
每个 SKILL.md 文件都有相同的结构:YAML 前置数据用于元信息,后接 Markdown 内容用于指令。
前置数据字段参考
---
name: my-skill
description: What this skill does and when to use it
disable-model-invocation: true
user-invocable: true
allowed-tools: Read, Grep, Glob
argument-hint: [filename] [format]
model: opus
context: fork
agent: Explore
---
各字段说明:
| 字段 | 必需 | 说明 |
|---|---|---|
name | 否 | 显示名称和斜杠命令。默认为目录名。小写、连字符、最多 64 字符。 |
description | 推荐 | Skill 的功能描述。Claude 根据它判断何时加载。 |
disable-model-invocation | 否 | 设为 true 可禁止自动触发。用户必须手动输入 /name。 |
user-invocable | 否 | 设为 false 可从 / 菜单中隐藏。仅作为后台知识。 |
allowed-tools | 否 | Skill 激活时 Claude 可无需确认使用的工具。 |
argument-hint | 否 | 自动补全提示(如 [issue-number])。 |
model | 否 | Skill 激活时覆盖使用的模型。 |
context | 否 | 设为 fork 可在子代理中运行。 |
agent | 否 | 当 context: fork 时指定子代理类型。 |
内容结构
前置数据之后的 Markdown 内容完全自由,但高效的 Skills 通常包含这些部分:
- 概述 —— Skill 的功能(1-2 句话)
- 输入要求 —— Claude 需要用户提供什么
- 分步指令 —— 实际的工作流
- 输出格式 —— 如何组织结果
- 约束条件 —— 需要避免的、边界情况、护栏
变量替换
Skills 支持内容中的动态变量:
| 变量 | 说明 |
|---|---|
$ARGUMENTS | 调用时传递的所有参数 |
$ARGUMENTS[0]、$0 | 第一个参数 |
$ARGUMENTS[1]、$1 | 第二个参数 |
${CLAUDE_SESSION_ID} | 当前会话 ID |
使用参数的示例:
---
name: fix-issue
description: Fix a GitHub issue by number
disable-model-invocation: true
---
Fix GitHub issue #$ARGUMENTS following our coding standards:
1. Read the issue description with `gh issue view $0`
2. Understand the requirements
3. Implement the fix
4. Write tests
5. Create a commit referencing the issue
执行 /fix-issue 423 时,$ARGUMENTS 被替换为 423,$0 也替换为 423。
动态上下文注入
!`command` 语法可在 Skill 内容发送给 Claude 之前执行 shell 命令。命令输出会替换占位符:
---
name: pr-summary
description: Summarize the current pull request
context: fork
agent: Explore
---
## Current PR Context
- PR diff: !`gh pr diff`
- PR comments: !`gh pr view --comments`
- Changed files: !`gh pr diff --name-only`
## Task
Provide a concise summary of this pull request highlighting:
1. What changed and why
2. Potential risks
3. Suggested reviewers
运行此 Skill 时,每个 !`command` 会立即执行。Claude 只看到包含实际 PR 数据的最终渲染内容。
三个实战 Skills(可直接复制使用)
以下三个生产级 Skills 可以立即投入使用。
Skill 1:带清单的代码审查
已在上面的教程中详细介绍。快速参考文件路径:
~/.claude/skills/code-review/SKILL.md
Skill 2:API 文档生成器
这个 Skill 从源代码生成统一格式的 API 文档。它读取 endpoint handler 并生成标准化文档。
创建 ~/.claude/skills/api-docs/SKILL.md:
---
name: api-docs
description: Generate API documentation from source code. Use when the user asks to document an API endpoint, route handler, or controller. Also triggers when creating or updating API reference documentation.
allowed-tools: Read, Grep, Glob
---
# API Documentation Generator
Generate comprehensive API documentation for the specified endpoint(s).
## Process
1. **Find the endpoint** — Locate the route handler, controller, or API function
2. **Analyze the code** — Read the implementation to understand:
- HTTP method and path
- Request parameters (path, query, body)
- Authentication requirements
- Response format and status codes
- Error handling
3. **Check for existing docs** — Look for JSDoc, docstrings, or OpenAPI annotations
4. **Generate documentation** — Follow the output format below
## Output Format
For each endpoint, generate:
### `METHOD /path/to/endpoint`
**Description**: One-sentence description of what this endpoint does.
**Authentication**: Required / Optional / None
**Parameters**:
| Name | In | Type | Required | Description |
|------|-----|------|----------|-------------|
| id | path | string | Yes | Resource identifier |
| limit | query | integer | No | Max results (default: 20) |
**Request Body** (if applicable):
```json
{
"field": "type — description"
}
Response 200 OK:
{
"data": "example response"
}
Error Responses:
| Status | Description |
|---|---|
| 400 | Invalid request parameters |
| 401 | Authentication required |
| 404 | Resource not found |
Example:
curl -X METHOD https://api.example.com/path \
-H "Authorization: Bearer TOKEN" \
-d '{"field": "value"}'
Constraints
- Always include real field names and types from the source code
- Document all possible error responses, not just the happy path
- If authentication middleware is present, always note it
- Use the project’s actual base URL if found in configuration
- Flag any undocumented endpoints as needing attention
### Skill 3:Git Commit 信息标准化工具
这个 Skill 根据你团队的规则强制执行 [Conventional Commits](https://www.conventionalcommits.org/) 格式。
创建 `~/.claude/skills/commit-msg/SKILL.md`:
```yaml
---
name: commit-msg
description: Generate a standardized git commit message following Conventional Commits format. Use when the user asks to commit, create a commit message, or after completing a task that should be committed.
disable-model-invocation: true
---
# Commit Message Generator
Generate a git commit message following our team's Conventional Commits standard.
## Process
1. Run `git diff --staged` to see what's being committed
2. If nothing is staged, run `git diff` and ask the user what to stage
3. Analyze the changes to determine the commit type and scope
4. Generate the commit message following the format below
## Commit Format
Comments
Join the discussion — requires a GitHub account