send()/stream() cycle. The API surface reduced to three concepts:
createSession()/resumeSession(): Start or continue a conversationsession.send(): Send a messagesession.stream(): Get the response
Installation
Agent SDK 0.2.x is the last version that includes the V2 interface. The package version jumped from 0.2.x directly to 0.3.142, so the removal version above and the install pin below describe the same boundary. To install the last V2-compatible release, pin the major and minor version:The SDK bundles a native Claude Code binary for your platform as an optional dependency, so most installs need no separate Claude Code install. See the quickstart’s install note for the installs that need one.
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.
The examples below show only the cleanup pattern and don’t send any messages, so running them produces no output.
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
The V2 session API does not support every V1 feature. The following require the V1 SDK:- Session forking (
forkSessionoption) - Some advanced streaming input patterns
See also
- TypeScript SDK reference (V1) - Full V1 SDK documentation
- SDK overview - General SDK concepts
- V2 examples on GitHub - Working code examples