Claude Agent SDKClaude Agent SDK
它是什么:Claude Code 的编程接口(Python 版)。
它把一个成熟的 coding CLI 变成 Python 能驱动的对象。
它不是一个自建 agent 的框架。
模型接入受该产品官方接口限制 —— 后面所有取舍都由这条决定。
官方 README 首行:"Python SDK for Claude Agent."
What it is: the Python programming interface to Claude Code. It turns a polished coding CLI into an object you can drive from Python. It is not a framework for building your own agent. It is also the only coding base among our ten objects that cannot switch models — every trade-off below follows from that.
Official README, first line: "Python SDK for Claude Agent."
适合与不适合When to use it, when not to
适合三类情况:
- 想要一个已经被打磨过的 coding agent —— 工具集、权限链、hooks、session fork 都在
- 团队用 Python
- 愿意把推理完全交给 Claude
不适合三类情况:
- 需要换模型,或接自托管权重 —— 直接排除
- 需要框架级的自定义 agent loop —— 形状由 Claude Code 决定(备选 OpenAI Agents SDK 或 LangGraph)
- 需要多 agent 编排 —— 它没有 agents-as-tools 那种一等委派机制
One line: it is a coding agent that has already been polished.
Swapping models and shaping the agent loop are the two things it cannot do.
Fits:
- you want a coding agent that has already been polished — tool set, permission chain, hooks and session fork all included
- your team works in Python
- you are happy handing reasoning entirely to Claude
Not a fit:
- you need a different model / self-hosted weights — ruled out outright
- you need a framework-level custom agent loop — the loop shape is decided by Claude Code
- you need multi-agent orchestration — it has no first-class agents-as-tools delegation
中文生态China availability 国产 / 国内可直接用 / 备案状态 · 快照domestic / direct access / CAC filing · snapshot 2026-10-20
- 出品属地Origin
- 国外出品foreign-made 国外主体(网信办公示名单未见对应备案主体)。foreign entity (no matching filing entity in the CAC registry).
- 国内使用Direct access
- 大陆可直连direct OK 文档站直连实测 200。docs site measured 200 direct.
- 备案状态CAC filing
- 未见于备案名单not in filing list:未见于网信办公示名单(2026-10-20 检索 13 批附件,https://www.cac.gov.cn/2024-04/02/c_1713729983803145.htm);该产品未面向大陆公众提供服务,备案不适用。not in the CAC public registry (13 batch attachments searched 2026-10-20, https://www.cac.gov.cn/2024-04/02/c_1713729983803145.htm); the product does not serve the mainland public, so filing does not apply.
备案信息以网信办《生成式人工智能服务已备案信息》公示名单为准(按批次滚动更新,本站只抄编号不解读);「大陆可直连」是本站从大陆网络直连实测的结果,不是厂商承诺。GitHub 类站点当日实测有波动,口径写在依据里。Filing status is quoted from the CAC public registry (updated batch by batch; this site copies the number and does not interpret it). "Direct access" is this site's own reachability measurement from a mainland network, not a vendor promise. GitHub-hosted sites fluctuated on the measurement day — the note says so.
固定坐标系Fixed coordinate system
8 个维度dimensions, 与同分区其他对象逐项可比each comparable item by item against others in the same section.
- 模型与开放条件Model & openness
只支持 Claude,没有第二条路可走。
pyproject.toml里没有 litellm / any-llm 之类的可选依赖- 本站其他 harness 都能接自托管模型(Ollama / vLLM / llama.cpp),本 SDK 不能
⚠ 这是它与其他九个对象最本质的差别。
模型层(
models.specul.com)推荐的本地量化路线,在这里走不通。核验:README 全文没有 provider-agnostic 的表述。
Claude only — there is no second path.
pyproject.tomlhas no optional dependencies like litellm / any-llm- every other harness on this site can attach self-hosted models (Ollama / vLLM / llama.cpp); this SDK cannot
⚠ This is its most fundamental difference from the other nine objects.
The local-quantization route recommended by the model layer (
models.specul.com) does not work here.Verified: the README contains no provider-agnostic wording.
- 运行位置Where it runs
它是「你运营 Python 进程 + 它驱动一个 CLI 子进程」的双层结构。
实际形态:pip 包,内含 Claude Code CLI。
CLI 再去连 Anthropic API。换句话说,它不是「库」,是「带 CLI 的库」。
CLI 默认用捆绑版,也可以指向系统安装:
- 换成本机已装的 CLI:
ClaudeAgentOptions(cli_path="/path/to/claude") - 工作目录:
ClaudeAgentOptions(cwd="/path/to/project")
核验:README 明说 CLI 随 wheel 捆绑;
_cli_version.py里__cli_version__ = "2.1.286"。A two-layer structure: "you operate a Python process + it drives a CLI subprocess."
Actual form: a pip package containing the Claude Code CLI.
The CLI then connects to the Anthropic API. In other words it is not "a library" but "a library with a CLI inside".
The CLI defaults to the bundled version but can point at a system install:
- switch to a locally installed CLI:
ClaudeAgentOptions(cli_path="/path/to/claude") - working directory:
ClaudeAgentOptions(cwd="/path/to/project")
Verified: the README states the CLI ships bundled with the wheel;
_cli_version.pyhas__cli_version__ = "2.1.286".- 换成本机已装的 CLI:
- 本地文件Local files
默认就是全套文件工具,而且是全权限起步。
README 原文:
By default, Claude has access to the full Claude Code toolset (Read, Write, Edit, Bash, and others).
- 工作目录:
ClaudeAgentOptions(cwd="/path/to/project")
⚠ 这是与本站其他对象最大的风险差异:默认就能读文件、改文件、跑 Bash。
The default is the full file toolset, starting at full permissions.
README wording:
By default, Claude has access to the full Claude Code toolset (Read, Write, Edit, Bash, and others).
- working directory:
ClaudeAgentOptions(cwd="/path/to/project")
⚠ This is the biggest risk difference vs other objects on this site: by default it can read files, edit files and run Bash.
- 工作目录:
- 关机后的任务Tasks after shutdown
进程关了就停 —— 真正的「关机也跑」不存在。
但会话有官方持久化与续跑语义。存储位置已核验:
~/.claude/projects/<encoded-cwd>/*.jsonl;设CLAUDE_CONFIG_DIR则在其projects/下。续跑有三档:
continue_conversation=True:续最近会话resume=<session_id>:续指定会话fork_session=True:分叉
官方对
resume的说明:"picks up with full context from wherever the session left off"官方 sessions 页原文:
官方原文:"A session is the conversation history the SDK accumulates... The SDK writes it to disk automatically"
一个会话等于一个
claudeCLI 子进程,进程停则 loop 停。跨主机续跑要把 transcript 镜像到共享存储。适配器是
SessionStore。⚠ 官方提醒:session 持久化的是对话而非文件系统。
文件回滚要用 file checkpointing。
官方原文:"Sessions persist the conversation, not the filesystem"
Kill the process and it stops — true "runs after shutdown" does not exist.
But sessions have official persistence and resume semantics. Storage location verified:
~/.claude/projects/<encoded-cwd>/*.jsonl; withCLAUDE_CONFIG_DIRset, under itsprojects/.Resume comes in three flavors:
continue_conversation=True: resume the most recent sessionresume=<session_id>: resume a specific sessionfork_session=True: fork
Official note on
resume: "picks up with full context from wherever the session left off"Official sessions page wording:
Official wording: "A session is the conversation history the SDK accumulates... The SDK writes it to disk automatically"
One session equals one
claudeCLI subprocess; when the process stops, the loop stops.Cross-host resume means mirroring transcripts to shared storage. The adapter is
SessionStore.⚠ The official docs remind: session persistence covers the conversation, not the filesystem.
For file rollback use file checkpointing.
Official wording: "Sessions persist the conversation, not the filesystem"
- 工具与扩展Tools & extensions
两个入口,能力不同 —— 这是很关键的设计细节。
query():单向提问,只能用 CLI 自带工具集ClaudeSDKClient:双向会话,额外解锁 custom tools 与 hooks
custom tools 是「进程内 MCP server」 —— 官方原话:
"in-process MCP servers that run directly within your Python application"
官方列的收益有五条:
收益 说明 No subprocess management 与应用同进程,无需管理子进程 Better performance 工具调用无 IPC 开销 Simpler deployment 单进程而非多进程 Easier debugging 全部代码同进程 Type safety 带类型提示的直接 Python 函数调用 外部 MCP server 照样支持,两者可混用。
mcp_servers字典里可以同时放:create_sdk_mcp_server(...)的产物{"type":"stdio", ...}外部 server
Two entry points with different capabilities — a key design detail.
query(): one-way prompting, can only use the CLI's built-in toolsetClaudeSDKClient: two-way conversation, additionally unlocks custom tools and hooks
Custom tools are "in-process MCP servers" — official wording:
"in-process MCP servers that run directly within your Python application"
The official list of benefits has five items:
Benefit Note No subprocess management same process as the app, no subprocess management Better performance tool calls with no IPC overhead Simpler deployment single process instead of multiple Easier debugging all code in one process Type safety typed direct Python function calls External MCP servers are still supported; the two can be mixed.
The
mcp_serversdict can hold at the same time:- the output of
create_sdk_mcp_server(...) {"type":"stdio", ...}external servers
- 上下文与记忆Context & memory
容易忽略的一个机制:system prompt 的 snapshot 语义。
Claude Code 在会话首次请求时构建它,之后每次请求都复用。
恢复会话之后也照样复用。
改了自定义 prompt 不会立刻生效。
改了
claude_codepreset 的append文本也一样。要等会话被 compact,或者开一个新会话。
除非把
snapshot设为False(需要 CLI 2.1.257+)。这实质上是「上下文快照不可变」的设计。
它保证了 prompt 的确定性,代价是改动生效有延迟。
这与本站「状态 ≠ 上下文」的讨论直接相关。
An easily overlooked mechanism: the system-prompt snapshot semantics.
Claude Code builds it on the session's first request, then reuses it on every later request.
It is still reused after resuming a session.
Changing a custom prompt does not take effect immediately.
The same goes for changed
appendtext of theclaude_codepreset.You must wait for the session to be compacted, or start a new session.
Unless
snapshotis set toFalse(needs CLI 2.1.257+).This is essentially an "immutable context snapshot" design.
It guarantees prompt determinism; the cost is delayed change effect.
This connects directly to this site's "state ≠ context" discussion.
- 权限与限制Permissions & limits
结论先给:默认全开,权限要你自己收。
官方给的求值链是本站目前见到的最详细的一份。README 原文:
allowed_toolsis a permission allowlist: listed tools are auto-approved, and unlisted tools fall through topermission_modeandcan_use_toolfor a decision. It does not remove tools from Claude's toolset. To block specific tools, usedisallowed_tools.翻译过来是四条:
allowed_tools是准入白名单:列进去 = 自动批准- 未列的工具走
permission_mode与can_use_tool判定 - 它不会把工具从工具集里移除
- 要真正禁用某个工具,得用
disallowed_tools
hooks 提供确定性拦截:
- 它是「Python 函数,由 Claude Code *应用*(不是 Claude)调用」
- 拦截点:
PreToolUse返回permissionDecision: "deny"加理由 - 官方示例:拦 Bash 命令
⚠ 默认起步是全工具 + 无沙箱,想收紧必须主动配置。
三个开关:
disallowed_tools- hooks
permission_mode
Conclusion first: everything is open by default; you tighten permissions yourself.
The official evaluation chain is the most detailed one seen on this site so far. README wording:
allowed_toolsis a permission allowlist: listed tools are auto-approved, and unlisted tools fall through topermission_modeandcan_use_toolfor a decision. It does not remove tools from Claude's toolset. To block specific tools, usedisallowed_tools.Which unpacks into four points:
allowed_toolsis an admission allowlist: listed = auto-approved- unlisted tools fall through to
permission_modeandcan_use_tool - it does not remove tools from the toolset
- to actually disable a tool, use
disallowed_tools
Hooks provide deterministic interception:
- they are "Python functions invoked by Claude Code *applying* them (not Claude)"
- interception point:
PreToolUsereturningpermissionDecision: "deny"plus a reason - official example: blocking Bash commands
⚠ The default starting point is all tools + no sandbox; tightening requires deliberate configuration.
Three switches:
disallowed_tools- hooks
permission_mode
- 适合什么任务Best for
一句话:它是一个已经被打磨过的 coding agent。
换模型和自定义 loop 形状,它做不到。
适合:
- 想要一个已经被打磨过的 coding agent —— 工具集、权限链、hooks、session fork 都在
- 团队用 Python
- 愿意把推理完全交给 Claude
不适合:
- 需要换模型 / 接自托管权重 —— 直接排除
- 需要框架级的自定义 agent loop —— loop 形状由 Claude Code 决定
- 需要多 agent 编排 —— 它没有 agents-as-tools 那种一等委派机制
In one line: it is an already-polished coding agent.
Swapping models and customizing loop shape — it cannot do that.
Fits:
- wanting an already-polished coding agent — toolset, permission chain, hooks, session fork all present
- teams using Python
- being willing to hand reasoning entirely to Claude
Does not fit:
- needing to swap models / attach self-hosted weights — ruled out outright
- needing framework-level custom agent loops — the loop shape is decided by Claude Code
- needing multi-agent orchestration — it has no first-class agents-as-tools delegation mechanism
头号误解Top misconception
- 以为它是「Anthropic 版的 OpenAI Agents SDK」—— 不是。它是 Claude Code CLI 的 SDK,能力来自那个 CLI,不是框架自带Assuming it is "Anthropic's version of the OpenAI Agents SDK" — it is not. It is the SDK for the Claude Code CLI; its capabilities come from that CLI, not from the framework
- 以为
allowed_tools能限制工具集 —— 官方明确「It does not remove tools from Claude's toolset」,禁用要用disallowed_toolsAssumingallowed_toolsrestricts the toolset — the official docs state "It does not remove tools from Claude's toolset"; usedisallowed_toolsto disable - 以为默认是安全的 —— 默认是 full toolset(Read/Write/Edit/Bash),且没有沙箱层Assuming the default is safe — the default is the full toolset (Read/Write/Edit/Bash), with no sandbox layer
- 以为改了 system prompt 立刻生效 —— 会被 snapshot 冻结,要 compact 或新会话才生效(除非 snapshot=False)Assuming a system prompt change takes effect immediately — a snapshot freezes it. It lands only after a compact or a new session (unless snapshot=False)
- 以为 MIT = 无附加条款 —— 受 Anthropic 商业条款约束(见正文)Assuming MIT means no additional terms — Anthropic's commercial terms still apply (see the body above)
- 以为 TS 版授权与 Python 版一致 —— 核验发现 TS 版无 LICENSE 文件、license API 返回 null,授权状态不明(核验 2026-09-30)Assuming the TypeScript build is licensed like the Python one: verification found no LICENSE file in the TS build, and the licence API returns null. Its licensing status is unknown (checked 2026-09-30)
未知项清单Known unknowns
- Anthropic 商业条款的具体约束(尤其面向客户的场景)
- TypeScript 版的授权状态
- 会话持久化的多进程并发语义
- session forking 的具体形态与限制
permission_mode全部取值与can_use_tool的优先级细节- 捆绑 CLI 版本与系统安装版本的兼容差异
- The specific constraints of Anthropic's Commercial Terms (especially customer-facing scenarios)
- The TypeScript version's authorization status
- Multi-process concurrency semantics of session persistence
- The exact shape and limits of session forking
- All
permission_modevalues and the precedence details ofcan_use_tool - Compatibility differences between the bundled CLI version and a system-installed version
证据来源Evidence sources
判断可回到以下一手源复核。本站核验日Every judgement can be rechecked against the primary sources below. Verified on 2026-10-23,内容更新日content updated on 2026-09-30。
| 类型Type | 名称Name | 链接Link |
|---|---|---|
| repo | Claude Agent SDK for Python · 仓库Claude Agent SDK for Python · repository | https://github.com/anthropics/claude-agent-sdk-python |
| docs | 官方文档(Python)Official docs (Python) | https://platform.claude.com/docs/en/agent-sdk/python |
| docs | 权限指南(求值顺序的权威说明)Permission guide (the authoritative statement of the evaluation order) | https://platform.claude.com/docs/en/agent-sdk/permissions |
| docs | Hooks(官方专章)Hooks (official dedicated chapter) | https://platform.claude.com/docs/en/agent-sdk/hooks |
| docs | Claude Code 工具集清单(tools available to Claude)Claude Code toolset list (tools available to Claude) | https://code.claude.com/docs/en/settings#tools-available-to-claude |
| docs | 修改 system prompts(snapshot 语义的权威说明)Modifying system prompts (the authoritative statement of snapshot semantics) | https://code.claude.com/docs/en/agent-sdk/modifying-system-prompts |
| docs | Work with sessions(会话写盘位置、continue/resume/fork、跨主机 SessionStore,核验 2026-10-08)Work with sessions (session write-to-disk location, continue/resume/fork, cross-host SessionStore, verified 2026-10-08) | https://code.claude.com/docs/en/agent-sdk/sessions |
| docs | Hosting the Agent SDK(subprocess 模型、本地磁盘状态、跨主机持久化,核验 2026-10-08)Hosting the Agent SDK (subprocess model, local-disk state, cross-host persistence, verified 2026-10-08) | https://code.claude.com/docs/en/agent-sdk/hosting |
| docs | Anthropic 商业条款(README 末节指向,授权关键)Anthropic Commercial Terms (pointed to by the README's final section, key to authorization) | https://www.anthropic.com/legal/commercial-terms |
| changelog | Releases(0.2.163 @ 2026-09-30)Releases (0.2.163 @ 2026-09-30) | https://github.com/anthropics/claude-agent-sdk-python/releases |
| changelog | CHANGELOG(Claude Code SDK <0.1.0 的破坏性变更)CHANGELOG (breaking changes from Claude Code SDK <0.1.0) | https://github.com/anthropics/claude-agent-sdk-python/blob/main/CHANGELOG.md |
实测记录Measured runs
本档未跑集成与性能实测;结论只覆盖 API 与官方文档披露的能力面。
实测建议:
| # | 测什么 | 为什么值得测 |
|---|---|---|
| 1 | pip 装完到底拉了多少东西(含捆绑 CLI 的体积) | CLI 随包捆绑,部署体积影响未知 |
| 2 | 默认配置下 agent 实际能碰到哪些目录 | 默认 full toolset 且无沙箱,边界要实测 |
| 3 | disallowed_tools 与 hooks 哪条先生效 | README 只说求值顺序概述,实测才知道 |
| 4 | resume 会话后状态恢复到什么粒度 | 本站最关心的「可靠长期运行」 |
| 5 | 改 system prompt 后多久生效(测 compact 触发条件) | snapshot 语义的边界 |
This site has not completed measured runs.
The test protocol is at protocols/framework/_protocol.md.
Suggested measurement:
| # | What to test | Why it is worth testing |
|---|---|---|
| 1 | How much a pip install actually pulls in (including the bundled CLI's size) | the CLI ships with the package; deployment-size impact unknown |
| 2 | Which directories the agent can actually reach under default config | default full toolset with no sandbox; the boundary needs measuring |
| 3 | Whether disallowed_tools or hooks take effect first | the README only gives an evaluation-order overview; only measurement tells |
| 4 | What granularity resume restores session state to | this site's top concern: "reliable long-running operation" |
| 5 | How long after changing the system prompt it takes effect (test compact trigger conditions) | the boundary of snapshot semantics |
它是 CLI 的编程接口,不是独立框架⚠ First, the most misunderstood point: it wraps a CLI, it is not a framework
README 首行就把这事说了:
Python SDK for Claude Agent. The Claude Code CLI is automatically bundled with the package — no separate installation required!
捆绑的是哪个 CLI,有确切记录:
核验_cli_version.py:__cli_version__ = "2.1.286"(捆绑的确切 CLI 版本)。
这意味着能力边界是固定的:
| 你以为 | 实际 |
|---|---|
| 引入一个 agent 框架 | 引入一个内含 CLI 的 wheel,CLI 再去连 API |
| agent loop 由 SDK 定义 | agent loop 由 Claude Code CLI 定义 |
| 可以改 agent 的形状 | 只能通过 options 配置它暴露出来的开关 |
与 OpenAI Agents SDK 的根本差别:
- 后者是自建 agent loop 的框架(primitives-only)
- 本 SDK 是既有 CLI 的 SDK 化
这是本站最需要讲清的一组对照。
The README's first line already says it:
Python SDK for Claude Agent. The Claude Code CLI is automatically bundled with the package — no separate installation required!
Which CLI is bundled has an exact record:
verified_cli_version.py:__cli_version__ = "2.1.286"(the exact bundled CLI version).
This means the capability boundary is fixed:
| What you might think | Reality |
|---|---|
| importing an agent framework | importing a wheel containing a CLI, which then connects to the API |
| the agent loop is defined by the SDK | the agent loop is defined by the Claude Code CLI |
| you can change the agent's shape | you can only configure the switches it exposes via options |
The fundamental difference vs the OpenAI Agents SDK:
- the latter is a framework that builds its own agent loop (primitives-only)
- this SDK is the SDK-ification of an existing CLI
This is the contrast pair this site most needs to make clear.
十个对象里唯一不能换模型的编程底座The only coding base of the ten that cannot swap models
换 provider 这条路,在这套 SDK 上是关着的。
| OpenAI Agents SDK | Claude Agent SDK | |
|---|---|---|
| provider | provider-agnostic,100+ | 仅 Claude |
| 换自托管权重 | 可(litellm / any-llm extra) | 不可 |
| agent loop | SDK 给出原语 | CLI 决定形状 |
⚠ 这直接关掉了本站的一条推荐路线。
- 模型层(
models.specul.com)主推的本地量化模型 + Ollama/vLLM 自托管 - 在这套 SDK 上走不通 —— 想用本地权重就得换方案
The provider-swap path is closed on this SDK.
| OpenAI Agents SDK | Claude Agent SDK | |
|---|---|---|
| provider | provider-agnostic, 100+ | Claude only |
| swapping to self-hosted weights | possible (litellm / any-llm extra) | not possible |
| agent loop | the SDK gives primitives | the CLI decides the shape |
⚠ This directly closes one of this site's recommended routes.
- the model layer (
models.specul.com) mainly recommends local quantized models + Ollama/vLLM self-hosting - it does not work on this SDK — to use local weights you must switch solutions
权限:官方给了完整求值链,但默认是全开的Permissions: the official docs give a full evaluation chain, but the default is wide open
官方给了完整的权限求值链 —— 这是本站目前见到的最详细的一份。
README 这段是权威说明:
allowed_toolsis a permission allowlist: listed tools are auto-approved, and unlisted tools fall through topermission_modeandcan_use_toolfor a decision. It does not remove tools from Claude's toolset. To block specific tools, usedisallowed_tools.
翻译成一张表:
| 机制 | 作用 | 常被误解之处 |
|---|---|---|
allowed_tools | 准入白名单,列进去 = 自动批准 | ⚠ 不会把工具从工具集移除 |
permission_mode | 未列入的工具走这里判定 | 例:acceptEdits 自动接受文件编辑 |
can_use_tool | 自定义回调函数 | 官方给了完整求值顺序文档 |
disallowed_tools | 真正禁用某些工具 | 想真正拦就得用这个 |
| hooks | PreToolUse 返回 deny 加理由 | 确定性拦截,由应用层而非模型层执行 |
⚠ 默认起步状态必须知道:
By default, Claude has access to the full Claude Code toolset (Read, Write, Edit, Bash, and others).
默认就有 Read / Write / Edit / Bash,而且没有沙箱层。
想收紧必须主动配置,三个开关:
disallowed_tools- hooks
permission_mode
hooks 的定位值得单独记:
- 它是「Python 函数,由 Claude Code *应用*(not Claude)调用」
- 拦截发生在确定性代码里,不是靠模型自我约束
对照 Deep Agents:
- 那个的立场是「trust the LLM,不要指望模型自我约束」
- Anthropic 的答案:把拦截放在应用层
The official docs give a complete permission evaluation chain — the most detailed one seen on this site so far.
This README passage is the authoritative statement:
allowed_toolsis a permission allowlist: listed tools are auto-approved, and unlisted tools fall through topermission_modeandcan_use_toolfor a decision. It does not remove tools from Claude's toolset. To block specific tools, usedisallowed_tools.
Translated into a table:
| Mechanism | Role | Commonly misunderstood |
|---|---|---|
allowed_tools | admission allowlist, listed = auto-approved | ⚠ does not remove tools from the toolset |
permission_mode | unlisted tools are decided here | e.g. acceptEdits auto-accepts file edits |
can_use_tool | custom callback function | the official docs give the full evaluation order |
disallowed_tools | actually disables tools | to truly block, use this |
| hooks | PreToolUse returns deny plus a reason | deterministic interception, executed by the application layer, not the model layer |
⚠ The default starting state must be known:
By default, Claude has access to the full Claude Code toolset (Read, Write, Edit, Bash, and others).
By default it has Read / Write / Edit / Bash, with no sandbox layer.
Tightening requires deliberate configuration, three switches:
disallowed_tools- hooks
permission_mode
The positioning of hooks deserves its own note:
- they are "Python functions invoked by Claude Code *applying* them (not Claude)"
- interception happens in deterministic code, not by relying on the model to self-restrain
Contrast with Deep Agents:
- that one's stance is "trust the LLM, don't expect the model to self-restrain"
- Anthropic's answer: put interception in the application layer
custom tools = 进程内 MCP server(一个省心的设计)Custom tools = in-process MCP servers (a tidy design)
custom tools 以「进程内 MCP server」的形式运行。
官方原话:
"in-process MCP servers that run directly within your Python application"
官方列的收益有五条:
| 收益 | 说明 |
|---|---|
| No subprocess management | 与应用同进程,无需管理子进程 |
| Better performance | 工具调用无 IPC 开销 |
| Simpler deployment | 单进程而非多进程 |
| Easier debugging | 全部代码同进程 |
| Type safety | 带类型提示的直接 Python 函数调用 |
外部 MCP server 照样支持,两者可混用。
mcp_servers 字典里可以同时放:
create_sdk_mcp_server(...)的产物{"type":"stdio", ...}外部 server
⚠ 注意能力边界:
- custom tools 与 hooks 只在
ClaudeSDKClient下可用 query()用不了
Custom tools are not plain function calls but "in-process MCP servers."
Official wording:
"in-process MCP servers that run directly within your Python application"
The official list of benefits has five items:
| Benefit | Note |
|---|---|
| No subprocess management | same process as the app, no subprocess management |
| Better performance | tool calls with no IPC overhead |
| Simpler deployment | single process instead of multiple |
| Easier debugging | all code in one process |
| Type safety | typed direct Python function calls |
External MCP servers are still supported; the two can be mixed.
The mcp_servers dict can hold at the same time:
- the output of
create_sdk_mcp_server(...) {"type":"stdio", ...}external servers
⚠ Note the capability boundary:
- custom tools and hooks are only available under
ClaudeSDKClient query()cannot use them
「状态 ≠ 上下文」:这里有个反直觉的 snapshot 设计"State ≠ context": a counterintuitive snapshot design here
system prompt 有「快照」语义。
会话中途改 prompt 不会立刻生效。
README 原文:
Claude Code builds the system prompt on a session's first request, records it, and reuses it on every later request, including after you resume the session. A changed custom prompt, or changedappendtext... then has no effect until the session is compacted or you start a new session. To rebuild the prompt on every request instead... setsnapshottoFalse.
想每次重建,要显式设 snapshot: False。
这需要 CLI 2.1.257+。
README 还记了一个历史 bug:
"Before 2.1.265, a session with anappendor custom prompt recorded it only whensnapshotwas True"
这实质是「上下文快照不可变」:保证了 prompt 的确定性,代价是修改有延迟。
与本站主张的关系:
- 其他 harness 讲的是「如何压缩/持久化上下文」
- 这里讲的是「如何冻结上下文」
它保证同一会话内 prompt 恒定,避免 prompt 漂移导致行为不可预测。
这是一个第三种思路,本站首次记录(标注为本站观察,非官方表述)。
The system prompt has "snapshot" semantics.
Changing the prompt mid-session does not take effect immediately.
README wording:
Claude Code builds the system prompt on a session's first request, records it, and reuses it on every later request, including after you resume the session. A changed custom prompt, or changedappendtext... then has no effect until the session is compacted or you start a new session. To rebuild the prompt on every request instead... setsnapshottoFalse.
To rebuild every time, explicitly set snapshot: False.
This needs CLI 2.1.257+.
The README also records a historical bug:
"Before 2.1.265, a session with anappendor custom prompt recorded it only whensnapshotwas True"
This is essentially an "immutable context snapshot": it guarantees prompt determinism; the cost is delayed changes.
Relationship to this site's stance:
- other harnesses discuss "how to compact/persist context"
- this one discusses "how to freeze context"
It keeps the prompt constant within one session, avoiding unpredictable behavior from prompt drift.
This is a third approach, recorded here for the first time on this site (marked as this site's observation, not official wording).
许可条款的真相:MIT 但不是「无附加条件」The licensing truth: MIT, but not "no additional conditions"
结论:仓库是 MIT,用起来却还受 Anthropic 商业条款约束。
README 末节原文:
Use of this SDK is governed by Anthropic's Commercial Terms of Service, including when you use it to power products and services that you make available to your own customers and end users, except to the extent a specific component or dependency is covered by a different license.
仓库许可证标记是 MIT。
license API 确认 spdx_id: MIT。
但使用受 Anthropic 商业条款约束。
尤其「用它给你自己的客户提供产品/服务」这一条。
⚠ 本站标注:这不是「非开源」,也不是「不能商用」。
但做产品前必须读那份商业条款,MIT 标记本身不构成完整授权判断。
附一处核验发现:
- TypeScript 版仓库:
anthropics/claude-agent-sdk-typescript - 1,780★,活跃
- 没有 LICENSE 文件,license API 返回
null
授权状态与 Python 版是否一致未核实。
Python 版是 MIT + 商业条款;TS 版无文件可查。
选型时若要用 TS 版,授权需另行确认。
Conclusion: the repository is MIT, but usage is still governed by Anthropic's Commercial Terms.
README final-section wording:
Use of this SDK is governed by Anthropic's Commercial Terms of Service, including when you use it to power products and services that you make available to your own customers and end users, except to the extent a specific component or dependency is covered by a different license.
The repository license marker is MIT.
the license API confirms spdx_id: MIT.
But usage is governed by Anthropic's Commercial Terms.
Especially the clause about "using it to power products/services for your own customers".
⚠ This site's note: this is not "not open source", nor "cannot be used commercially".
But you must read those commercial terms before building a product; the MIT marker alone is not a complete authorization judgment.
One verification finding attached:
- the TypeScript repository:
anthropics/claude-agent-sdk-typescript - 1,780★, active
- no LICENSE file, the license API returns
null
Whether its authorization status matches the Python version is not verified.
The Python version is MIT + commercial terms; the TS version has no file to check.
If selecting the TS version, authorization needs separate confirmation.
附:仓库描述字段为空Appendix: the repository description fields are empty
两条元数据是空的,如实记下来。
核验:description为null(GitHub API),homepage也为 null。 文档入口在 README 首行的platform.claude.com/docs/en/agent-sdk/python。
这样记,是为了避免读者以为信息缺失是我们没查。
Two metadata fields are empty; recorded as-is.
verified:descriptionisnull(GitHub API), andhomepageis also null. The docs entry is in the README's first line:platform.claude.com/docs/en/agent-sdk/python.
Recording it this way prevents readers from assuming the missing information is because we did not check.
核验说明Verification notes
confidence: partial 的依据如下。
降级时间:2026-10-03,由 verified 降为 partial。
为什么降级:下方 ❌ 项的主语是本站的取证缺口,不是「官方未提供」。 按 v3 铁律「未知就说未知」,这些条目存在时标 verified 属于虚高,故降为 partial。
主要缺口有三处:
- Anthropic 商业条款的具体条款内容
- TS 版许可状态
- hooks 生命周期边界
已核验:
- ✅ 仓库存在与星数(8,334 · 滚动核验 2026-10-23)
- ✅ 许可(license API 返回 MIT)
- ✅ 最近推送(2026-10-22,仍活跃)
- ✅ 最新版本 0.2.173(2026-10-22 发布)
- ✅ 捆绑 CLI 版本 2.1.286(
_cli_version.py原文) - ✅ README 全文 —— 权限求值链、hooks 定位、错误类型清单、default full toolset 表述
- ✅ README 全文 —— custom tools 为进程内 MCP server、system prompt snapshot 语义
- ✅ 许可条款末节原文
- ✅ v3 计划记录的 0.2.162 已被超越(2026-10-23 滚动核验时最新为 0.2.173)
⚠ 异常发现:
- 仓库
description与homepage均为null - TS 版无 LICENSE 文件,license API 返回
null
❌ 未核验:
- Anthropic 商业条款的具体条款内容(未读原文,属法务范畴)
- 会话的多进程并发语义
- session forking 的具体形态与限制
can_use_tool与permission_mode的完整优先级链(README 只给了概述,细节在 permissions 专章)
一处计划数据修正:
v3 计划 §2.2 记的是 v0.2.162。
实际核验时已发布 v0.2.163,晚于计划编写。
这再次印证「快照会过期、必须现场核验」。
Basis for confidence: partial.
Downgrade time: 2026-10-03, downgraded from verified to partial.
Why downgraded: the subject of the ❌ items below is this site's evidence gap, not "not provided officially". Per the v3 rule "say unknown when unknown", marking verified while these items exist would be inflated, hence partial.
Three main gaps:
- the specific clauses of Anthropic's Commercial Terms
- the TS version's license status
- hooks' lifecycle boundaries
Verified:
- ✅ repository exists and star count (8,334 · rolling check 2026-10-23)
- ✅ license (license API returns MIT)
- ✅ last push (2026-10-22, still active)
- ✅ latest version 0.2.173 (released 2026-10-22)
- ✅ bundled CLI version 2.1.286 (
_cli_version.pyverbatim) - ✅ full README — permission evaluation chain, hooks positioning, error-type list, default full toolset wording
- ✅ full README — custom tools as in-process MCP servers, system-prompt snapshot semantics
- ✅ the licensing final-section verbatim
- ✅ the 0.2.162 recorded in the v3 plan has been overtaken (latest 0.2.173 at the 2026-10-23 rolling check)
⚠ Anomaly findings:
- repository
descriptionandhomepageare bothnull - the TS version has no LICENSE file; the license API returns
null
❌ Not verified:
- the specific clauses of Anthropic's Commercial Terms (original text not read; legal territory)
- multi-process concurrency semantics of sessions
- the exact shape and limits of session forking
- the full precedence chain of
can_use_toolandpermission_mode(the README gives an overview only; details are in the permissions chapter)
One plan-data correction:
v3 plan §2.2 recorded v0.2.162.
At verification time v0.2.163 had been released, later than the plan was written.
This again confirms "snapshots go stale; verification must happen live."
相关条目Related entries
- OpenAI Agents SDKOpenAI Agents SDK本站最该对照的一对:同为模型厂商出品、同为「编程底座」档。OpenAI 侧是自建 loop 的框架 + provider-agnostic;Anthropic 侧是 CLI 的 SDK + 仅 Claude。The pair this site should contrast most: both from model vendors, both in the "coding base" tier. The OpenAI side is a build-your-own-loop framework + provider-agnostic; the Anthropic side is a CLI's SDK + Claude only.
- Codex SDKCodex SDK第三家厂商的同档选择。三家是同一个问题的三种答案:自建框架 / 包装 CLI / 包装 CLI。The third vendor's same-tier choice. The three are three answers to the same question: build your own framework / wrap a CLI / wrap a CLI.
- Deep AgentsDeep Agents权限哲学的对照:「信任 LLM」vs「应用层确定性拦截」Permission-philosophy contrast: "trust the LLM" vs "deterministic interception at the application layer"
- Hermes AgentHermes Agent路线相反、厂商不同(本站前者 Anthropic、后者 Nous Research):那个是自托管优先的通用 harness,本条只包 Anthropic 自家的 CLI。Opposite routes, different vendors (this site's former is Anthropic, the latter Nous Research): that one is a self-hosting-first general harness; this entry only wraps Anthropic's own CLI.
- Claude Code CLIClaude Code CLI另一层 · AI 编程Other layer · AI coding
本页由This page is generated from the ai-handbook 数据层生成(CC BY 4.0)。data layer (CC BY 4.0).
方法论与坐标系定义见仓库内For the methodology and coordinate definitions, see METHODOLOGY.md.