Skip to main content

A layered semantic cache for LLM applications: exact-match then semantic-match, embeddable, in-process, NumPy-only.

Project description

mneme

A layered semantic cache for LLMs and any expensive function with embeddable input.

mneme (Greek: μνήμη, "memory"; pronounced NEE-mee) is an embeddable, in-process Python library for semantic memoization: cache an expensive function once, return the cached result whenever a later input means the same thing. LLM completions are the canonical use case; the same machinery covers RAG retrievals, translations, classifications, deduplication, and agent memory. It pairs an exact-match layer (normalized query hash) with a semantic-match layer (cosine similarity over L2-normalized embeddings) and persists durably to a single SQLite file by default.

Full documentation: https://anthonynystrom.github.io/mneme/

from mneme import SemanticCache

with SemanticCache(path="cache.db", embedder=my_embedder) as cache:
    hit = cache.get("How do I reset my password?")
    if hit is None:
        response = call_my_llm("How do I reset my password?")
        cache.put("How do I reset my password?", response)
    else:
        response = hit.response

Why

  • Cache before you call. Turn redundant expensive operations - LLM calls, RAG rerankers, paid translation APIs, slow classifiers - into a microsecond dict lookup or a millisecond NumPy matvec. For chatbots, agent loops, classification pipelines, and batch-style scoring, this is the difference between a viable product and one that pays for every paraphrase.
  • One required dependency. NumPy. Optional extras for hnsw, redis, postgres, dynamodb, prometheus, otel. Bring your own embedder, your own LLM client, your own server.
  • In-process, no daemon. A library you import, not a service you operate. Persists to a single SQLite file by default; swap in Redis / Postgres / DynamoDB for cross-host shared state.
  • Strict typing, zero magic. Public surface is a small set of frozen @dataclasses and Protocols. py.typed shipped.

Features

  • Layered cache - O(1) exact match, then cosine similarity over an in-memory matrix
  • Sync + async APIs (SemanticCache, AsyncSemanticCache)
  • 5 Store backends: Memory, SQLite (default), Redis, Postgres, DynamoDB
  • 2 Index backends: NumPy (default; bandwidth-bound exact search, comfortable at typical d=768 to ~500k and at d=384 well past 1M) and hnswlib (opt-in; sub-millisecond approximate search at 1M+)
  • 3 vector dtypes: float32, float16, int8 for memory-constrained deployments
  • 3 multi-process modes: single, stale-tolerant, mmap-shared
  • Multi-tenant via namespaces with per-namespace LRU quotas
  • Calibration tooling (Python API + CLI) for tuning similarity thresholds
  • Checkpoint export/import for backup and environment promotion
  • Re-embed migration tool when the embedder changes
  • Prometheus and OpenTelemetry metrics adapters

Install

pip install mneme-cache                       # core (NumPy only)
pip install "mneme-cache[hnsw]"               # approximate-NN at 1M+ entries
pip install "mneme-cache[redis]"              # RedisStore
pip install "mneme-cache[postgres]"           # PostgresStore
pip install "mneme-cache[dynamodb]"           # DynamoDBStore
pip install "mneme-cache[prometheus,otel]"    # metrics adapters
pip install "mneme-cache[all]"                # everything

Python 3.10+. The distribution is mneme-cache on PyPI; the import name is mneme. See the full install matrix.

Quickstart

from mneme import SemanticCache, MemoryStore

with SemanticCache(store=MemoryStore(), embedder=my_embedder) as cache:
    cache.put("How do I reset my password?", "Click 'Forgot password' on login.")
    hit = cache.get("Where do I reset my password?")  # paraphrase
    assert hit is not None
    print(hit.layer, hit.similarity, hit.response)

For the async API, see Async quickstart. For wrapping an actual LLM call, see Your first cached LLM.

Use cases

The same machinery covers more than LLM caching. Each pattern is the same three lines (cache.get, cache.put, your function); only what your function does changes.

Pattern What it caches
LLM caching Wrap any LLM call so paraphrases hit a microsecond cache instead of a multi-second model
RAG retrieval Top-k chunks behind paraphrased questions; skips the cross-encoder reranker on cache hits
Translation "Source text → translated text" per language pair; cuts billed translation API calls
Semantic deduplication Read Hit.similarity directly to detect near-duplicate content in ingestion pipelines
Classification Cache labels from any classifier (sklearn, fastText, BERT, rules engines)
Agent memory Per-agent task → plan lookup; consistency on similar tasks across runs

Full walkthrough with runnable scripts →

Performance

Apple M4 Max baseline at 100k entries (full table on the docs site):

Operation Latency
Exact-match get ~2.3 ms p99
Semantic get (fp32, d=768) ~2.7 ms p99
put (no eviction) ~0.9 ms p99
Single-thread throughput ~5,700 ops/sec

Documentation

Getting started Sync + async quickstarts, bring your own embedder
Use cases Five patterns: LLM, RAG retrieval, translation, dedup, classification, agent memory
How mneme is different Where mneme makes different choices than other semantic-cache libraries
Concepts Layered cache, embedders, quantization, multi-process, multi-tenant
Stores Memory · SQLite · Redis · Postgres · DynamoDB
Guides Calibration, checkpoints, re-embed migration, metrics, custom stores, perf tuning
API reference Auto-generated from docstrings
Performance Measured baseline against the original targets
Showcase Flask demo covering all 5 use cases against Nemotron on a DGX Spark
Changelog Release notes

Comparison

mneme GPTCache
Required runtime deps NumPy many (faiss, etc.)
Bundled embedder no (BYOE) yes
Bundled LLM client no yes
Sync + async parity yes partial
Strict typing (py.typed) yes no
Multi-process modes 3 n/a
Multi-tenant quotas per-namespace LRU n/a
Calibration tooling yes (CLI + Python API) no

Status

v1.0. Public surface locked; future minor versions are additive. See Changelog.

License

Apache 2.0. See 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

mneme_cache-1.0.0.tar.gz (171.2 kB view details)

Uploaded Source

Built Distribution

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

mneme_cache-1.0.0-py3-none-any.whl (86.0 kB view details)

Uploaded Python 3

File details

Details for the file mneme_cache-1.0.0.tar.gz.

File metadata

  • Download URL: mneme_cache-1.0.0.tar.gz
  • Upload date:
  • Size: 171.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mneme_cache-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ff5e3932b49c72886d89e8c3f89dd4fa319209e24874ad2f5f1378b762349af0
MD5 f19359b4db81a2184b207d8b53305e22
BLAKE2b-256 6d39a5bdaf1ff46b5422bbc090bfcb62ba6efb2a1fff6cca764217b2438311a5

See more details on using hashes here.

Provenance

The following attestation bundles were made for mneme_cache-1.0.0.tar.gz:

Publisher: publish.yml on AnthonyNystrom/mneme

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

File details

Details for the file mneme_cache-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: mneme_cache-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 86.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mneme_cache-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b4f25df5aeeb84adbeac091a5a6a125976c399c7f027b97c573c2ad15bbb247b
MD5 db6a979277f5be9adb9555aa7e3adf70
BLAKE2b-256 e81763d3c48877c88bb804fb319bc02c5058d4343e967fee0ec33f5af8e53708

See more details on using hashes here.

Provenance

The following attestation bundles were made for mneme_cache-1.0.0-py3-none-any.whl:

Publisher: publish.yml on AnthonyNystrom/mneme

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