Session
The Session class manages multi-turn conversations with Claude. It matches the official Claude Agent SDK V2 interface.Creating Sessions
Sessions are created via the client. Connection happens automatically on firstsend().
Methods
send()
Send a message to Claude. Returns a Promise that resolves when the message is sent. Usestream() to receive the response.
Supports both text-only messages and multimodal content (text + images + documents).
Text Message
With Image
With Document (PDF)
Multiple Attachments
stream()
Stream the response after sending a message. Returns an async generator that yields SDK messages.SDKMessage Types
| Type | Description |
|---|---|
system | System initialization message with session info |
assistant | Claude’s response with content blocks |
result | Final result with cost and usage info |
stream_event | Partial streaming events |
Example
receive()
Alias forstream() for V2 compatibility.
close()
Close the session and release resources.Example
on()
Register event handlers.SessionEventHandlers
| Handler | Parameters | Description |
|---|---|---|
onSessionInfo | (info: SessionInfo) | Session initialized |
onError | (error: Error) | Error occurred |