1- # MemState - Transactional Memory for AI Agents
1+ # MemState — Transactional Memory for AI Agents
22
33** Agents hallucinate because their memory drifts.**
44SQL says one thing, the Vector DB says another. MemState keeps them in sync, always.
@@ -7,22 +7,25 @@ SQL says one thing, the Vector DB says another. MemState keeps them in sync, alw
77> One unit. One commit. One rollback.
88
99[ ![ PyPI version] ( https://img.shields.io/pypi/v/memstate.svg )] ( https://pypi.org/project/memstate/ )
10- [ ![ PyPI Downloads] ( https://static.pepy.tech/personalized-badge/memstate?period=total&units=INTERNATIONAL_SYSTEM&left_color=GREY&right_color=GREEN&left_text=downloads )] ( https://pepy.tech/projects/memstate )
1110[ ![ Python versions] ( https://img.shields.io/pypi/pyversions/memstate.svg )] ( https://pypi.org/project/memstate/ )
1211[ ![ License] ( https://img.shields.io/pypi/l/memstate.svg )] ( https://github.com/scream4ik/MemState/blob/main/LICENSE )
1312[ ![ Tests] ( https://github.com/scream4ik/MemState/actions/workflows/test.yml/badge.svg )] ( https://github.com/scream4ik/MemState/actions )
1413
14+ ---
15+
16+ ** Documentation** : <a href =" https://scream4ik.github.io/MemState/ " target =" _blank " >https://scream4ik.github.io/MemState/ </a >
17+
18+ ** Source Code** : <a href =" https://github.com/scream4ik/MemState " target =" _blank " >https://github.com/scream4ik/MemState </a >
19+
20+ ---
21+
1522<p align =" center " >
16- <img src =" https://raw.githubusercontent.com/scream4ik/MemState/main/assets/docs/demo.gif " width =" 100% " />
17- <br >
18- <strong >Demo:</strong > Without MemState → memory gets inconsistent ❌   ;  ; |  ;  ; With MemState → atomic, type-safe, rollbackable agent state ✅
19- <br >
20- <em >All demo scripts are available in the <code >examples/</code > folder for reproducibility.</em >
23+ <img src =" https://raw.githubusercontent.com/scream4ik/MemState/main/assets/docs/demo.gif " width =" 100% " alt =" MemState Demo " />
2124</p >
2225
2326---
2427
25- ## Quick Start
28+ ## ⚡ Quick Start
2629
2730``` bash
2831pip install memstate[chromadb]
@@ -34,107 +37,52 @@ from memstate import MemoryStore, SQLiteStorage, HookError
3437from memstate.integrations.chroma import ChromaSyncHook
3538import chromadb
3639
37- # Define your Data Schema
40+ # 1. Define Data Schema
3841class UserPref (BaseModel ):
3942 content: str
4043 role: str
4144
42- # Storage
45+ # 2. Setup Storage (Local)
4346sqlite = SQLiteStorage(" agent_memory.db" )
4447chroma = chromadb.Client()
4548
46- # Hook: sync vectors atomically with SQL
49+ # 3. Initialize with Sync Hook
4750mem = MemoryStore(sqlite)
4851mem.add_hook(ChromaSyncHook(chroma, " agent_memory" , text_field = " content" , metadata_fields = [" role" ]))
49- mem.register_schema(" preference" , UserPref) # Register type for validation
52+ mem.register_schema(" preference" , UserPref)
5053
51- # Atomic Commit (Type-Safe)
54+ # 4. Atomic Commit
5255# Validates Pydantic model -> Writes SQL -> Upserts Vector
5356try :
5457 mem.commit_model(model = UserPref(content = " User prefers vegetarian" , role = " preference" ))
5558except HookError as e:
56- print (" Commit failed, operation rolled back automatically:" , e)
57-
58- # Optional manual rollback of previous step
59- # mem.rollback(1) # uncomment if you want to undo the last saved fact
60- ```
61-
62- That's it. Your agent memory is now transactional.
63-
64- ---
65-
66- ## LangGraph integration
67-
68- ``` python
69- from memstate.integrations.langgraph import MemStateCheckpointer
70-
71- checkpointer = MemStateCheckpointer(memory = mem)
72- app = workflow.compile(checkpointer = checkpointer)
73- ```
74-
75- ---
76-
77- ## Why MemState exists
78-
79- ### The Problem
80-
81- AI agents usually store memory in ** two places** :
82-
83- * ** SQL** : structured facts (preferences, task history)
84- * ** Vector DB** : semantic search (embeddings for RAG)
85-
86- These two stores ** drift easily** . Even small failures create inconsistency:
87-
88- ``` python
89- # Step 1: SQL write succeeds
90- db.update(" user_city" , " London" )
59+ print (" Commit failed, SQL rolled back automatically:" , e)
9160
92- # Step 2: Vector DB update fails
93- vectors.upsert(" User lives in London" ) # ❌ failed
94-
95- # Final state:
96- # SQL: London
97- # Vectors: New York (stale embedding)
61+ # 5. Undo (if needed)
62+ # mem.rollback(1)
9863```
9964
100- ** Result:** ghost vectors, inconsistent state, unpredictable agent behavior.<br >
101- Drift accumulates silently, agents continue retrieving outdated or mismatched memory.
65+ 👉 ** [ See full Documentation & Examples] ( https://scream4ik.github.io/MemState/ ) **
10266
10367---
10468
105- ### Why this happens
106-
107- Real-world agent pipelines create drift ** even with correct code** , because:
69+ ## The Problem
10870
109- * Vector DB upserts are ** not atomic**
110- * Retried writes can produce ** duplicates or stale embeddings**
111- * Async ingestion leads to ** race conditions**
112- * LLM outputs often contain ** non-schema JSON**
113- * Embedding model/version changes create ** semantic mismatch**
114- * SQL writes succeed while vector DB fails, partial updates persist
71+ AI agents usually store memory in ** two places** : SQL (structured facts) and Vector DB (semantic search).
11572
116- These issues are ** invisible until retrieval fails** , making debugging extremely difficult.
117- MemState prevents this by enforcing ** atomic memory operations** : if any part fails, the whole operation is rolled back.
73+ These two stores ** drift** easily. If a network request to the Vector DB fails, or the agent crashes mid-operation, you end up with ** "Split-Brain" memory** :
74+ * ** SQL:** "User lives in London"
75+ * ** Vector DB:** "User lives in New York" (Stale embedding)
11876
119- ---
77+ ** Result: ** The agent retrieves wrong context and hallucinates.
12078
12179## The Solution
12280
123- MemState makes memory operations ** atomic** :
124-
125- ```
126- SQL write + Vector upsert
127- → succeed together or rollback together
128- ```
129-
130- ## Key features
81+ MemState acts as a ** Transactional Layer** . It ensures that every memory operation is ** Atomic** :
13182
132- * ** Atomic commits** : SQL and Vector DB stay in sync
133- * ** Rollback** : undo N steps across SQL and vectors
134- * ** Type safety** : Pydantic validation prevents malformed JSON
135- * ** Append-only Fact Log** : full versioned history
136- * ** Crash-safe atomicity** : if a vector DB write fails, the entire memory operation (SQL + vector) is ** rolled back** .
137- No partial writes, no ghost embeddings, no inconsistent checkpoints.
83+ * ** Atomic Commits:** SQL and Vector DB stay in sync. If one fails, both rollback.
84+ * ** Type Safety:** Pydantic validation prevents LLMs from corrupting your JSON schema.
85+ * ** Time Travel:** Undo N steps with ` rollback(n) ` . Useful for user corrections.
13886
13987---
14088
@@ -160,56 +108,31 @@ Full benchmark script: [`benchmarks/`](benchmarks/)
160108
161109---
162110
163- ## Ideal for
164-
165- * Long-running agents
166- * LangGraph workflows
167- * RAG systems requiring strict DB <-> embedding consistency
168- * Local-first / offline-first setups (SQLite/Redis + Chroma/Qdrant/FAISS)
169- * Deterministic, debuggable agentic pipelines
170-
171- ---
172-
173- ## Storage backends
174-
175- All backends participate in the same atomic commit cycle:
176-
177- * SQLite (JSON1)
178- * Redis
179- * In-memory
180- * Custom backends via simple interface
181-
182- Vector sync hooks: ChromaDB (more coming)
111+ ## Ecosystem
183112
184- ---
113+ | Category | Supported |
114+ | :--- | :--- |
115+ | ** Storage Backends** | SQLite, PostgreSQL (JSONB), Redis, In-Memory |
116+ | ** Vector Hooks** | ChromaDB, Qdrant (more coming) |
117+ | ** Frameworks** | ** LangGraph** (Native Checkpointer), LangChain |
118+ | ** Runtime** | Sync & ** Async** (FastAPI ready) |
185119
186- ## When you don't need it
120+ ### LangGraph Integration
187121
188- * Your agent is fully stateless
189- * You store everything in a single SQL table
190- * You never update embeddings after creation
122+ ``` python
123+ from memstate.integrations.langgraph import MemStateCheckpointer
191124
192- If your pipelines depend on RAG or long-term state, consistency ** is** required - most teams realize this only when debugging unpredictable retrieval.
125+ checkpointer = MemStateCheckpointer(memory = mem)
126+ app = workflow.compile(checkpointer = checkpointer)
127+ ```
193128
194129---
195130
196131## Status
197132
198- ** Early Access.**
199- Production-ready for local agents and prototypes. API is stable (Semantic Versioning).
200-
201- Community contributions welcome.
202-
203- ---
204-
205- ## Install
206-
207- ``` bash
208- pip install memstate[chromadb]
209- ```
133+ ** Beta.** The API is stable. Suitable for production agents that require high reliability.
210134
211- ⭐ ** [ Star the repo] ( https://github.com/scream4ik/MemState ) **
212- 🐛 ** [ Report issues] ( https://github.com/scream4ik/MemState/issues ) **
135+ ** [ Read the Docs] ( https://scream4ik.github.io/MemState/ ) ** | ** [ Report an Issue] ( https://github.com/scream4ik/MemState/issues ) **
213136
214137---
215138
0 commit comments