TH1/Unity/Assets/Scripts/Document/WorkFlowHint/Skills进阶说明.md
2026-02-14 22:57:12 +08:00

26 KiB
Raw Blame History

Claude Skills解决了一个AI Agent面临的根本问题如何简单快速的让AI具备特定领域的专业能力

导读Claude Skills通过简单的文件夹+Markdown组织形式实现了知识的模块化、可复用化和可交易化。当Agent能够自主创建和演化Skills时我们将见证AI从"工具"真正转变为"专家团队"。


Anthropic刚刚发布了一个看似简单、实则颠覆性的功能——Claude Skills它解决了一个AI Agent面临的根本问题如何让通用型AI具备特定领域的专业能力过去我们通过prompt engineering、fine-tuning或构建自定义agent来实现但这些方案要么不够灵活要么成本高昂。

而Skills提出了一个优雅的解决方案——用文件夹和Markdown文件将你的专业知识打包成"技能包"让AI像学习新技能一样按需加载。巧妙的解决了Agents设定容易上下文爆炸而MCP又无法覆盖专业资源以及脚本琐碎等难题。

本文章将深入解析Claude Skills的技术架构、实现原理、与AI Agent、MCP的关系以及可能的应用场景第5部分。希望对你有所启发。


PART 01 - Agent能力困境与Skills突破

通用Agent的"技能缺失"难题

近年来Claude、GPT-4等大语言模型展现了惊人的通用能力但在实际企业应用中我们很快遇到了三个核心痛点

1. 专业知识鸿沟

AI可以写代码、分析数据但当你需要它按照公司特定的编码规范、遵守特定的审批流程、使用内部工具时它就捉襟见肘。传统做法是在每次对话开始时提供长篇prompt但这既低效又难以维护。

2. 上下文爆炸

企业级应用往往需要大量领域知识。某金融公司的风控系统有200页的规则文档全部塞进context window不仅昂贵还会稀释AI对当前任务的关注度。更糟糕的是当规则更新时你需要在所有地方同步修改prompt。

3. 能力不可复用

每个项目、每个团队都在重复打造自己的"定制化agent"。某公司的数据分析师配置了完美的数据清洗agent但隔壁的产品团队无法复用这套能力只能从头搭建。这种碎片化导致大量重复劳动。

现有解决方案的局限

在Skills出现之前业界主要有三种应对策略

方案1System Prompt工程

在系统提示词中塞入所有规则和指令这种做法最大问题就是Token消耗巨大且每次对话都要加载全部内容即使只用到其中10%。而且维护困难,修改规则需要更新所有集成点。

方案2Fine-tuning定制模型

用特定领域数据微调模型,成本巨高,而且训练周期长,一般长达数周,且模型一旦训练完成就"固化"了,难以快速迭代,只适合极其稳定的领域知识,对于动态变化的知识领域,爱莫能助,一般小企业也不会用这种笨重的方法。

方案3Subagents子代理模式

目前最流行的方案创建多个专门化的子agent由主agent调度同样面临通信开销大调试复杂且子agent之间难以共享context的问题有的多agent系统足以让人望而却步学习曲线陡峭需要复杂的agent编排逻辑。

Skills的创新突破

Claude Skills采用了一个简单而强大的设计理念将专业能力打包成"文件夹+Markdown"的标准格式。这个设计有三大突破:

1. 渐进式披露Progressive Disclosure

Skills不是一次性加载所有内容而是分三层逐步展开

  • 第一层Skill的名称和描述启动时加载到system prompt
  • 第二层SKILL.md的完整内容AI判断相关时才读取
  • 第三层:引用的辅助文件(需要时才加载)

这种设计的巧妙之处就是让AI可以管理几乎无限量的知识而不会撑爆context window。

2. 文件系统即接口

Skills不需要API、不需要数据库就是普通的文件夹结构。这意味着

  • 版本极易控制用Git即可管理Skills的迭代
  • 团队成员可以像编辑文档一样贡献Skills
  • 最重要的就是零学习成本任何会写Markdown的人都能创建Skills

3. 代码与文档的统一

Skills可以同时包含

  • 指令性知识(如"填写PDF表单的5个步骤"
  • 可执行代码(如extract_pdf_fields.py脚本)
  • 参考资料如公司编码规范PDF

AI可以读取指令也可以直接执行代码实现了声明式和过程式知识的完美融合。

对比表

维度 System Prompt Fine-tuning Subagents Claude Skills
上下文效率 低(全量加载) 高(内化于模型) 子agent隔离 高(按需加载)
迭代速度 快(秒级) 慢(周级) 中(分钟级) 快(秒级)
复用性 差(复制粘贴) 差(模型绑定) 中(需要调度层) 优(直接共享文件夹)
维护成本 高(多处同步) 低(模型固化) 高(复杂编排) 低(单一文件源)
灵活性
学习曲线

从这个对比可以看出Skills在几乎所有维度都取得了最优或次优的平衡。


PART 02 - Skills核心技术解析

从文件到能力

一个Skill的基本结构非常简单以Anthropic官方的PDF Skill为例

pdf/
├── SKILL.md              # 核心技能文件
├── reference.md          # 参考文档
├── forms.md              # 表单填写指令
└── scripts/
    └── extract_fields.py # PDF字段提取脚本

SKILL.md的结构

---
name: "PDF Processing"
description: "Read, analyze, and manipulate PDF documents including form filling"
---

# PDF Processing Skill

This skill enables Claude to work with PDF files effectively.

## Capabilities

1. Extract text and metadata from PDFs
2. Fill out PDF forms
3. Analyze document structure
4. ... (more instructions)

## Related Resources

- See [forms.md](forms.md) for form-filling workflows
- Use `scripts/extract_fields.py` to enumerate form fields

工作流程:

  1. 启动时Claude的system prompt中加载所有Skills的name和description

    Available Skills:
    - PDF Processing: Read, analyze, and manipulate PDF documents
    - Database Query: Execute SQL queries with safety checks...
    
  2. 用户请求"帮我填写这个PDF表单"

  3. Skills触发Claude判断需要PDF Processing Skill执行Bash命令读取SKILL.md

  4. 深度导航SKILL.md提到forms.mdClaude继续读取该文件获取详细步骤

  5. 代码执行需要提取表单字段时Claude运行extract_fields.py脚本

  6. 任务完成:根据加载的知识完成表单填写

渐进式披露

这是Skills最核心的设计模式。传统的prompt engineering会这样做

system_prompt = f"""
You are a helpful assistant.

PDF Processing Guide:
{load_file('pdf_guide.md')}  # 50KB

Database Guide:
{load_file('db_guide.md')}   # 30KB

... (10 more guides)
"""

每次对话都加载400KB的知识但用户可能只问一个简单的数学问题。

Skills的做法

Level 1 (System Prompt)

system_prompt = """
You are a helpful assistant with access to these skills:

Skills:
- PDF Processing: {short_description}
- Database Query: {short_description}
...

To use a skill, read its SKILL.md file.
"""

只加载几百字节的元数据。

Level 2 (按需加载)

# Claude执行cat pdf/SKILL.md
# 只有在判断需要时才触发

Level 3 (深度导航)

# Claude在SKILL.md中看到forms.md的引用
# 执行cat pdf/forms.md

这种设计让单个Skill可以包含几MB的文档而不影响常规对话的效率。


PART 03 - Skills与Agent

Skills与Subagents的架构对比

如果是传统的Subagents面对一个复杂专业任务要怎么做呢

Subagents模式

[Main Agent]
    |
    |-- dispatch_to() --> [PDF Agent] (独立context)
    |
    |-- dispatch_to() --> [Data Agent] (独立context)
    |
    |-- dispatch_to() --> [Code Agent] (独立context)

每个Subagent是独立的AI实例有自己的system prompt和context主agent需要显式调度和结果汇总。

Skills模式

[Claude Agent] + Context Window
    |
    |-- load_skill("pdf") --> [PDF知识注入context]
    |
    |-- load_skill("data") --> [Data知识注入context]
    |
[单一agent动态能力]

始终是同一个AI实例Skills动态注入到context中无需调度开销知识可以融合。

关键差异

维度 Subagents Skills
Context共享 困难(需要显式传递) 自然同一context
Token成本 多个AI实例 低(单实例)
并行能力 可以(多实例并发) 有限(单实例)
调试复杂度 跨agent追踪 低(单一轨迹)
知识融合 需要主agent整合 易(自然融合)

实际案例PDF表单填写

使用Subagents

  1. MainAgent识别需求
  2. dispatch_to(PDF_Agent, "分析这个PDF")
  3. PDF_Agent返回"这是一个包含15个字段的表单"
  4. MainAgent: "我需要填写数据"
  5. dispatch_to(Data_Agent, "获取用户信息")
  6. Data_Agent返回数据
  7. MainAgent: dispatch_to(PDF_Agent, "用这些数据填表")
  8. PDF_Agent填写完成

使用Skills

  1. Claude识别需求
  2. load_skill("pdf")
  3. 在同一context中分析PDF、获取数据、填写表单
  4. 完成(无需多次调度)

Skills的效率优势显而易见。但不能完全否认Subagents的作用在某些场景仍有价值例如

  • 需要真正并行处理多个任务
  • 不同子任务需要完全隔离的context
  • 使用不同的模型如GPT-4 + Claude混合等场景

Skills可以调用Subagents吗

答案是可以。这是一种高级模式:

---
name: "Complex Data Pipeline"
---

# Complex Data Pipeline Skill

## Step 1: Data Extraction
Use the data_extraction skill to pull data.

## Step 2: Parallel Processing
For heavy processing, spawn subagents:
- Subagent A: Data cleaning
- Subagent B: Feature engineering

## Step 3: Aggregation
Collect results and generate report.

这种混合架构结合了两者的优势Skills提供workflow框架Subagents处理计算密集型任务。


PART 04 - Skills与MCP

很多读者可能会很好奇这个Skills和MCP是什么关系呢

与MCPModel Context Protocol的关系

MCP是用于让AI访问外部数据源如数据库、API。Skills与MCP是互补的

  • MCP解决"数据访问"问题如何连接MySQL提供数据通道
  • Skills解决"知识处理"问题如何设计SQL查询提供操作规范

两者组合可能会带来爆炸级别的应用效果例如与企业CRM集成

案例企业CRM集成

# CRM Skill (SKILL.md)

## Prerequisites
- MCP server for Salesforce must be configured
- Required permissions: read_contacts, read_opportunities

## Capabilities
1. **Customer Lookup**: Find customer by name/email/ID
2. **Opportunity Analysis**: Analyze sales pipeline
3. **Report Generation**: Create custom reports

## Workflow Example
User: "生成Q4销售报告"

Steps:
1. Use MCP to query Salesforce API
2. Fetch opportunities with close_date in Q4
3. Group by sales_rep and product_line
4. Calculate metrics (total_value, conversion_rate, avg_deal_size)
5. Generate markdown report with visualizations

## MCP Configuration
mcp_servers:
  - name: salesforce
    protocol: oauth2
    endpoints:
      - contacts: /services/data/v57.0/sobjects/Contact
      - opportunities: /services/data/v57.0/sobjects/Opportunity

分工明确:

  • MCP:解决"连接到Salesforce"认证、API调用、数据格式
  • Skills:解决"如何分析销售数据"(业务逻辑、报告模板)

Agent自主创建Skills

当前Skills由人类创建但Anthropic的路线图指向一个更激进的未来Agent自主创建和演化Skills

场景想象:

你: "帮我分析这个CSV文件的客户流失模式"

Claude: 分析任务...
        发现:我在类似任务中重复使用相同的分析流程
        询问:"我注意到这是第5次分析客户流失我可以创建一个'Churn Analysis Skill'
              来标准化这个流程吗?"

你: "可以"

Claude: 创建 ~/.skills/churn-analysis/
        ├── SKILL.md (自动生成)
        ├── analysis_template.py (从历史任务提取)
        └── visualization_config.json

        完成!下次只需说"使用churn analysis skill"即可。

PART 05 - Skills数据流转分析

知识组织与流转

Skills的数据流遵循"元数据索引 → 内容加载 → 代码执行"的三阶段模式:

阶段1启动索引

Agent启动
    ↓
扫描 ~/.skills/ 目录
    ↓
读取每个Skill的YAML frontmatter
    ↓
构建技能索引:{name: description}
    ↓
注入到system prompt

阶段2按需加载

用户输入:"帮我处理PDF"
    ↓
Claude推理"这需要PDF Processing Skill"
    ↓
执行工具调用read_file("~/.skills/pdf/SKILL.md")
    ↓
内容加载到context window
    ↓
Claude根据指令执行任务

阶段3代码执行

SKILL.md中提到使用extract_fields.py提取字段
    ↓
Claude执行python ~/.skills/pdf/scripts/extract_fields.py input.pdf
    ↓
脚本输出JSON{"name": "", "email": "", ...}
    ↓
Claude解析输出继续任务

知识图谱视角

可以将Skills看作一个知识图谱

[Claude Agent]
    ↓ has_skill
[PDF Skill] ───references───> [forms.md]
    ↓ has_script
[extract_fields.py]

[Database Skill] ───uses_mcp───> [MySQL MCP Server]
    ↓ references
[query_patterns.md]

这种结构化组织让知识具有可发现性和可组合性。

底层实现与运行时

Claude Skills建立在Claude Code的Agent运行时之上核心技术栈包括

1. 文件系统访问

Claude Agent有完整的本地文件系统读写权限通过Bash工具执行cat、ls等命令默认限制在项目目录和~/.skills/

2. 代码执行引擎

支持Python、JavaScript、Bash等脚本每次执行在隔离的子进程中输出捕获stdout/stderr重定向到AI的context

3. YAML前置元数据解析

def parse_skill(skill_dir):
    skill_md = read_file(f"{skill_dir}/SKILL.md")
    frontmatter, content = split_yaml_frontmatter(skill_md)

    return {
        'name': frontmatter['name'],
        'description': frontmatter['description'],
        'content': content,
        'directory': skill_dir
    }

4. 动态Prompt注入

def build_system_prompt(skills):
    base_prompt = "You are Claude, an AI assistant..."

    skills_section = "\n\nAvailable Skills:\n"
    for skill in skills:
        skills_section += f"- {skill['name']}: {skill['description']}\n"

    skills_section += "\nTo use a skill, read its SKILL.md file."

    return base_prompt + skills_section

5. Context Window管理

Claude 3.5 Sonnet有200K token的context window。Skills通过渐进式披露高效利用这个空间

  • System prompt含Skills索引~2K tokens
  • 当前对话历史:~10K tokens
  • 已加载的Skills内容按需增长0-50K
  • 保留buffer~138K tokens

即使加载多个Skills仍有充足的空间处理复杂任务。


PART 06 - Skills部署

环境准备与安装

目前该功能已在Claude所有产品中上线最简单的方法就是打开Claude客户端。

针对Claude Code用户Mac示例

# 下载安装包
curl -O https://storage.googleapis.com/anthropic-downloads/claude-code-latest-mac.dmg

# 安装
open claude-code-latest-mac.dmg
# 拖拽到Applications

# 启动并登录
open /Applications/Claude\ Code.app

创建你的第一个Skill

让我们创建一个实用的SkillAPI文档生成器。

场景你的团队有特定的API文档规范需要Claude帮你生成符合规范的文档。

Step 1创建Skill目录

mkdir -p ~/.skills/api-doc-generator
cd ~/.skills/api-doc-generator

Step 2编写SKILL.md

---
name: "API Documentation Generator"
description: "Generate API documentation following company standards"
---

# API Documentation Generator Skill

## Purpose
Generate standardized API documentation for RESTful endpoints.

## Documentation Standards

### Structure
Each API endpoint document must include:
1. Endpoint overview
2. Authentication requirements
3. Request format (with example)
4. Response format (with example)
5. Error codes
6. Rate limiting info

### Example Template
See [api_template.md](api_template.md) for the complete template.

## Code Generation
Use `scripts/validate_api_spec.py` to validate OpenAPI specs before documenting.

## Usage Example

User: "Document the GET /users/:id endpoint"

Steps:
1. Load api_template.md
2. Ask user for endpoint details
3. Fill in template sections
4. Validate with scripts/validate_api_spec.py
5. Output formatted markdown

Step 3创建模板文件

# {ENDPOINT_NAME}

## Overview
{BRIEF_DESCRIPTION}

## Authentication
- **Type:** {AUTH_TYPE}
- **Required Scopes:** {SCOPES}

## Request

### HTTP Method
`{METHOD}`

### URL
`{BASE_URL}/{ENDPOINT_PATH}`

### Parameters
| Name | Type | Required | Description |
|------|------|----------|-------------|
| {PARAM_NAME} | {PARAM_TYPE} | {YES/NO} | {PARAM_DESC} |

### Example Request
```bash
curl -X {METHOD} \
  {BASE_URL}/{ENDPOINT_PATH} \
  -H "Authorization: Bearer {TOKEN}" \
  -H "Content-Type: application/json" \
  -d '{REQUEST_BODY}'

Step 4添加验证脚本

mkdir scripts
cat > scripts/validate_api_spec.py << 'EOF'
#!/usr/bin/env python3
"""
Validate API endpoint specification
"""
import sys
import json

def validate_endpoint(spec):
    """Validate that API spec has all required fields"""
    required_fields = [
        'method', 'path', 'description',
        'auth_type', 'parameters', 'responses'
    ]

    missing = [f for f in required_fields if f not in spec]

    if missing:
        print(f"❌ Missing required fields: {', '.join(missing)}")
        return False

    # Validate HTTP method
    valid_methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH']
    if spec['method'].upper() not in valid_methods:
        print(f"❌ Invalid HTTP method: {spec['method']}")
        return False

    print("✅ API specification is valid")
    return True

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python validate_api_spec.py <spec.json>")
        sys.exit(1)

    with open(sys.argv[1]) as f:
        spec = json.load(f)

    is_valid = validate_endpoint(spec)
    sys.exit(0 if is_valid else 1)
EOF

chmod +x scripts/validate_api_spec.py

Step 5测试Skill

打开Claude Code输入

请帮我为 GET /api/v1/users/:id 端点生成API文档。
这个端点返回用户详细信息需要Bearer token认证。

Claude会

  1. 检测到需要使用API Documentation Generator skill
  2. 读取SKILL.md
  3. 加载api_template.md模板
  4. 询问你详细参数
  5. 生成符合规范的文档
  6. 运行validate脚本验证

高级Skill模式

模式1多文件知识库

对于复杂领域,可以组织多层文档:

database-skill/
├── SKILL.md                 # 入口
├── query-patterns/
│   ├── select.md           # SELECT查询模式
│   ├── join.md             # JOIN最佳实践
│   └── optimization.md     # 性能优化
├── security/
│   ├── injection.md        # SQL注入防御
│   └── permissions.md      # 权限检查
└── scripts/
    └── explain_query.py    # 查询分析工具

SKILL.md作为导航中心

# Database Query Skill

## Query Patterns
- [SELECT queries](query-patterns/select.md)
- [JOIN best practices](query-patterns/join.md)
- ...

## Security
Before executing any query, review [injection防御指南](security/injection.md).

模式2Skills组合

一个Skill可以引用另一个Skill

# Full-Stack Deployment Skill

## Prerequisites
This skill depends on:
- **Docker Skill**: For containerization
- **AWS Skill**: For cloud deployment

## Workflow
1. Use Docker skill to create containers
2. Use AWS skill to provision infrastructure
3. Deploy using custom scripts

模式3Skill版本管理

使用Git管理Skills的演进

cd ~/.skills/api-doc-generator
git init
git add .
git commit -m "v1.0: Initial API doc generator"

# 创建特性分支
git checkout -b feature/add-graphql-support

# 修改SKILL.md添加GraphQL文档支持
# ...

git commit -m "Add GraphQL documentation support"
git tag v1.1

PART 07 - Skills应用场景

场景1代码审查自动化

某科技公司有严格的代码审查规范50页文档手动培训新员工需要数周。

Code Review Skill结构

code-review-skill/
├── SKILL.md
├── standards/
│   ├── python.md       # Python编码规范
│   ├── javascript.md   # JS规范
│   └── security.md     # 安全检查清单
├── scripts/
│   └── lint_runner.py  # 自动运行linter
└── examples/
    ├── good_pr.md      # 好的PR示例
    └── bad_pr.md       # 常见错误示例

场景2客户支持知识库

某SaaS公司的客服团队需要回答数百种产品问题。传统知识库检索效率低且答案常常过时。

Customer Support Skill

---
name: "Product Support Knowledge Base"
description: "Answer customer questions about product features, troubleshooting, and billing"
---

# Customer Support Skill

## Knowledge Base Structure
- [Getting Started Guide](kb/getting-started.md)
- [Feature Documentation](kb/features/)
- [Troubleshooting](kb/troubleshooting/)
- [Billing FAQs](kb/billing.md)

## Response Guidelines
1. Always check kb/troubleshooting/ first for known issues
2. Cite specific documentation sections in responses
3. If answer not found, escalate to human agent

## Escalation Criteria
Escalate if:
- Question involves account security
- Customer is frustrated (detected in tone)
- Issue not documented in KB

场景3数据管道构建

某数据团队需要为不同业务部门构建ETL管道。每个管道都有相似的模式提取、转换、加载但细节不同。

Data Pipeline Skill

data-pipeline-skill/
├── SKILL.md
├── templates/
│   ├── extraction.py.j2      # Jinja2模板
│   ├── transformation.py.j2
│   └── loading.py.j2
├── connectors/
│   ├── mysql.md
│   ├── postgres.md
│   └── s3.md
└── scripts/
    └── generate_pipeline.py

工作流:

数据分析师: "我需要一个从MySQL到Snowflake的管道
             转换逻辑是聚合用户行为数据。"

Claude (加载Data Pipeline Skill):
1. 询问源表结构和目标schema
2. 从connectors/mysql.md加载MySQL最佳实践
3. 使用templates/生成pipeline代码
4. 运行scripts/generate_pipeline.py创建文件
5. 输出可直接运行的Airflow DAG

场景4金融合规检查

某金融机构的交易系统需要符合多项监管要求GDPR、SOX、PCI-DSS等。合规检查涉及数十个checkpoints。

Compliance Check Skill

---
name: "Financial Compliance Checker"
description: "Validate transactions and code changes against GDPR, SOX, and PCI-DSS requirements"
---

# Compliance Checker Skill

## Supported Regulations
- [GDPR Requirements](regulations/gdpr.md)
- [SOX Section 404](regulations/sox.md)
- [PCI-DSS v3.2.1](regulations/pci-dss.md)

## Check Process
For each transaction or code change:
1. Identify applicable regulations
2. Run automated checks (scripts/compliance_scanner.py)
3. Flag violations with severity (Critical/High/Medium/Low)
4. Generate remediation recommendations

## Audit Trail
All checks logged to compliance_audit.log with:
- Timestamp
- Checked entity
- Regulations applied
- Results
- Reviewer (AI or Human)

场景5多语言文档同步

某开源项目需要维护英文、中文、日文三种语言的文档。传统翻译流程慢且容易遗漏更新。

Documentation Translation Skill

doc-translation-skill/
├── SKILL.md
├── glossary/
│   ├── technical-terms.csv  # 术语对照表
│   └── product-names.csv    # 产品名称规范
├── style-guides/
│   ├── en.md               # 英文风格指南
│   ├── zh.md               # 中文风格指南
│   └── ja.md               # 日文风格指南
└── scripts/
    └── consistency_check.py # 检查翻译一致性

自动化流程:

# 开发者更新英文文档
echo "Added new feature: Real-time sync" >> docs/en/features.md

# Git pre-commit hook触发Skills
git commit
# → Claude检测到英文文档更新
# → 加载Translation Skill
# → 参考glossary/翻译关键术语
# → 遵循zh.md和ja.md的风格指南
# → 生成中文和日文版本
# → 运行consistency_check.py验证
# → 自动提交翻译PR

结论

Claude Skills的推出标志着AI Agent从"通用型工具"向"专业化专家"的进化。通过简单的文件夹+Markdown组织形式Skills实现了知识的模块化、可复用化和可交易化。

当Agent能够自主创建和演化Skills时我们将见证AI从"工具"真正转变为"专家团队"。每个对话都会沉淀为可复用的知识AI的能力将呈指数级增长。Claude Skills不仅是一个技术特性更是AI生态演进的关键转折点。


参考资料

  1. Anthropic官方博客 - Equipping agents for the real world with Agent Skills
  2. Anthropic新闻 - Skills功能发布
  3. Claude Skills官方文档
  4. Claude Support - 使用Skills教程
  5. GitHub - Anthropic Skills官方仓库
  6. Simon Willison博客 - Claude Skills分析
  7. Anthropic工程博客 - Building agents with the Claude Agent SDK
  8. Model Context Protocol官网