← ブログに戻る

Claude Agent SDK 3つの使い分け

この記事を含む総合ガイド Claude Code 実戦運用ガイド

「Claude Agent SDK って、Claude Code の新しい呼び名でしょ?」と読み飛ばすと、後で「terminal で claude を打つ話」と「import して自前アプリの中で動かす話」が同じ記事に混ざり、どちらが自分の業務に効くか分からなくなります。

公式 docs (code.claude.com/docs/en/agent-sdk/overview、取得 2026-11-27) を読むと、Anthropic 自身が 4つの経路 を1つの表で区別していました。Agent SDK は「Claude Code 本体を呼び替えたもの」ではなく、Claude Code の agent loop をライブラリとして出した別物という位置づけです。

公式 docs の分類表には4経路が並ぶ

docs の “Compare the Agent SDK to other Claude tools” 節の表を要約するとこの4つです。

経路何を得るかいつ選ぶか
Agent SDKClaude Code の agent loop・tools・permissions・hooks を Python / TypeScript のライブラリで自前の Python / TS アプリに埋め込みたい
Claude Code CLIターミナル UI。対話開発と単発タスク日々の対話開発や、手元で1回限りのタスク
Client SDKClaude API への直接アクセス (他言語含む)。tool loop は自前で書くか tool runner に任せるモデルAPIだけ使いたい。agent loop は自分で設計する
Managed AgentsAnthropic (または自前) のサンドボックスでホストされる agent harnessAnthropic にホストさせたい

docs の原文は “The Agent SDK, the CLI, the Client SDK, and Managed Agents differ in who runs the agent, what comes built in, and how you reach it.” です。この「誰が走らせるか / 何が入っているか / どう呼ぶか」が、以降で整理する3つの軸の元ネタです。関連: Claude Code・Cursor・Codexを1日並列で走らせた判断 412 回 で Claude Code 側の運用を追った記事があります。

1つ目の軸: 実行者 — 誰のプロセスで動くか

Agent SDK は docs の表に A library that runs the Claude Code binary と書かれています。ユーザー側のプロセスの中で Claude Code の binary を呼び出すライブラリで、@anthropic-ai/claude-agent-sdk (npm) または claude-agent-sdk (PyPI) を import して使います (バージョン確認は後述)。

Claude Code CLI は逆で、Anthropic が配布している binary (claude) を手元のターミナルから起動します。docs の overview には “Claude Code is an agentic coding tool that reads your codebase, edits files, runs commands” とあり、開発者本人がターミナルの前にいる前提で設計されています。

Managed Agents はさらに立ち位置が違い、“A hosted agent harness that runs the agent loop, with sessions in an Anthropic-managed cloud sandbox” です。Anthropic のクラウドで走るので、手元にプロセスを用意する必要がありません。

「自分のサーバーで動かしたいか / 自分のターミナルで動かしたいか / 預けたいか」で1行目が決まります。

2つ目の軸: ループの所有 — どのコードが turn を回すか

docs の agent loop ページ (agent-loop) を読むと、turn を回すのは SDK の中の Claude Code の実行ループです。原文は “When you start an agent, the SDK runs the same execution loop that powers Claude Code: Claude evaluates your prompt, calls tools to take action, receives the results, and repeats until the task is complete.” です。

ユーザーのアプリがやることは 非同期イテレータを回してメッセージを受けることだけで、ツール呼び出しの turn 間に制御は戻ってきません。docs にも “This happens without yielding control back to your code” と明記されています。

async for message in query(prompt="Fix the failing tests in auth.ts"):
    if isinstance(message, ResultMessage):
        if message.subtype == "success":
            print(message.result)

ループを止める / 絞るのはユーザー側の責任です。docs の “Control how the loop runs” で ClaudeAgentOptions / Options のフィールドとして並ぶ主な制御は3つ:

  • max_turns / maxTurns: turn 数の上限 (到達すると error_max_turns で終わる)
  • max_budget_usd / maxBudgetUsd: 見積もり支出額の上限 (到達すると error_max_budget_usd で終わる)
  • permission_mode / permissionMode: ツール実行をどこまで自動承認するか (default / acceptEdits / plan / dontAsk / auto / bypassPermissions)

Client SDK を選んだ場合はここが反転します。docs の表に “You write the tool loop yourself, or let the client SDK’s beta tool runner drive it.” とあり、ループ自体を自分で書くことになります。Agent SDK と Client SDK を混同すると、「ループが勝手に回り続ける」/「ループが勝手に止まる」どちらで困るかが逆向きになります。

3つ目の軸: 利用者層 — 誰が叩くか

Claude Code CLI の docs overview は “Terminal: The full-featured CLI for working with Claude Code directly in your terminal” で、開発者本人の手元が想定です。CLAUDE.md・skills・hooks を ~/.claude/ と .claude/ から読み込み、/compact や /clear を対話的に打つ運用になります。

Agent SDK の想定は違います。docs の overview に “Embed Claude Code’s agent in your own Python or TypeScript application, in a process you operate” とあり、他のソフトの中に埋め込まれる開発者が利用者です。自分のサービスの中で「ユーザー入力 → Claude Code の agent → 結果を自分の DB に書く」のような経路を作るときに query() を呼びます。

docs の branding guidelines にも線が引かれています: Agent SDK を組み込んだプロダクトは “Claude Code” や “Claude Code Agent” を名乗ることは許可されません。“Claude Agent” または {YourAgentName} Powered by Claude を使うよう指示されていて、SDK 側は匿名のエンジンとして裏に入る設計が明文化されています。

  • Claude Code CLI = 開発者 (本人) 向け
  • Agent SDK = 開発者が作るソフトの裏側エンジン
  • Managed Agents = SDK を自前ホストする手間を Anthropic に預ける層

自分で確かめた1点: 2026-11-27 時点の両レジストリに実在する

この整理が「docs だけの話」でないことを確かめるため、npm と PyPI の両レジストリに配布パッケージが実在するかだけ、手元で引きました。記録は iris-lab/experiments/auto/2026-11-27-claude-agent-sdk-packages に output.txt ごと残してあります。

$ npm view @anthropic-ai/claude-agent-sdk name version
name = '@anthropic-ai/claude-agent-sdk'
version = '0.3.332'

$ curl -s https://pypi.org/pypi/claude-agent-sdk/json | python3 -c "..."
name: claude-agent-sdk
version: 0.2.189
requires_python: >=3.10

両方とも Anthropic 公式の repository を指していました (anthropics/claude-agent-sdk-typescript / anthropics/claude-agent-sdk-python)。npm の description は “SDK for building AI agents with Claude Code’s capabilities” で、docs 側の “same tools, agent loop, and context management that power Claude Code” と同じ主張です。

これ以上の挙動 (実行コスト・レイテンシなど) までは測っていません。「SDK は実在し、Python ≥3.10 と TypeScript で入る」ところまでが、この記事の実測です。

気をつけたい境界

docs を全部読むと、使い分けの前に踏みやすい境界が2つあります。

1. Agent SDK は claude.ai ログインを他社プロダクトに貸せない。docs の note に “Unless previously approved, Anthropic does not allow third party developers to offer claude.ai login or rate limits for their products, including agents built on the Claude Agent SDK.” と1文で書かれています。自分のサービスの認証を claude.ai の rate limit で代替することは明示的に禁じられているので、API key 認証を自前で設計します。

2. Python / TypeScript 以外から Claude Code の loop を使いたい場合は、SDK ではなく CLI をサブプロセスで叩く。docs の overview に “To drive the same agent loop from a language other than Python or TypeScript, run the CLI as a subprocess with the -p flag and --output-format json.” と逃げ道が書かれていました。Go や Rust から呼ぶなら Agent SDK ではなくこのルートになります。

読者にとって何が変わるか

  • 「terminal で claude を叩く作業」= Claude Code CLI の話。CLAUDE.md・/compact などの対話機能の文脈
  • 「自前の Python / TS アプリに組み込みたい」= Agent SDK の話。query() と ClaudeAgentOptions / Options、max_turns / max_budget_usd の設計
  • 「Anthropic にホストさせたい」= Managed Agents の話。手元に SDK は要らない
  • 「Go / Rust / Ruby から回したい」= CLI をサブプロセスで叩く 迂回路

次に「Claude Agent SDK」と書かれた記事を見るときは、その中で claude と打っているのか、query() を import しているのかを最初の段落で見分けると、残りがどちらの文脈なのかで読める形になります。

まとめ

  • Agent SDK ≠ Claude Code の新名称。公式 docs は Agent SDK / Claude Code CLI / Client SDK / Managed Agents の4経路を “who runs the agent, what comes built in, and how you reach it” で区別している
  • 3つの軸 = 実行者 (ユーザーのプロセス / ターミナル / Anthropic のクラウド) × ループ所有 (SDK 内で回る / 自分で書く) × 利用者層 (開発者本人 / 他ソフトの裏側 / ホスト委託)
  • Agent SDK は npm @anthropic-ai/claude-agent-sdk v0.3.332 / PyPI claude-agent-sdk v0.2.189 で 2026-11-27 時点に実在 (Python は ≥3.10)
  • claude.ai ログインの他社提供は不可。Python / TypeScript 以外は CLI subprocess 経由
  • Claude Code の pillar は /ja/blog/claude-code/。CLI 側の文法はこちらから辿れます

References

実践Claude Code 関連書籍 実践Claude Code 毎日 Claude Code を使うエンジニアへ — CLAUDE.md・Plan Mode・チーム展開の実践ガイド 書籍ページを見る →