[Feature Request] Add --include-history flag to output historical messages when resuming sessions
Problem
When resuming a session with claude --resume <session-id>, the CLI creates a new JSONL file and starts streaming only new messages. There's no built-in way to get historical messages from the previous session through the CLI's stdout stream.
This forces developers integrating Claude Code to implement complex workarounds:
- Manually parse JSONL files from
~/.claude/projects/ - Load historical messages before starting the CLI process
- Merge historical and streaming data with different parsing logic
- Handle deduplication and synchronization issues
Current Workaround
Currently, integrations must:
- Read and parse
.jsonlfiles directly from the filesystem - Implement custom JSONL parsers to extract historical messages
- Load history separately before starting the CLI process
- Maintain two different data pipelines (file-based for history, stream-based for new messages)
- Handle potential format inconsistencies between parsed files and streamed output
This approach has several issues:
- Duplicate parsing logic for files vs stream
- Risk of format inconsistencies
- Complex state management
- No guarantee that file format won't change
- Requires filesystem access permissions
Proposed Solution
Add an --include-history flag that streams historical messages before entering interactive mode:
claude --resume <session-id> --output-format json --include-history
Expected behavior:
- First, output all historical messages from the resumed session in chronological order
- Clearly mark the boundary between historical and new messages using one of these approaches:
- Add an
"isHistorical": truefield to historical message entries - Output a special delimiter entry like
{"type": "history_delimiter"}after all historical messages - Include metadata in each entry indicating its source (e.g.,
"source": "history"vs"source": "live")
- Then seamlessly transition to streaming new messages in real-time
- Use the same JSON format structure for both historical and new messages
Benefits
- Unified data source: All messages come from stdout, no file parsing needed
- Consistent format: Same parsing logic for all messages
- Clear boundaries: Developers can easily distinguish historical from new messages
- Simplified integration: Remove need for filesystem access and custom parsers
- Future-proof: CLI handles any format changes internally
- Better performance: No need to read potentially large JSONL files
Related Issue
This request is related to anthropics/claude-code-sdk-python#109, where we initially requested similar functionality for the Python SDK. However, this feature would be better implemented at the CLI level, making it available to all SDKs and integrations.
Use Cases
- Chat UI applications that need to display conversation history
- Session analysis tools
- Debugging tools that need full conversation context
---
🤖 Generated with Claude Code
This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗