The Claude Code SDK provides session management capabilities for handling conversation state, persistence, and resumption. This guide covers how sessions are created, managed, persisted to files, and resumed within the SDK.
Session transcripts are stored as JSONL (JSON Lines) files, with each line representing a message or event:
Copy
{"type":"user","uuid":"abc123","timestamp":"2024-01-01T10:00:00Z","message":{"content":"Hello Claude"}}{"type":"assistant","uuid":"def456","parentUuid":"abc123","timestamp":"2024-01-01T10:00:01Z","message":{"content":[{"type":"text","text":"Hello! How can I help?"}]}}{"type":"checkpoint","sessionId":"session123","commit":"a1b2c3d","timestamp":"2024-01-01T10:00:02Z","label":"Initial state","id":"chk456"}
Each line in the JSONL file represents:
User messages: Input from the user
Assistant messages: Responses from Claude
Checkpoints: Saved states in the conversation (e.g., after completing a task)
Tool use: Records of when tools were invoked and their results
The session ID is provided in the initial system message when you start a conversation. You can capture it for later use:
Copy
import { query } from "@anthropic-ai/claude-code"let sessionId: string | undefinedconst response = query({ prompt: "Help me build a web application", options: { model: "claude-sonnet-4-20250514" }})for await (const message of response) { // The first message is a system init message with the session ID if (message.type === 'system' && message.subtype === 'init') { sessionId = message.session_id console.log(`Session started with ID: ${sessionId}`) // You can save this ID for later resumption } // Process other messages... console.log(message)}// Later, you can use the saved sessionId to resumeif (sessionId) { const resumedResponse = query({ prompt: "Continue where we left off", options: { resume: sessionId } })}
import { query } from "@anthropic-ai/claude-code"// Resume a previous session using its IDconst response = query({ prompt: "Continue implementing the authentication system from where we left off", options: { resume: "session-xyz", // Session ID from previous conversation model: "claude-sonnet-4-20250514", allowedTools: ["Read", "Edit", "Write", "Glob", "Grep", "Bash"] }})// The conversation continues with full context from the previous sessionfor await (const message of response) { console.log(message)}
import { query } from '@anthropic-ai/claude-code'import { readFile } from 'fs/promises'import { homedir } from 'os'import { join } from 'path'// Check if a session was interruptedconst checkSessionStatus = async (sessionId: string) => { const metadataPath = join(homedir(), '.config/claude/sessions/sessions.json') const metadata = JSON.parse(await readFile(metadataPath, 'utf-8')) const session = metadata.find(s => s.id === sessionId) if (session?.status === 'interrupted') { console.log('Session was interrupted. Ready for resumption...') // The SDK handles loading the transcript internally return { canResume: true, sessionId: sessionId } } return { canResume: false }}// Resume an interrupted sessionconst resumeInterrupted = async (sessionId: string) => { const status = await checkSessionStatus(sessionId) if (status.canResume) { const response = query({ prompt: "Let's continue from where we left off", options: { resume: status.sessionId } }) for await (const message of response) { console.log(message) } }}
The Claude Code SDK’s session management system provides a robust foundation for maintaining conversation state and enabling seamless resumption of development tasks, all through a simple file-based approach that requires no external infrastructure.