- V2 session API (
unstable_v2_*) — persistent, named sessions for multi-turn conversations where the sameSDKSessionobject drives many prompts over time. - Session management utilities (
listSessions,getSessionInfo,getSessionMessages,renameSession,tagSession,forkSession) — read and mutate the on-disk session records that Claude Code persists at~/.claude/projects/.
V2 session API
unstable_v2_createSession()
Creates a new, persistent session and returns an SDKSession handle. Use this when you need to send multiple prompts to the same agent over time.
SDKSessionOptions
Model to use for this session, e.g.
'claude-opus-4-5' or 'claude-sonnet-4-6'.Working directory for the session. Defaults to
process.cwd().Custom system prompt to replace the default.
Additional instructions appended to the default system prompt.
Permission handling mode for tool calls in this session.
Maximum agent turns per prompt.
MCP servers to attach to the session.
Example
unstable_v2_resumeSession()
Resumes an existing session by ID, loading its prior conversation history before accepting new prompts.
UUID of the session to resume. Obtain this from
listSessions() or a previously created session.Session options. The
model field is required.Example
unstable_v2_prompt()
A one-shot convenience function that creates a session, sends a single prompt, and returns the result — without keeping the session object alive.
Example
Session management utilities
These functions read and write the on-disk JSONL transcripts that Claude Code stores in~/.claude/projects/. They work independently of any running session.
listSessions()
Lists sessions with metadata.
dir is provided, returns sessions for that project directory and its git worktrees. When omitted, returns sessions across all projects.
ListSessionsOptions
Project directory. When provided, returns only sessions for that project (and its git worktrees). When omitted, returns all sessions across every project.
Maximum number of sessions to return.
Number of sessions to skip from the sorted result. Use with
limit for pagination. Defaults to 0.When
dir is inside a git repository, include sessions from all git worktree paths. Defaults to true.SDKSessionInfo fields
Unique session identifier (UUID).
Display title: custom title if set, otherwise the auto-generated summary or first prompt.
Last modified time in milliseconds since epoch.
Creation time in milliseconds since epoch, extracted from the first entry’s timestamp field.
Working directory the session ran in.
Git branch at the end of the session.
User-set title (from
/rename or renameSession()).First meaningful user prompt in the session.
User-set tag (from
tagSession()).Size of the JSONL transcript in bytes.
Example
getSessionInfo()
Reads metadata for a single session by ID. More efficient than listSessions() when you already know the session ID, because it only reads that one file.
undefined if the session file is not found, is a sidechain session, or has no extractable summary.
GetSessionInfoOptions
Project directory to search. Omit to search across all project directories.
Example
getSessionMessages()
Reads the full conversation history from a session’s JSONL transcript. Parses the file, builds the message chain from parentUuid links, and returns messages in chronological order.
GetSessionMessagesOptions
Project directory. Omit to search all project directories.
Maximum number of messages to return.
Number of messages to skip from the beginning of the ordered list. Use with
limit for pagination.When
true, includes system messages in the returned list. Defaults to false (only user and assistant messages).Example
renameSession()
Sets a custom title for a session. Appends a custom-title entry to the session’s JSONL file.
SessionMutationOptions
Project directory. Omit to search all projects.
Example
tagSession()
Sets or clears a tag on a session. Pass null to remove the existing tag.
Example
forkSession()
Copies a session transcript into a new session file with fresh UUIDs, preserving the parentUuid chain. Useful for creating alternate branches from a known conversation state.
{ sessionId } — the UUID of the newly created session.
Forked sessions start without undo history. File-system snapshots are not copied; only the conversation transcript is forked.
ForkSessionOptions
Project directory of the source session.
Fork from a specific point in the conversation. Messages after this UUID are not included in the new session. Omit to copy the full transcript.
Custom title for the forked session.
Example
Working with session IDs
Session IDs are UUIDs. You can obtain them from:listSessions()— returnsSDKSessionInfo[], each with asessionIdgetSessionInfo()— looks up a single session- The
session_idfield on anySDKMessageyielded byquery() - The
SDKSystemMessageinit message, which carries the activesession_id