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

[Feature] Add a file based session persistent client

Open randombet opened this issue 4 months ago • 5 comments

Current ClaudeSDKClient is base on subprocess and once subprocess is dead or quit unexpectedly. We will lose the session info. SessionPersistentClient leverages ClaudeSDKClient and auto save and persist session data on local file.

randombet avatar Aug 01 '25 20:08 randombet

Thanks for this PR. I think we should just build this resilience into the default ClaudeSDKClient itself by leveraging the resume option. That way, we can avoid having to duplicate the conversation history storage (once here, once within Claude Code)

dicksontsai avatar Aug 04 '25 21:08 dicksontsai

Thanks for this PR. I think we should just build this resilience into the default ClaudeSDKClient itself by leveraging the resume option. That way, we can avoid having to duplicate the conversation history storage (once here, once within Claude Code)

By "once within Claude Code", you mean on the server side, right? In that case, we still need session info (e.g session id, cost etc) on client side. LMK what do you think? @dicksontsai

randombet avatar Aug 04 '25 23:08 randombet

For reference, the init messages received right at the start of the SDK agent running have a sessionID and that can be used to resume sessions. BUT - when you use the built-in resume functionality, you pass it the OLD sessionID and you get a NEW one. So you have to capture that and remember that the new SessionID is the new session if you need to re-resume again. I've tested this extensively over a large batch of tools, and it's been rock solid.

I have an example of capturing and updating the claude code session ID for being able to resume here: https://github.com/EdanStarfire/claudecode_webui/blob/main/src/claude_sdk.py#L706-L722

and this is me passing the session ID for resumption when creating the agent. https://github.com/EdanStarfire/claudecode_webui/blob/main/src/claude_sdk.py#L674-L676

EdanStarfire avatar Oct 14 '25 00:10 EdanStarfire

Where is the specific conversation information stored? It seems to be lost after each service restart within the Kubernetes cluster

bobqiu-hub avatar Nov 28 '25 08:11 bobqiu-hub

At ~/.claude

If you're running in k8s though, you'll need to make that folder be persistent storage. Otherwise when the container goes poof, so does that history.

EdanStarfire avatar Nov 28 '25 12:11 EdanStarfire

It would be great if we could migrate the memory-tool later on。https://platform.claude.com/docs/en/agents-and-tools/tool-use/memory-tool

bobqiu-hub avatar Dec 03 '25 04:12 bobqiu-hub