Sessions
Sessions enable multi-turn conversations where Claude remembers previous messages. Use sessions for chat interfaces, complex tasks requiring multiple steps, or any scenario where context matters.Creating a Session
Session Options
| Option | Type | Description |
|---|---|---|
model | string | Claude model to use |
systemPrompt | string | System prompt for the session |
tools | Tool[] | Tools available to Claude |
mcpServers | McpServer[] | MCP servers to connect |
maxTurns | number | Maximum conversation turns |
maxBudgetUsd | number | Budget limit for this session |
sessionId | string | Resume an existing session |
Sending Messages
Basic Send
Streaming
Session States
Sessions progress through these states:| State | Description |
|---|---|
idle | Created, not connected |
initializing | Connecting to server |
ready | Ready to send messages |
processing | Waiting for response |
waiting_tool | Waiting for tool result |
completed | Session finished |
error | Error occurred |
Event Handlers
Listen to session events:Session Persistence
Sessions are automatically persisted. You can resume them later:Get Session ID
Resume Session
Closing Sessions
Always close sessions when done to free resources:Session Results
After each message, you get aSessionResult:
Prompt Mode vs Session Mode
| Feature | client.prompt() | session.send() |
|---|---|---|
| Stateless | Yes | No |
| Multi-turn | No | Yes |
| Resumable | No | Yes |
| Context | Single message | Full conversation |
| Use case | One-off questions | Chat, complex tasks |