Biologically-inspired agent memory with decay, consolidation, and tiered storage
Project description
cognitive-memory
Biologically-inspired agent memory with decay, consolidation, and tiered storage.
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, ephemeralJsonlFileAdapter— append-only event log, replay on startup- Custom — implement
MemoryAdapterfromcognitive_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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7a0cdae189fd8bf4e35dc7ef8e1c6276439253e79d38627a1693c4fea09747f7
|
|
| MD5 |
b433150aaa36bb9c309d964458a1a95f
|
|
| BLAKE2b-256 |
70ddc83d95a89ee85f66fd3c91917aa6963ffddf0f10dd5e7b307e8762a667f6
|
File details
Details for the file cognitive_memory-0.5.0-py3-none-any.whl.
File metadata
- Download URL: cognitive_memory-0.5.0-py3-none-any.whl
- Upload date:
- Size: 46.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e6eb723ec2c4ba43015b25f3a4ba5cdeb36c191a7880a66c39b36c6f537c979
|
|
| MD5 |
5649680f90f8be349692f21f66300f4f
|
|
| BLAKE2b-256 |
d7e7d61d736fca926b6beb58e12f09224e1aa49adcae272f0c5506fb6a8714bb
|