Claude Code SDK 更名为 Claude Agent SDK 的迁移要点
Claude Code SDK 已更名为 Claude Agent SDK,文档同步重组,以体现其构建 AI 智能体而非仅限编码任务的能力。迁移涉及 TypeScript/JavaScript 包名替换,以及 Python 类型改名等破坏性变更。
## 概览 Claude代码SDK 现已更名为 **克劳德探员 SDK** 及其文件已重新编排。SDK建设的更广泛能力AI无法执行编码任务的代理。OpenAI探员SDK换成? [OpenAI 代理 SDK 迁移食谱](<https://platform.claude.com/cookbook/claude-agent-sdk-04-migrating-from-openai-agents-sdk>) 通过单个实例将每个原始地图映射到克劳德特工SDK上。 ## 改变的是什么 | 侧面 | 旧 | 新设 | | --- | --- | --- | | **软件包名称( TS / JS )** | `@anthropic-ai/claude-code` | `@anthropic-ai/claude-agent-sdk` | | **Python 软件包** | `claude-code-sdk` | `claude-agent-sdk` | | **文档位置** | Claude 代码文件 | Claude 代码文件 —— 专用 [探员SDK](<https://code.claude.com/docs/en/agent-sdk/overview>) 节次 | ## 移徙步骤 ### 迁移一个 TypeScript 或 JavaScript 项目 **1. 解开旧软件包 :** ``` npm uninstall @anthropic-ai/claude-code ``` **2. 安装新软件包 :** ``` npm install @anthropic-ai/claude-agent-sdk ``` **3. 更新您的导入 :** 更改所有从 `@anthropic-ai/claude-code` 改为: `@anthropic-ai/claude-agent-sdk`: ``` // Before import { query, tool, createSdkMcpServer } from "@anthropic-ai/claude-code"; // After import { query, tool, createSdkMcpServer } from "@anthropic-ai/claude-agent-sdk"; ``` **4. 更新软件包。 json :** 若为: `@anthropic-ai/claude-code` 仍然列在您的列表中 `package.json`,将其替换为 `@anthropic-ai/claude-agent-sdk` 并更新版本范围,例如从 `"^0.0.42"` 改为: `"^0.3.0"`. **5. 审查 [断开更改](<https://code.claude.com/docs/en/agent-sdk/migration-guide#breaking-changes>)** 完成迁移所需的代码修改。 ### 移动 Python 工程 **1. 解开旧软件包 :** ``` pip uninstall -y claude-code-sdk ``` 如果旧软件包没有安装, pip 打印 `WARNING: Skipping claude-code-sdk as it is not installed.` 这已经是预料之中了,你可以继续下一步。 **2. 安装新软件包 :** ``` pip install claude-agent-sdk ``` 若为: `claude-code-sdk` 列在您的 `requirements.txt` 或 时 间 `pyproject.toml`,将其替换为 `claude-agent-sdk`. **3. 更新您的导入 :** 更改所有从 `claude_code_sdk` 改为: `claude_agent_sdk`: ``` # Before from claude_code_sdk import query, ClaudeCodeOptions # After from claude_agent_sdk import query, ClaudeAgentOptions ``` **4. 审查 [断开更改](<https://code.claude.com/docs/en/agent-sdk/migration-guide#breaking-changes>)** 完成迁移所需的代码修改。 ## 中断更改 为了改进隔离和明确配置,克劳德特工SDK v0.1.0引入了从克劳德代码SDK迁移的用户的突破变化. ### Python: ClaudeCodeOptions 重命名为 ClaudeAgentOptions **改变的是什么:** Python SDK 类型 `ClaudeCodeOptions` 已重新命名为 `ClaudeAgentOptions`. **移徙:** ``` # BEFORE (claude-code-sdk) from claude_code_sdk import query, ClaudeCodeOptions options = ClaudeCodeOptions(model="claude-opus-4-7", permission_mode="acceptEdits") # AFTER (claude-agent-sdk) from claude_agent_sdk import query, ClaudeAgentOptions options = ClaudeAgentOptions(model="claude-opus-4-7", permission_mode="acceptEdits") ``` ### 系统提示不再默认 **改变的是什么:** SDK 默认不再使用 Claude Code 的系统提示. **移徙:** ``` import { query } from "@anthropic-ai/claude-agent-sdk"; // BEFORE (v0.0.x) - Used Claude Code's system prompt by default const before = query({ prompt: "Hello" }); // AFTER (v0.1.0) - Uses minimal system prompt by default // To get the old behavior, explicitly request Claude Code's preset: const presetResult = query({ prompt: "Hello", options: { systemPrompt: { type: "preset", preset: "claude_code" } } }); // Or use a custom system prompt: const customResult = query({ prompt: "Hello", options: { systemPrompt: "You are a helpful coding assistant" } }); ``` ``` from claude_agent_sdk import query, ClaudeAgentOptions import asyncio async def main(): # BEFORE (v0.0.x) - Used Claude Code's system prompt by default async for message in query(prompt="Hello"): print(message) # AFTER (v0.1.0) - Uses minimal system prompt by default # To get the old behavior, explicitly request Claude Code's preset: async for message in query( prompt="Hello", options=ClaudeAgentOptions( system_prompt={"type": "preset", "preset": "claude_code"} # Use the preset ), ): print(message) # Or use a custom system prompt: async for message in query( prompt="Hello", options=ClaudeAgentOptions(system_prompt="You are a helpful coding assistant"), ): print(message) asyncio.run(main()) ``` ### 设置源默认值 此默认在 v0.1.0 中被短暂修改为不装入文件系统设置,然后返回,所以不需要迁移动作. **当前行为 :** 发声 `settingSources` 打开 `query()` 装入用户、工程和本地文件系统设置, 匹配 CLI。 这包括 `~/.claude/settings.json`, `.claude/settings.json`, `.claude/settings.local.json`, CLAUDE 文件和自定义命令。要从文件系统设置中分离运行,请通过 `settingSources: []`,或 `setting_sources=[]` 在 Python 中。见 [使用 settingSources 控制文件系统设置](<https://code.claude.com/docs/en/agent-sdk/claude-code-features#control-filesystem-settings-with-settingsources>) 隔离对于CI / CD 管道、部署的应用程序、测试环境以及当地定制不应泄露的多租户系统特别重要。 Python SDK 0.1.59 和之前处理过一个与省略选项相同的空列表,因此在依赖之前升级 `setting_sources=[]`。见 [settingSources 无法控制的东西](<https://code.claude.com/docs/en/agent-sdk/claude-code-features#what-settingsources-does-not-control>) 输入,即使已读 `settingSources` 为: `[]`. ## 下一个步骤 - 探索 [代理 SDK 概览](<https://code.claude.com/docs/en/agent-sdk/overview>) 学习现有特性 - 检查一下 [TypeScript SDK 参考文献](<https://code.claude.com/docs/en/agent-sdk/typescript>) 详细API文档 - 审查《公约》执行情况 [Python SDK 参考文献](<https://code.claude.com/docs/en/agent-sdk/python>) 用于 Python 特定文档 - 学习关于 [自定义工具](<https://code.claude.com/docs/en/agent-sdk/custom-tools>) 财务报告和财务报告 [MCP 整合](<https://code.claude.com/docs/en/agent-sdk/mcp>)
查看原始来源