send()/stream() cycle. The API surface reduces to three concepts:
createSession()/resumeSession(): Start or continue a conversationsession.send(): Send a messagesession.stream(): Get the response
Installation
The V2 interface is included in the existing SDK package:Quick start
One-shot prompt
For simple single-turn queries where you don’t need to maintain a session, useunstable_v2_prompt(). This example sends a math question and logs the answer:
Basic session
For interactions beyond a single prompt, create a session. V2 separates sending and streaming into distinct steps:send()dispatches your messagestream()streams back the response
await using (TypeScript 5.2+) to automatically close the session when the block exits. You can also call session.close() manually.
Multi-turn conversation
Sessions persist context across multiple exchanges. To continue a conversation, callsend() again on the same session. Claude remembers the previous turns.
This example asks a math question, then asks a follow-up that references the previous answer:
Session resume
If you have a session ID from a previous interaction, you can resume it later. This is useful for long-running workflows or when you need to persist conversations across application restarts. This example creates a session, stores its ID, closes it, then resumes the conversation:Cleanup
Sessions can be closed manually or automatically usingawait using, a TypeScript 5.2+ feature for automatic resource cleanup. If you’re using an older TypeScript version or encounter compatibility issues, use manual cleanup instead.
Automatic cleanup (TypeScript 5.2+):
API reference
unstable_v2_createSession()
Creates a new session for multi-turn conversations.
unstable_v2_resumeSession()
Resumes an existing session by ID.
unstable_v2_prompt()
One-shot convenience function for single-turn queries.
SDKSession interface
Feature availability
Not all V1 features are available in V2 yet. The following require using the V1 SDK:- Session forking (
forkSessionoption) - Some advanced streaming input patterns
Feedback
Share your feedback on the V2 interface before it becomes stable. Report issues and suggestions through GitHub Issues.See also
- TypeScript SDK reference (V1) - Full V1 SDK documentation
- SDK overview - General SDK concepts
- V2 examples on GitHub - Working code examples