在 API 中开始使用 Agent Skills
本教程向您展示如何使用 Agent Skills 创建 PowerPoint 演示文稿。您将学习如何启用 Skills、进行简单请求以及访问生成的文件。
前置条件
- Anthropic API 密钥
- Python 3.7+ 或已安装 curl
- 对进行 API 请求的基本熟悉
什么是 Agent Skills?
预构建的 Agent Skills 通过专门的专业知识扩展 Claude 的功能,用于创建文档、分析数据和处理文件等任务。Anthropic 在 API 中提供以下预构建的 Agent Skills:
- PowerPoint (pptx):创建和编辑演示文稿
- Excel (xlsx):创建和分析电子表格
- Word (docx):创建和编辑文档
- PDF (pdf):生成 PDF 文档
想要创建自定义 Skills? 请查看 Agent Skills Cookbook 以获取使用特定领域专业知识构建您自己的 Skills 的示例。
步骤 1:列出可用的 Skills
首先,让我们看看有哪些可用的 Skills。我们将使用 Skills API 列出所有 Anthropic 管理的 Skills:
import anthropic
client = anthropic.Anthropic()
# List Anthropic-managed Skills
skills = client.beta.skills.list(
source="anthropic",
betas=["skills-2025-10-02"]
)
for skill in skills.data:
print(f"{skill.id}: {skill.display_title}")import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic();
// List Anthropic-managed Skills
const skills = await client.beta.skills.list({
source: 'anthropic',
betas: ['skills-2025-10-02']
});
for (const skill of skills.data) {
console.log(`${skill.id}: ${skill.display_title}`);
}curl "https://api.anthropic.com/v1/skills?source=anthropic" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "anthropic-beta: skills-2025-10-02"您会看到以下 Skills:pptx、xlsx、docx 和 pdf。
此 API 返回每个 Skill 的元数据:其名称和描述。Claude 在启动时加载此元数据以了解有哪些可用的 Skills。这是渐进式披露的第一个级别,其中 Claude 发现 Skills 而不加载其完整说明。
步骤 2:创建演示文稿
现在我们将使用 PowerPoint Skill 创建关于可再生能源的演示文稿。我们使用 Messages API 中的 container 参数指定 Skills:
import anthropic
client = anthropic.Anthropic()
# Create a message with the PowerPoint Skill
response = client.beta.messages.create(
model="claude-sonnet-4-5-20250929",
max_tokens=4096,
betas=["code-execution-2025-08-25", "skills-2025-10-02"],
container={
"skills": [
{
"type": "anthropic",
"skill_id": "pptx",
"version": "latest"
}
]
},
messages=[{
"role": "user",
"content": "Create a presentation about renewable energy with 5 slides"
}],
tools=[{
"type": "code_execution_20250825",
"name": "code_execution"
}]
)
print(response.content)让我们分解每个部分的作用:
container.skills:指定 Claude 可以使用哪些 Skillstype: "anthropic":表示这是 Anthropic 管理的 Skillskill_id: "pptx":PowerPoint Skill 标识符version: "latest":Skill 版本设置为最近发布的版本tools:启用代码执行(Skills 所需)- Beta 标头:
code-execution-2025-08-25和skills-2025-10-02
当您进行此请求时,Claude 会自动将您的任务与相关的 Skill 匹配。由于您要求演示文稿,Claude 确定 PowerPoint Skill 是相关的并加载其完整说明:这是渐进式披露的第二个级别。然后 Claude 执行 Skill 的代码来创建您的演示文稿。
步骤 3:下载创建的文件
演示文稿是在代码执行容器中创建的,并保存为文件。响应包括带有文件 ID 的文件引用。提取文件 ID 并使用 Files API 下载它:
# Extract file ID from response
file_id = None
for block in response.content:
if block.type == 'tool_use' and block.name == 'code_execution':
# File ID is in the tool result
for result_block in block.content:
if hasattr(result_block, 'file_id'):
file_id = result_block.file_id
break
if file_id:
# Download the file
file_content = client.beta.files.download(
file_id=file_id,
betas=["files-api-2025-04-14"]
)
# Save to disk
with open("renewable_energy.pptx", "wb") as f:
file_content.write_to_file(f.name)
print(f"Presentation saved to renewable_energy.pptx")有关处理生成文件的完整详细信息,请参阅 代码执行工具文档。
尝试更多示例
现在您已经使用 Skills 创建了第一个文档,请尝试这些变体:
创建电子表格
response = client.beta.messages.create(
model="claude-sonnet-4-5-20250929",
max_tokens=4096,
betas=["code-execution-2025-08-25", "skills-2025-10-02"],
container={
"skills": [
{
"type": "anthropic",
"skill_id": "xlsx",
"version": "latest"
}
]
},
messages=[{
"role": "user",
"content": "Create a quarterly sales tracking spreadsheet with sample data"
}],
tools=[{
"type": "code_execution_20250825",
"name": "code_execution"
}]
)创建 Word 文档
response = client.beta.messages.create(
model="claude-sonnet-4-5-20250929",
max_tokens=4096,
betas=["code-execution-2025-08-25", "skills-2025-10-02"],
container={
"skills": [
{
"type": "anthropic",
"skill_id": "docx",
"version": "latest"
}
]
},
messages=[{
"role": "user",
"content": "Write a 2-page report on the benefits of renewable energy"
}],
tools=[{
"type": "code_execution_20250825",
"name": "code_execution"
}]
)生成 PDF
response = client.beta.messages.create(
model="claude-sonnet-4-5-20250929",
max_tokens=4096,
betas=["code-execution-2025-08-25", "skills-2025-10-02"],
container={
"skills": [
{
"type": "anthropic",
"skill_id": "pdf",
"version": "latest"
}
]
},
messages=[{
"role": "user",
"content": "Generate a PDF invoice template"
}],
tools=[{
"type": "code_execution_20250825",
"name": "code_execution"
}]
)后续步骤
现在您已经使用了预构建的 Agent Skills,您可以: