在 API 中開始使用 Agent Skills
了解如何在 10 分鐘內使用 Agent Skills 透過 Claude API 建立文件。
本教學將示範如何使用 Agent Skills 建立 PowerPoint 簡報。您將學會如何啟用 Skills、發送請求,以及存取所產生的檔案。
開始之前
您需要適用於您所用語言的用戶端 SDK,或 curl 與 jq,並對發送 API 請求有基本的認識。
建立 Claude Console 帳戶
註冊 Claude Console 帳戶,或登入您既有的帳戶。
選擇驗證方式
對於本機開發,建議使用 CLI 登入。
CLIAPI 金鑰
安裝 CLI
brew install anthropics/tap/ant對於 Linux 環境,請直接下載發行版本的二進位檔。
VERSION=2.7.1 OS=$(uname -s | tr '[:upper:]' '[:lower:]') case $(uname -m) in x86_64) ARCH=amd64 ;; aarch64) ARCH=arm64 ;; esac curl -fsSL "https://github.com/anthropics/anthropic-cli/releases/download/v${VERSION}/ant_${VERSION}_${OS}_${ARCH}.tar.gz" \ | sudo tar -xz -C /usr/local/bin ant您可以在 GitHub 發行版本頁面上找到所有發行版本。
您也可以使用
go install從原始碼安裝 CLI,這需要 Go 1.26.10 或更新版本。go install github.com/anthropics/anthropic-cli/v2/cmd/ant@latestgo install會將二進位檔放置在$(go env GOPATH)/bin中。如果該目錄尚未加入您的PATH,請將其加入:export PATH="$PATH:$(go env GOPATH)/bin"登入
ant auth login登入完成後,SDK 和 CLI 將會自動取得您的憑證。
建立 API 金鑰
在 Claude Console 中建立 API 金鑰。
設定環境變數
SDK 和 CLI 會從
ANTHROPIC_API_KEY讀取您的金鑰:export ANTHROPIC_API_KEY="your-api-key-here"
若要執行 CLI 範例,請安裝 CLI:
brew install anthropics/tap/ant對於 Linux 環境,請直接下載發行版本的二進位檔。
VERSION=2.7.1
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
case $(uname -m) in
x86_64) ARCH=amd64 ;;
aarch64) ARCH=arm64 ;;
esac
curl -fsSL "https://github.com/anthropics/anthropic-cli/releases/download/v${VERSION}/ant_${VERSION}_${OS}_${ARCH}.tar.gz" \
| sudo tar -xz -C /usr/local/bin ant您可以在 GitHub 發行版本頁面上找到所有發行版本。
您也可以使用 go install 從原始碼安裝 CLI,這需要 Go 1.26.10 或更新版本。
go install github.com/anthropics/anthropic-cli/v2/cmd/ant@latestgo install 會將二進位檔放置在 $(go env GOPATH)/bin 中。如果該目錄尚未加入您的 PATH,請將其加入:
export PATH="$PATH:$(go env GOPATH)/bin"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
skills = client.skills.list(source="anthropic")
for skill in skills.data:
print(f"{skill.id}: {skill.display_name}")您會看到下列 Skills:pptx、xlsx、docx 與 pdf。
此 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-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 可以使用哪些 Skillstype: "anthropic": 表示這是由 Anthropic 管理的 Skillskill_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-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-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-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。
了解如何透過 API 使用 Agent Skills 來擴展 Claude 的能力。
上傳您自己的 Skills 以處理專門任務。
了解 Claude Code 中的 Skills。
探索範例 Skills 與實作模式。
這個頁面有幫助嗎?