Skip to main content

Production caching seam for AI workloads (Python mirror): exact-match cache, verified semantic cache, embedding cache, singleflight request coalescing, negative cache, and epoch-based invalidation. In-memory reference default; Redis extra.

Project description

@demystify/platform-cache · demystify-platform-cache

Production caching seam for AI workloads — the layer that makes the Demystify suite fast and cost-efficient without ever trading away correctness. Dual-language leaf package: @demystify/platform-cache (TypeScript) and demystify-platform-cache (Python), with an in-memory reference adapter (keyless, offline, deterministic — the default) and a Redis adapter (integration-marked, opt-in).

Built from the production playbook, not the demo playbook:

  • Real-world semantic-cache hit rates are 20–40%, and the hard problem is false hits — embedding-close is not meaning-equal ("capital of France" ≈ "capital of Germany" in dense-embedding space).
  • A cache amplifies whatever it stores — errors, refusals, and low-groundedness answers must never be admitted.
  • TTLs don't know when your data changed — event-driven (epoch) invalidation does.

What's in the box

Primitive What it does Guarantee
ExactCache + exactKey digest of the full canonical request → value zero false hits by construction
SemanticResponseCache embed → nearest-neighbour ≥ threshold → verify before serve; admission-gate before store verified paraphrase hits
TieredCache exact tier first, then semantic free/safe hits absorbed before any similarity math
withEmbeddingCache content-hash → vector decorator over any batch embedder pure-function cache: can never be wrong
SingleFlight N concurrent identical misses → 1 upstream call thundering-herd guard
NegativeCache short-TTL tombstones for known-empty results bounded staleness
EpochStore monotonic epoch per scope, folded into keys O(1) invalidation on ingest/delete
CacheStats hit/miss/store/reject events + µUSD saved/spent measure the false-hit rate, not just the hit rate

Every namespace is tenant-scoped by the caller and every key carries a version segment (embedder/model/schema/epoch), so version swaps invalidate instead of corrupting.

Quickstart (TypeScript)

import {
  createMemoryCacheBackend,
  HashCacheEmbedder,
  SemanticResponseCache,
  TieredCache,
} from "@demystify/platform-cache";

const backend = createMemoryCacheBackend();
const semantic = new SemanticResponseCache<string>({
  embedder: new HashCacheEmbedder(), // deterministic reference; swap a real embedder in prod
  store: backend.semantic,
  admission: (answer) =>
    answer.startsWith("ERROR") ? { admit: false, reason: "error response" } : { admit: true },
});
const cache = new TieredCache<string>({
  exact: backend.exact,
  semantic,
  keyVersion: `epoch:${await backend.epochs.current("tenant:corpus")}`,
});

const hit = await cache.lookup("tenant-1", requestPayload, questionText);
if (!hit) {
  const answer = await expensiveCall();
  await cache.store("tenant-1", requestPayload, questionText, answer, { costMicroUsd: 1200 });
}

Quickstart (Python)

from demystify_platform_cache import (
    HashCacheEmbedder, MemoryCacheBackend, SemanticResponseCache, TieredCache,
)

backend = MemoryCacheBackend()
semantic = SemanticResponseCache(embedder=HashCacheEmbedder(), store=backend.semantic)
cache = TieredCache(exact=backend.exact, semantic=semantic, key_version="epoch:0")

hit = await cache.lookup("tenant-1", request_payload, question_text)

The two guards (why this is safe to run in production)

  1. Admission — a quality gate before store: never cache errors, tool calls, abstentions, or low-groundedness answers. The gateway and RAG integrations pass their own policies.
  2. Verification — a guard before serve on every semantic candidate. The deterministic reference is content-token overlap (kills the France/Germany class of false hit); real deployments swap a cross-encoder or LLM judge behind the same HitVerifier signature.

Who uses it in the suite

  • dmstfy-gateway — exact + verified-semantic completion cache (DMSTFY_GATEWAY_SEMANTIC_CACHE*, OFF by default), singleflight, saved-µUSD accounting.
  • dmstfy-rag — embedding cache (ingest + query), retrieval cache and answer cache keyed by corpus epoch (every ingest/delete invalidates instantly), groundedness-gated admission (DMSTFY_RAG_CACHE*, OFF by default).
  • dmstfy-cache (module) — make cachebench: the offline proof harness that reports per-tier hit rates, false-hit rate, and µUSD saved, ON vs OFF.

Install

npm add @demystify/platform-cache          # TypeScript
pip install demystify-platform-cache        # Python (redis extra: [redis])

Caches are never the default (CONVENTIONS.md §14.7): every consumer wires them behind an explicit opt-in.

License

MIT © Demystify Systems

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

demystify_platform_cache-0.3.0.tar.gz (106.0 kB view details)

Uploaded Source

Built Distribution

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

demystify_platform_cache-0.3.0-py3-none-any.whl (24.3 kB view details)

Uploaded Python 3

File details

Details for the file demystify_platform_cache-0.3.0.tar.gz.

File metadata

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

File hashes

Hashes for demystify_platform_cache-0.3.0.tar.gz
Algorithm Hash digest
SHA256 39a748222a18b7b52a45cd50164853b90adfe8e73aa34aae0426348a7e35258d
MD5 3139380706923234cc622f3e11575386
BLAKE2b-256 e9e5f3cbe7b5d086f48f6740615ea835dd4ba9f700fa0d4b61a94024fe8e0538

See more details on using hashes here.

Provenance

The following attestation bundles were made for demystify_platform_cache-0.3.0.tar.gz:

Publisher: publish-pypi.yml on demystify-systems/ai-services-tools

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

File details

Details for the file demystify_platform_cache-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for demystify_platform_cache-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 87d205e0da748826cd0290482cf8e5708b5bd5ce110eb2131723a2d3fa18f71b
MD5 e6dfbbca47b2bf4805cde75eeafa2cd5
BLAKE2b-256 f9511aa17849c2751eee1177e62823c96876f66fc50ad47057cba36ec97ae5f8

See more details on using hashes here.

Provenance

The following attestation bundles were made for demystify_platform_cache-0.3.0-py3-none-any.whl:

Publisher: publish-pypi.yml on demystify-systems/ai-services-tools

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