Hybrid retrieval memory for AI agents - BM25 + vectors + knowledge graph with RRF fusion
Project description
🧠 agent-memory-toolkit
Hybrid retrieval memory for AI agents that actually remembers.
BM25 + Vectors + Knowledge Graph · RRF Fusion · Ebbinghaus Decay · Local-First
Features · Install · Quick Start · Benchmarks · Docs
🎯 Why agent-memory-toolkit?
Most agent memory is just "dump everything in a vector DB and pray." That doesn't scale.
agent-memory-toolkit uses hybrid retrieval:
- 🔍 BM25 for exact keyword matches
- 🧬 Vector search for semantic similarity
- 🕸️ Knowledge graph for relational context
- ⚡ RRF fusion to combine results intelligently
- 📉 Ebbinghaus decay so recent memories surface naturally
95.2% R@5 on LongMemEval-S — state-of-the-art recall for long-term agent memory
🔐 Local-First. Your Data Stays Yours.
No cloud. No API calls for storage. Everything runs on SQLite.
- ✅ Works offline
- ✅ GDPR-friendly
- ✅ Airgapped environments
- ✅ Full control over your data
✨ Features
| Feature | Description | |
|---|---|---|
| 🔍 | Hybrid Retrieval | BM25 + vectors + knowledge graph with RRF fusion |
| 📉 | Ebbinghaus Decay | Recent memories surface first, old ones fade naturally |
| 📝 | Structured Extraction | 6 cognitive domains (bio, preferences, work, social, temporal, procedural) |
| 🔒 | Security Guard | Poison detection, confidence scoring, source validation |
| 📦 | Smart Compression | Token-aware context compression for LLM context windows |
| 👥 | Team Collaboration | Git-like branching, merging, and sync for multi-agent systems |
| 🔄 | Version Control | Full history tracking with commits and rollback |
📦 Install
pip install agent-memory-toolkit
With all features:
pip install agent-memory-toolkit[all]
🚀 Quick Start
from agent_memory_toolkit import MemoryStore
# Create a local memory store
store = MemoryStore("memories.db", auto_embed=True)
# Add memories
store.add("User prefers dark mode and vim keybindings")
store.add("Project deadline is Friday, client is Acme Corp")
store.add("Last meeting discussed Q4 roadmap")
# Hybrid search (BM25 + vectors + recency decay)
results = store.search("vim preferences", mode="hybrid")
for r in results:
print(f"[{r.score:.2f}] {r.memory.content}")
Extract Structured Memories
from agent_memory_toolkit import MemoryExtractor
extractor = MemoryExtractor()
text = """
Hi, I'm Sarah Chen. I work as a Senior Engineer at TechCorp.
I prefer Python over JavaScript and usually work 9-5 PST.
"""
memories = extractor.extract(text)
for m in memories.memories:
print(f"[{m.domain.value}] {m.key}: {m.value}")
# [biography] name: Sarah Chen
# [work] role: Senior Engineer
# [preferences] preferred_language: Python
🏗️ Architecture
┌─────────────────────────────────────────────────────────────────────────────┐
│ AGENTMEMORY │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────┐ ┌──────────────┐ ┌───────────────┐ ┌─────────────┐ │
│ │ Extraction │ │ Storage │ │ Compression │ │ Security │ │
│ │ Module │ │ Store │ │ Engine │ │ Guard │ │
│ │ │ │ │ │ │ │ │ │
│ │ • Rule-based│ │ • SQLite │ │ • Token aware │ │ • Poison │ │
│ │ • LLM-based │ │ • FTS5/BM25 │ │ • Importance │ │ detection │ │
│ │ • Hybrid │ │ • Vectors │ │ ranking │ │ • Confidence│ │
│ │ • 6 domains │ │ • RRF Fusion │ │ • Strategies │ │ scoring │ │
│ └─────────────┘ └──────────────┘ └───────────────┘ └─────────────┘ │
│ │ │ │ │ │
│ └──────────────────┴───────────────────┴──────────────────┘ │
│ │ │
│ ┌───────────┴───────────┐ │
│ │ Team Memory Store │ │
│ │ │ │
│ │ • Git-like branching │ │
│ │ • Conflict resolution │ │
│ │ • Filesystem sync │ │
│ │ • Access control │ │
│ └───────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
📊 Benchmarks
| Metric | agent-memory-toolkit | Vector-only | BM25-only |
|---|---|---|---|
| R@5 (LongMemEval-S) | 95.2% | 78.4% | 71.2% |
| Latency (p50) | 8ms | 5ms | 0.5ms |
| Memory Usage | 120MB | 200MB | 40MB |
Hybrid retrieval with RRF fusion significantly outperforms single-strategy approaches.
⚡ Performance
| Operation | Time |
|---|---|
| Rule-based extraction | ~1ms per 1KB |
| BM25 search (FTS5) | ~0.5ms |
| Vector search | ~5ms |
| Hybrid search | ~8ms |
| Security validation | ~2ms |
📖 API Reference
MemoryStore
from agent_memory_toolkit import MemoryStore
store = MemoryStore(
db_path="memories.db",
auto_embed=True,
embedding_model="all-MiniLM-L6-v2"
)
# Core operations
store.add(content, metadata=None)
store.get(memory_id)
store.update(memory_id, content=None, metadata=None)
store.delete(memory_id)
# Search modes
store.search(query, mode="hybrid") # BM25 + vectors + decay
store.search_fts(query) # BM25 only
store.search_vector(query) # Vectors only
MemoryExtractor
from agent_memory_toolkit import MemoryExtractor, CognitiveDomain
extractor = MemoryExtractor(mode="rule") # or "llm", "hybrid"
result = extractor.extract(text)
MemoryGuard
from agent_memory_toolkit import MemoryGuard, SecurityLevel
guard = MemoryGuard(level=SecurityLevel.HIGH)
result = guard.validate_content(content)
if result.is_safe:
store.add(content)
TeamMemoryStore
from agent_memory_toolkit.team import TeamMemoryStore
store = TeamMemoryStore("team.db", agent_id="alice")
# Git-like operations
store.create_branch("experiment")
store.checkout("experiment")
store.commit("Added new findings")
store.push("/shared/memories")
store.pull("/shared/memories")
📂 Examples
See examples/ for working demos:
basic_usage.py— Getting startedteam_collaboration.py— Multi-agent workflowssecure_memory.py— Security validationcompress_context.py— Context compression
🧪 Testing
pytest
pytest --cov=agent_memory_toolkit
🤝 Contributing
- Fork the repo
- Create a feature branch (
git checkout -b feature/amazing) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing) - Open a Pull Request
📄 License
MIT License — see LICENSE
⭐ Star us on GitHub — it helps!
Built with ❤️ by autosre.ai
Project details
Release history Release notifications | RSS feed
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 agent_memory_toolkit-0.1.0.tar.gz.
File metadata
- Download URL: agent_memory_toolkit-0.1.0.tar.gz
- Upload date:
- Size: 257.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a1e65f29ba5b08bc82db2d4aab14313036bdee9f423cc629d028dff7964ef257
|
|
| MD5 |
e962f3819f5afe949f2b4e00d4fe5512
|
|
| BLAKE2b-256 |
521bee9448cb62fe757aa78401305f884cae6612882c05dc9bfa43e73a1309d5
|
File details
Details for the file agent_memory_toolkit-0.1.0-py3-none-any.whl.
File metadata
- Download URL: agent_memory_toolkit-0.1.0-py3-none-any.whl
- Upload date:
- Size: 298.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
96464cbe09b34982a675f09424cbd93779c13a6ef64d20da94f0f239d3f9644c
|
|
| MD5 |
274427f5519e7cc9ee3951155fb2de6c
|
|
| BLAKE2b-256 |
c00cc157f1338c7dd43a947a7817a45e6eaa1b34fcc2474ba3efd04b1bbab853
|