Pages
Sections
● Open source · Apache 2.0 · v1.2.0

Your LLM forgets what it said.
beliefstate doesn't.

beliefstate intercepts LLM conversations, extracts factual claims, and updates a local belief database in the background to catch contradictions before they reach your users.

$ pip install beliefstate
session: support_bot · user_7821
Turn 2 · user
We have a strict budget of $5,000 for the initial pilot.
beliefstate stored
("user", "budget", 5000) · confidence 0.98
Turn 2 · assistant
Understood. I will restrict all recommendations to stay below $5,000.
Turn 14 · assistant
Since you have a $10,000 budget, we recommend the premium package.
⚠ contradiction detected · score 0.94
✓ LLM self-corrects before user sees it

The problem

LLMs are stateless.
Conversations are not.

Without beliefstate

Silent contradictions ship to users

The model makes claims early in the session, then contradicts them later because it does not maintain active state.

Turn 3: user: My name is Raj.
Turn 6: assistant: Nice to meet you, Raj.
Turn 15: assistant: Happy to help, Alex!
// wrong name — never caught
Without beliefstate

Context window is not memory

Even with long context windows, crucial facts established early in a session get buried or ignored as the prompt grows.

Turn 2: user: Note that I have a severe allergy to peanuts.
Turn 30: user: What dessert options do you recommend?
Turn 30: assistant: Try our peanut butter brownie.
// allergy fact buried in context — never caught

How it works

Tracks every turn.
Catches every conflict.

01
🎯

Intercept

One decorator wraps your existing function. Works with any provider SDK.

02
💡

Inject

The system prompt gets relevant facts before the LLM call.

03
📤

Return instantly

The response returns to the user immediately. Zero latency added.

04
🔍

Extract

beliefstate extracts factual claims from both user and assistant messages as subject–predicate–value triples.

05

Detect

The checker evaluates contradictions using embedding similarity and NLI.

06
🛡

Resolve

The dispatcher applies your strategy: overwrite, keep_old, or raise.

Quickstart

Add one decorator. Done.

app.py
from beliefstate import BeliefTracker, TrackerConfig from beliefstate.adapters import OpenAIAdapter import openai # 1. Configure belief store config = TrackerConfig( store_type="sqlite", store_kwargs={"db_path": "beliefs.db"} ) # 2. Initialize provider adapter adapter = OpenAIAdapter(model="gpt-4o") tracker = BeliefTracker(config=config, adapter=adapter) # 3. Intercept your LLM handler @tracker.wrap async def chat(messages): client = openai.AsyncOpenAI() return await client.chat.completions.create( model="gpt-4o", messages=messages ) # 4. Set session and run tracker.set_session("user_123") await chat(messages)

Three lines of integration

beliefstate integrates without altering your core logic. The decorator intercepts messages, queries the database, and injects state automatically.

You retain full control over the LLM arguments. You can also query the session database directly or use manual hooks for custom workflows.

✓ Works with async def and regular def
✓ Zero latency on user-facing responses
✓ Fails safely — never crashes your app

Providers

Works with every LLM.

🤖
OpenAI
gpt-4o / embeddings
🧠
Anthropic
claude-3-5-sonnet
Gemini
gemini-2.5-flash
🦙
Ollama
local / offline
LiteLLM
100+ API models

Capabilities

Everything you need. Nothing you don't.

🔍

Semantic detection

Utilizes vector embeddings and natural language inference rather than exact string matching.

🔄

Dual-source extraction

Extracts beliefs from both user messages and assistant responses every turn, with per-source confidence caps.

🛡

Circuit breakers

Implements automated retries and fail-fast triggers to prevent downtime.

🔌

Framework integrations

Provides middleware for FastAPI and callbacks for LangChain and LlamaIndex.

📋

GDPR compliant

Supports permanent session deletion with task draining and receipts.

Get started

Ready to track beliefs?

Install in less than a minute. Add state to your agent conversations today.