snkv-agent-memory
Persistent, offline memory for AI agents. one file on disk, zero API keys, works offline forever.
Built on SNKV — a pure C embedded KV + vector store with HNSW indexing.
https://github.com/user-attachments/assets/8547848a-e42c-46ba-90c8-5569a7c5a8a9
Why snkv-agent-memory?
|
snkv-agent-memory |
| Works fully offline |
✅ |
| Zero API keys |
✅ |
| Free forever |
✅ |
| No cloud dependency |
✅ |
Your local LLM has no memory between sessions. snkv-agent-memory fixes that — privately, on your machine, with no external calls.
Get started
Clone the repo, install dependencies, and start chatting:
git clone https://github.com/hash-anu/agent-memory.git
cd agent-memory
pip install snkv[vector]==0.8.2 ollama numpy usearch
ollama pull nomic-embed-text
ollama pull qwen2.5:7b-instruct
python examples/chat.py
That's it — memories persist in agent.db across every run.
Run the demo
# Smoke test — no Ollama needed, runs fully offline
python examples/smoke_test.py
# Interactive chat with memory (requires Ollama)
ollama pull nomic-embed-text
ollama pull qwen2.5:7b-instruct
python examples/chat.py
Run tests
# Unit tests — no Ollama required
pytest tests/test_memory.py -v
# Integration tests — requires Ollama with nomic-embed-text
pytest tests/test_integration.py -v -k "not sentence_transformer"
How it works
- Embeddings — text is converted to a float32 vector by your chosen backend (Ollama, sentence-transformers, or a mock for tests).
- Hybrid recall —
recall() runs both HNSW vector search (semantic similarity) and BM25 keyword search, then fuses the results with Reciprocal Rank Fusion (RRF) for better accuracy.
- Structured storage — conversation history, sessions, and user profiles are stored in a KVStore with column families so scans stay fast and data stays isolated.
┌──────────────────────────────────────────────────────────────┐
│ AgentMemory │
│ │
│ remember(text) ──► Embedder ──► VectorStore (agent.db) │
│ └──► BM25Index (agent.db.kv) │
│ │
│ recall(query) ──► vector search ─┐ │
│ └──► BM25 search ─┴─► RRF fusion │
│ │
│ remember_exchange() ──────────────► history CF │
│ save_profile() ──────────────────► profile CF │
│ save_session() ──────────────────► sessions CF │
│ KVStore (agent.db.kv)│
└──────────────────────────────────────────────────────────────┘
Embedder backends
| Class |
Model |
Dim |
Requires |
OllamaEmbedder |
nomic-embed-text |
768 |
Ollama server running locally |
SentenceTransformerEmbedder |
all-MiniLM-L6-v2 |
384 |
pip install sentence-transformers |
MockEmbedder |
— |
128 |
nothing (tests only) |
All three share the same BaseEmbedder interface — drop-in replaceable.
Model configuration
Both models can be changed without touching code.
Via environment variables:
CHAT_MODEL=llama3.2 EMBED_MODEL=mxbai-embed-large python examples/chat.py
Via constructor/function arguments:
embedder = OllamaEmbedder(model="mxbai-embed-large") # custom embed model
reply = chat(user_input, session_id, memory, model="llama3.2") # custom chat model
| Env var |
Default |
Controls |
EMBED_MODEL |
nomic-embed-text |
Ollama embedding model |
CHAT_MODEL |
qwen2.5:7b-instruct |
Ollama chat/LLM model |
EXCHANGE_TTL |
(unset — permanent) |
Seconds before chat exchanges expire |
Valid model values are any model names returned by ollama list on your machine. Pull a model first, then set the env var:
ollama pull llama3.2
CHAT_MODEL=llama3.2 python examples/chat.py
To expire chat history after 7 days:
EXCHANGE_TTL=604800 python examples/chat.py
API reference
AgentMemory(path, embedder, dim=None, max_memories=1000)
# Semantic memory
.remember(text, metadata=None, ttl=None) -> str
.recall(query, top_k=5) -> List[Memory] # hybrid BM25 + vector + RRF
.list_memories(top_n=50) -> List[Memory] # all stored memories, unfiltered
.remember_exchange(user_msg, agent_reply, ttl=86400, session_id="default")
# Conversation history
.get_history(session_id, last_n=10) -> List[dict]
# Sessions & profiles
.save_session(session_id, metadata, ttl=3600)
.save_profile(user_id, data)
.get_profile(user_id) -> dict | None
# Deletion & maintenance
.forget(key)
.forget_all()
.purge_expired() -> int
.vacuum()
.stats() -> dict
.close()
License
Apache 2.0