Agent SDK overview
Agent SDK 總覽
Build production AI agents with Claude Code as a library. The Agent SDK gives you the same tools, agent loop, and context management that power Claude Code, programmable in Python and TypeScript.
使用 Claude Code 作為函式庫來建構生產級 AI agents。Agent SDK 提供與 Claude Code 相同的工具、agent 迴圈和上下文管理,可在 Python 和 TypeScript 中程式化使用。
Capabilities
功能
Everything that makes Claude Code powerful is available in the SDK:
讓 Claude Code 強大的所有功能都可以在 SDK 中使用:
Built-in tools
內建工具
Your agent can read files, run commands, and search codebases out of the box.
你的 agent 可以直接讀取檔案、執行命令和搜尋程式碼庫。
| Tool | What it does |
|---|---|
| Read | Read any file in the working directory |
| Write | Create new files |
| Edit | Make precise edits to existing files |
| Bash | Run terminal commands, scripts, git operations |
| Glob | Find files by pattern |
| Grep | Search file contents with regex |
| WebSearch | Search the web for current information |
| WebFetch | Fetch and parse web page content |
| 工具 | 功能 |
|---|---|
| Read | 讀取工作目錄中的任何檔案 |
| Write | 建立新檔案 |
| Edit | 對現有檔案進行精確編輯 |
| Bash | 執行終端命令、腳本、git 操作 |
| Glob | 按模式尋找檔案 |
| Grep | 用正規表達式搜尋檔案內容 |
| WebSearch | 搜尋網路以取得最新資訊 |
| WebFetch | 抓取和解析網頁內容 |
Hooks
Hooks
Run custom code at key points in the agent lifecycle. SDK hooks use callback functions to validate, log, block, or transform agent behavior.
在 agent 生命週期的關鍵點執行自訂程式碼。SDK hooks 使用回呼函式來驗證、記錄、阻止或轉換 agent 行為。
Subagents
子代理
Spawn specialized agents to handle focused subtasks. Your main agent delegates work, and subagents report back with results.
產生專門的 agent 來處理特定子任務。你的主 agent 委派工作,子代理回報結果。
MCP
MCP
Connect to external systems via the Model Context Protocol: databases, browsers, APIs, and hundreds more.
透過 Model Context Protocol 連接外部系統:資料庫、瀏覽器、API 等數百種系統。
Get started
開始使用
- Step 1: Install Claude Code - The SDK uses Claude Code as its runtime.
- Step 2: Install the SDK - npm install @anthropic-ai/claude-agent-sdk (TypeScript) or pip install claude-agent-sdk (Python).
- Step 3: Set your API key - export ANTHROPIC_API_KEY=your-api-key
- Step 4: Run your first agent - Create an agent that uses built-in tools.
- 步驟 1:安裝 Claude Code - SDK 使用 Claude Code 作為其執行環境。
- 步驟 2:安裝 SDK - npm install @anthropic-ai/claude-agent-sdk (TypeScript) 或 pip install claude-agent-sdk (Python)。
- 步驟 3:設定 API 金鑰 - export ANTHROPIC_API_KEY=your-api-key
- 步驟 4:執行你的第一個 agent - 建立一個使用內建工具的 agent。
Agent SDK vs Client SDK
Agent SDK vs Client SDK
The Anthropic Client SDK gives you direct API access: you send prompts and implement tool execution yourself. The Agent SDK gives you Claude with built-in tool execution - Claude handles the tool loop autonomously.
Anthropic Client SDK 提供直接的 API 存取:你發送提示並自己實作工具執行。Agent SDK 提供內建工具執行的 Claude - Claude 自主處理工具迴圈。