跳轉到主要內容

Documentation Index

Fetch the complete documentation index at: https://code.claude.com/docs/llms.txt

Use this file to discover all available pages before exploring further.

Starting June 15, 2026, Agent SDK and claude -p usage on subscription plans will draw from a new monthly Agent SDK credit, separate from your interactive usage limits. See Use the Claude Agent SDK with your Claude plan for details.
構建能夠自主讀取檔案、執行命令、搜尋網路、編輯程式碼等的 AI 代理。Agent SDK 提供與 Claude Code 相同的工具、代理迴圈和上下文管理,可在 Python 和 TypeScript 中進行程式設計。
import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions


async def main():
    async for message in query(
        prompt="Find and fix the bug in auth.py",
        options=ClaudeAgentOptions(allowed_tools=["Read", "Edit", "Bash"]),
    ):
        print(message)  # Claude reads the file, finds the bug, edits it


asyncio.run(main())
Agent SDK 包含用於讀取檔案、執行命令和編輯程式碼的內建工具,因此您的代理可以立即開始工作,無需您實現工具執行。深入了解快速入門或探索使用 SDK 構建的真實代理:

快速入門

在幾分鐘內構建一個除錯代理

範例代理

電子郵件助手、研究代理等

開始使用

1

安裝 SDK

npm install @anthropic-ai/claude-agent-sdk
TypeScript SDK 為您的平台捆綁了原生 Claude Code 二進位檔案作為可選依賴項,因此您無需單獨安裝 Claude Code。
2

設定您的 API 金鑰

主控台取得 API 金鑰,然後將其設定為環境變數:
export ANTHROPIC_API_KEY=your-api-key
SDK 也支援透過第三方 API 提供者進行身份驗證:
  • Amazon Bedrock:設定 CLAUDE_CODE_USE_BEDROCK=1 環境變數並配置 AWS 認證
  • Claude Platform on AWS:設定 CLAUDE_CODE_USE_ANTHROPIC_AWS=1ANTHROPIC_AWS_WORKSPACE_ID,然後配置 AWS 認證
  • Google Vertex AI:設定 CLAUDE_CODE_USE_VERTEX=1 環境變數並配置 Google Cloud 認證
  • Microsoft Azure:設定 CLAUDE_CODE_USE_FOUNDRY=1 環境變數並配置 Azure 認證
有關詳細資訊,請參閱 BedrockClaude Platform on AWSVertex AIAzure AI Foundry 的設定指南。
除非事先獲得批准,否則 Anthropic 不允許第三方開發人員為其產品(包括基於 Claude Agent SDK 構建的代理)提供 claude.ai 登入或速率限制。請改用本文件中描述的 API 金鑰身份驗證方法。
3

執行您的第一個代理

此範例建立一個使用內建工具列出目前目錄中檔案的代理。
import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions


async def main():
    async for message in query(
        prompt="What files are in this directory?",
        options=ClaudeAgentOptions(allowed_tools=["Bash", "Glob"]),
    ):
        if hasattr(message, "result"):
            print(message.result)


asyncio.run(main())
準備好構建了嗎? 遵循快速入門在幾分鐘內建立一個尋找和修復錯誤的代理。

功能

使 Claude Code 強大的一切都可在 SDK 中使用:
您的代理可以開箱即用地讀取檔案、執行命令和搜尋程式碼庫。主要工具包括:
工具功能
Read讀取工作目錄中的任何檔案
Write建立新檔案
Edit對現有檔案進行精確編輯
Bash執行終端命令、指令碼、git 操作
Monitor監視背景指令碼並對每個輸出行作為事件做出反應
Glob按模式尋找檔案(**/*.tssrc/**/*.py
Grep使用正規表達式搜尋檔案內容
WebSearch搜尋網路以獲取最新資訊
WebFetch擷取並解析網頁內容
AskUserQuestion向使用者提出具有多選選項的澄清問題
此範例建立一個搜尋程式碼庫中 TODO 註解的代理:
import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions


async def main():
    async for message in query(
        prompt="Find all TODO comments and create a summary",
        options=ClaudeAgentOptions(allowed_tools=["Read", "Glob", "Grep"]),
    ):
        if hasattr(message, "result"):
            print(message.result)


asyncio.run(main())

Claude Code 功能

SDK 也支援 Claude Code 的基於檔案系統的配置。使用預設選項,SDK 從工作目錄中的 .claude/~/.claude/ 載入這些。要限制載入哪些來源,請在選項中設定 setting_sources(Python)或 settingSources(TypeScript)。
功能描述位置
Skills在 Markdown 中定義的專門功能.claude/skills/*/SKILL.md
Slash commands用於常見任務的自訂命令.claude/commands/*.md
Memory專案上下文和指令CLAUDE.md.claude/CLAUDE.md
Plugins使用自訂命令、代理和 MCP 伺服器進行擴展透過 plugins 選項進行程式設計

將 Agent SDK 與其他 Claude 工具進行比較

Claude 平台提供多種方式來使用 Claude 進行構建。以下是 Agent SDK 的適用方式:
Anthropic Client SDK 為您提供直接 API 存取:您傳送提示並自己實現工具執行。Agent SDK 為您提供具有內建工具執行的 Claude。使用 Client SDK,您實現工具迴圈。使用 Agent SDK,Claude 處理它:
# Client SDK: You implement the tool loop
response = client.messages.create(...)
while response.stop_reason == "tool_use":
    result = your_tool_executor(response.tool_use)
    response = client.messages.create(tool_result=result, **params)

# Agent SDK: Claude handles tools autonomously
async for message in query(prompt="Fix the bug in auth.py"):
    print(message)

變更日誌

查看完整的變更日誌以了解 SDK 更新、錯誤修復和新功能:

報告錯誤

如果您遇到 Agent SDK 的錯誤或問題:

品牌指南

對於整合 Claude Agent SDK 的合作夥伴,使用 Claude 品牌是可選的。在您的產品中引用 Claude 時: 允許:
  • “Claude Agent”(下拉選單的首選)
  • “Claude”(當已在標記為”Agents”的選單中時)
  • Powered by Claude”(如果您有現有的代理名稱)
不允許:
  • “Claude Code” 或 “Claude Code Agent”
  • Claude Code 品牌的 ASCII 藝術或模仿 Claude Code 的視覺元素
您的產品應保持自己的品牌,不應顯示為 Claude Code 或任何 Anthropic 產品。有關品牌合規性的問題,請聯絡 Anthropic 銷售團隊

許可證和條款

Claude Agent SDK 的使用受 Anthropic 商業服務條款管制,包括當您使用它為您自己的客戶和最終使用者提供的產品和服務提供動力時,除非特定元件或依賴項受到該元件 LICENSE 檔案中指示的不同許可證的保護。

後續步驟

快速入門

構建在幾分鐘內尋找和修復錯誤的代理

範例代理

電子郵件助手、研究代理等

TypeScript SDK

完整的 TypeScript API 參考和範例

Python SDK

完整的 Python API 參考和範例