Skills 是 Claude Code 最灵活的扩展方式��一个 SKILL.md 文件,可以是随时调用的命令、按需加载的知识库,也可以是在独立 Subagent 中运行的隔离工作流。
四大内置 Skills
Claude Code 随附四个开箱即用的 Bundled Skills,无需安装:
/simplify — 代码质量自动优化
审查最近修改的文件,处理代码复用、质量和效率问题,然后自动修复。
适用时机:实现功能或修复 Bug 后,快速清理代码。
工作方式:并行启动三个审查 Agent(代码复用、代码质量、效率),汇总发现并应用修复。
/simplify
/simplify focus on memory efficiency # 聚焦特定问题
/batch <instruction> — 大规模并行变更
在整个代码库中协调大规模变更。提供变更描述,/batch 研究代码库,将工作分解为 5-30 个独立单元,呈现计划供你审批。批准后,为每个单元启动一个后台 Agent,在独立 Git Worktree 中运行,完成后各自开 PR。
需要:Git 仓库。
/batch migrate src/ from Solid to React
/batch add TypeScript types to all functions in lib/
/batch add unit tests for all exported functions
/debug [description] — 会话调试
读取当前 Claude Code 会话的调试日志,排查异常行为。可选描述问题以聚焦分析。
/debug
/debug Claude keeps forgetting my instructions after compact
/claude-api — Claude API 参考加载
为项目语言加载 Claude API 参考资料(支持 Python、TypeScript、Java、Go、Ruby、C#、PHP、cURL)和 Agent SDK 参考(Python/TypeScript),涵盖工具使用、流式、批处理、结构化输出和常见坑。
自动触发:代码中导入 anthropic、@anthropic-ai/sdk 或 claude_agent_sdk 时自动激活。
创建你的第一个 Skill
目录结构
~/.claude/skills/my-skill/
├── SKILL.md # 必须:主指令文件
├── template.md # 可选:模板文件
├── examples/
│ └── sample.md # 可选:示例输出
└── scripts/
└── validate.sh # 可选:可执行脚本
SKILL.md 基本结构
---
name: explain-code
description: Explains code with visual diagrams and analogies.
Use when explaining how code works or user asks
"how does this work?"
---
When explaining code, always include:
1. **Start with an analogy**: Compare to something from everyday life
2. **Draw a diagram**: Use ASCII art to show flow or structure
3. **Walk through the code**: Step-by-step explanation
4. **Highlight a gotcha**: Common mistake or misconception调用方式:
/explain-code src/auth/login.ts # 直接调用
"how does this work?" # 触发自动调用
Skill 存储位置和优先级
| 位置 | 路径 | 适用范围 | 优先级 |
|---|---|---|---|
| 企业托管 | 托管策略目录 | 组织所有用户 | 最高 |
| 个人 | ~/.claude/skills/<name>/SKILL.md | 所有项目 | 2 |
| 项目 | .claude/skills/<name>/SKILL.md | 当前项目 | 3 |
| Plugin | <plugin>/skills/<name>/SKILL.md | Plugin 启用的地方 | 最低(命名空间隔离) |
同名 Skill 时:优先级高的覆盖低的(企业 > 个人 > 项目)。Plugin Skills 使用 plugin-name:skill-name 命名空间,不会与其他级别冲突。
Monorepo 自动发现:编辑 packages/frontend/ 下的文件时,Claude Code 自动加载 packages/frontend/.claude/skills/ 中的 Skills。
Frontmatter 完整配置参考
---
name: deploy # Skill 名称(创建 /deploy 命令)
description: Deploy to production # 描述(Claude 自动调用的依据)
disable-model-invocation: true # 禁止 Claude 自动触发(只能手动 /deploy)
context: fork # 在独立 Subagent 中运行
tools: # 限制此 Skill 可用的工具
allow:
- Bash
- Read
deny:
- Write
---关键字段说明
| 字段 | 默认值 | 说明 |
|---|---|---|
name | 目录名 | Slash 命令名称(/name) |
description | — | Claude 判断何时自动使用的依据,写清楚触发场景 |
disable-model-invocation | false | true = 只能手动 /name 调用,防止 Claude 自动触发 |
context | inline | fork = 在独立 Subagent 中运行 |
tools.allow | 继承全部 | 只允许列表中的工具 |
tools.deny | 空 | 明确禁止的工具 |
两类 Skill 内容
参考型(Reference)— 持续注入知识
---
name: api-conventions
description: API 设计约定,编写 API 端点时使用
---
编写 API 端点时:
- 使用 RESTful 命名约定
- 返回一致的错误格式:{"error": {"code": "...", "message": "..."}}
- 包含请求验证(zod/joi)
- POST 返回 201,DELETE 返回 204这类 Skill 在当前对话 Context 中内联运行,Claude 随时可以参考。
任务型(Task)— 执行特定操作
---
name: commit
description: Create a git commit with conventional commit format
disable-model-invocation: true
---
1. Run `git diff --staged` to see what's staged
2. Analyze the changes and determine the commit type
(feat/fix/docs/style/refactor/test/chore)
3. Write a commit message: `<type>(<scope>): <description>`
4. Run `git commit -m "<message>"`
5. Confirm the commit was created successfullydisable-model-invocation: true 确保只有你手动输入 /commit 时才执行,Claude 不会自动触发。
动态上下文注入
Skill 可以在加载时动态执行命令,将命令输出注入上下文:
---
name: db-query
description: Database query helper with current schema
---
# Current Database Schema
$CURRENT_SCHEMA=$(psql $DATABASE_URL -c "\d" 2>/dev/null || echo "Schema unavailable")
---
当写数据库查询时,参考上面的 Schema。使用适当的索引,避免全表扫描。Skill 加载时自动执行 psql 命令,将实时 Schema 注入到 Claude 的上下文中。
其他动态上下文示例:
# 当前 Git 状态
$GIT_STATUS=$(git status --short)
# 环境信息
$NODE_VERSION=$(node --version)
$PYTHON_VERSION=$(python3 --version)
# 最近的测试结果
$LAST_TEST=$(cat .test-cache/last-result.txt 2>/dev/null || echo "No cache")在 Subagent 中运行 Skill
将 context: fork 加到 Frontmatter,让 Skill 在独立的 Subagent 中运行,不占用主对话 Context:
---
name: research
description: 深度研究某个话题,读取大量文件后返回摘要
context: fork
tools:
allow:
- Read
- Grep
- Glob
- WebFetch
---
对指定话题进行深度研究:
1. 搜索整个代码库的相关文件
2. 阅读所有相关文档
3. 搜索网络了解背景
4. 返回一份结构化的研究报告
只返回最终报告,不要显示中间步骤。这个 Skill 在独立 Subagent 中运行,读取数十个文件的详细内容留在 Subagent Context 里,主对话只收到最终报告摘要。
向 Skill 传递参数
/skill-name arg1 arg2
/deploy production v2.1.0
/review src/auth/login.ts --focus security
在 SKILL.md 中用 $ARGUMENTS 变量引用传入的参数:
---
name: deploy
disable-model-invocation: true
---
Deploy to environment: $ARGUMENTS
1. Validate the target environment
2. Run pre-deployment checks
3. Execute deployment
4. Verify the deployment succeeded可用字符串替换变量
| 变量 | 内容 |
|---|---|
$ARGUMENTS | 调用 Skill 时传入的参数 |
$CURRENT_FILE | 当前在 IDE 中打开的文件路径 |
$CURRENT_SELECTION | IDE 中当前选中的文本 |
$PROJECT_ROOT | 项目根目录路径 |
故障排查
| 问题 | 解决方案 |
|---|---|
| Skill 不触发 | 检查 description 是否清晰描述触发场景;检查 disable-model-invocation |
| Skill 触发太频繁 | 改写 description 缩小触发范围,或添加 disable-model-invocation: true |
| Claude 看不到我的 Skill | 检查文件路径(必须有 SKILL.md,不是 skill.md);Skill 太多时 Claude 可能只加载描述,调用时才加载完整内容 |
原文:Extend Claude with skills | 来源:Anthropic 官方文档