上一篇文章里我说「Agent SDK 本质就是 Claude Code 的 Python 包装/套壳」, 有朋友不信,说 SDK 是独立库直接调 API、跟 Claude Code 没关系。 那就掏源码实证,看看到底谁对。
不上博客文章、不引第三方解读,直接看官方 PyPI 包 claude-agent-sdk v0.2.82
(截至 2026-05 最新)解压后的 Python 源码。
掏源码的好处:所有结论可以用 5 秒 grep 复现,不是"我说",是"它自己写的"。
装完包后看 METADATA 文件:
Name: claude-agent-sdk
Version: 0.2.82
Summary: Python SDK for Claude Code
Project-URL: Homepage, https://github.com/anthropics/claude-agent-sdk-python
Project-URL: Documentation, https://docs.anthropic.com/en/docs/claude-code/sdk
Author-email: Anthropic <support@anthropic.com>
官方一句话定位:Python SDK for Claude Code。
文档链接里都是 claude-code/sdk 路径。
SDK 内部负责跟外界通信的 transport 文件,第一行 docstring:
"""Subprocess transport implementation using Claude Code CLI."""
import atexit
import json
import logging
...
class SubprocessCLITransport(Transport):
"""Subprocess transport using Claude Code CLI."""
类名直接叫 SubprocessCLITransport,注释明确说"using Claude Code CLI"。
MINIMUM_CLAUDE_CODE_VERSION = "2.0.0"
SDK 要求系统装的 Claude Code 必须 ≥ 2.0。也就是说:
SDK 不是独立的 — 它强依赖 Claude Code CLI 在系统里。
def _find_cli(self) -> str:
"""Find Claude Code CLI binary."""
# First, check for bundled CLI
...
# Fall back to system-wide search
if cli := shutil.which("claude"):
return cli
# 启动 subprocess 时的实际命令:
cmd = [self._cli_path, "--output-format", "stream-json", "--verbose"]
SDK 的 connect() 内部做的事:
claude 二进制--output-format stream-json --verbose spawn 子进程# Track live CLI subprocesses so we can terminate them when the parent
# Python process exits. This mirrors the TypeScript SDK's parent-exit cleanup
# and prevents orphaned `claude` processes from leaking when callers
# crash or exit before awaiting close().
_ACTIVE_CHILDREN: set[Process] = set()
atexit.register(_kill_active_children)
SDK 注册了 Python atexit 钩子,目的明确:
"防止 orphaned claude processes 在调用方崩溃/退出时泄漏"
— SDK 源码 line 39-42
SDK 心心念念担心的是 "orphaned claude processes"(孤儿 claude 进程)。 如果 SDK 跟 Claude Code 没关系,根本不会有这个担心。
实证完源码再去看官方文档怎么说,原文用的是更精准的措辞:
"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." — code.claude.com/docs/en/agent-sdk/overview
关键短语:"Claude Code as a library"(把 Claude Code 当库用)+ "the same tools that power Claude Code"(给你 Claude Code 同款工具集)。
所以:"套壳"技术上准确,但语气不公允。更精准的说法是 "Claude Code as a library" 或 "Claude Code 的程序化接口"。
有人想"绕开 SDK 直接 spawn claude 子进程"以为更直接。其实你只是重写一遍
subprocess_cli.py,而且大概率没 SDK 写得好(错过 atexit 清理、JSON streaming 解析、
session 管理等等几千行的细节)。
不是"二选一谁更好",而是侧重不同:
不信?你装个 SDK 自己看:
$ pip install claude-agent-sdk
$ python -c "from claude_agent_sdk import SystemMessage; \
import claude_agent_sdk._internal.transport.subprocess_cli as t; \
print(open(t.__file__).read()[:200])"
"""Subprocess transport implementation using Claude Code CLI."""
import atexit
...
5 秒搞定。源码不会撒谎。
是的,Claude Agent SDK 确实通过 spawn claude CLI 子进程实现,
系统必须有 Claude Code(≥ 2.0)才能跑。源码层面 5 条铁证 + 官方文档明文。
"套壳"这词技术准确但带情绪。更精准的说法是 "Claude Code as a library" — 这是官方自己的措辞,把 Claude Code 的工具循环、subagent 框架、hook 机制、MCP 集成 完整暴露给 Python/TypeScript 程序。
所以选 SDK 还是 API 直连,跟"哪个 Claude 更聪明"无关(都是同一个 Opus 4.7), 跟"你要不要那套程序化 agent 框架"有关。
.venv/Lib/site-packages/claude_agent_sdk/_internal/transport/subprocess_cli.py