A Python SDK for checkpoint-based conversation branching in LLM applications. Solve context pollution by creating isolated exploration branches without losing your place in the main conversation.
Problem: When exploring alternative approaches with LLMs, you pollute the conversation context, making it impossible to return to your original line of reasoning without starting fresh.
Solution: Context Branching SDK provides Git-like branching for LLM conversations:
- Checkpoint conversation state at decision points
- Branch to explore alternatives in isolation
- Switch between branches freely
- Inject insights from explorations back to main
- Export and share reproducible conversation states
pip install context-branching
# Or install from source
git clone https://github.com/glanzz/context-branching
cd context-branching
pip install -e .from context_branching import ContextBranchingSDK, Message
# Initialize SDK
sdk = ContextBranchingSDK(storage_backend="file", storage_path="./data")
workspace = sdk.create_workspace("my_session")
# Have a conversation
workspace.add_message(Message(role="user", content="Help me optimize this code"))
workspace.add_message(Message(role="assistant", content="Here are some options..."))
# Create checkpoint at decision point
checkpoint_id = workspace.create_checkpoint("optimization-decision")
# Branch to explore alternative
workspace.create_branch(checkpoint_id, "explore-rust")
workspace.switch_branch("explore-rust")
workspace.add_message(Message(role="user", content="What about Rust?"))
# Get context for LLM
context = workspace.get_current_context() # Returns List[Message]
# ... send to your LLM ...
# Return to main and inject insights
workspace.switch_branch("main")
workspace.inject_messages("explore-rust", [0]) # Cherry-pick specific messages- Checkpoint Management: Create immutable snapshots of conversation state
- Branch Creation: Explore multiple paths without pollution
- Context Isolation: Each branch maintains its own message history
- Selective Injection: Cherry-pick insights to bring back to main
- Export/Import: Share checkpoints with team members
- Storage Agnostic: File, memory, or custom backends
# File storage (default)
sdk = ContextBranchingSDK(storage_backend="file", storage_path="./my_data")
# In-memory storage (for testing)
sdk = ContextBranchingSDK(storage_backend="memory")
# Custom storage (implement StorageBackend interface)
from my_storage import PostgreSQLStorage
sdk = ContextBranchingSDK(custom_backend=PostgreSQLStorage())# Explore Rust rewrite vs Python optimization in parallel
checkpoint = workspace.create_checkpoint("refactoring-decision")
# Branch 1: Explore Rust
workspace.create_branch(checkpoint, "rust-option")
workspace.switch_branch("rust-option")
# ... explore Rust approach ...
# Branch 2: Explore Python optimization
workspace.switch_branch("main")
workspace.create_branch(checkpoint, "python-option")
workspace.switch_branch("python-option")
# ... explore Python approach ...
# Return to main with insights from both
workspace.switch_branch("main")
workspace.inject_messages("rust-option", [1, 3])
workspace.inject_messages("python-option", [1, 2])Explore multiple hypotheses in parallel without polluting the main debugging conversation.
python app/simple_chat.py --session my_session
# Commands available:
# /checkpoint <name> - Create checkpoint
# /branch <name> - Create and switch to branch
# /switch <name> - Switch to existing branch
# /inject <branch> <idx> - Inject messages from branch
# /status - Show workspace statuspython app/web_chat.py --port 5000
# Visit http://localhost:5000 for web interface# Install development dependencies
pip install -e ".[dev]"
# Run all tests
pytest tests/ -v
# Run with coverage
pytest tests/ --cov=sdk --cov-report=html
# Run specific test file
pytest tests/test_workspace.py -vMain SDK interface.
sdk = ContextBranchingSDK(
storage_backend="file", # "file", "memory", or "custom"
storage_path="./data", # Path for file storage
custom_backend=None # Custom StorageBackend implementation
)Manages branches, checkpoints, and conversation state.
Methods:
add_message(message: Message)- Add message to current branchcreate_checkpoint(name: str) -> str- Create checkpoint, returns IDcreate_branch(from_checkpoint: str, name: str) -> Branch- Create new branchswitch_branch(name: str)- Switch to different branchget_current_context() -> List[Message]- Get full context for LLMinject_messages(from_branch: str, indices: List[int])- Inject messageslist_branches() -> List[Dict]- List all brancheslist_checkpoints() -> List[Dict]- List all checkpointsexport_checkpoint(checkpoint_id: str, filepath: str)- Export checkpointimport_checkpoint(filepath: str) -> str- Import checkpointget_statistics() -> Dict- Get workspace statistics
Represents a single conversation message.
message = Message(
role="user", # "user", "assistant", or "system"
content="Hello", # Message content
timestamp=datetime.now(), # Optional, auto-generated
metadata={} # Optional metadata
)Implement StorageBackend interface for custom storage:
from context_branching import StorageBackend
class MyCustomStorage(StorageBackend):
def save_workspace(self, session_id: str, data: dict) -> None:
# Implement save logic
pass
def load_workspace(self, session_id: str) -> dict:
# Implement load logic
pass
def list_workspaces(self) -> List[str]:
# Implement list logic
pass
def delete_workspace(self, session_id: str) -> None:
# Implement delete logic
pass
# Use custom backend
sdk = ContextBranchingSDK(custom_backend=MyCustomStorage())The evaluation design and setup is present in results/quantitative_context_pollution. The folder contains the 30 conversation scenerios along with evaluated data results.
context-branching-sdk/
├── sdk/ # Core SDK library
│ ├── __init__.py
│ ├── sdk.py # Main SDK class
│ ├── workspace.py # Workspace & Message
│ ├── checkpoint.py # Checkpoint implementation
│ ├── branch.py # Branch implementation
│ └── storage.py # Storage backends
├── app/ # Example applications
│ ├── simple_chat.py # CLI chat
│ └── web_chat.py # Web chat
├── tests/ # Unit and integration tests
├── docs/ # Documentation
├── README.md
├── setup.py
└── requirements.txt
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests:
pytest tests/ - Submit a pull request
MIT License - see LICENSE file for details
For questions, issues, or contributions:
- GitHub Issues: https://github.com/glanzz/context-branching/issues
- Email: chickmagalurnanjun.b[at]northeastern[dot]edu
Version: 0.1.0 (Alpha) Status: Research Prototype Python: 3.9+
Star ⭐ this repository if you find it useful!