---
name: migrating-claude-agent-sdk-to-pydantic-ai
description: 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.
title: migrating-claude-agent-sdk-to-pydantic-ai
canonical_url: https://skilld.dev/gh/pydantic/pydantic-ai/migrating-claude-agent-sdk-to-pydantic-ai
last_updated: 2026-10-04T16:47:42.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [agents/openai.yaml](https://skilld.dev/api/skills-raw/pydantic/pydantic-ai/migrating-claude-agent-sdk-to-pydantic-ai/agents/openai.yaml), [references/RESEARCH-AND-MAPPING.md](https://skilld.dev/api/skills-raw/pydantic/pydantic-ai/migrating-claude-agent-sdk-to-pydantic-ai/references/RESEARCH-AND-MAPPING.md), [references/VERIFICATION-AND-CUTOVER.md](https://skilld.dev/api/skills-raw/pydantic/pydantic-ai/migrating-claude-agent-sdk-to-pydantic-ai/references/VERIFICATION-AND-CUTOVER.md).
>
> If the user asked to install this Skill, run `npx skilld install pydantic/pydantic-ai/migrating-claude-agent-sdk-to-pydantic-ai`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/migrating-claude-agent-sdk-to-pydantic-ai`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/pydantic/pydantic-ai/migrating-claude-agent-sdk-to-pydantic-ai) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/migrating-claude-agent-sdk-to-pydantic-ai`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/migrating-claude-agent-sdk-to-pydantic-ai --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/migrating-claude-agent-sdk-to-pydantic-ai --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# 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](https://skilld.dev/api/skills-raw/pydantic/pydantic-ai/migrating-claude-agent-sdk-to-pydantic-ai/references/RESEARCH-AND-MAPPING.md) for the detected source features. Read [Verification and cutover](https://skilld.dev/api/skills-raw/pydantic/pydantic-ai/migrating-claude-agent-sdk-to-pydantic-ai/references/VERIFICATION-AND-CUTOVER.md) 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](https://skilld.dev/api/skills-raw/pydantic/pydantic-ai/migrating-claude-agent-sdk-to-pydantic-ai/references/VERIFICATION-AND-CUTOVER.md). Label evidence from fakes, recordings, and live providers accurately.
