Manage sessions, continue conversations, and track file changes with checkpointing
The Claude Agent SDK provides powerful session management features including conversation continuity, session listing, message history, and file checkpointing.
from claude_agent_sdk import ClaudeAgentOptions, query# First interactionoptions = ClaudeAgentOptions( cwd="/path/to/project")async for message in query( prompt="Create a hello.py file", options=options): print(message)# Continue the conversationoptions.continue_conversation = Trueasync for message in query( prompt="Now add error handling to hello.py", options=options): print(message)
from claude_agent_sdk import ClaudeAgentOptions, query# Resume by session IDoptions = ClaudeAgentOptions( resume="550e8400-e29b-41d4-a716-446655440000", # Session UUID cwd="/path/to/project")async for message in query( prompt="Continue working on the previous task", options=options): print(message)
Create a new session starting from a previous session’s state:
from claude_agent_sdk import ClaudeAgentOptions, queryoptions = ClaudeAgentOptions( resume="550e8400-e29b-41d4-a716-446655440000", fork_session=True # Create new session ID)async for message in query( prompt="Try a different approach to the problem", options=options): print(message)
Session forking creates a new session with a new ID while preserving the conversation history from the resumed session. This is useful for exploring different approaches without modifying the original session.
List sessions for a project or across all projects:
from claude_agent_sdk import list_sessions# List sessions for a specific projectsessions = list_sessions(directory="/path/to/project")for session in sessions: print(f"Session ID: {session.session_id}") print(f"Summary: {session.summary}") print(f"Last Modified: {session.last_modified}") print(f"Branch: {session.git_branch}") print(f"First Prompt: {session.first_prompt}") print()
from claude_agent_sdk import get_session_messages# Get all messages from a sessionmessages = get_session_messages( session_id="550e8400-e29b-41d4-a716-446655440000", directory="/path/to/project")for msg in messages: print(f"Type: {msg.type}") # "user" or "assistant" print(f"UUID: {msg.uuid}") # Message ID print(f"Session: {msg.session_id}") # Session ID print(f"Message: {msg.message}") # Message content print()
from claude_agent_sdk import ClaudeAgentOptions, ClaudeSDKClientoptions = ClaudeAgentOptions( enable_file_checkpointing=True, cwd="/path/to/project")async with ClaudeSDKClient(options=options) as client: await client.query("Create and modify several files") async for msg in client.receive_response(): print(msg)
Restore files to their state at a specific user message:
from claude_agent_sdk import ( ClaudeAgentOptions, ClaudeSDKClient, get_session_messages)options = ClaudeAgentOptions( enable_file_checkpointing=True, resume="550e8400-e29b-41d4-a716-446655440000", cwd="/path/to/project")async with ClaudeSDKClient(options=options) as client: # Get messages to find a checkpoint messages = get_session_messages( session_id="550e8400-e29b-41d4-a716-446655440000", directory="/path/to/project" ) # Find a user message UUID to rewind to user_messages = [m for m in messages if m.type == "user"] checkpoint_uuid = user_messages[2].uuid # Rewind to 3rd user message # Rewind files to that point await client.rewind_files(checkpoint_uuid) # Continue from that point await client.query("Try a different approach") async for msg in client.receive_response(): print(msg)
File checkpointing only tracks files that are modified through Claude’s tools (Write, Edit, MultiEdit). Manual changes outside of the session are not tracked.
import asynciofrom claude_agent_sdk import ( ClaudeAgentOptions, ClaudeSDKClient, get_session_messages, AssistantMessage, TextBlock)async def explore_alternatives(): """Try different implementations and rewind if needed.""" options = ClaudeAgentOptions( enable_file_checkpointing=True, cwd="/path/to/project" ) async with ClaudeSDKClient(options=options) as client: # Initial implementation await client.query("Implement a sorting algorithm in sort.py") async for msg in client.receive_response(): if isinstance(msg, AssistantMessage): for block in msg.content: if isinstance(block, TextBlock): print(f"Claude: {block.text}") # Get checkpoint before optimization messages = get_session_messages( session_id=client.session_id, directory="/path/to/project" ) checkpoint = [m for m in messages if m.type == "user"][-1].uuid # Try optimization await client.query("Optimize for performance") async for msg in client.receive_response(): pass # Not satisfied? Rewind and try different approach await client.rewind_files(checkpoint) await client.query("Optimize for readability instead") async for msg in client.receive_response(): passif __name__ == "__main__": asyncio.run(explore_alternatives())