Conversation
The TypeScript SDK's persistSession option has no Python counterpart, so turning off transcript writes meant reaching for extra_args. With persist_session=False the CLI runs with --no-session-persistence and saves nothing under ~/.claude/projects/. As in the TypeScript SDK, the flag is sent only for an explicit False, and session_store is rejected alongside it because the store mirrors the transcript from those local writes. The e2e tests follow the verbatim_prompts layout: a control run that list_sessions() finds, then query() and ClaudeSDKClient runs with persist_session=False that it doesn't.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
ClaudeAgentOptions.persist_session, the Python counterpart of the TypeScript SDK'spersistSession. Withpersist_session=Falsethe CLI runs with--no-session-persistenceand writes no transcript under~/.claude/projects/, so ephemeral or automated runs leave no session history behind. The default (True) adds nothing to the command line, so existing behaviour is unchanged.Until now the only way to get this from Python was
extra_args={"no-session-persistence": None}.It follows the TypeScript SDK (checked against
@anthropic-ai/claude-agent-sdk0.3.283):persist_session=False(TS:if (this.options.persistSession === false) args.push("--no-session-persistence")).session_storetogether withpersist_session=Falseis rejected up front, as TS does (sessionStore cannot be used with persistSession: false), because the store mirrors the transcript from those local writes. The check sits invalidate_session_store_options, soquery()andClaudeSDKClientboth fail before the CLI is spawned.Verification
Real model calls. CLI 2.1.283 through
ClaudeSDKClientwithmodel="haiku",setting_sources=[], a fresh tempcwd: two turns (the first reads a file with theReadtool), then aquery(resume=<session_id>).projects/-tmp-persist-real-m5d-fb86/09f8511b-5713-4ef1-92d2-76765b91de18.jsonlis written, and the resume answers from the earlier turnspersist_session=False: both turns succeed, no file anywhere under~/.claudenames the session, and the resume fails withNo conversation found with session ID: a4521e2a-80b8-4019-858a-3192b82a85b8Real CLI, no model calls. The bundled CLI version (2.1.283) driven through
query()with an isolatedCLAUDE_CONFIG_DIR,cwdin a temp dir, andANTHROPIC_BASE_URLpointed at a local server that answers 401. The transcript is written before the first API request, so this shows what persists:projects/-tmp-persist-probe-work-0in8dcqu/0d5dc4b2-ab19-4c6d-b5ba-b491f5e1ad3b.jsonlis writtenpersist_session=False: no.jsonlis writtenThe CLI's
--helpsays this flag "only works with --print". The SDK doesn't pass--print, and the run above shows the flag is honoured in SDK mode anyway.query()andClaudeSDKClientwithsession_store=InMemorySessionStore(), persist_session=Falseboth raise theValueErrorbefore spawning.New tests:
test_build_command_persist_session(tests/test_transport.py), andtest_rejects_persist_session_false_comboandtest_persist_session_false_without_store_is_valid(tests/test_session_store_conformance.py). With thesrc/changes stashed, all three fail (TypeError: ... unexpected keyword argument 'persist_session'); with them, all three pass.New e2e tests in
e2e-tests/test_persist_session.py, laid out like theverbatim_promptsones: a control run thatlist_sessions()finds, thenquery()andClaudeSDKClientruns withpersist_session=Falsethat it doesn't. 3 passed against the real API.ruff check,ruff format --check,mypy src/ scripts/are clean.pytest tests/: 1590 passed, 6 skipped on Python 3.12 and 3.13, and again withmcp==1.23.0(the floor job).