Module 11: Session Resume
Continuing conversations across application restarts.
What You’ll Learn
- Extracting session IDs from
ResultMessage
- Resuming previous sessions using
query(prompt, sessionId)
- When to use session resume vs starting fresh
The Session ID
Every conversation has a unique session ID returned in the ResultMessage:
Resuming a Session
Use query(prompt, sessionId) to continue a previous conversation:
How Session Resume Works
Storing Session IDs
For persistence across restarts, store the session ID:
When to Use Session Resume
Limitations
- Session expiration: Sessions stored by Claude CLI have a retention period. Very old session IDs may become invalid.
- Storage dependency: Your application must persist session IDs. If lost, the conversation cannot be resumed.
- No context pruning: Resumed sessions include the full history. You cannot trim old messages to reduce token usage.
- Model changes: Resuming a session started with one model using a different model may produce inconsistent behavior.
- Security consideration: Anyone with the session ID can resume the conversation. Treat session IDs as sensitive data.
Do not resume sessions when the previous context contains outdated or incorrect information that should be discarded.
Source Code
View on GitHub
Running the Example
Next Module
Module 12: Session Fork - Creating parallel conversation branches.