Session Management
The Claude Agent SDK provides session management capabilities for handling conversation state and resumption. Sessions allow you to continue conversations across multiple interactions while maintaining full context.How Sessions Work
When you start a new query, the SDK automatically creates a session and returns a session ID in the initial system message. You can capture this ID to resume the session later.Getting the Session ID
Resuming Sessions
The SDK supports resuming sessions from previous conversation states, enabling continuous development workflows. Use theresume
option with a session ID to continue a previous conversation.
Forking Sessions
When resuming a session, you can choose to either continue the original session or fork it into a new branch. By default, resuming continues the original session. Use theforkSession
option (TypeScript) or fork_session
option (Python) to create a new session ID that starts from the resumed state.
When to Fork a Session
Forking is useful when you want to:- Explore different approaches from the same starting point
- Create multiple conversation branches without modifying the original
- Test changes without affecting the original session history
- Maintain separate conversation paths for different experiments
Forking vs Continuing
Behavior | forkSession: false (default) | forkSession: true |
---|---|---|
Session ID | Same as original | New session ID generated |
History | Appends to original session | Creates new branch from resume point |
Original Session | Modified | Preserved unchanged |
Use Case | Continue linear conversation | Branch to explore alternatives |