Skip to main content

Biologically-inspired agent memory with decay, consolidation, and tiered storage

Project description

cognitive-memory

Biologically-inspired agent memory with decay, consolidation, and tiered storage.

PyPI version

Python SDK. v0.4.0 brings hybrid retrieval (BM25 + vector), power-law decay, graph expansion, LLM rerank, deferred conflict resolution, multi-tenancy, a pluggable LLMProvider, and a JSONL file adapter. Behavioural parity with the TypeScript SDK.

Install

pip install cognitive-memory

For the OpenAI extractor and embedder:

pip install "cognitive-memory[openai]"
export OPENAI_API_KEY=sk-...

Requires Python 3.10+.

Quick Start

from cognitive_memory import SyncCognitiveMemory

mem = SyncCognitiveMemory(embedder="hash")  # zero-dep, deterministic

mem.add("User is allergic to shellfish", category="core", importance=0.95)

response = mem.search("what allergies does the user have?")
for r in response.results:
    print(r.memory.content, f"(score: {r.combined_score:.2f})")

For async code, use CognitiveMemory directly (same API, await mem.add(...) / await mem.search(...)).

Production setup

from cognitive_memory import CognitiveMemory, JsonlFileAdapter

mem = CognitiveMemory(
    embedder="openai",                                  # OpenAIEmbeddings, reads OPENAI_API_KEY
    adapter=JsonlFileAdapter("/var/lib/myapp/mem.jsonl"),  # durable, single-process
    user_id="alice",                                    # multi-tenant scoping
)

await mem.extract_and_store(conversation_text, session_id="sess-1")
results = await mem.search("UI preferences", deep_recall=True, rerank=True)

extract_and_store(...) runs the LLM extractor; add(...) skips it for pre-extracted facts.

Custom LLM provider

The extractor and conflict-resolver talk to an LLMProvider interface — swap OpenAI for Anthropic, a local model, or a gateway:

from cognitive_memory import CognitiveMemory, LLMProvider

class MyProvider(LLMProvider):
    def complete(self, prompt: str, **kwargs) -> str:
        ...  # your model

mem = CognitiveMemory(llm=MyProvider())

Multi-tenancy

user_id namespaces every read and write. Two instances sharing an adapter are fully isolated.

alice = CognitiveMemory(adapter=shared, user_id="alice")
bob   = CognitiveMemory(adapter=shared, user_id="bob")

await alice.add("alice's secret")
# bob.search() never returns alice's memories

Adapters

  • InMemoryAdapter — default, ephemeral
  • JsonlFileAdapter — append-only event log, replay on startup
  • Custom — implement MemoryAdapter from cognitive_memory.adapters
  • Postgres (pgvector) — planned for 0.4.1

Migration

See MIGRATION.md for the 0.3.0 → 0.4.0 changes (all additive — existing code keeps working).

Docs

Full documentation, guides, concepts, and API reference: planetaryescape.github.io/cognitive-memory.

License

MIT

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

cognitive_memory-0.5.1.tar.gz (56.1 kB view details)

Uploaded Source

Built Distribution

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

cognitive_memory-0.5.1-py3-none-any.whl (47.0 kB view details)

Uploaded Python 3

File details

Details for the file cognitive_memory-0.5.1.tar.gz.

File metadata

  • Download URL: cognitive_memory-0.5.1.tar.gz
  • Upload date:
  • Size: 56.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for cognitive_memory-0.5.1.tar.gz
Algorithm Hash digest
SHA256 456762ad8f00aa2d20a4313ee02137dd14afe08dd1c631ff270b3664e998f97d
MD5 0f8976d04910220446c78eaa90a9bdc0
BLAKE2b-256 98d40584f5b0ecaa46f57fa742618ab69fcdfd1bff0827f91850c769ed833e51

See more details on using hashes here.

File details

Details for the file cognitive_memory-0.5.1-py3-none-any.whl.

File metadata

File hashes

Hashes for cognitive_memory-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 916d4996169b24167f044a1eae13298e295222ad01f9bc08bdc026a6c1e0d2fa
MD5 e8868ce18c8ad7002100f7b7459df594
BLAKE2b-256 0dae7a596c74037cc3560be9ec79af83ef2853c29581fcfad9607ed433a009c4

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