Sessions persist the conversation, not the filesystem. To snapshot and revert file changes the agent made, use file checkpointing.
resume and fork manually, and what to know about resuming sessions across hosts.
Choose an approach
How much session handling you need depends on your application’s shape. Session management comes into play when you send multiple prompts that should share context. Within a singlequery() call, the agent already takes as many turns as it needs, and permission prompts and AskUserQuestion are handled in-loop (they don’t end the call).
Continue, resume, and fork
Continue, resume, and fork are option fields you set onquery() (ClaudeAgentOptions in Python, Options in TypeScript).
Continue and resume both pick up an existing session and add to it. The difference is how they find that session:
- Continue finds the most recent session in the current directory. You don’t track anything. Works well when your app runs one conversation at a time.
- Resume takes a specific session ID. You track the ID. Required when you have multiple sessions (for example, one per user in a multi-user app) or want to return to one that isn’t the most recent.
Automatic session management
Both SDKs offer an interface that tracks session state for you across calls, so you don’t pass IDs around manually. Use these for multi-turn conversations within a single process.Python: ClaudeSDKClient
ClaudeSDKClient handles session IDs internally. Each call to client.query() automatically continues the same session. Call client.receive_response() to iterate over the messages for the current query. Use the client as an async context manager so connection setup and teardown are handled for you, or call connect() and disconnect() manually.
This example runs two queries against the same client. The first asks the agent to analyze a module; the second asks it to refactor that module. Because both calls go through the same client instance, the second query has full context from the first without any explicit resume or session ID:
Python