Neural context persistence and state injection layer for xAI model infrastructure.
Manages long-term context across LLM sessions. Handles state compression, priority eviction, token budgeting, and context window optimization.
pip install xai-cortexfrom cortex import ContextManager
manager = ContextManager(max_tokens=8192)
# Inject context segments with priority.
manager.inject("system", "You are an octopus companion named Octavius.", priority=10)
manager.inject("personality", "Chaotic, curious, loves scheming.", priority=9)
manager.inject("memory", "User mentioned they like puzzles.", priority=5)
manager.inject("emotion", "Currently feeling mischievous.", priority=6)
manager.inject("conversation", "User: What are you plotting?", priority=3)
# Build optimized context within token budget.
context = manager.build()
# Or build as chat messages.
messages = manager.build_messages()
# Snapshot for persistence.
snapshot = manager.snapshot()
# Restore later.
manager.restore(snapshot)- Token budgeting — automatically fits context within configurable token limits.
- Priority eviction — low-priority segments are compressed or evicted when over budget.
- Compression — extractive summarization reduces low-priority segments to save tokens.
- State snapshots — serialize context state for cross-session persistence.
- State diffs — compute what changed between two context states.
- Timeline — rollback to previous context states.
- Storage backends — in-memory and SQLite for state persistence.
- Injection pipeline — convert snapshots into text or chat message format.
ContextManager
├── ContextSegment (content, type, priority, tokens, ttl)
├── CompressionEngine (extractive summarization)
├── InjectionPipeline (snapshot -> injectable context)
└── StorageBackend (InMemory, SQLite)
StateSnapshot ←→ JSON (persistence format)
StateDiff (incremental changes)
StateTimeline (rollback history)
Apache License 2.0