Backends
MemState separates logic from storage. You can start with InMemoryStorage for testing and switch to PostgresStorage or RedisStorage for production without changing your agent logic.
Pro Tip: Dependency Injection
MemState fully supports Dependency Injection. For all backends (Postgres, Redis, SQLite), you can pass an existing connection object instead of a connection string.
PostgreSQL
Uses SQLAlchemy + psycopg. It supports JSONB for efficient querying.
Install the requirements
Initialize the storage
import asyncio
from memstate import AsyncMemoryStore
from memstate.backends.postgres import AsyncPostgresStorage
async def main():
url = "postgresql+psycopg://user:pass@localhost:5432/db_name"
storage = AsyncPostgresStorage(url)
# Important: You must create tables explicitly in async mode
await store.create_tables()
store = AsyncMemoryStore(storage=storage)
if __name__ == "__main__":
asyncio.run(main())
Redis
Stores facts as JSON strings and maintains sets for efficient indexing.
Install the requirements
Initialize the storage
SQLite
The default, zero-config backend. Uses the JSON1 extension for querying.
Install the requirements
For synchronous use, SQLite is built-in.
For async use, you need aiosqlite.
Initialize the storage
In-memory
Non-persistent storage. Best for testing and prototyping.