Claude Platform Docs
Messages技能

在 API 中开始使用 Agent Skills

了解如何在 10 分钟内使用 Agent Skills 通过 Claude API 创建文档。

本教程向您展示如何使用 Agent Skills 创建 PowerPoint 演示文稿。您将学习如何启用 Skills、发出请求以及访问生成的文件。

前提条件

Agent Skills 概述

预构建的 Agent Skills 通过专业知识扩展 Claude 的能力,用于创建文档、分析数据和处理文件等任务。Anthropic 在 API 中提供以下预构建的 Agent Skills:

  • PowerPoint (pptx): 创建和编辑演示文稿
  • Excel (xlsx): 创建和分析电子表格
  • Word (docx): 创建和编辑文档
  • PDF (pdf): 生成 PDF 文档

步骤 1:列出可用的 Skills

首先,检查有哪些 Skills 可用。使用 Skills API 列出所有由 Anthropic 管理的 Skills。每个语言选项卡都是一个连续脚本的节选,所有导入和客户端设置都位于顶部:

# 列出由 Anthropic 管理的 Skills
ant skills list --source anthropic

您会看到以下 Skills:pptxxlsxdocxpdf

此 API 返回每个 Skill 的元数据:其名称和描述。Claude 在启动时加载这些元数据,以确定哪些 Skills 可用。这是 "progressive disclosure"(渐进式披露) 的第一层,在这一层中,Claude 发现 Skills,但尚未加载其完整指令。

步骤 2:创建演示文稿

使用 PowerPoint Skill 创建一个关于可再生能源的演示文稿。在 Messages API 中使用 container 参数指定 Skills:

# 使用 PowerPoint Skill 创建消息
response = client.messages.create(
    model="claude-opus-5",
    max_tokens=16000,
    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_20260521", "name": "code_execution"}],
)

print(f"stop_reason={response.stop_reason}, blocks={len(response.content)}")

该请求包含以下部分:

  • model 一个支持代码执行工具的模型
  • container.skills 指定 Claude 可以使用哪些 Skills
  • type: "anthropic" 表示这是一个由 Anthropic 管理的 Skill
  • skill_id: "pptx" PowerPoint Skill 的标识符
  • version: "latest" Skill 版本设置为最新发布的版本
  • tools 启用代码执行(Skills 所必需)

当您发出此请求时,Claude 会自动将您的任务与相关的 Skill 进行匹配。由于您请求的是演示文稿,Claude 判断 PowerPoint Skill 是相关的,并加载其完整指令:这是渐进式披露的第二层。然后 Claude 运行该 Skill 的代码来创建您的演示文稿。

步骤 3:下载创建的文件

演示文稿是在代码执行容器中创建的,并保存为文件。步骤 2 的 response 包含一个带有文件 ID 的文件引用。提取文件 ID 并使用 Files API 下载该文件。示例将其保存到您的系统临时目录:

# 提取文件 ID。代码执行工具通过其 Bash 子工具运行 Skill 的代码,
# 生成的文件以 bash_code_execution_output 项的形式
# 出现在 bash_code_execution_tool_result 块中。
file_id = None
for block in response.content:
    if block.type == "bash_code_execution_tool_result":
        if block.content.type == "bash_code_execution_result":
            for output in block.content.content:
                file_id = output.file_id

if file_id:
    # 下载文件并保存
    output_path = Path(tempfile.gettempdir()) / "renewable_energy.pptx"
    file_content = client.files.download(file_id=file_id)
    file_content.write_to_file(output_path)
    print(f"Presentation saved to {output_path}")

尝试更多示例

尝试以下变体:

创建电子表格

response = client.messages.create(
    model="claude-opus-5",
    max_tokens=16000,
    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_20260521", "name": "code_execution"}],
)

创建 Word 文档

response = client.messages.create(
    model="claude-opus-5",
    max_tokens=16000,
    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_20260521", "name": "code_execution"}],
)

生成 PDF

response = client.messages.create(
    model="claude-opus-5",
    max_tokens=16000,
    container={
        "skills": [{"type": "anthropic", "skill_id": "pdf", "version": "latest"}]
    },
    messages=[
        {
            "role": "user",
            "content": "Generate a PDF invoice template",
        }
    ],
    tools=[{"type": "code_execution_20260521", "name": "code_execution"}],
)

后续步骤

了解如何编写 Claude 能够发现并成功使用的高效 Skills。

了解如何使用 Agent Skills 通过 API 扩展 Claude 的能力。

上传您自己的 Skills 以用于专门任务。

了解 Claude Code 中的 Skills。

探索示例 Skills 和实现模式。

Was this page helpful?