Skip to main content
The Claude Agent SDK provides two main ways to interact with Claude: the query() function for simple one-shot queries and ClaudeSDKClient for interactive, stateful conversations.

Quick Comparison

query() Function

The query() 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()

Perfect for simple questions where you know the entire input upfront.
Ideal for processing multiple independent prompts.
Great for automated scripts where all inputs are known.
Useful for one-shot code generation tasks.

ClaudeSDKClient

The ClaudeSDKClient 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

Building conversational UIs or chat applications.
When you need to react to Claude’s responses and ask follow-up questions.
When you need to maintain context across multiple exchanges.
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
Use ClaudeSDKClient when:
  • Building chat interfaces
  • Need interactive debugging
  • Require follow-up questions
  • Need to interrupt operations
  • Want to change settings mid-conversation
  • Building REPL-like tools