Skip to main content

Installation

Install the package into a virtual environment. On recent Debian, Ubuntu, and Homebrew Python installs, running pip install against system Python fails with error: externally-managed-environment.
For uv, Windows PowerShell, and API key setup, see Setup in the Agent SDK quickstart.

Choosing between query() and ClaudeSDKClient

The Python SDK provides two ways to interact with Claude Code: Use ClaudeSDKClient for interactive applications such as chat interfaces, or when the next action depends on Claude’s response.

Functions

Signature blocks and bare async for / async with fragments on this page are illustrative. To run them, wrap the body in async def main(): ... and call asyncio.run(main()).

query()

Creates a new session for each interaction with Claude Code by default. Returns an async iterator that yields messages as they arrive. Each call to query() starts fresh with no memory of previous interactions unless you pass continue_conversation=True or resume in ClaudeAgentOptions. See Sessions.

Parameters

Returns

Returns an AsyncIterator[Message] that yields messages from the conversation.

Example - With options

tool()

Decorator for defining MCP tools with type safety.

Parameters

Input schema options

  1. Simple type mapping (recommended):
  2. JSON Schema format (for complex validation):

Returns

A decorator function that wraps the tool implementation and returns an SdkMcpTool instance.

Example

ToolAnnotations

Re-exported from mcp.types (also available as from claude_agent_sdk import ToolAnnotations). All fields are optional hints; clients should not rely on them for security decisions.

create_sdk_mcp_server()

Create an in-process MCP server that runs within your Python application.