源码级别解析 · 源码解析 · GitHubanthropics/skills
2026-04-20 | 每日技术深度解读
项目地址: https://github.com/anthropics/skills
类似插件系统,让 Claude 能够执行专业化任务
从通用AI助手到专业工具的转变
Skills 通过 API 动态加载到执行上下文中
渐进式披露设计,按需加载资源
---
name: my-skill-name
description: A clear description of what this skill does and when to use it
---
# My Skill Name
[Add your instructions here that Claude will follow when this skill is active]
## Examples
- Example usage 1
- Example usage 2
## Guidelines
- Guideline 1
- Guideline 2
YAML frontmatter + Markdown body的标准格式
| 字段名 | 必需 | 约束条件 | 示例 |
|---|---|---|---|
| name | ✅ | 1-64字符,小写字母、数字、连字符 | pdf-processing |
| description | ✅ | 1-1024字符,描述用途 | Extract PDF text, fill forms |
| license | ❌ | 许可证名称或文件引用 | Apache-2.0 |
| compatibility | ❌ | 1-500字符,环境要求 | Requires Python 3.14+ |
| metadata | ❌ | 任意键值映射 | author: example-org |
| allowed-tools | ❌ | 空格分隔的工具列表 | Bash(git:*) Read |
确保标识符的稳定性和可预测性
👍 示例: "Extracts text and tables from PDF files, fills PDF forms, and merges multiple PDFs. Use when working with PDF documents."
---
name: pdf-processing
description: Extracts text and tables from PDF files, fills PDF forms, and merges multiple PDFs. Use when working with PDF documents or when the user mentions PDFs, forms, or document extraction.
license: Apache-2.0
metadata:
author: example-org
version: "1.0"
category: document-processing
tags: [pdf, extraction, forms, merge]
compatibility: Requires Python 3.14+ and pypdf, pdfplumber
---
展示了完整的前matter配置,包含所有可选字段
脚本应该自包含或明确说明依赖关系,包含错误处理
小文件提高加载效率,按需读取
支持各种静态资源类型
优化上下文使用效率
# Skill 主文件
See [the reference guide](references/REFERENCE.md) for details.
Run the extraction script:
scripts/extract.py
Use [form template](assets/form-template.docx) for standard forms.
使用相对路径引用,保持引用结构简单
动态加载确保性能优化
从发现到执行的完整流程
不同类别的技能有不同的特点和使用场景
skills/
├── docx/
│ ├── SKILL.md
│ ├── scripts/
│ │ ├── extract_text.py
│ │ ├── merge_documents.py
│ │ └── templates/
│ ├── references/
│ │ ├── REFERENCE.md
│ │ └── FORMS.md
│ └── assets/
│ ├── templates/
│ └── examples/
├── pdf/
├── pptx/
└── xlsx/
Document Skills 的标准目录结构
多平台支持,统一体验
# 添加技能库到插件市场
/plugin marketplace add anthropics/skills
# 安装文档技能
/plugin install document-skills@anthropic-agent-skills
# 安装示例技能
/plugin install example-skills@anthropic-agent-skills
通过插件市场快速部署技能
无需额外配置,开箱即用
API 支持程序化技能管理
import anthropic
client = anthropic.Anthropic()
# 创建技能
skill = client.skills.create(
name="pdf-processing",
description="Extract text and tables from PDF files",
skill="skills/pdf/SKILL.md"
)
# 在会话中使用
response = client.messages.create(
model="claude-3-opus-20240229",
max_tokens=1000,
messages=[
{"role": "user", "content": "Use PDF skill to extract text from document.pdf"}
],
skills=[skill.id]
)
Python SDK 的技能使用示例
避免格式错误导致技能无法加载
# 安装验证工具
pip install skills-ref
# 验证技能
skills-ref validate ./my-skill
# 输出示例
# ✅ Valid skill metadata
# ✅ Valid naming conventions
# ✅ No broken file references
使用官方工具验证技能格式
健壮的错误处理确保技能可靠性
# 错误处理示例
import logging
from pathlib import Path
def safe_load_resource(resource_path):
try:
if Path(resource_path).exists():
with open(resource_path, 'r', encoding='utf-8') as f:
return f.read()
else:
logging.warning(f"Resource not found: {resource_path}")
return None
except Exception as e:
logging.error(f"Error loading {resource_path}: {e}")
return None
安全的资源加载示例
确保大量技能情况下的系统性能
减少重复加载,提高响应速度
智能缓存机制平衡性能和内存使用
确保技能执行的安全性
防止恶意工具使用
# 基本工具限制
allowed-tools: Bash(git:*) Bash(jq:*) Read
# 复杂权限模式
allowed-tools:
- Bash(/usr/bin/git:*)
- Bash(/usr/bin/jq:*)
- Read(/home/user/docs:*)
- Write(/tmp/skill-output:*)
灵活的工具权限配置
防止技能对系统造成影响
丰富的技能生态推动系统发展
高质量、官方维护的技能集
开放的贡献机制保证生态健康
满足企业特定需求
平滑迁移保证业务连续性
提高技能质量和可用性
有效的调试方法提高开发效率
# 技能调试日志
import logging
import sys
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
def debug_skill_execution():
logger.info("Skill execution started")
try:
# 业务逻辑
result = process_data()
logger.info(f"Processing completed: {result}")
return result
except Exception as e:
logger.error(f"Skill execution failed: {e}", exc_info=True)
raise
完整的调试日志配置
全面的测试保证技能质量
版本管理确保技能稳定性
数据驱动优化技能
持续创新推动生态发展
类似应用商店的技能分发模式
降低技能开发门槛
解决企业实际业务需求
让技能开发更简单高效
Skills 将重新定义 AI 助手的边界
# 1. 克隆技能库
git clone https://github.com/anthropics/skills.git
cd skills
# 2. 创建新技能
mkdir my-skill
cat > my-skill/SKILL.md << 'EOF'
---
name: my-skill
description: A sample skill for testing
---
# My Skill
This is my first skill.
EOF
# 3. 验证技能
skills-ref validate my-skill
# 4. 在 Claude Code 中使用
/plugin marketplace add ./skills
/plugin install example-skills@anthropic-agent-skills
从零开始创建和部署技能
持续学习和参与社区
解决问题的实用指南
确保企业成功采用
定期检查和优化
安全第一
结构化的开发过程保证质量
自动化流程提高效率
感谢阅读!
访问 https://atcfu.com/ai-articles/anthropics-skills/ 回顾本文