← Claude Docs

Work with sessions - Claude Code Docs

Claude Docs · April 21, 2026
Sessions store conversation history including prompts, tool calls, results, and responses, and the SDK automatically saves them to disk for later retrieval. Different session-handling approaches serve different needs: continue resumes the most recent session, resume targets a specific session by ID, and fork creates a divergent copy of an existing session without changing the original. The ClaudeSDKClient in Python and continue: true in TypeScript offer automatic session management for multi-turn conversations within a single process.

Detailed Analysis

Anthropic's Claude Code SDK introduces a structured session management system that gives developers granular control over how conversational context is stored, retrieved, and branched across agent interactions. A session, as defined in the documentation, is the full accumulated conversation history an agent builds during its operation — encompassing the original prompt, every tool call and its result, and every model response. The SDK writes this history to disk automatically, enabling persistence across process restarts and allowing agents to return to prior states with complete awareness of files already read, analyses already performed, and decisions already made. This architecture reflects a deliberate design philosophy: treating agent memory not as an ephemeral runtime artifact but as a durable, reusable resource.

The documentation delineates four distinct session interaction modes — single-call, continue, resume, and fork — each calibrated to a different application shape. For simple, one-shot tasks, no session handling is required at all, as a single `query()` call is self-contained. For multi-turn conversations within a running process, the Python SDK provides `ClaudeSDKClient`, a context-manager-based client that tracks session IDs internally across successive `client.query()` calls, while the TypeScript SDK achieves equivalent behavior through a `continue: true` flag that automatically locates and resumes the most recent on-disk session. Where developers need to return to a *specific* past session rather than the most recent one — the paradigmatic case being a multi-user application maintaining one session per user — they must capture and store session IDs manually and pass them to the `resume` option. The `fork` mechanism stands apart: rather than continuing a session linearly, it branches a new session from an existing history, leaving the original intact and enabling exploratory or experimental agent runs without sacrificing prior context.

The session system carries significant practical implications for how agentic AI applications are architected. By persisting full tool-call chains and results to disk, the SDK enables a class of long-running, interruptible workflows that would otherwise require custom state management infrastructure from developers. The distinction between `continue` (most-recent session) and `resume` (specific session by ID) maps cleanly onto single-user versus multi-user deployment scenarios, reflecting an awareness that production applications require per-entity session isolation. The `fork` capability is particularly noteworthy: it introduces a version-control-like model of agent reasoning, where different strategies or approaches can be explored in parallel branches without destructive side effects on established context — a pattern more commonly associated with source control than conversational AI.

Taken together, these features position Claude Code's session system as a foundational layer for building production-grade agentic software, rather than a convenience wrapper around a simple chat loop. The ability to persist, retrieve, branch, and optionally suppress session storage (via TypeScript's `persistSession: false` for fully stateless, in-memory execution) gives developers a spectrum of control that mirrors the needs of real application architectures. This approach is consistent with broader trends in the AI agent ecosystem, where the field is moving away from stateless prompt-response patterns toward agents that maintain rich, durable working memory across extended task horizons — a shift that demands exactly the kind of structured session management infrastructure that Anthropic is codifying here.

Read original article →