Skip to main content

Hebbian memory for AI agents — memories that fire together wire together.

Project description

hebbmem

Hebbian memory for AI agents — memories that fire together wire together.

Install

pip install hebbmem

For higher-quality semantic embeddings:

pip install hebbmem[ml]

Quick Start

from hebbmem import HebbMem

mem = HebbMem()

# Store memories
mem.store("Python is great for data science", importance=0.8)
mem.store("JavaScript runs in the browser", importance=0.5)
mem.store("Neural networks learn from data", importance=0.7)

# Time passes, memories decay
mem.step(5)

# Recall activates related memories through the graph
results = mem.recall("machine learning with Python", top_k=3)
for r in results:
    print(f"{r.content} (score={r.score:.3f})")

How It Works

hebbmem replaces flat vector storage with three neuroscience mechanisms:

Decay — Memories fade over time unless reinforced, following the Ebbinghaus forgetting curve. Recent and frequently accessed memories stay strong.

Hebbian Learning — Memories recalled together strengthen their connections. "Neurons that fire together wire together." Over time, the graph learns which memories are related through usage, not just embedding similarity.

Spreading Activation — Recalling one memory activates related ones through the graph, surfacing connections that keyword or vector search alone would miss.

Persistence

Save and restore memory state across sessions:

mem.save("agent_memory.hebb")

# Later...
mem = HebbMem.load("agent_memory.hebb", encoder="hash")

Uses SQLite internally — single file, zero dependencies, crash-safe.

Configuration

from hebbmem import HebbMem, Config

config = Config(
    activation_decay=0.9,    # how fast activation fades (0-1)
    strength_decay=0.999,    # how fast long-term strength fades (0-1)
    hebbian_lr=0.2,          # learning rate for co-activation (0-1)
    spread_factor=0.5,       # energy spread per hop (0-1)
    max_hops=3,              # BFS depth for spreading activation
    scoring_weights={        # recall ranking formula
        "activation": 0.4,
        "similarity": 0.35,
        "strength": 0.15,
        "importance": 0.1,
    },
)
mem = HebbMem(encoder="hash", config=config)

Batch Store

ids = mem.store_batch(
    ["memory one", "memory two", "memory three"],
    importances=[0.9, 0.5, 0.3],
)

Thread Safety

All public methods are thread-safe (protected by threading.RLock). Safe to use from multiple threads in agent frameworks.

Logging

hebbmem uses stdlib logging. Enable debug output:

import logging
logging.basicConfig(level=logging.DEBUG)

Benchmark

hebbmem outperforms flat vector search (pure cosine similarity) on key scenarios:

Scenario Metric hebbmem Baseline Delta
Associative Precision@5 0.56 0.44 +27.3%
Associative Assoc. Hit Rate 0.13 0.00
Noise Precision@5 0.60 0.56 +7.1%
Contradiction Precision@1 0.50 0.00
Contradiction MRR 0.75 0.50 +50.0%
Temporal Precision@5 0.36 0.36 tied

Key wins: contradiction handling (50% MRR improvement — newer memories rank higher) and associative recall (finds indirectly connected memories that baseline misses entirely). Results use HashEncoder; advantages are larger with semantic embeddings.

Run yourself: uv run python benchmarks/run_benchmark.py — see benchmarks/ for methodology.

Visualization

Interactive D3.js demo showing spreading activation, Hebbian reinforcement, and decay in real-time. Dark theme with glowing nodes — neurons firing.

uv pip install fastapi uvicorn
uv run python demo/server.py
# Open http://localhost:8765

See demo/ for walkthrough.

Examples

See examples/ for runnable scripts:

Links

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

hebbmem-0.4.0.tar.gz (152.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hebbmem-0.4.0-py3-none-any.whl (18.9 kB view details)

Uploaded Python 3

File details

Details for the file hebbmem-0.4.0.tar.gz.

File metadata

  • Download URL: hebbmem-0.4.0.tar.gz
  • Upload date:
  • Size: 152.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hebbmem-0.4.0.tar.gz
Algorithm Hash digest
SHA256 e8a41b6d2a8acdbe68773ae005dac6a9780b15f89e0e8d9967b8893abed72ddf
MD5 b935300a7247848403ef308a19aeba1b
BLAKE2b-256 ed5e34ab4e913192610e53b63ce00d456502b7a98a0406cf4d6cad54daee5780

See more details on using hashes here.

Provenance

The following attestation bundles were made for hebbmem-0.4.0.tar.gz:

Publisher: publish.yml on codepawl/hebbmem

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hebbmem-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: hebbmem-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 18.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hebbmem-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9b7431faf3f8f941655a5913f5499717f5305e353e720b1fda4795315c8a074e
MD5 5340c06113ffedc7fdfcfa8bfd509c95
BLAKE2b-256 d12e3cf36b39162be939461bb81644b3717274cc722f2c65ed9f9581580ac0c8

See more details on using hashes here.

Provenance

The following attestation bundles were made for hebbmem-0.4.0-py3-none-any.whl:

Publisher: publish.yml on codepawl/hebbmem

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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