Skip to main content

Transactional, type-safe state management for LLM Agents. The 'Git' for AI memory.

Project description

🧠 MemState: The "Git" for AI Agent Memory

Stop building agents on JSON blobs and hope. MemState is a transactional, typed, and reversible state management layer for LLM Agents.

It replaces "Context Stuffing" and "Vector Soup" with a deterministic Source of Truth.


⚡ Why MemState?

Most agent memory systems today are just wrappers around Vector DBs. This leads to:

  • Hallucinations: The agent retrieves two contradictory facts (e.g., "User likes cats" vs "User hates cats") and guesses.
  • State Corruption: No validation. Agents overwrite critical data (like IDs or balances) with garbage.
  • No Undo Button: If an agent makes a mistake, you can't roll back the state. You have to wipe the memory.

MemState is different. It treats Agent Memory like a Database, not a text dump.

✨ Key Features

  • 🛡️ Type-Safe: Uses Pydantic schemas. If an agent tries to save a string into an age: int field, it fails before corruption happens.
  • ⏪ Time Travel: Every change is a transaction. You can rollback(steps=1) to undo an agent's mistake instantly.
  • 🔒 Constraints: Enforce logic like "One User Profile per Email" (Singleton). No more duplicate profiles.
  • 🔌 Hybrid Hooks: Use MemState as the Source of Truth and automatically sync to Vector DBs (Chroma, Qdrant) via hooks.
  • 🔍 JSON Querying: Fast, structured search (WHERE role = 'admin') via SQLite JSON1 extension. No need to embed everything.

🚀 Quick Start

Installation

# Clone the repo
git clone https://github.com/scream4ik/MemState.git
cd MemState

# Create a virtual environment
uv venv

# Install dependencies
uv sync

Usage

from memstate.storage import MemoryStore, Fact, Constraint
from memstate.backends.sqlite import SQLiteStorage
from pydantic import BaseModel

# 1. Define what your agent is allowed to remember
class UserProfile(BaseModel):
    username: str
    level: int = 1

# 2. Initialize Storage (SQLite)
storage = SQLiteStorage("agent_brain.db")
memory = MemoryStore(storage)

# 3. Register Schema with Rules
# Rule: "username" must be unique. If it exists, UPDATE it (don't duplicate).
memory.register_schema("user", UserProfile, Constraint(singleton_key="username"))

# 4. Commit a Fact (Transactional)
memory.commit(
    Fact(type="user", payload={"username": "neo", "level": 99}),
    actor="Agent_Smith"
)

# 5. Agent makes a mistake? Rollback!
memory.update(fact_id="...", patch={"payload": {"level": 0}})  # Oops
print("Before rollback:", memory.query(typename="user")[0]['payload'])

memory.rollback(1)
print("After rollback:", memory.query(typename="user")[0]['payload'])
# Level is back to 99.

💡 Use Cases

1. Financial & Legal Bots (Compliance)

Problem: An LLM hallucinates a loan interest rate. Solution: Use Immutable constraints for signed contracts. Use Transaction Logs to audit exactly when and why a fact was changed.

2. RPGs & Interactive Fiction

Problem: The player picked up a key, used it, then lost it. The LLM forgets the door is now unlocked. Solution: Use MemState to track the World State (door_status: open). If the player dies, use rollback() to reset the state to the last checkpoint perfectly.

3. Form Filling (Slot Filling)

Problem: User corrects themselves ("My car is a BMW... wait, no, an Audi"). Vector DBs return both. Solution: Use Singleton constraint on car_model. The correction automatically overwrites the old fact. The agent only sees the latest truth.


📂 Demos

Check the examples/ folder for runnable scripts:

  1. examples/main_demo.py

    • Full tour: Schemas, Singletons, Hallucination Correction via Rollback.
  2. examples/rag_hook_demo.py

    • Hybrid Memory Pattern.
    • Shows how to use MemState as the "Master DB" that automatically syncs text to a mock Vector DB for RAG.
    • Demonstrates automatic cleanup: Delete a fact in SQL -> It vanishes from Vectors.

🛠 Status

Alpha / MVP. Ready for local development.

Supports: InMemoryStorage, RedisStorage, SQLiteStorage.

Planned: PostgresStorage.


📄 License

Licensed under the Apache 2.0 License.


🤝 Contributing

Issues and PRs welcome. See CONTRIBUTING.md for details.


⭐️ Like the idea?

Star the repo and share feedback — we’re building in the open.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

memstate-0.1.0.tar.gz (46.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

memstate-0.1.0-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file memstate-0.1.0.tar.gz.

File metadata

  • Download URL: memstate-0.1.0.tar.gz
  • Upload date:
  • Size: 46.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for memstate-0.1.0.tar.gz
Algorithm Hash digest
SHA256 83f29dc6f0314de35949613ce328c7cd78678ad0fc71bff07f1d27aa09f2b6ec
MD5 820c345125a7b6b555eddaf4103518da
BLAKE2b-256 2fa49f3c841070878ac3b535b388955b73adc713c56fc60cb7bd290575e45451

See more details on using hashes here.

File details

Details for the file memstate-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: memstate-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for memstate-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 58aa9fba8c0596c44c04c7dc64ebb0baf4db87a0ebbc63044984056f4a6c6bfa
MD5 87f8a799774a3640fb74ddbaf7083f3b
BLAKE2b-256 5e17ac394486d074e513aa3d5a0597fe2d0a8ec36a33b747b8c8b9bf9db70354

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page