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.0.tar.gz (55.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.0-py3-none-any.whl (46.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cognitive_memory-0.5.0.tar.gz
  • Upload date:
  • Size: 55.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.0.tar.gz
Algorithm Hash digest
SHA256 7a0cdae189fd8bf4e35dc7ef8e1c6276439253e79d38627a1693c4fea09747f7
MD5 b433150aaa36bb9c309d964458a1a95f
BLAKE2b-256 70ddc83d95a89ee85f66fd3c91917aa6963ffddf0f10dd5e7b307e8762a667f6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cognitive_memory-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7e6eb723ec2c4ba43015b25f3a4ba5cdeb36c191a7880a66c39b36c6f537c979
MD5 5649680f90f8be349692f21f66300f4f
BLAKE2b-256 d7e7d61d736fca926b6beb58e12f09224e1aa49adcae272f0c5506fb6a8714bb

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