AI agent memory system powered by Modern Hopfield Networks — no LLM calls, no database, one matrix multiply
Project description
MHN AI Agent Memory
Associative Memory for AI Agents Using Modern Hopfield Networks
Give your AI agents real memory. Not a database with an LLM wrapper. Actual associative memory backed by mathematics.
Install • Quick Start • Features • How It Works • API Reference
┌─────────────────────────────────────────────┐
│ │
│ xi_new = X @ softmax( beta * X^T @ xi ) │
│ │
│ One equation. One matrix multiply. │
│ Deterministic. Microseconds. Free. │
│ │
└─────────────────────────────────────────────┘
What is this?
When an AI agent needs to "remember" something today, the standard approach is:
- Store text in a database
- Call an LLM to reason about it (costs money, takes seconds, non-deterministic)
- Hope the LLM doesn't hallucinate about what it stored
This library replaces that pipeline with the Modern Hopfield Network update rule -- the same mathematical structure as transformer attention, but exposed as an explicit, controllable memory.
| Traditional (LLM + DB) | This Library | |
|---|---|---|
| Retrieval | LLM API call | One matrix multiply |
| Latency | Seconds | Microseconds |
| Cost | Per-token | Zero after storage |
| Determinism | Non-deterministic | Deterministic |
| Capacity | Depends on embedding quality | Exponential in dimension (proven) |
Store a fact. It becomes a pattern in an energy landscape. Query with a partial cue. The network relaxes to the nearest stored pattern. Get the answer. Confidence score included.
Install
pip install mhn-ai-agent-memory
Optional extras for better quality and scale:
pip install mhn-ai-agent-memory[semantic] # sentence-transformers (~80MB model)
pip install mhn-ai-agent-memory[openai] # OpenAI embedding API
pip install mhn-ai-agent-memory[scale] # FAISS for million-scale storage
pip install mhn-ai-agent-memory[all] # everything
30-Second Example
from hopfield_memory import HopfieldMemory
mem = HopfieldMemory()
mem.store("Alice is a mathematician who studies topology")
mem.store("Bob is a painter who works with oil on canvas")
mem.store("Carol is a physicist researching quantum entanglement")
fact, confidence = mem.query_with_confidence("topology math")
print(fact) # "Alice is a mathematician who studies topology"
print(confidence) # 0.9999
No API keys. No database. No config files. The memory is a numpy array.
Features at a Glance
Pluggable Encoders
Swap how text becomes vectors. Start simple, upgrade when you need to.
from hopfield_memory import HopfieldMemory, SentenceTransformerEncoder
mem = HopfieldMemory(encoder=SentenceTransformerEncoder())
| Encoder | Quality | Dependencies |
|---|---|---|
RandomIndexEncoder |
Basic (exact word match) | numpy only |
TFIDFEncoder |
Medium | scikit-learn |
SentenceTransformerEncoder |
High | sentence-transformers |
OpenAIEncoder |
Highest | openai SDK + API key |
Contradiction Detection
Catch conflicting facts before they corrupt your memory.
from hopfield_memory import check_and_store, ContradictionDetector
detector = ContradictionDetector()
idx, conflict = check_and_store(mem, "The capital of France is Lyon", detector=detector)
if conflict.has_conflict:
print(conflict.explanation)
Multi-Hop Retrieval
Chain queries to follow reasoning across related facts.
from hopfield_memory import chain_query
mem.store("Alice lives in France")
mem.store("The capital of France is Paris")
chain_query(mem, "capital of Alice's country", max_hops=3)
# -> ["Alice lives in France", "The capital of France is Paris"]
Scale from 10 to 10 Million Facts
from hopfield_memory import small_memory, large_memory, massive_memory
mem = small_memory() # ~100 facts, for tools and plugins
mem = large_memory() # ~100k facts, tiered hot/cold storage
mem = massive_memory() # millions, FAISS-backed cold store
The tiered system keeps your most-accessed memories in an exact Hopfield network (microsecond retrieval) and archives the rest to an approximate nearest-neighbor index on disk.
Repulsive Attention (Opt-In)
Up to 17x faster convergence for multi-step retrieval by adding contrastive "hills" to the energy landscape.
mem = HopfieldMemory(repulsive=True)
mem.store("Alice is a mathematician")
mem.store_negative("Known confusable pattern to avoid")
diag = mem.diagnose("topology math")
print(diag["recommendation"]) # agents decide at runtime
"Nothing Matches" Detection
AI agents need to know when a query has no relevant memory -- not just pick the least-bad option. This library solves it with three independent signals combined via a sentinel pattern.
from hopfield_memory import HopfieldMemory
mem = HopfieldMemory()
mem.store("The Eiffel Tower is in Paris")
mem.store("Mount Fuji is in Japan")
# Returns the fact when there's a match
result = mem.query_or_none("Eiffel Tower Paris")
print(result) # "The Eiffel Tower is in Paris"
# Returns None when nothing matches
result = mem.query_or_none("basketball playoffs score")
print(result) # None
# For more detail, inspect the match signals
mq = mem.match_quality("basketball playoffs score")
print(mq["max_similarity"]) # ~0.14 (low -- no real word overlap)
print(mq["is_match"]) # False
Under the hood, the network stores a zero-vector sentinel pattern. Three signals are combined:
- max_similarity -- raw dot product before softmax (the primary signal)
- gap -- attention weight separation between top patterns
- sentinel_weight -- how much attention goes to the "nothing" anchor
How It Works (Plain English)
-
You store a fact. The text is encoded into a vector and added to a matrix of stored patterns. This matrix IS the memory.
-
You query with a cue. The network computes similarity between the cue and every stored pattern, then uses a softmax to sharply concentrate attention on the best match.
-
You get a result. The output is the stored pattern the network "attracted" to -- the nearest memory. The softmax weight is your confidence score.
Key insight: This has the same mathematical structure as a single step of transformer attention (with tied keys and values). This library exposes that operation directly as a memory system, without wrapping it in an LLM.
How It Compares
| This Library | Honcho | Zep | MemGPT/Letta | Vector DB + LLM | |
|---|---|---|---|---|---|
| Architecture | Hopfield energy landscape | LLM reasoning + DB | Embedding + temporal graph | LLM self-editing context | ANN index + LLM |
| Retrieval latency | ~10 us (numpy matmul) | Seconds (LLM call) | ~ms (vector search) | Seconds (LLM call) | ~ms (ANN) + seconds (LLM) |
| Cost per query | Zero | LLM token cost | Zero (self-hosted) | LLM token cost | LLM token cost |
| Deterministic | Yes | No | Partially | No | No |
| "No match" detection | Built-in (sentinel) | Via LLM judgment | No | Via LLM judgment | No |
| Capacity theory | Exponential in dim (proven) | Unbounded (DB) | Unbounded (DB) | Context window | Unbounded (DB) |
| Dependencies | numpy | Python + LLM API + DB | Python + DB | Python + LLM API | Python + vector DB + LLM API |
| MCP server | Included | Cursor/Claude plugins | No | No | Custom |
| Best for | Fast, deterministic agent memory | Personalized long-term user models | Session history | Autonomous context management | Semantic search over documents |
This library is not a replacement for Honcho, Zep, or MemGPT -- they solve different problems. Use this when you need fast, deterministic, cost-free associative recall. Use them when you need LLM-powered reasoning about memory, user modeling, or unbounded storage.
Limitations
This section exists because honest documentation matters more than marketing.
- Default encoder is bag-of-words. "dog" and "canine" get zero similarity without
[semantic]extra. - Contradiction detection is heuristic. Works best with simple factual statements.
- Multi-hop is retrieval chaining, not logical inference. It finds related facts, not derived conclusions.
- Confidence is relative, not absolute. Softmax always sums to 1, so
query_with_confidence()always reports high confidence. Usequery_or_none()orhas_match()to detect non-matches. - Adaptive beta is a heuristic. The convergence proof assumes fixed inverse temperature.
- Exponential capacity has conditions. Requires patterns with sufficient separation in high dimension.
Project Structure
mhn-ai-agent-memory/
pyproject.toml # Package config
llms.txt # AI agent discoverability
CITATION.cff # Academic citation metadata
src/hopfield_memory/
network.py # ModernHopfieldNetwork (the math)
memory.py # HopfieldMemory (the user API)
repulsive.py # RepulsiveMHN (contrastive attention)
encoders.py # 4 text encoders
contradiction.py # Conflict detection
multihop.py # Chained retrieval
tiered.py # Hot/cold storage for scale
presets.py # small/medium/large/massive factories
mcp-server/ # MCP server for Cursor, Claude Code, etc.
tests/ # 43 tests
examples/ # Runnable demos
benchmarks/ # A/B: baseline vs repulsive
docs/ # GitHub Pages blog post
Development
git clone https://github.com/shahzebqazi/mhn-ai-agent-memory.git
cd mhn-ai-agent-memory
pip install -e ".[dev]"
pytest
References
- Ramsauer et al., Hopfield Networks is All You Need, ICLR 2021
- Krotov & Hopfield, Dense Associative Memory for Pattern Recognition, NeurIPS 2016
- Comparison baseline: Honcho (LLM-augmented database approach)
MIT License • Built by @shahzebqazi
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 mhn_ai_agent_memory-1.0.0.tar.gz.
File metadata
- Download URL: mhn_ai_agent_memory-1.0.0.tar.gz
- Upload date:
- Size: 49.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
28759776acbe5248c0f20c4b727fe44523b2a641b482cfa4eaa9af4d2f5c5fba
|
|
| MD5 |
51a98b56d099a7680b0a7e86a6935ac9
|
|
| BLAKE2b-256 |
7d7c3ad883ed4c21c923c513ef48300813cd37f282a522e4eb0bc9a84d5cb253
|
File details
Details for the file mhn_ai_agent_memory-1.0.0-py3-none-any.whl.
File metadata
- Download URL: mhn_ai_agent_memory-1.0.0-py3-none-any.whl
- Upload date:
- Size: 24.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bc6cbd3c52deaaa000a5da911108f654a464c20d7552c2087a4db658671fc0ae
|
|
| MD5 |
8f96f062e182058682ff7bc363769d1a
|
|
| BLAKE2b-256 |
1de5f25f7f12ad8583303cf13afdaf93a6da87130f2b357c278bb43af9b27f97
|