Claude Agent SDK 真的是 Claude Code 套壳吗?

5 条源码实证 · 直接掏出官方 site-packages 看
📅 2026 年 5 月 · by Marcus🐦 · 实证向 · 给爱较真的朋友
❓ 上次分享有朋友质疑

上一篇文章里我说「Agent SDK 本质就是 Claude Code 的 Python 包装/套壳」, 有朋友不信,说 SDK 是独立库直接调 API、跟 Claude Code 没关系。 那就掏源码实证,看看到底谁对。

实证方法

不上博客文章、不引第三方解读,直接看官方 PyPI 包 claude-agent-sdk v0.2.82 (截至 2026-05 最新)解压后的 Python 源码。

掏源码的好处:所有结论可以用 5 秒 grep 复现,不是"我说",是"它自己写的"。

5 条源码证据

1 包元数据自我介绍

装完包后看 METADATA 文件:

.venv/Lib/site-packages/claude_agent_sdk-0.2.82.dist-info/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 路径。

2 Transport 文件第一行注释

SDK 内部负责跟外界通信的 transport 文件,第一行 docstring:

claude_agent_sdk/_internal/transport/subprocess_cli.py
"""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"。

3 最低 Claude Code 版本依赖
MINIMUM_CLAUDE_CODE_VERSION = "2.0.0"

SDK 要求系统装的 Claude Code 必须 ≥ 2.0。也就是说:

→ 系统没装 claude(≥2.0),SDK 根本跑不起来。

SDK 不是独立的 — 它强依赖 Claude Code CLI 在系统里。

4 启动时实际命令:spawn `claude` 二进制
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() 内部做的事:

  1. 找系统 PATH 里的 claude 二进制
  2. --output-format stream-json --verbose spawn 子进程
  3. 通过 stdin/stdout 跟子进程通讯
→ 这不是"调用 API",这是"启动了一个 Claude Code 进程"。
5 进程管理:清理孤儿子进程
# 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 同款工具集)。

所以"套壳"这词准确吗?

措辞
中文社区说法
官方原话
"套壳"
SDK 是 Claude Code 套壳
SDK 是 Claude Code as a library
语气
偏轻蔑("原来就是套壳啊")
中性偏正面("提供编程接口")
技术含义
完全一致:都指"通过 subprocess 调 claude CLI,把它的能力暴露给程序"

所以:"套壳"技术上准确,但语气不公允。更精准的说法是 "Claude Code as a library""Claude Code 的程序化接口"

这事的几个实用结论

① 你"自己 spawn claude" = 重新发明 SDK

有人想"绕开 SDK 直接 spawn claude 子进程"以为更直接。其实你只是重写一遍 subprocess_cli.py,而且大概率没 SDK 写得好(错过 atexit 清理、JSON streaming 解析、 session 管理等等几千行的细节)。

② SDK 跟 API 直连是不同分层的方案

不是"二选一谁更好",而是侧重不同

③ 计费三个池子互相独立

5 秒自己验证

不信?你装个 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 框架"有关。

参考资料