教程

Claude Code Skills 进阶指南:四大内置技能、动态上下文注入、Subagent 运行与参数传递

Claude Code Skills 进阶完整指南:四大内置 Skill(/simplify 三 Agent 并行优化/batch 5-30 单元并行大规模变更//debug 会话调试//claude-api 自动加载 API 参考)、目录结构和四级存储位置(优先级规则)、完整 Frontmatter 字段(disable-model-invocation/context/tools.allow/deny)、参考型 vs 任务型内容对比、动态上下文注入($GIT_STATUS/$CURRENT_SCHEMA)、context:fork 在 Subagent 运行、$ARGUMENTS 参数传递。

2026/3/67分钟 阅读ClaudeEagle

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/sdkclaude_agent_sdk 时自动激活。

创建你的第一个 Skill

目录结构

~/.claude/skills/my-skill/ ├── SKILL.md # 必须:主指令文件 ├── template.md # 可选:模板文件 ├── examples/ │ └── sample.md # 可选:示例输出 └── scripts/ └── validate.sh # 可选:可执行脚本

SKILL.md 基本结构

markdown
---
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.mdPlugin 启用的地方最低(命名空间隔离)

同名 Skill 时:优先级高的覆盖低的(企业 > 个人 > 项目)。Plugin Skills 使用 plugin-name:skill-name 命名空间,不会与其他级别冲突。

Monorepo 自动发现:编辑 packages/frontend/ 下的文件时,Claude Code 自动加载 packages/frontend/.claude/skills/ 中的 Skills。

Frontmatter 完整配置参考

yaml
---
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
descriptionClaude 判断何时自动使用的依据,写清楚触发场景
disable-model-invocationfalsetrue = 只能手动 /name 调用,防止 Claude 自动触发
contextinlinefork = 在独立 Subagent 中运行
tools.allow继承全部只允许列表中的工具
tools.deny明确禁止的工具

两类 Skill 内容

参考型(Reference)— 持续注入知识

markdown
---
name: api-conventions
description: API 设计约定,编写 API 端点时使用
---

编写 API 端点时:
- 使用 RESTful 命名约定
- 返回一致的错误格式:{"error": {"code": "...", "message": "..."}}
- 包含请求验证(zod/joi)
- POST 返回 201,DELETE 返回 204

这类 Skill 在当前对话 Context 中内联运行,Claude 随时可以参考。

任务型(Task)— 执行特定操作

markdown
---
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 successfully

disable-model-invocation: true 确保只有你手动输入 /commit 时才执行,Claude 不会自动触发。

动态上下文注入

Skill 可以在加载时动态执行命令,将命令输出注入上下文:

markdown
---
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 的上下文中。

其他动态上下文示例:

bash
# 当前 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:

markdown
---
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 变量引用传入的参数:

markdown
---
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_SELECTIONIDE 中当前选中的文本
$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 官方文档

相关文章推荐

教程Claude Code Skills 完全指南:创建自定义技能、Bundled Skills 与高级模式Claude Code Skills 完全指南:5 个内置 Bundled Skills(/simplify 并行 3 Agent/\batch Git Worktree 并行/\debug 会话日志/\loop 定时执行/\claude-api 自动激活)、3 步创建自定义 Skill(目录/SKILL.md/测试)、存储位置与优先级(Enterprise/Personal/Project/Plugin 命名空间)、Frontmatter 完整配置(name/description/disable-model-invocation/tools)、两种内容类型(参考型内联/任务型手动)、高级模式($ARGUMENTS 字符串替换/支持文件目录/动态注入上下文/子 Agent 执行),以及三种共享方式和故障排查。2026/3/8教程Claude Code Skills 自定义命令:打造你的团队专属 AI 工作流Claude Code Skills 自定义命令完整教程:Skills vs CLAUDE.md 使用场景对比、内置 Skills 速览(/batch/simplify/loop)、SKILL.md 文件格式与 Frontmatter 配置、四大实用 Skills 示例(代码审查/部署检查/功能开发/团队 OnBoarding)、传参方式、子代理执行与 Git 团队共享。2026/3/14教程Claude Code 插件开发指南:plugin.json 结构、Skills/Hooks/MCP 集成与官方市场提交Claude Code 插件开发完整指南:独立配置 vs 插件对比(命名空间/适用场景)、5 步快速创建(目录/plugin.json 清单字段/Skill/本地 --plugin-dir 测试/分享)、完整插件目录结构(.claude-plugin/commands/skills/agents/hooks/mcp/.lsp.json/settings.json)、各组件配置示例(Skills SKILL.md/LSP 服务器.lsp.json/默认 settings.json agent 键)、从独立配置迁移步骤对比表、三步调试方法,以及通过 claude.ai 和 Console 提交官方市场的方式。2026/3/8教程Claude Code 插件开发指南:从 plugin.json 到 Skills/Agents/Hooks 打包发布全流程Claude Code Plugin 开发完整指南:独立配置 vs Plugin 选型(短名称 vs 命名空间)、5 分钟创建第一个 Plugin(plugin.json Manifest + SKILL.md)、Plugin 目录结构(skills/agents/hooks/settings/lsp)、LSP 服务器集成、随 Plugin 发布默认 Hooks 设置、--plugin-dir 本地测试、从独立配置迁移(名称变化说明)、Git/npm 发布方式,以及 /plugin install/list/enable/disable/remove 用户命令。2026/3/6教程Claude Code Skills 完全指南:创建自定义技能、内置技能与子代理执行Claude Code Skills 完全指南:三个内置技能(/simplify、/batch、/debug)、创建自定义 Skill 的完整步骤、Frontmatter 字段参考(invocation/tools 控制)、动态上下文注入、子代理执行,以及 Skills 的共享和发现方式。2026/3/2教程Claude Code Plugins 开发完整指南:从创建 Manifest 到发布到官方市场Claude Code Plugin 开发完整指南:从创建 plugin.json 清单、添加 Skills/Subagent/Hooks/MCP/LSP 服务器,到本地测试、团队分发和提交到 Anthropic 官方市场。附安全审查 Plugin 完整示例和现有配置迁移步骤。2026/2/28