GlyphCache
Deterministic exact and Hyper-Glyph-inspired semantic caching for LLM responses.
Package name:
glyphcache
Project name: GlyphCache
Install:pip install glyphcache
Import:from glyphcache import PromptCache
GlyphCache is a lightweight, offline-first LLM response cache combining deterministic exact matching with an opt-in Hyper-Glyph-inspired semantic signature index.
It requires no embedding API, vector database, external cache server, or model download.
GlyphCache combines:
- Authoritative exact matching over the complete normalized LLM request.
- Provider-neutral requests with messages, tools, schemas, model parameters, tenant, namespace, and application cache version.
- Deterministic Hash-HDC signatures derived directly from cryptographic hash output rather than model embeddings.
- Immutable semantic prototypes with compact sparse XOR residual encoding.
- LSH candidate filtering that keeps full similarity comparisons bounded.
- Conservative semantic guards for numbers, money, dates, URLs, email addresses, UUIDs, paths, quoted text, and identifiers.
- Memory and SQLite backends with TTL and LRU/size eviction.
- Synchronous and asynchronous APIs with in-process single-flight handling.
- SQLite generation leases for cross-process duplicate suppression.
- Cacheability and redaction hooks for application-specific safety.
- Statistics and event callbacks without raw prompt content by default.
- A small CLI for initialization, inspection, maintenance, health checks, and benchmarks.
- A typed Python API designed for extension through backend, serializer, and encoder protocols.
Before / After
Without GlyphCache, every request reaches the model:
response = call_model(request)
With GlyphCache, compatible cached responses bypass the model call:
from glyphcache import Message, PromptCache, PromptRequest, SQLiteBackend
cache = PromptCache(SQLiteBackend("glyphcache.db"))
request = PromptRequest(
provider="my-provider",
model="my-model",
messages=(
Message(role="system", content="Answer clearly and concisely."),
Message(role="user", content="Explain hyperdimensional computing."),
),
params={"temperature": 0, "max_tokens": 500},
)
response = cache.get_or_set(
request,
producer=lambda: call_model(request),
ttl_seconds=86_400,
)
The core job is simple:
Complete request
-> deterministic exact key
-> compatible cached response
Exact miss and semantic mode enabled
-> strict semantic scope
-> compact deterministic signature
-> bounded candidates
-> safety guards and acceptance policy
-> optional semantic response
The original prompt is never reconstructed from the semantic representation. The exact request hash remains separate and authoritative.
Why Not Just an Embedding Cache?
Embedding caches typically require an embedding API or local model, a vector index, and operational choices around model versions and distance thresholds. GlyphCache uses a different representation: normalized prompt features are bundled into a deterministic bit-packed signature, grouped around immutable prototypes, and filtered through explicit safety controls.
That makes GlyphCache useful when you want a small, inspectable, offline cache layer. It does not claim to understand language better than neural embeddings, and semantic reuse is never treated as proof that two requests are equivalent.
Architecture
+----------------------+
| PromptRequest |
+----------+-----------+
|
+----------v-----------+
| Canonical normalizer |
+------+---------+-----+
| |
exact key| |semantic scope
| |
+----------v--+ +--v----------------+
| Exact lookup | | Hash-HDC encoder |
+-------+------+ +--------+---------+
| |
hit| bit signature
| |
| +--------v---------+
| | Prototype codec |
| +--------+---------+
| |
| +--------v---------+
| | LSH candidates |
| +--------+---------+
| |
| +--------v---------+
| | Safety guards |
| +--------+---------+
| |
+---------+---------+
|
Cached response
The exact key includes provider, model, messages, tool definitions, structured-output schema, generation parameters, namespace, tenant, and application cache version. Semantic comparisons remain inside a stricter scope that also fixes system/developer prompts, encoder version, and safety-policy version.
Install
pip install glyphcache
For optional Zstandard compression support:
pip install "glyphcache[compression]"
For documentation dependencies:
pip install "glyphcache[docs]"
For development:
pip install -e ".[dev,compression,docs]"
pytest
python -m build
Quick Start
Exact Cache
from glyphcache import MemoryBackend, Message, PromptCache, PromptRequest
cache = PromptCache(MemoryBackend())
request = PromptRequest(
provider="example",
model="example-model",
messages=(Message("user", "What is HDC?"),),
params={"temperature": 0},
)
hit = cache.get(request)
if hit is None:
response = call_model(request)
cache.set(request, response)
else:
response = hit.value
Exact matching is enabled by default and always checked first.
Semantic Cache
from glyphcache import CacheConfig, SemanticConfig
config = CacheConfig(
semantic=SemanticConfig(
enabled=True,
similarity_threshold=0.94,
ambiguity_margin=0.025,
require_anchor_match=True,
)
)
cache = PromptCache(MemoryBackend(), config)
Semantic caching is opt-in. By default, it only considers single-user-turn, low-temperature requests without tools or tool messages.
Async Cache
response = await cache.aget_or_set(
request,
producer=lambda: call_model_async(request),
ttl_seconds=86_400,
)
Multi-Tenant Isolation
request = PromptRequest(
provider="example",
model="example-model",
messages=(Message("user", "Summarize my account."),),
namespace="support",
tenant_id="customer-42",
)
Tenant and namespace participate in exact keys and semantic scopes, so entries cannot cross those boundaries.
Cacheability Hooks
cache = PromptCache(
MemoryBackend(),
should_cache_request=lambda request: request.namespace != "sensitive",
should_cache_response=lambda request, response: not contains_secret(response),
redact_metadata=lambda request, metadata: {"trace_id": metadata.get("trace_id")},
)
GlyphCache cannot automatically recognize every secret, permission boundary, one-time code, medical or legal response, or side effect. Applications must use these hooks where appropriate.
CLI
Initialize a SQLite cache:
glyphcache init ./glyphcache.db
Inspect cache totals:
glyphcache stats ./glyphcache.db
Inspect an entry with the response redacted:
glyphcache inspect ./glyphcache.db --entry ENTRY_ID
Explicitly show stored response bytes:
glyphcache inspect ./glyphcache.db --entry ENTRY_ID --show-response
Purge expired entries or a tenant namespace:
glyphcache purge ./glyphcache.db --expired
glyphcache purge ./glyphcache.db --namespace support --tenant customer-42
Run maintenance and health checks:
glyphcache optimize ./glyphcache.db
glyphcache vacuum ./glyphcache.db
glyphcache doctor ./glyphcache.db
Run a local storage benchmark:
glyphcache benchmark --entries 100000 --database ./benchmark.db
Main Features
1. Deterministic Exact Keys
Canonicalization normalizes NFKC Unicode, CRLF/CR line endings, insignificant trailing line whitespace, and mapping insertion order. It rejects NaN and infinity. Output-affecting fields still change the key.
from glyphcache.canonical import exact_cache_key
key = exact_cache_key(request)
2. Hash-HDC Signatures
The default semantic encoder derives bipolar feature vectors directly from SHAKE-256 output. It is role-aware, message-position-aware, token-block-aware, deterministic across processes, and requires no downloaded model.
A default 4,096-bit signature occupies 512 raw bytes before prototype-residual encoding.
3. Prototype-Residual Encoding
Each semantic scope keeps a bounded set of immutable signatures as prototypes. When a signature is close to a prototype, GlyphCache stores the differing bit indices as delta-varints. Dense residuals fall back to the raw signature.
signature: 101101001110...
prototype: 101100001110...
xor: 000001000000...
residual: [5]
4. Conservative Safety
A semantic hit must satisfy:
- The same strict semantic scope.
- The configured anchor policy.
- A non-expired and non-rejected entry.
- The minimum similarity threshold.
- The ambiguity margin over the second-best candidate.
Hard negatives such as Refund order 123 versus Refund order 124, Python
versions, CVE identifiers, invoice IDs, and currency amounts are expected to
miss under the default guard policy.
5. SQLite Persistence
The standard-library SQLite backend enables WAL mode, foreign keys, a bounded busy timeout, short transactions, LRU/size eviction, integrity checks, prototype storage, LSH rows, and expiring generation leases.
SQLite is intended for local disk, not arbitrary shared network filesystems.
6. Single-Flight Protection
Concurrent misses for the same exact key share one producer call inside a process. SQLite-backed synchronous producers also use expiring cross-process leases and never hold a write transaction while the model call runs.
7. Statistics and Events
stats = cache.statistics()
print(stats.exact_hits)
print(stats.semantic_hits)
print(stats.misses)
print(stats.stored_bytes)
Events contain IDs, timing, match type, and rejection reason rather than raw prompt content by default.
Configuration
from glyphcache import CacheConfig, SemanticConfig
config = CacheConfig(
default_ttl_seconds=86_400,
max_entries=100_000,
max_bytes=1_073_741_824,
store_request_body=False,
compress_values=True,
busy_timeout_ms=5_000,
lease_seconds=120,
semantic=SemanticConfig(
enabled=False,
dimension=4096,
token_block_size=16,
max_features=4096,
similarity_threshold=0.94,
ambiguity_margin=0.025,
prototype_threshold=0.78,
max_candidates=128,
max_prototypes_per_scope=32,
lsh_bands=8,
lsh_bits_per_band=16,
require_anchor_match=True,
single_turn_only=True,
allow_tools=False,
maximum_temperature=0.2,
seed=42,
),
)
Key settings:
default_ttl_secondscontrols expiry when a write does not supply a TTL.max_entriesandmax_bytesbound backend storage.similarity_thresholdis the minimum accepted semantic similarity.ambiguity_marginrejects a best match that is too close to the runner-up.prototype_thresholdcontrols assignment to an existing prototype.max_candidatesprovides a hard bound on full similarity comparisons.require_anchor_matchprotects numeric and identifier-sensitive prompts.single_turn_onlykeeps multi-turn semantic reuse disabled by default.allow_toolskeeps tool-bearing requests excluded unless explicitly enabled.seedmakes signature and LSH generation deterministic.
The default threshold is a starting point, not a universal constant. Measure precision and false-hit rate on representative application data.
Benchmarking
The repository includes:
python benchmarks/benchmark_exact.py --entries 10000
python benchmarks/benchmark_semantic.py
python benchmarks/benchmark_storage.py
Semantic evaluation should report precision, recall, F1, false-hit rate, candidate counts, signature time, lookup percentiles, prototype compression rate, and raw-signature fallback rate. Performance reports should identify the hardware, Python version, operating system, concurrency, database warmth, and dataset.
The test suite currently covers exact caching, TTL, semantic retrieval, hard negative guards, signature and codec behavior, SQLite WAL/integrity, eviction, hooks, CLI operations, and sync/async single-flight handling.
Project Structure
src/glyphcache/
__init__.py # Stable public API
__about__.py # Runtime package version
cache.py # PromptCache lookup, write, and single-flight flow
canonical.py # Canonical requests, exact keys, semantic scopes
cli.py # Command-line interface
config.py # CacheConfig and SemanticConfig
decorators.py # Function caching decorator
exceptions.py # Package exceptions
guards.py # Numeric and identifier anchor extraction
models.py # Typed public and internal data models
policies.py # Semantic eligibility policy
statistics.py # Runtime statistics collector
backends/
base.py # Backend protocol
memory.py # In-process backend
sqlite.py # SQLite backend and schema
codec/
prototypes.py # Raw and prototype-XOR signature codec
signature.py # Hamming similarity
varint.py # Varint and delta-index encoding
encoders/
base.py # Encoder protocol
hash_hdc.py # Deterministic Hash-HDC encoder
indexes/
lsh.py # Deterministic LSH bands
serializers/
base.py # Serializer protocol
json.py # JSON, text, and bytes serializer
py.typed # Typing marker
tests/
test_*.py # Unit, safety, storage, and concurrency tests
docs/
index.md # Documentation home
concepts.md # Exact and semantic concepts
exact-cache.md # Exact key behavior
semantic-cache.md # Semantic pipeline
safety.md # Risks and safety policy
configuration.md # Configuration reference
backends.md # Backend notes
cli.md # CLI reference
benchmarks.md # Benchmark methodology
examples/
basic_cache.py # Exact in-memory example
async_cache.py # Async single-flight example
semantic_cache.py # Semantic cache example
multitenant_cache.py # Tenant isolation example
custom_serializer.py # Serializer extension example
benchmarks/
benchmark_exact.py # Exact-cache throughput and latency
benchmark_semantic.py # Paraphrase and hard-negative evaluation
benchmark_storage.py # SQLite byte accounting
glyphcache.png # Project logo
pyproject.toml # Package metadata and dependencies
CHANGELOG.md # Release history
CONTRIBUTING.md # Contribution guide
RELEASE.md # Release checklist
LICENSE # MIT license
Development
# Install development, compression, and documentation extras
pip install -e ".[dev,compression,docs]"
# Run tests with the release coverage threshold
pytest --cov=glyphcache --cov-report=term-missing --cov-fail-under=90
# Run linting and formatting checks
ruff check .
ruff format --check .
# Type-check package code
mypy src/glyphcache
# Build and validate distributions
python -m build
twine check dist/*
Security and Limitations
GlyphCache stores response payloads in plaintext unless the application places its SQLite database on encrypted storage. It does not implement encryption, distributed consistency, a hosted cache server, GPU indexing, automatic provider monkey-patching, cross-model reuse, or automatic tool-execution caching.
Semantic caching can return an incorrect response when application-specific differences are not captured by scope or anchors. Keep it disabled for risky request classes and favor false misses over false hits.
License
MIT
Contributing
Contributions are welcome. Open an issue or pull request with the request shape, backend, configuration, expected cache behavior, hard-negative examples, and benchmark or correctness evidence used to evaluate the change.
Citation
If you use GlyphCache in research, please cite:
@software{GlyphCache2026,
title={GlyphCache: Deterministic Exact and Hyper-Glyph-Inspired Semantic Caching for LLM Responses},
author={Robert McMenemy},
year={2026},
version={1.0.0},
url={https://github.com/Arkay92/GlyphCache},
}
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 glyphcache-1.0.0.tar.gz.
File metadata
- Download URL: glyphcache-1.0.0.tar.gz
- Upload date:
- Size: 2.0 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
57f0777a0a7f484a581368b28549ef80a67c3b86f9fec41bdcc499ef0624324a
|
|
| MD5 |
ee82ccf4ca003b7f9772dad6e224150a
|
|
| BLAKE2b-256 |
f162af5f595c0d19d0348e6b4c8f656da559df2cc296535da4819c0a859f45d7
|
Provenance
The following attestation bundles were made for glyphcache-1.0.0.tar.gz:
Publisher:
publish.yml on Arkay92/GlyphCache
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
glyphcache-1.0.0.tar.gz -
Subject digest:
57f0777a0a7f484a581368b28549ef80a67c3b86f9fec41bdcc499ef0624324a - Sigstore transparency entry: 2233034776
- Sigstore integration time:
-
Permalink:
Arkay92/GlyphCache@86dd801f876bde58bb4eedaa067d0c598015b7eb -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/Arkay92
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@86dd801f876bde58bb4eedaa067d0c598015b7eb -
Trigger Event:
release
-
Statement type:
File details
Details for the file glyphcache-1.0.0-py3-none-any.whl.
File metadata
- Download URL: glyphcache-1.0.0-py3-none-any.whl
- Upload date:
- Size: 31.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60099940e3718d7dd4ae51548db09880015186ed60b9c0e9e9addba68f6c4e87
|
|
| MD5 |
fad681ee049cec9c42749215ce4db1e6
|
|
| BLAKE2b-256 |
2648df0711e681ca7649b042176121e8d0ce53a6f5d441474ef357b6822e747e
|
Provenance
The following attestation bundles were made for glyphcache-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on Arkay92/GlyphCache
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
glyphcache-1.0.0-py3-none-any.whl -
Subject digest:
60099940e3718d7dd4ae51548db09880015186ed60b9c0e9e9addba68f6c4e87 - Sigstore transparency entry: 2233035650
- Sigstore integration time:
-
Permalink:
Arkay92/GlyphCache@86dd801f876bde58bb4eedaa067d0c598015b7eb -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/Arkay92
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@86dd801f876bde58bb4eedaa067d0c598015b7eb -
Trigger Event:
release
-
Statement type: