Production Ready ยท MIT Licensed

Preserve Semantic Continuity
Across LLM Session Boundaries

Context Handover extracts Semantic Atoms, measures Context Drift, and optimally packs context into new sessions using a Bounded Knapsack Algorithm.

The Problem & Solution

LLMs forget everything when a session ends. Standard memory is either too dumb or too expensive.

โŒ

The Problem

LLMs have no persistent memory across sessions. When a conversation ends, all context is lost. Existing solutions either use linear history (too dumb) or full vector re-indexing (too expensive).

โœ…

Our Solution

Context Handover extracts Semantic Atoms, measures Context Drift, and optimally packs context using a Bounded Knapsack Algorithm for maximum efficiency.

Core Features

Powerful tools for managing LLM context across sessions

๐Ÿ”

Semantic Extraction

Extract meaningful context units from conversations using LLM + Regex parsing

๐Ÿ“Š

Drift Detection

Measure topic shifts in real-time using KL Divergence and Cosine similarity

๐ŸŽฏ

Smart Packing

Optimize token usage mathematically with value-density knapsack algorithm

๐Ÿ”—

DAG Tracking

Track dependencies across sessions using directed acyclic graphs

๐Ÿ›ก๏ธ

Enterprise Ready

Circuit breakers, retries, dead letter queue, and idempotency guarantees

๐Ÿ“ˆ

Visual Analytics

Interactive observability dashboard for debugging and monitoring

Architecture Flow

How Context Handover processes and preserves semantic context

Processing Pipeline

flowchart TD
    subgraph Input["Input Layer"]
        A[User Chat
Raw Text] end subgraph Processing["Processing Layer"] B[Atom Extractor
LLM + Regex] C[Atom Registry
Dedup + Embed] D[Vector Store
Chroma/Qdrant] end subgraph Optimization["Optimization Layer"] E[Drift Detector
KL + Cosine] F[Token Budgeter
Knapsack Algo] end subgraph Output["Output Layer"] G[Loss Ledger
Audit Trail] H[New Session
Optimized Prompt] end A --> B B --> C C --> D D --> E D --> F E --> G F --> H style Input fill:#1a1a1a,stroke:#c41e3a,color:#fff style Processing fill:#1a1a1a,stroke:#8b1528,color:#fff style Optimization fill:#1a1a1a,stroke:#c41e3a,color:#fff style Output fill:#1a1a1a,stroke:#8b1528,color:#fff style A fill:#2a1a1a,stroke:#c41e3a,color:#fff style B fill:#2a1a1a,stroke:#c41e3a,color:#fff style C fill:#2a1a1a,stroke:#c41e3a,color:#fff style D fill:#2a1a1a,stroke:#c41e3a,color:#fff style E fill:#2a1a1a,stroke:#c41e3a,color:#fff style F fill:#2a1a1a,stroke:#c41e3a,color:#fff

Data Flow

graph LR
    A[User Chat] --> B[Atom Extractor]
    B --> C[Atom Registry]
    C --> D[Vector Store]
    D --> E[Drift Detector]
    D --> F[Token Budgeter]
    E --> G[Loss Ledger]
    F --> H[New Session]
    
    style A fill:#1a1a1a,stroke:#ff2d4f,color:#fff
    style B fill:#1a1a1a,stroke:#c41e3a,color:#fff
    style C fill:#1a1a1a,stroke:#8b1528,color:#fff
    style D fill:#1a1a1a,stroke:#c41e3a,color:#fff
    style E fill:#1a1a1a,stroke:#ff2d4f,color:#fff
    style F fill:#1a1a1a,stroke:#8b1528,color:#fff
    style G fill:#1a1a1a,stroke:#c41e3a,color:#fff
    style H fill:#1a1a1a,stroke:#ff2d4f,color:#fff
                    

Key Concepts

๐Ÿ”ท

Semantic Atom

The smallest unit of meaningful context. Like a single Lego brick that can be recombined.

๐ŸŒณ

Session DAG

Directed Acyclic Graph tracking atom relationships across sessions like a family tree.

๐Ÿงญ

Drift Metric

Measures topic change since last handover, like a compass checking your course.

๐ŸŽ’

Knapsack Budget

Mathematically selects the most valuable atoms for the token limit, like packing a suitcase.

How It Works

Three simple steps to preserve context across sessions

1

Initialize & Add Messages

Create a SessionManager and add your conversation messages. The system automatically tracks context as it's built.

from context_handover import SessionManager manager = SessionManager(session_id="session_001") manager.add_message(role="user", content="I want to build a rocket engine using methane.") manager.add_message(role="assistant", content="Understood. Methane (CH4) offers high specific impulse...")
2

Extract Semantic Atoms

Automatically extract meaningful context units from your conversation history. Atoms capture semantic meaning, not just raw text.

# Extract atoms automatically atoms = manager.extract_atoms() print(f"Extracted {len(atoms)} semantic atoms.")
3

Build Handover Package

Create an optimized context package for the new session. The Knapsack algorithm ensures maximum value within token limits.

# Handover to a new session (preserving context) new_session_pkg = manager.build_handover_package() manager.handover_to_new_session("session_002", new_session_pkg)

Why Context Handover?

Compare our approach with existing solutions

Approach Token Efficiency Semantic Coherence Latency Auditability
โŒ Naive Buffer
Low
Low
Fast โœ… None โŒ
โšก Vector Recall
Medium
Medium
Slow ~200ms โš ๏ธ Low โš ๏ธ
โœ… Context Handover
High ๐ŸŽฏ
High ๐ŸŽฏ
Fast ~40ms โšก Full โœ…

Works With Your Stack

Seamless integration with popular frameworks

Integration Architecture

flowchart LR
    A[Context Handover] --> B[LangChain]
    A --> C[LlamaIndex]
    A --> D[AutoGen]
    A --> E[LangGraph]
    A --> F[OpenTelemetry]
    A --> G[Langfuse]
    
    style A fill:#c41e3a,color:white
    style B fill:#1a1a1a,stroke:#2196F3,color:#fff
    style C fill:#1a1a1a,stroke:#FF9800,color:#fff
    style D fill:#1a1a1a,stroke:#9C27B0,color:#fff
    style E fill:#1a1a1a,stroke:#FF5722,color:#fff
    style F fill:#1a1a1a,stroke:#00BCD4,color:#fff
    style G fill:#1a1a1a,stroke:#E91E63,color:#fff
                    
โ›“๏ธ

LangChain

Custom Memory Module

๐Ÿ“„

LlamaIndex

Node Parser

๐Ÿค–

AutoGen

State Handover

๐Ÿ“Š

OpenTelemetry

Native Tracing

๐Ÿ“ˆ

Langfuse

Observability

Production Ready

Built for reliability at scale

๐Ÿ”„

Idempotency

Duplicate events auto-detected & ignored

๐Ÿ”

Smart Retries

Exponential backoff for LLM/Redis failures

โšก

Circuit Breakers

Prevents cascading failures

๐Ÿ“ฆ

Dead Letter Queue

Failed events saved for replay

๐Ÿ”’

PII Ready

Redaction & encryption hooks

๐Ÿ“Š

Full Auditability

Complete loss ledger for compliance

Semantic Atom Lifecycle

How atoms move through the system

sequenceDiagram
    participant User
    participant Manager
    participant Extractor
    participant Registry
    participant Vector
    
    User->>Manager: Add Message
    Manager->>Extractor: Trigger Extraction
    Extractor->>Extractor: Parse LLM + Regex
    Extractor->>Registry: Submit Atoms
    Registry->>Registry: Deduplicate
    Registry->>Vector: Generate Embeddings
    Vector-->>Manager: Confirmation
    Manager-->>User: Done
    
    style User fill:#1a1a1a,stroke:#c41e3a,color:#fff
    style Manager fill:#1a1a1a,stroke:#ff2d4f,color:#fff
    style Extractor fill:#1a1a1a,stroke:#8b1528,color:#fff
    style Registry fill:#1a1a1a,stroke:#c41e3a,color:#fff
    style Vector fill:#1a1a1a,stroke:#ff2d4f,color:#fff
                    

Quick Start

Get up and running in minutes

context_handover_example.py

Python
from context_handover import SessionManager, SemanticAtom

# Initialize manager
manager = SessionManager(session_id="session_001")

# Add meaningful interactions
manager.add_message(
    role="user", 
    content="I want to build a rocket engine using methane."
)
manager.add_message(
    role="assistant", 
    content="Understood. Methane (CH4) offers high specific impulse..."
)

# Extract atoms automatically
atoms = manager.extract_atoms() 
print(f"Extracted {len(atoms)} semantic atoms.")

# Handover to a new session
new_session_pkg = manager.build_handover_package()
manager.handover_to_new_session("session_002", new_session_pkg)

Ready to Preserve Context?

Start building intelligent memory for your LLM applications today.