10 概述10 概述
11</h2>11</h2>
12 12
13Claude Code SDK 已重命名为 **Claude Agent SDK**,其文档已重新组织。这一变化反映了该 SDK 在构建超越编码任务的 AI 代理方面的更广泛功能。13Claude Code SDK 已更名为 **Claude Agent SDK**,其文档已重新组织。这一变化反映了该 SDK 在构建 AI 代理方面的更广泛功能,不仅限于编码任务。
14
15从 OpenAI Agents SDK 迁移?[OpenAI Agents SDK 迁移指南](https://platform.claude.com/cookbook/claude-agent-sdk-04-migrating-from-openai-agents-sdk)通过一个完整的示例将每个原语映射到 Claude Agent SDK。
14 16
15<h2 id="what’s-changed">17<h2 id="what’s-changed">
16 变更内容18 有什么改变
17</h2>19</h2>
18 20
19| 方面 | 旧版本 | 新版本 |21| 方面 | 旧版 | 新版 |
20| :-------------- | :-------------------------- | :------------------------------- |22| :-------------- | :-------------------------- | :------------------------------------------------------------ |
21| **包名称 (TS/JS)** | `@anthropic-ai/claude-code` | `@anthropic-ai/claude-agent-sdk` |23| **包名称 (TS/JS)** | `@anthropic-ai/claude-code` | `@anthropic-ai/claude-agent-sdk` |
22| **Python 包** | `claude-code-sdk` | `claude-agent-sdk` |24| **Python 包** | `claude-code-sdk` | `claude-agent-sdk` |
23| **文档位置** | Claude Code 文档 | API 指南 → Agent SDK 部分 |25| **文档位置** | Claude Code 文档 | Claude Code 文档 → 专用 [Agent SDK](/docs/zh-CN/agent-sdk/overview) 部分 |
24
25<Note>
26 **文档变更:** Agent SDK 文档已从 Claude Code 文档移至 API 指南下的专门 [Agent SDK](/zh-CN/agent-sdk/overview) 部分。Claude Code 文档现在专注于 CLI 工具和自动化功能。
27</Note>
28 26
29<h2 id="migration-steps">27<h2 id="migration-steps">
30 迁移步骤28 迁移步骤
46npm install @anthropic-ai/claude-agent-sdk44npm install @anthropic-ai/claude-agent-sdk
47```45```
48 46
49**3. 更新导入:**47**3. 更新你的导入:**
50 48
51将所有导入从 `@anthropic-ai/claude-code` 更改为 `@anthropic-ai/claude-agent-sdk`:49将所有导入从 `@anthropic-ai/claude-code` 更改为 `@anthropic-ai/claude-agent-sdk`:
52 50
58import { query, tool, createSdkMcpServer } from "@anthropic-ai/claude-agent-sdk";56import { query, tool, createSdkMcpServer } from "@anthropic-ai/claude-agent-sdk";
59```57```
60 58
61**4. 更新 package.json 依赖项:**59**4. 更新 package.json:**
62
63如果您在 `package.json` 中列出了该包,请更新它:
64
65之前:
66
67```json theme={null}
68{
69 "dependencies": {
70 "@anthropic-ai/claude-code": "^0.0.42"
71 }
72}
73```
74
75之后:
76 60
77```json theme={null}61如果 `@anthropic-ai/claude-code` 仍在你的 `package.json` 中列出,请将其替换为 `@anthropic-ai/claude-agent-sdk` 并同时更新版本范围,例如从 `"^0.0.42"` 更新到 `"^0.3.0"`。
78{
79 "dependencies": {
80 "@anthropic-ai/claude-agent-sdk": "^0.2.0"
81 }
82}
83```
84 62
85**5. 查看 [破坏性变更](#breaking-changes)**63**5. 查看[破坏性变更](#breaking-changes)**
86 64
87进行完成迁移所需的任何代码更改。65进行任何必要的代码更改以完成迁移。
88 66
89<h3 id="for-python-projects">67<h3 id="for-python-projects">
90 对于 Python 项目68 对于 Python 项目
93**1. 卸载旧包:**71**1. 卸载旧包:**
94 72
95```bash theme={null}73```bash theme={null}
96pip uninstall claude-code-sdk74pip uninstall -y claude-code-sdk
97```75```
98 76
77如果未安装旧包,pip 会打印 `WARNING: Skipping claude-code-sdk as it is not installed.` 这是预期的,你可以继续下一步。
78
99**2. 安装新包:**79**2. 安装新包:**
100 80
101```bash theme={null}81```bash theme={null}
102pip install claude-agent-sdk82pip install claude-agent-sdk
103```83```
104 84
105**3. 更新导入:**85如果 `claude-code-sdk` 在你的 `requirements.txt` 或 `pyproject.toml` 中列出,请将其替换为 `claude-agent-sdk`。
106
107将所有导入从 `claude_code_sdk` 更改为 `claude_agent_sdk`:
108 86
109```python theme={null}87**3. 更新你的导入:**
110# 之前
111from claude_code_sdk import query, ClaudeCodeOptions
112 88
113# 之后89将所有导入从 `claude_code_sdk` 更改为 `claude_agent_sdk`:
114from claude_agent_sdk import query, ClaudeAgentOptions
115```
116
117**4. 更新类型名称:**
118
119将 `ClaudeCodeOptions` 更改为 `ClaudeAgentOptions`:
120 90
121```python theme={null}91```python theme={null}
122# 之前92# 之前
123from claude_code_sdk import query, ClaudeCodeOptions93from claude_code_sdk import query, ClaudeCodeOptions
124 94
125options = ClaudeCodeOptions(model="claude-opus-4-7")
126
127# 之后95# 之后
128from claude_agent_sdk import query, ClaudeAgentOptions96from claude_agent_sdk import query, ClaudeAgentOptions
129
130options = ClaudeAgentOptions(model="claude-opus-4-7")
131```97```
132 98
133**5. 查看 [破坏性变更](#breaking-changes)**99**4. 查看[破坏性变更](#breaking-changes)**
134 100
135进行完成迁移所需的任何代码更改。101进行任何必要的代码更改以完成迁移。
136 102
137<h2 id="breaking-changes">103<h2 id="breaking-changes">
138 破坏性变更104 破坏性变更
139</h2>105</h2>
140 106
141<Warning>107<Warning>
142 为了改进隔离和显式配置,Claude Agent SDK v0.1.0 为从 Claude Code SDK 迁移的用户引入了破坏性变更。在迁移前请仔细查看本部分。108 为了改进隔离和显式配置,Claude Agent SDK v0.1.0 为从 Claude Code SDK 迁移的用户引入了破坏性变更。
143</Warning>109</Warning>
144 110
145<h3 id="python-claudecodeoptions-renamed-to-claudeagentoptions">111<h3 id="python-claudecodeoptions-renamed-to-claudeagentoptions">
151**迁移:**117**迁移:**
152 118
153```python theme={null}119```python theme={null}
154# 之前 (claude-code-sdk)120# BEFORE (claude-code-sdk)
155from claude_code_sdk import query, ClaudeCodeOptions121from claude_code_sdk import query, ClaudeCodeOptions
156 122
157options = ClaudeCodeOptions(model="claude-opus-4-7", permission_mode="acceptEdits")123options = ClaudeCodeOptions(model="claude-opus-4-7", permission_mode="acceptEdits")
158 124
159# 之后 (claude-agent-sdk)125# AFTER (claude-agent-sdk)
160from claude_agent_sdk import query, ClaudeAgentOptions126from claude_agent_sdk import query, ClaudeAgentOptions
161 127
162options = ClaudeAgentOptions(model="claude-opus-4-7", permission_mode="acceptEdits")128options = ClaudeAgentOptions(model="claude-opus-4-7", permission_mode="acceptEdits")
163```129```
164 130
165**为什么变更:** 类型名称现在与"Claude Agent SDK"品牌相匹配,并在 SDK 的命名约定中提供一致性。
166
167<h3 id="system-prompt-no-longer-default">131<h3 id="system-prompt-no-longer-default">
168 系统提示不再是默认值132 系统提示词不再是默认值
169</h3>133</h3>
170 134
171**变更内容:** SDK 不再默认使用 Claude Code 的系统提示。135**变更内容:** SDK 不再默认使用 Claude Code 的系统提示词。
172 136
173**迁移:**137**迁移:**
174 138
176 ```typescript TypeScript theme={null}140 ```typescript TypeScript theme={null}
177 import { query } from "@anthropic-ai/claude-agent-sdk";141 import { query } from "@anthropic-ai/claude-agent-sdk";
178 142
179 // 之前 (v0.0.x) - 默认使用 Claude Code 的系统提示143 // BEFORE (v0.0.x) - 默认使用 Claude Code 的系统提示词
180 const before = query({ prompt: "Hello" });144 const before = query({ prompt: "Hello" });
181 145
182 // 之后 (v0.1.0) - 默认使用最小系统提示146 // AFTER (v0.1.0) - 默认使用最小系统提示词
183 // 要获得旧行为,请显式请求 Claude Code 的预设:147 // 要获得旧行为,请显式请求 Claude Code 的预设:
184 const presetResult = query({148 const presetResult = query({
185 prompt: "Hello",149 prompt: "Hello",
188 }152 }
189 });153 });
190 154
191 // 或使用自定义系统提示:155 // 或使用自定义系统提示词:
192 const customResult = query({156 const customResult = query({
193 prompt: "Hello",157 prompt: "Hello",
194 options: {158 options: {
198 ```162 ```
199 163
200 ```python Python theme={null}164 ```python Python theme={null}
201 # 之前 (v0.0.x) - 默认使用 Claude Code 的系统提示165 from claude_agent_sdk import query, ClaudeAgentOptions
166 import asyncio
167
168
169 async def main():
170 # BEFORE (v0.0.x) - 默认使用 Claude Code 的系统提示词
202 async for message in query(prompt="Hello"):171 async for message in query(prompt="Hello"):
203 print(message)172 print(message)
204 173
205 # 之后 (v0.1.0) - 默认使用最小系统提示174 # AFTER (v0.1.0) - 默认使用最小系统提示词
206 # 要获得旧行为,请显式请求 Claude Code 的预设:175 # 要获得旧行为,请显式请求 Claude Code 的预设:
207 from claude_agent_sdk import query, ClaudeAgentOptions
208
209 async for message in query(176 async for message in query(
210 prompt="Hello",177 prompt="Hello",
211 options=ClaudeAgentOptions(178 options=ClaudeAgentOptions(
214 ):181 ):
215 print(message)182 print(message)
216 183
217 # 或使用自定义系统提示:184 # 或使用自定义系统提示词:
218 async for message in query(185 async for message in query(
219 prompt="Hello",186 prompt="Hello",
220 options=ClaudeAgentOptions(system_prompt="You are a helpful coding assistant"),187 options=ClaudeAgentOptions(system_prompt="You are a helpful coding assistant"),
221 ):188 ):
222 print(message)189 print(message)
190
191
192 asyncio.run(main())
223 ```193 ```
224</CodeGroup>194</CodeGroup>
225 195
226**为什么变更:** 为 SDK 应用程序提供更好的控制和隔离。您现在可以构建具有自定义行为的代理,而无需继承 Claude Code 的 CLI 焦点指令。
227
228<h3 id="settings-sources-default">196<h3 id="settings-sources-default">
229 设置源默认值197 设置源默认值
230</h3>198</h3>
231 199
232此默认值在 v0.1.0 中曾短暂更改,然后被还原,因此无需迁移操作。200此默认值在 v0.1.0 中曾被短暂更改为不加载任何文件系统设置,然后已恢复,因此无需迁移操作。
233 201
234**当前行为:** 在 `query()` 上省略 `settingSources` 会加载用户、项目和本地文件系统设置,与 CLI 匹配。这包括 `~/.claude/settings.json`、`.claude/settings.json`、`.claude/settings.local.json`、CLAUDE.md 文件和自定义命令。202**当前行为:** 在 `query()` 上省略 `settingSources` 会加载用户、项目和本地文件系统设置,与 CLI 匹配。这包括 `~/.claude/settings.json`、`.claude/settings.json`、`.claude/settings.local.json`、CLAUDE.md 文件和自定义命令。
235 203
236要从文件系统设置中隔离运行,请传递空数组:204要从文件系统设置中隔离运行,请传递 `settingSources: []`,或在 Python 中传递 `setting_sources=[]`。请参阅 [使用 settingSources 控制文件系统设置](/docs/zh-CN/agent-sdk/claude-code-features#control-filesystem-settings-with-settingsources) 了解每个源加载的内容。
237
238<CodeGroup>
239 ```typescript TypeScript theme={null}
240 import { query } from "@anthropic-ai/claude-agent-sdk";
241
242 const isolatedResult = query({
243 prompt: "Hello",
244 options: {
245 settingSources: [] // 未加载文件系统设置
246 }
247 });
248
249 // 或仅加载特定源:
250 const projectOnlyResult = query({
251 prompt: "Hello",
252 options: {
253 settingSources: ["project"] // 仅项目设置
254 }
255 });
256 ```
257
258 ```python Python theme={null}
259 from claude_agent_sdk import query, ClaudeAgentOptions
260
261 async for message in query(
262 prompt="Hello",
263 options=ClaudeAgentOptions(setting_sources=[]), # 未加载文件系统设置
264 ):
265 print(message)
266
267 # 或仅加载特定源:
268 async for message in query(
269 prompt="Hello",
270 options=ClaudeAgentOptions(
271 setting_sources=["project"] # 仅项目设置
272 ),
273 ):
274 print(message)
275 ```
276</CodeGroup>
277 205
278隔离对于 CI/CD 管道、已部署的应用程序、测试环境和多租户系统特别重要,其中本地自定义不应泄露。206隔离对于 CI/CD 管道、已部署的应用程序、测试环境和多租户系统特别重要,其中本地自定义不应泄露。
279 207
280<Note>208<Note>
281 SDK v0.1.0 曾短暂默认为不加载任何设置;这在后续版本中被还原。Python SDK 0.1.59 及更早版本将空列表视为与省略选项相同,因此在依赖 `setting_sources=[]` 之前请升级。有关即使 `settingSources` 为 `[]` 时仍会读取的输入,请参阅 [settingSources 不控制的内容](/zh-CN/agent-sdk/claude-code-features#what-settingsources-does-not-control)。209 Python SDK 0.1.59 及更早版本将空列表视为与省略该选项相同,因此在依赖 `setting_sources=[]` 之前请升级。请参阅 [settingSources 不控制的内容](/docs/zh-CN/agent-sdk/claude-code-features#what-settingsources-does-not-control) 了解即使在 `settingSources` 为 `[]` 时也会读取的输入。
282</Note>210</Note>
283 211
284<h2 id="why-the-rename">
285 为什么重命名?
286</h2>
287
288Claude Code SDK 最初是为编码任务设计的,但它已发展成为构建所有类型 AI 代理的强大框架。新名称"Claude Agent SDK"更好地反映了其功能:
289
290* 构建业务代理(法律助手、财务顾问、客户支持)
291* 创建专门的编码代理(SRE 机器人、安全审查员、代码审查代理)
292* 为任何领域开发自定义代理,具有工具使用、MCP 集成等功能
293
294<h2 id="getting-help">
295 获取帮助
296</h2>
297
298如果您在迁移过程中遇到任何问题:
299
300**对于 TypeScript/JavaScript:**
301
3021. 检查所有导入是否已更新为使用 `@anthropic-ai/claude-agent-sdk`
3032. 验证您的 package.json 具有新的包名称
3043. 运行 `npm install` 以确保依赖项已更新
305
306**对于 Python:**
307
3081. 检查所有导入是否已更新为使用 `claude_agent_sdk`
3092. 验证您的 requirements.txt 或 pyproject.toml 具有新的包名称
3103. 运行 `pip install claude-agent-sdk` 以确保包已安装
311
312<h2 id="next-steps">212<h2 id="next-steps">
313 后续步骤213 后续步骤
314</h2>214</h2>
315 215
316* 探索 [Agent SDK 概述](/zh-CN/agent-sdk/overview) 以了解可用功能216* 探索 [Agent SDK 概述](/docs/zh-CN/agent-sdk/overview) 以了解可用功能
317* 查看 [TypeScript SDK 参考](/zh-CN/agent-sdk/typescript) 以获取详细的 API 文档217* 查看 [TypeScript SDK 参考](/docs/zh-CN/agent-sdk/typescript) 以获取详细的 API 文档
318* 查看 [Python SDK 参考](/zh-CN/agent-sdk/python) 以获取 Python 特定文档218* 查看 [Python SDK 参考](/docs/zh-CN/agent-sdk/python) 以获取 Python 特定文档
319* 了解 [自定义工具](/zh-CN/agent-sdk/custom-tools) 和 [MCP 集成](/zh-CN/agent-sdk/mcp)219* 了解 [自定义工具](/docs/zh-CN/agent-sdk/custom-tools) 和 [MCP 集成](/docs/zh-CN/agent-sdk/mcp)