Stores
Stores determine where extracted beliefs are persisted. Configure via TrackerConfig(store_type="...") or inject directly.
SQLite Store (Default)
Asynchronous, persistent single-file database using aiosqlite. Supports cosine similarity search, belief pruning, and session statistics.
pythonfrom beliefstate import SQLiteStore
# Via config (automatic)
config = TrackerConfig(
store_type="sqlite",
store_kwargs={"db_path": "user_beliefs.db"}
)
# Or inject directly
store = SQLiteStore(db_path="user_beliefs.db")
tracker = BeliefTracker(config=config, store=store)
# In-memory for testing
store = SQLiteStore(db_path=":memory:")
ℹ️ SQLite Schema & WAL mode: The database creates a schema optimized for session isolation and turn order:
CREATE TABLE IF NOT EXISTS beliefs (
session_id TEXT,
subject TEXT,
predicate TEXT,
value TEXT,
confidence REAL,
turn INTEGER,
source TEXT,
embedding TEXT,
created_at TIMESTAMP,
last_referenced_at TIMESTAMP,
PRIMARY KEY (session_id, subject, predicate)
);
Composite primary keys and unique indexes prevent duplicate subject-predicate mappings. Write-Ahead Logging (WAL) is enabled on database startup, allowing concurrent readers (your user threads) to query beliefs without being blocked by background write tasks.
Redis Store
Distributed caching backend. Essential when running multiple application workers behind a load balancer.
pythonfrom beliefstate import RedisStore
store = RedisStore(redis_url="redis://localhost:6379/0")
await store.set_session_ttl("user_123", ttl_seconds=86400) # 24 hours
ℹ️ Redis Hash layout & Session Expiry: Data is organized cleanly to prevent key namespace bloat:
- Each session is represented by a single Redis HASH at key
beliefstate:session:{session_id}. - Each belief is stored inside the hash, using
subject::predicateas the hash field and the serialized belief object as the value. - Using HASH structures allows TTLs to be set on the parent key via
EXPIRE, enabling automatic expiration cleanup of the entire session state at once.
In-Memory Store
Simple in-memory storage with LRU eviction. Ideal for testing and single-process development.
pythonfrom beliefstate import InMemoryBeliefStore
store = InMemoryBeliefStore(max_bytes=100 * 1024 * 1024) # 100MB limit
# Get usage stats
stats = store.get_stats()
print(stats) # {"total_sessions": 5, "total_beliefs": 142, "utilization_percent": 12.3}
ℹ️ LRU Eviction & Footprint Estimation: The in-memory store uses a Python collections.OrderedDict:
- Reading or writing a belief updates its usage rank, moving the key to the end of the dictionary (MRU).
- The memory footprint of each Pydantic model is estimated in bytes by checking the length of its JSON string representation.
- When size limit is reached, it evicts least-recently-used beliefs from the oldest sessions until the memory footprint fits inside the
max_bytesbudget.
⚠️ Warning: In-memory store is NOT durable. All beliefs are lost on process restart. Use SQLite or Redis for production.