@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)
- 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.
- 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
HitVerifiersignature.
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
Metadata
Release files for demystify-platform-cache 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| demystify_platform_cache-0.3.0.tar.gz | 106.0 kB | With provenance | Details for demystify_platform_cache-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| demystify_platform_cache-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for demystify_platform_cache-0.3.0-py3-none-any.whl |
Total release size: 130.3 kB
Release files / demystify_platform_cache-0.3.0.tar.gz
| Download URL | demystify_platform_cache-0.3.0.tar.gz |
|---|---|
| Size | 106.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
39a748222a18b7b52a45cd50164853b90adfe8e73aa34aae0426348a7e35258d
|
|
BLAKE2b-256 checksum How to use checksums |
e9e5f3cbe7b5d086f48f6740615ea835dd4ba9f700fa0d4b61a94024fe8e0538
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 17, 2026.
Transparency logRelease files / demystify_platform_cache-0.3.0-py3-none-any.whl
| Download URL | demystify_platform_cache-0.3.0-py3-none-any.whl |
|---|---|
| Size | 24.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
87d205e0da748826cd0290482cf8e5708b5bd5ce110eb2131723a2d3fa18f71b
|
|
BLAKE2b-256 checksum How to use checksums |
f9511aa17849c2751eee1177e62823c96876f66fc50ad47057cba36ec97ae5f8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 17, 2026.
Transparency logSecurity for demystify-platform-cache 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for demystify_platform_cache-0.3.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 17, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.