# Useful but Little-Known Features of Claude Agent SDK
🌍 Let AI modify your files with confidence -- you can always roll back.
File checkpointing automatically tracks changes made by Write/Edit/NotebookEdit and lets you rewind to any point in time.
📌 Title: File Checkpointing and Rollback
🔗 URL:
🧩 Overview
File checkpointing tracks file modifications during agent sessions. When you set enable_file_checkpointing=True, the SDK creates backups before any Write, Edit, or NotebookEdit tool modifies a file. Each UserMessage's UUID serves as a checkpoint, and you can call rewind_files() with that UUID to restore files to that point. Created files get deleted, and modified files are restored to their original content.
🛠 How to Use
```python
import asyncio
from claude_agent_sdk import (
ClaudeSDKClient,
ClaudeAgentOptions,
UserMessage,
ResultMessage,
)
async def main():
# Step 1: Enable checkpointing
options = ClaudeAgentOptions(
enable_file_checkpointing=True,
permission_mode="acceptEdits",
extra_args={"replay-user-messages": None}, # Required for UUIDs
)
checkpoint_id = None
session_id = None
async with ClaudeSDKClient(options) as client:
await client.query("Refactor the authentication module")
# Step 2: Capture checkpoint UUID from UserMessage
async for message in client.receive_response():
if isinstance(message, UserMessage) and message.uuid and not checkpoint_id:
checkpoint_id = message.uuid
if isinstance(message, ResultMessage) and not session_id:
session_id = message.session_id
# Step 3: Resume session and rollback
if checkpoint_id and session_id:
async with ClaudeSDKClient(
ClaudeAgentOptions(
enable_file_checkpointing=True, resume=session_id
)
) as client:
await client.query("") # Empty prompt to open connection
async for message in client.receive_response():
await client.rewind_files(checkpoint_id)
break
print(f"Rewound to checkpoint: {checkpoint_id}")
```
🏗 Integration into Production Systems
- extra_args={"replay-user-messages": None} is required; without it, UUIDs won't appear in the stream
- To rollback, resume the session, send an empty prompt, then call rewind_files()
- Persist checkpoint UUIDs and session IDs to enable rollback after process restarts
- Combine with permission_mode="acceptEdits" to auto-approve file changes while maintaining rollback safety
💡 Use Cases
🔧 Safe refactoring: try changes and instantly rollback if issues arise
🧪 Experimental code generation: validate AI output and revert if quality is low
📝 Auto-generated documentation: review results and undo unwanted changes
⚠️ Caveats
- Changes via Bash commands (echo > file.txt, sed -i, etc.) are NOT tracked
- Only file content is tracked; directory creation/move/deletion is not undone
- Conversation history is not rolled back -- only files are restored
- Checkpoints are tied to the session that created them
✨ Develop fearlessly with "undo-able" AI -- let the agent code with confidence!
#
ClaudeAgentSDK# #
AIAgent#