[Feature Request] True session isolation with session_id in ClaudeSDKClient.query()

Resolved 💬 2 comments Opened Feb 10, 2026 by cdxiaodong Closed Mar 11, 2026

Problem

The query() method accepts a session_id parameter, but switching session IDs does NOT create isolated conversation contexts. All sessions share the same context within a single client connection.

Test Evidence

async with ClaudeSDKClient(options=options) as client:
    # Session A: set secret
    await client.query("Remember secret number: 12345", session_id="session_A")
    await collect_response(client)
    
    # Session B: ask for secret (EXPECTED: doesn't know)
    await client.query("What's the secret number I told you?", session_id="session_B")
    response = await collect_response(client)
    # ACTUAL RESULT: Session B responds "12345" ❌ Context leaked across sessions

A test script is available to reproduce this behavior.

Expected Behavior

Different session_id values should maintain independent conversation histories:

async with ClaudeSDKClient(options=options) as client:
    await client.query("Remember A=1", session_id="ctx_1")
    await client.query("Remember B=2", session_id="ctx_2")
    
    # ctx_1 only knows A=1 ✅
    # ctx_2 only knows B=2 ✅

Proposed Solution

Add an isolated_sessions flag (defaulting to False for backward compatibility):

options = ClaudeAgentOptions(
    isolated_sessions=True,  # Enable true session isolation
)

Or make it the default behavior since isolated sessions is what most users would expect from different session_id values.

Use Cases

  1. Multi-tenant applications serving multiple users through one client
  2. Parallel independent tasks that must not share context
  3. Testing scenarios requiring clean state between runs

Environment

  • SDK Version: 0.1.33
  • CLI Version: 2.1.37

View original on GitHub ↗

This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗