Installation
Install the package into a virtual environment. On recent Debian, Ubuntu, and Homebrew Python installs, runningpip install against system Python fails with error: externally-managed-environment.
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 anAsyncIterator[Message] that yields messages from the conversation.
Example - With options
tool()
Decorator for defining MCP tools with type safety.
Parameters
Input schema options
-
Simple type mapping (recommended):
-
JSON Schema format (for complex validation):
Returns
A decorator function that wraps the tool implementation and returns anSdkMcpTool 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.