Skip to content

sttor/memory-correctness

Repository files navigation

memory-correctness

Post-retrieval correctness for AI agent memory.

Use it after your memory store retrieves candidates. It removes stale, wrong-scope, contradicted, or low-quality memories before they enter the LLM context.

Install

Use Python 3.10+.

python -m pip install memory-correctness

Optional integrations:

python -m pip install "memory-correctness[mem0,openai,postgres]"
python -m pip install "memory-correctness[all]"

Quick Start

from memory_correctness import MemoryCorrectnessCompiler
from memory_correctness.sidecar.sqlite import SQLiteSidecar
from memory_correctness.stores.dict_store import DictStore

memories = [
    {
        "id": "old",
        "text": "User prefers detailed explanations.",
        "score": 0.91,
        "metadata": {
            "subject": "user.answer_style",
            "predicate": "prefers",
            "object": "detailed",
            "source": "explicit_user_statement",
            "scope": {"user_id": "u1"},
        },
    },
    {
        "id": "new",
        "text": "User now prefers short technical answers.",
        "score": 0.86,
        "metadata": {
            "subject": "user.answer_style",
            "predicate": "prefers",
            "object": "short",
            "source": "explicit_user_correction",
            "scope": {"user_id": "u1"},
        },
    },
]

compiler = MemoryCorrectnessCompiler(
    store=DictStore(memories),
    sidecar=SQLiteSidecar(":memory:"),
)

result = compiler.compile(
    user_id="u1",
    message="Explain the architecture.",
    context={"domain": "tech", "token_budget": 700},
)

print(result.memory_context)  # text to pass to the LLM
print(result.suppressed)      # memories removed, with reasons

Providers

Use any store implementing MemoryStore. Built-in adapters include DictStore, Mem0, Zep, Supermemory, Qdrant, ChromaDB, Weaviate, Pinecone, and pgvector.

from mem0 import MemoryClient
from memory_correctness import MemoryCorrectnessCompiler
from memory_correctness.stores.mem0 import Mem0Store

client = MemoryClient(api_key="...")
compiler = MemoryCorrectnessCompiler(store=Mem0Store(client=client))

Smoke-test third-party adapters against the exact SDK/API version you deploy.

Metadata

Best results need subject, predicate, object, source, and scope on each memory. Add them yourself, use compiler.add_memory(), or use an extractor such as OpenAIExtractor during writes/backfills.

Development

python -m pip install -e ".[dev]"
pytest

More details live in docs/.

License

MIT

About

Prevent AI agents from using the wrong memory.

Resources

License

Stars

4 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors