• Projects
  • Service
  • About
  • branding.bz
  • Podcast
  • Tips
  • FAQ
  • Recruit
  • Download
  • Contact
  • branding.bz(ブランド構築SaaS)
  • DESIGN NOW(デザインメディア)
  • X
  • LinkedIn
  • Spotify
  • Facebook

213-0011 神奈川県川崎市高津区久本3-6-7-303

© 2026 ID INC. All rights reserved

claude-skills/スキル
SKILLOfficialdevelopment

migrating-claude-agent-sdk-to-pydantic-ai

プラグイン
pydantic-ai
ソース
GitHub で見る ↗
説明

Python アプリケーションを Claude Agent SDK から Pydantic AI に、また必要な場合のみ Pydantic AI Harness に移行します。 **次のような場合に使用:** - ソースコードが `claude_agent_sdk` をインポートしている - Claude Code のエージェント・ループ(処理の流れ)、セッション(利用中の情報保持)、組み込みツール、フック(処理を挿入するポイント)、権限設定、スキル、またはサブエージェント(補助的なエージェント)に依存している **使用しないケース:** - Anthropic Messages SDK または Claude Managed Agents を対象とする場合

原文を表示

Migrate Python applications from the Claude Agent SDK to Pydantic AI and, only when needed, Pydantic AI Harness. Use when source code imports `claude_agent_sdk` or relies on Claude Code's agent loop, sessions, built-in tools, hooks, permissions, skills, or subagents. Do not use for the Anthropic Messages SDK or Claude Managed Agents.

ユースケース
  • Claude Agent SDKをインポートしている
  • エージェント・ループに依存している
  • セッション管理機能を使用している
  • 組み込みツールやフックを活用している
  • サブエージェント機能を必要としている
本文(日本語訳)

Claude Agent SDK を Pydantic AI に移行する

外部から見える動作を保つ。Claude Code プロセスや API の形は変える。最小限の完全な呼び出し経路を移行し、アプリケーションのインフラは変えずに置く。

移行元を調査してから移行先を決める

  1. リポジトリの説明文、依存パッケージ、テスト、実行の開始点を読む。インストール済みの Claude Agent SDK、付属の Claude Code CLI、Pydantic AI、Harness のバージョンを記録する。

  2. query() か ClaudeSDKClient から始まる実際のリクエストを1つ追跡する。プロンプト、ツール、イベント、結果、状態、永続化(保存)、承認、呼び出し元が使う副作用を通すとして追跡する。焦点を絞ったテスト基準や特性テストを作る。

  3. 存在する場合、次の契約(呼び出し元との約束)を分ける:

    • 新しい query() 呼び出し、生きた ClaudeSDKClient の継続、ディスク・外部ストレージ再開、トランスクリプト分岐、ファイル巻き戻し
    • ツール利用可能性、自動承認、ポリシーコールバック(ポリシー決定時に実行される処理)、OS・コンテナ隔離
    • 完成したメッセージ、生のトークン増分、ライフサイクルイベント、終了状態の結果
    • 会話履歴、ワークフロー検査点、モデル所有の計画、長期記憶、ワークスペース状態
    • モデル指示のサブエージェント対 アプリケーション所有のループ、キュー、再試行、スケジューラー
  4. 見つけた契約、その所有者、意味的な違い、実行可能な証拠を記録する。見つけていない Claude Code 機能は移行の対象外とする。

研究と概念マッピング で検出された移行元の機能を読む。検証と切り替え を実装前に読む。

最小限の移行先を選ぶ

  • コア: モデル呼び出し、エージェントループ、型付き依存、ツール、出力、正規化されたメッセージ、汎用フック、ストリーミング、MCP、承認、使用量制限、計測、耐久性のある実行時統合に pydantic_ai.Agent を使う。

  • Harness: コードシステムやシェルのツール、リポジトリのコンテキスト、計画、記憶、モデル指示のサブエージェント、モデル非依存のコンテキスト管理、Agent Skills など見つけた再利用可能なポリシーについてのみ pydantic-ai-harness を加える。Harness の機能はコアエージェントループを通じて組み合わさる。Harness は第二の実行時ではない。

  • アプリケーション: 認証、サービスクライアント、データベース、キュー、スケジューラー、デプロイメント、製品の状態、セッション検索、テナントポリシー、既存の通信経路は保つ。要求されたスライスにそれらが含まれていない限り。

  • 隙間: 公開の接続点が保たない動作に名前をつける。その影響を説明し、制限されたアダプタをテストする。違いを隠すためだけに ClaudeSDKClient もどきを作ったり Claude トランスクリプトファイルをまねたりしない。

典型的な移行は、再利用可能な Agent 1つ、型付き依存で供給されるアプリケーションサービス、普通の Pydantic AI ツール、呼び出し元が構造を期待するときの型付き出力、後の ターン(会話のやり取り)がコンテキストを必要とするときの result.all_messages() のアプリケーション所有ストレージである。呼び出し元が移行する間、小さいアダプタで既存の公開要求・応答・イベント境界を保つ。

リスクの高い部分をゲートする

  • Claude Agent SDK の allowed_tools は自動承認を意味する。排他的な利用可能性ではなく、隔離でもない。利用可能性、承認、ポリシー、サンドボックス(実行環境の隔離)を独立して対応させる。

  • Pydantic AI の message_history は正規化されたメッセージを明示的に再生する。conversation_id は実行をまとめる。Claude セッション再開トークンではない。ID の名前変更ではなく、再開と分岐の動作を証明する。

  • Pydantic AI 承認は検証されたツール呼び出しを先延ばしにする。アプリケーションはなお認証済み認可、アイデンティティ、監査を所有し、保護された実行は承認前に走ってはいけない。

  • フック(処理の接続点)を発火点、入力、順序、変更、ブロッキング(遮断)、エラー動作で対応させる。Claude のフックマッチャーは並行して走るかもしれない。Pydantic AI は before_* フックを機能順に、after_* フックを逆順に走らせ、wrap_* フックを最初の機能が最も外側になるようにネストさせる。フック見える契約については黄金トレース(正しい実行の記録)が必要である。

  • ストリーミング消費者の意図を対応させる。Pydantic AI の出力ストリーミング、実行イベントストリーミング、グラフ反復は異なる表面である。いずれも Claude SDK メッセージ分類を約束しない。

  • Harness の SubAgents は モデル指示の委譲にのみ使う。決定論的なオーケストレーション(複数システムの統合制御)と検証ループはアプリケーションコードに保つ。

  • Harness Skills はオンデマンド(必要に応じて)で SKILL.md 指示を供給する。Claude Code スキル や プラグイン読み込みの対等性を想定する代わりに、リソースとスクリプトを別に検証する。

  • Harness コマンド制御はベストエフォート(最善を尽くす)ポリシーであり、OS セキュリティ境界ではない。信頼できない実行のため、 ModalSandbox が適す場合は使うか、アプリケーション所有のコンテナ、VM、サンドボックス隔離を保つ。

  • ファイル巻き戻し、Claude Code プロンプトプリセット、プラグインパッケージング、生のストリーム‐イベント ID、動的 MCP 制御は、移行の呼び出し元が実際に必要とし、公開の移行先接続点が証明された場合を除き、隙間である。

1つの縦の薄い層を実装して証明する

  1. 支援される呼び出し元の境界を保つ。エージェント所有の内部だけを置き換える。

  2. コアだけから始める。移行元の契約がそれを必要とした後にのみ、最も狭い Harness 機能を加える。

  3. その境界で入力、型付き出力、エラー、イベント順序、ツール引数・結果、承認決定、ターン間の状態、副作用をテストする。決定的なモデルとニセのアプリケーションサービスをオフラインテストに使う。プロバイダー動作が契約である場合にのみ、狭い記録済み・生きたテストを加える。

  4. 再開、耐久性、承認、並行ツール、外部実行の場合、中断・再起動、来歴(どこから来たか)、認可、べき等性(繰り返しても同じ結果)を移行元が約束する正確な境界でテストする。

  5. 保持されたパスが claude-agent-sdk をインポートしたり、Claude Code を起動したり、トランスクリプトを読んだり、イベント・結果型を消費したりしない後にのみ、claude-agent-sdk を削除する。

実装前に意味的な変化を説明する:移行元の動作を述べ、移行先の動作を述べ、呼び出し元への影響、推奨される選択肢、残るリスクを述べる。

完了

検証と切り替え の完了基準を当てはめる。ニセデータ、記録、生きたプロバイダーからの証拠に正確にラベルを付ける。

原文(English)を表示

Migrate Claude Agent SDK to Pydantic AI

Preserve observable behavior, not the Claude Code process or API shape. Migrate the smallest complete caller path and keep application infrastructure in place.

Trace the source before choosing a target

  1. Read repository instructions, dependencies, tests, and the runtime entrypoint. Record the installed Claude Agent SDK, bundled Claude Code CLI, Pydantic AI, and Harness versions.
  2. Trace one real request from query() or ClaudeSDKClient through prompts, tools, events, results, state, persistence, approvals, and side effects that callers use. Establish a focused baseline or characterization test.
  3. Separate these source contracts when present:
    • fresh query() calls, live ClaudeSDKClient continuation, disk/external-store resume, transcript forks, and file rewind;
    • tool availability, automatic approval, policy callbacks, and OS/container isolation;
    • completed messages, raw token deltas, lifecycle events, and terminal results;
    • conversation history, workflow checkpoints, model-owned plans, long-term memory, and workspace state;
    • model-directed subagents from application-owned loops, queues, retries, and schedulers.
  4. Record each observed contract, its owner, semantic difference, and executable proof. An unobserved Claude Code feature is not migration scope.

Read Research and concept mapping for the detected source features. Read Verification and cutover before implementation.

Choose the smallest target

  • Core: use pydantic_ai.Agent for model calls, the agent loop, typed dependencies, tools, outputs, normalized messages, generic hooks, streaming, MCP, approvals, usage limits, instrumentation, and durable-runtime integrations.
  • Harness: add pydantic-ai-harness only for observed reusable policy such as coding filesystem/shell tools, repository context, planning, memory, model-directed subagents, model-agnostic context management, or Agent Skills. Harness capabilities compose through the core agent loop; Harness is not a second runtime.
  • Application: retain authentication, service clients, databases, queues, schedulers, deployment, product state, session lookup, tenant policy, and existing transports unless the requested slice includes them.
  • Gap: name behavior that no supported public seam preserves, explain its impact, and test a bounded adapter. Do not create a lookalike ClaudeSDKClient or emulate Claude transcript files merely to hide a difference.

The normal migration is one reusable Agent, application services supplied through typed dependencies, ordinary Pydantic AI tools, a typed output when the caller expects structure, and application-owned storage of result.all_messages() when later turns need context. Preserve the existing public request/response/event boundary with a small adapter while callers migrate.

Apply high-risk gates

  • allowed_tools in the Claude Agent SDK means auto-approval, not exclusive availability and not isolation. Map availability, approval, policy, and sandboxing independently.
  • Pydantic AI message_history explicitly replays normalized messages. conversation_id correlates runs; it is not a Claude session-resume token. Prove restart and fork behavior rather than renaming IDs.
  • Pydantic AI approval defers a validated tool call. The application still owns authenticated authorization, identity, and audit, and the protected effect must not run before approval.
  • Map hooks by firing point, inputs, ordering, mutation, blocking, and error behavior. Claude hook matchers may run concurrently. Pydantic AI runs before_* hooks in capability order, after_* hooks in reverse order, and nests wrap_* hooks with the first capability outermost. Require a golden trace for any hook-visible contract.
  • Map consumer intent for streaming. Pydantic AI output streaming, run-event streaming, and graph iteration are different surfaces; none promises the Claude SDK message taxonomy.
  • Use Harness SubAgents only for model-directed delegation. Keep deterministic orchestration and verification loops in application code.
  • Harness Skills supplies on-demand SKILL.md instructions; verify resources and scripts separately instead of assuming Claude Code skill or plugin loading parity.
  • Harness command controls are best-effort policy, not an OS security boundary. For untrusted execution, use ModalSandbox where it fits or retain application-owned container, VM, or sandbox isolation.
  • File rewind, Claude Code prompt presets, plugin packaging, raw stream-event identity, and dynamic MCP control are gaps unless the migrated caller actually needs them and a public target seam is proven.

Implement and prove one vertical slice

  1. Preserve the supported caller boundary and replace only the agent-owned internals.
  2. Start core-only. Add the narrowest Harness capabilities only after a source contract requires them.
  3. Test inputs, typed outputs, errors, event order, tool arguments/results, approval decisions, state across turns, and side effects at that boundary. Use deterministic models and fake application services for offline tests; add a focused recorded/live test only when provider behavior is the contract.
  4. For resume, durability, approvals, concurrent tools, or external effects, test interruption/restart, lineage, authorization, and idempotency at the exact boundary promised by the source.
  5. Remove claude-agent-sdk only after no retained path imports it, spawns Claude Code, reads its transcripts, or consumes its event/result types.

Explain any consequential semantic change before implementing it: state the source behavior, target behavior, caller impact, recommended choice, and remaining risk.

Completion

Apply the completion criterion in Verification and cutover. Label evidence from fakes, recordings, and live providers accurately.

原文・著作権は Anthropic および各プラグイン作者に帰属します。日本語訳は Claude API による自動翻訳です。