claude-agent-sdk-python icon indicating copy to clipboard operation
claude-agent-sdk-python copied to clipboard

[QUESTION] How to access historical messages from resumed sessions via SDK?

Open Lewik opened this issue 5 months ago • 3 comments

[QUESTION] How to access historical messages from resumed sessions via SDK?

Environment

  • Platform: Claude Code Python SDK
  • Claude CLI version: 1.0.67 (Claude Code)
  • Python SDK version: 0.0.17
  • Operating System: macOS 15.0

Question

Is there a way to access historical messages from a resumed session through the Python SDK API?

Context

I'm building an application that needs to display conversation history when resuming a Claude Code session. Currently, I can successfully resume sessions and Claude remembers the context, but I need programmatic access to the previous messages.

Current Behavior

from claude_code_sdk import ClaudeSDKClient, ClaudeCodeOptions

# Resume existing session
options = ClaudeCodeOptions(resume="session-id-here")
async with ClaudeSDKClient(options=options) as client:
    # This works - Claude remembers context from previous messages
    await client.query("What did we discuss earlier?")
    
    # But how to get the actual historical messages?
    # client.get_history() - doesn't exist
    # client.get_session_messages() - doesn't exist

What I Need

One of these approaches would solve my use case:

  1. SDK Method: An API method to retrieve historical messages

    history = await client.get_session_history(session_id)
    # Returns list of UserMessage, AssistantMessage objects
    
  2. Session File Parser: Official utility to parse .jsonl session files

    from claude_code_sdk.utils import parse_session_file
    messages = parse_session_file("~/.claude/projects/project/session-id.jsonl")
    
  3. Resume with History: Option to receive historical messages during resume

    options = ClaudeCodeOptions(resume="session-id", include_history=True)
    async with ClaudeSDKClient(options=options) as client:
        # Receive historical messages first, then new messages
        async for msg in client.receive_messages():
            # Gets both historical and new messages
    

Current Workaround

I'm manually parsing the .jsonl files from ~/.claude/projects/, but this has challenges:

  • Need to handle duplicate entries (related to issue with stream-json duplication)
  • Need to understand the internal file format
  • Need to deal with session file location logic
  • No typing/validation

Use Cases

  • Chat UI applications that need to show conversation history
  • Session analysis and debugging tools
  • Conversation export/backup functionality
  • Multi-client access to same session

Related

  • Session resume works perfectly for context (Claude remembers previous conversation)
  • This is specifically about accessing the message objects programmatically
  • Similar to how other chat APIs provide conversation history endpoints

Is this functionality planned for the SDK? Any recommended approaches for accessing session history?

Lewik avatar Aug 03 '25 10:08 Lewik

A workaround that exists today is to use src/claude_code_sdk/_internal/message_parser.py to parse the jsonl objects.

dicksontsai avatar Aug 05 '25 05:08 dicksontsai

What's the recommended practice here by Claude team? Should I maintain a copy of the session on my side and assume it's in sync with whatever history Claude has on its side using the "session_id"?

sidPhoenix17 avatar Oct 08 '25 13:10 sidPhoenix17

@dicksontsai can you update the docs to show that resume and continue_conversation have to be in the same root directory, as I found that if you use them on different directories it fails.

cwd=str(temp_dir_2),  # this will fail, because it is not temp_dir_1

jackstine avatar Oct 17 '25 02:10 jackstine