The Claude Agent SDK provides two main ways to interact with Claude: theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/anthropics/claude-agent-sdk-python/llms.txt
Use this file to discover all available pages before exploring further.
query() function for simple one-shot queries and ClaudeSDKClient for interactive, stateful conversations.
Quick Comparison
| Feature | query() | ClaudeSDKClient |
|---|---|---|
| Communication | Unidirectional | Bidirectional |
| State | Stateless | Stateful |
| Follow-ups | Not supported | Supported |
| Interrupts | Not supported | Supported |
| Connection Management | Automatic | Manual (context manager) |
| Use Case | Simple automation | Interactive conversations |
query() Function
Thequery() function is ideal for simple, stateless queries where you don’t need bidirectional communication or conversation management.
Signature
Key Characteristics
Unidirectional
Send all messages upfront, receive all responses. No follow-up messages can be sent.
Stateless
Each query is independent with no conversation state maintained between calls.
Simple
Fire-and-forget style with automatic connection management.
No Interrupts
Cannot interrupt or send messages during execution.
When to Use query()
One-off Questions
One-off Questions
Perfect for simple questions where you know the entire input upfront.
Batch Processing
Batch Processing
Ideal for processing multiple independent prompts.
CI/CD Pipelines
CI/CD Pipelines
Great for automated scripts where all inputs are known.
Code Generation
Code Generation
Useful for one-shot code generation tasks.
ClaudeSDKClient
TheClaudeSDKClient provides full control over the conversation flow with support for streaming, interrupts, and dynamic message sending.
Signature
Key Characteristics
Bidirectional
Send and receive messages at any time during the conversation.
Stateful
Maintains conversation context across multiple message exchanges.
Interactive
Send follow-ups based on Claude’s responses in real-time.
Control Flow
Support for interrupts, permission changes, and session management.
When to Use ClaudeSDKClient
Chat Interfaces
Chat Interfaces
Building conversational UIs or chat applications.
Interactive Debugging
Interactive Debugging
When you need to react to Claude’s responses and ask follow-up questions.
Multi-turn Conversations
Multi-turn Conversations
When you need to maintain context across multiple exchanges.
Dynamic Control
Dynamic Control
When you need to change settings or interrupt during execution.
Complete Examples
Using query() for Code Generation
Using ClaudeSDKClient for Interactive Chat
Important Limitation: As of v0.0.20,
ClaudeSDKClient instances cannot be used across different async runtime contexts (e.g., different trio nurseries or asyncio task groups). The client must complete all operations within the same async context where it was connected.Decision Guide
Use query() when:- You know all inputs upfront
- No follow-up messages needed
- Automating batch operations
- Building CI/CD integrations
- Simple one-off tasks
- Building chat interfaces
- Need interactive debugging
- Require follow-up questions
- Need to interrupt operations
- Want to change settings mid-conversation
- Building REPL-like tools