Skip to main content
The V2 session API is no longer supported. TypeScript Agent SDK 0.3.142 removes unstable_v2_createSession, unstable_v2_resumeSession, unstable_v2_prompt, and the SDKSession and SDKSessionOptions types.To migrate, use the query() API and the session options it accepts. Pass an AsyncIterable<SDKUserMessage> for multi-turn conversations, or options.resume to continue a saved session. This page is kept for reference if you maintain code on Agent SDK 0.2.x or earlier.
V2 was an experimental session API that removed the need for async generators and yield coordination. Instead of managing generator state across turns, each turn was a separate send()/stream() cycle. The API surface reduced to three concepts:
  • createSession() / resumeSession(): Start or continue a conversation
  • session.send(): Send a message
  • session.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, use unstable_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 message
  • stream() streams back the response
This explicit separation makes it easier to add logic between turns (like processing responses before sending follow-ups). The example below creates a session, sends “Hello!” to Claude, and prints the text response. It uses 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, call send() 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 using await 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+):
Manual cleanup:

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 (forkSession option)
  • Some advanced streaming input patterns

See also