# anthropics/claude-agent-sdk-python architecture

> Architecture diagram of the GitHub repository anthropics/claude-agent-sdk-python, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/anthropics/claude-agent-sdk-python
- Repository: https://github.com/anthropics/claude-agent-sdk-python

## Overview

1. **What this repo is**
   - This is a Python SDK package, `claude_agent_sdk`, that wraps the Claude Code CLI and exposes it as an async API.
   - The package is distributed as a wheel/sdist and, importantly, bundles the Claude Code CLI inside the wheel by default, so users do not need a separate CLI install.

2. **Public API boundary**
   - The main entry points are `query()` for one-shot async conversations and `ClaudeSDKClient` for long-lived, bidirectional sessions.
   - The public surface is concentrated in `src/claude_agent_sdk/__init__.py`, `query.py`, `client.py`, `types.py`, and `_errors.py`.

3. **Core runtime flow**
   - User code calls `query()` or opens a `ClaudeSDKClient`.
   - Those APIs delegate into `_internal/client.py` and `_internal/transport/subprocess_cli.py`, which launch and talk to the Claude Code CLI as a subprocess.
   - Messages are streamed back asynchronously, parsed from CLI output, and surfaced as typed Python message objects.

4. **Message and protocol handling**
   - `_internal/message_parser.py` and related internal modules translate CLI-emitted JSON/event streams into SDK message types such as assistant, user, system, and result messages.
   - This parsing layer is a key boundary: the SDK is not reimplementing the model; it is adapting CLI protocol output into Python-native structures.

5. **Configuration and option layering**
   - `ClaudeAgentOptions` drives behavior such as working directory, system prompt, max turns, tool permissions, MCP server wiring, and CLI path selection.
   - `_internal/query.py`, `_internal/session_*`, and `_internal/session_store_validation.py` handle option normalization, session settings, and validation before execution.

6. **Tools and permissions subsystem**
   - Claude’s default toolset is mediated by SDK options like `allowed_tools`, `disallowed_tools`, `permission_mode`, and tool callbacks.
   - This repo also supports Python-defined custom tools via in-process MCP servers, created with `create_sdk_mcp_server()` and `@tool`.
   - There is a clear boundary between:
     - **external MCP servers** launched separately, and
     - **SDK MCP servers** that run in the same Python process.

7. **Hooks and agent interception**
   - Hooks are Python callbacks invoked by the SDK at agent lifecycle points such as pre-tool-use.
   - `examples/hooks.py` and the `hooks` option in `ClaudeAgentOptions` show that the application can inject deterministic policy, filtering, or automation before Claude executes an action.

8. **Session lifecycle and persistence**
   - The `_internal/session_*` modules support importing, resuming, mutating, summarizing, and storing sessions.
   - `src/claude_agent_sdk/_internal/session_store.py` defines the abstraction for persistent session storage, while example stores demonstrate concrete backends for Postgres, Redis, and S3.
   - This makes session state an explicit subsystem rather than an implicit CLI detail.

9. **Streaming, transcripts, and callbacks**
   - The SDK is designed around async streaming rather than blocking request/response.
   - `_internal/transcript_mirror_batcher.py` and callback examples indicate that message streams can be mirrored, inspected, or consumed incrementally while a conversation is in progress.

10. **Examples, tests, and conformance**
   - `examples/` documents the major usage modes: quick start, streaming, hooks, MCP tools, session stores, structured output, and permission control.
   - `tests/` covers unit/integration behavior, while `e2e-tests/` exercises full SDK-to-CLI flows, hooks, dynamic control, tool permissions, and MCP integration.
   - `src/claude_agent_sdk/testing/session_store_conformance.py` suggests a reusable conformance layer for validating session store implementations.

11. **Packaging, bundling, and release pipeline**
   - `scripts/build_wheel.py` downloads and bundles the Claude Code CLI into the package artifacts.
   - GitHub Actions workflows handle linting, tests, wheel validation, PyPI quota checks, release automation, and publishing.
   - The release process updates both the Python package version and the bundled CLI version, reflecting that the shipped artifact contains two independently versioned components.

12. **Architectural diagram takeaway**
   - The central diagram should show: Python application → SDK public API → internal orchestration/parsing/session layers → subprocess Claude Code CLI, with side paths for in-process MCP tools, hook callbacks, and persistent session stores.
   - Surrounding that core are build/release automation, examples, and test suites that validate the contract between the SDK and the CLI.

Main entry points: [`src/claude_agent_sdk/_internal/client.py`](https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/_internal/client.py) (Client core), [`src/claude_agent_sdk/__init__.py`](https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/__init__.py) (API surface), [`examples`](https://github.com/anthropics/claude-agent-sdk-python/tree/main/examples) (Examples), [`src/claude_agent_sdk/_internal/transport/subprocess_cli.py`](https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/_internal/transport/subprocess_cli.py) (Subprocess CLI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["Public SDK"]
  node_n1["API surface<br/>package exports<br/>[__init__.py]"]
  node_n2["Query API<br/>async helper<br/>[query.py]"]
  node_n3["Client API<br/>session client<br/>[client.py]"]
  node_n4["Types<br/>protocol models<br/>[types.py]"]
  node_n5["Errors<br/>exceptions<br/>[_errors.py]"]
end

subgraph group_runtime["Internal runtime"]
  node_n6["Client core<br/>orchestrator<br/>[client.py]"]
  node_n7["Query setup<br/>options normalization<br/>[query.py]"]
  node_n8["Subprocess CLI<br/>transport<br/>[subprocess_cli.py]"]
  node_n9["Message parser<br/>protocol adapter<br/>[message_parser.py]"]
  node_n10["Session flows<br/>session pipeline<br/>[sessions.py]"]
  node_n13["Transcript mirror<br/>stream mirror"]
end

subgraph group_tools["Tooling"]
  node_n11[("Session store<br/>persistence interface<br/>[session_store.py]")]
  node_n12["Store checks<br/>validation"]
  node_n14["MCP tools<br/>in-process server"]
  node_n15{{"Hooks<br/>callbacks<br/>[hooks.py]"}}
  node_n21["Conformance<br/>test harness"]
end

subgraph group_delivery["Delivery"]
  node_n16[("CLI bundle<br/>packaged binary")]
  node_n17["Wheel build<br/>packaging script<br/>[build_wheel.py]"]
  node_n18["Examples<br/>usage gallery"]
  node_n19["Tests<br/>test suites"]
  node_n20["E2E tests<br/>integration suite"]
end

node_n1 -->|"exports"| node_n2
node_n1 -->|"exports"| node_n3
node_n1 -->|"types"| node_n4
node_n1 -->|"errors"| node_n5
node_n2 -->|"configures"| node_n7
node_n3 -->|"drives"| node_n6
node_n7 -->|"creates"| node_n6
node_n6 -->|"launches"| node_n8
node_n8 -->|"streams"| node_n9
node_n9 -->|"maps to"| node_n4
node_n6 -->|"manages"| node_n10
node_n10 -->|"persists"| node_n11
node_n11 -->|"validated by"| node_n12
node_n6 -->|"mirrors"| node_n13
node_n6 -->|"invokes"| node_n14
node_n6 -->|"calls"| node_n15
node_n14 -->|"emits"| node_n4
node_n17 -->|"bundles"| node_n16
node_n16 -->|"used by"| node_n8
node_n18 -.->|"demonstrates"| node_n2
node_n18 -.->|"demonstrates"| node_n3
node_n18 -.->|"demonstrates"| node_n14
node_n18 -.->|"demonstrates"| node_n15
node_n19 -.->|"covers"| node_n6
node_n20 -.->|"exercises"| node_n8
node_n20 -.->|"exercises"| node_n14
node_n20 -.->|"exercises"| node_n15
node_n21 -->|"supports"| node_n11

click node_n1 "https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/__init__.py"
click node_n2 "https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/query.py"
click node_n3 "https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/client.py"
click node_n4 "https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/types.py"
click node_n5 "https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/_errors.py"
click node_n6 "https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/_internal/client.py"
click node_n7 "https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/_internal/query.py"
click node_n8 "https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/_internal/transport/subprocess_cli.py"
click node_n9 "https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/_internal/message_parser.py"
click node_n10 "https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/_internal/sessions.py"
click node_n11 "https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/_internal/session_store.py"
click node_n12 "https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/_internal/session_store_validation.py"
click node_n13 "https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/_internal/transcript_mirror_batcher.py"
click node_n15 "https://github.com/anthropics/claude-agent-sdk-python/blob/main/examples/hooks.py"
click node_n16 "https://github.com/anthropics/claude-agent-sdk-python/tree/main/src/claude_agent_sdk/_bundled"
click node_n17 "https://github.com/anthropics/claude-agent-sdk-python/blob/main/scripts/build_wheel.py"
click node_n18 "https://github.com/anthropics/claude-agent-sdk-python/tree/main/examples"
click node_n19 "https://github.com/anthropics/claude-agent-sdk-python/tree/main/tests"
click node_n20 "https://github.com/anthropics/claude-agent-sdk-python/tree/main/e2e-tests"
click node_n21 "https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/testing/session_store_conformance.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_n1,node_n2,node_n3,node_n4,node_n5 toneBlue
class node_n6,node_n7,node_n8,node_n9,node_n10,node_n13 toneAmber
class node_n11,node_n12,node_n14,node_n15,node_n21 toneMint
class node_n16,node_n17,node_n18,node_n19,node_n20 toneRose
```

## Components

### Public SDK

User-facing package surface and core runtime orchestration.

- [API surface](https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/__init__.py): package exports · Public entrypoint re-exporting the main SDK types and functions. · `src/claude_agent_sdk/__init__.py`
- [Query API](https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/query.py): async helper · One-shot async conversation API. · `src/claude_agent_sdk/query.py`
- [Client API](https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/client.py): session client · Long-lived bidirectional client for streaming conversations. · `src/claude_agent_sdk/client.py`
- [Types](https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/types.py): protocol models · Public message, option, and result type definitions. · `src/claude_agent_sdk/types.py`
- [Errors](https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/_errors.py): exceptions · SDK error hierarchy and error mapping. · `src/claude_agent_sdk/_errors.py`

### Internal runtime

Client lifecycle, transport, parsing, sessions, and transcript handling.

- [Client core](https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/_internal/client.py): orchestrator · Internal execution coordinator for query and client sessions. · `src/claude_agent_sdk/_internal/client.py`
- [Query setup](https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/_internal/query.py): options normalization · Normalizes agent options and prepares a runnable session. · `src/claude_agent_sdk/_internal/query.py`
- [Subprocess CLI](https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/_internal/transport/subprocess_cli.py): transport · Launches and communicates with the bundled Claude Code CLI. · `src/claude_agent_sdk/_internal/transport/subprocess_cli.py`
- [Message parser](https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/_internal/message_parser.py): protocol adapter · Parses CLI event streams into SDK message objects. · `src/claude_agent_sdk/_internal/message_parser.py`
- [Session flows](https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/_internal/sessions.py): session pipeline · Resume, import, mutate, and summarize session state. · `src/claude_agent_sdk/_internal/sessions.py`
- [Transcript mirror](https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/_internal/transcript_mirror_batcher.py): stream mirror · Batches and mirrors streamed transcript data. · `src/claude_agent_sdk/_internal/transcript_mirror_batcher.py`

### Tooling

MCP tools, hooks, and session store extensions.

- [Session store](https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/_internal/session_store.py): persistence interface · Abstraction for persistent session backends. · `src/claude_agent_sdk/_internal/session_store.py`
- [Store checks](https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/_internal/session_store_validation.py): validation · Validates session store implementations and settings. · `src/claude_agent_sdk/_internal/session_store_validation.py`
- **MCP tools**: in-process server · Python-defined tools hosted inside the SDK process.
- [Hooks](https://github.com/anthropics/claude-agent-sdk-python/blob/main/examples/hooks.py): callbacks · Lifecycle interception points such as pre-tool-use. · `examples/hooks.py`
- [Conformance](https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/testing/session_store_conformance.py): test harness · Reusable conformance checks for session store implementations. · `src/claude_agent_sdk/testing/session_store_conformance.py`

### Delivery

Packaging, examples, and validation surfaces.

- [CLI bundle](https://github.com/anthropics/claude-agent-sdk-python/tree/main/src/claude_agent_sdk/_bundled): packaged binary · Bundled Claude Code CLI artifact shipped inside the wheel. · `src/claude_agent_sdk/_bundled`
- [Wheel build](https://github.com/anthropics/claude-agent-sdk-python/blob/main/scripts/build_wheel.py): packaging script · Downloads and packages the bundled CLI into release artifacts. · `scripts/build_wheel.py`
- [Examples](https://github.com/anthropics/claude-agent-sdk-python/tree/main/examples): usage gallery · Representative usage modes including streaming, tools, hooks, and stores. · `examples`
- [Tests](https://github.com/anthropics/claude-agent-sdk-python/tree/main/tests): test suites · Unit, integration, and end-to-end coverage of SDK behavior. · `tests`
- [E2E tests](https://github.com/anthropics/claude-agent-sdk-python/tree/main/e2e-tests): integration suite · Full SDK-to-CLI flows including hooks, permissions, and MCP integration. · `e2e-tests`

## Connections

- API surface → Query API: exports · Public API exposes the one-shot query helper.
- API surface → Client API: exports · Public API exposes the long-lived client.
- API surface → Types: types · Public API publishes shared SDK types.
- API surface → Errors: errors · Public API publishes the exception surface.
- Query API → Query setup: configures · Query entrypoint normalizes agent options and session settings.
- Client API → Client core: drives · Client API delegates runtime behavior to the internal orchestrator.
- Query setup → Client core: creates · Prepared query configuration enters the client core.
- Client core → Subprocess CLI: launches · Core runtime starts and talks to the subprocess CLI.
- Subprocess CLI → Message parser: streams · CLI output is parsed into structured events and messages.
- Message parser → Types: maps to · Parsed events are surfaced as public message types.
- Client core → Session flows: manages · Client core coordinates session lifecycle operations.
- Session flows → Session store: persists · Session flows can be backed by persistent stores.
- Session store → Store checks: validated by · Store implementations are checked against the session contract.
- Client core → Transcript mirror: mirrors · Streaming runtime can mirror transcript data incrementally.
- Client core → MCP tools: invokes · Runtime can attach in-process MCP tool servers.
- Client core → Hooks: calls · Agent lifecycle hooks intercept execution at policy points.
- MCP tools → Types: emits · Custom tools integrate through the same typed message surface.
- Wheel build → CLI bundle: bundles · Build script packages the CLI into the wheel artifact.
- CLI bundle → Subprocess CLI: used by · Transport executes the bundled CLI binary or entrypoint.
- Examples → Query API: demonstrates · Examples show the query-oriented workflow.
- Examples → Client API: demonstrates · Examples show the streaming client workflow.
- Examples → MCP tools: demonstrates · Examples show SDK-defined MCP tools.
- Examples → Hooks: demonstrates · Examples show hook-based interception.
- Tests → Client core: covers · Unit and integration tests exercise the runtime core.
- E2E tests → Subprocess CLI: exercises · End-to-end tests validate subprocess CLI behavior.
- E2E tests → MCP tools: exercises · End-to-end tests cover MCP tool integration.
- E2E tests → Hooks: exercises · End-to-end tests cover hook execution paths.
- Conformance → Session store: supports · Conformance harness validates session store backends.
