Skip to main content
The ClaudeSDKClient provides a streaming interface for building interactive applications with multi-turn conversations and real-time message handling.
The queries in these examples are intentionally simple. In reality, Claude SDK can handle complex tasks using its agentic capabilities and tools (e.g., run bash commands, edit files, search the web, fetch content).

Basic Streaming

The simplest way to use streaming mode:

Multi-Turn Conversations

Handle follow-up questions that reference previous context:

Interrupting Execution

You can interrupt Claude’s execution mid-task:
Interrupts require active message consumption. You must have a task consuming messages from receive_response() or receive_messages() for the interrupt to be processed.

Custom Options

Configure the client with custom options:

Manual Message Handling

Process messages with custom logic:

Tool Use Blocks

Observe when Claude uses tools:

Server Info and Control

Access server information and capabilities:

Error Handling

Properly handle errors and timeouts:

Key Takeaways

  • Use ClaudeSDKClient for interactive, multi-turn conversations
  • Use context managers (async with) for automatic cleanup
  • Use receive_response() for simple message consumption
  • Use receive_messages() for manual control and custom logic
  • Interrupts require active message consumption
  • Always handle errors and use proper cleanup

Next Steps

MCP Calculator

Build an in-process MCP server

Hooks

Customize behavior with hooks