Skip to main content

semantic-cache-key

PyPI Python License: MIT

Stable cache keys for LLM requests. Invariant to harmless variation (whitespace, casing, dict key order); sensitive to material changes (model swap, tool list, retrieval context). Pure stdlib, zero runtime dependencies.

Python port of @mukundakatta/semantic-cache-key -- the canonical-form algorithm matches exactly so a JS frontend and a Python backend can share the same cache.

Install

pip install semantic-cache-key

Usage

from semantic_cache_key import key

cache_key = key(
    prompt="Summarize this article in three bullet points.",
    model="claude-sonnet-4-5",
    tools=[{"name": "fetch_url"}, {"name": "read_pdf"}],
    retrieval_context=["doc-id-42", "doc-id-7"],
    system="You are a careful research assistant.",
    version="v1",          # bump to invalidate every cache entry
)
# -> '8d3f7b91c2a04e6f' (16-char hex by default; pass length=64 for full sha256)

Two prompts that differ only in whitespace / casing produce the same key:

key("Hello,   World!", "gpt-5") == key("hello, world!", "gpt-5")  # True

Swap the model, the tools, or the retrieval context and the key changes:

key("p", "gpt-5") != key("p", "claude-sonnet-4-5")              # True

API

Function Purpose
key(prompt, model, *, tools=, retrieval_context=, system=, version="v1", length=16) The Pythonic short-key form.
semantic_cache_key(request: dict) JS-compatible -- takes a single dict, returns the full 64-char hash.
normalize_text(s) Lowercase + collapse whitespace + trim (the canonical-form primitive).
stable_value(v) Recursively sort dict keys for deterministic JSON.

See the JS sibling's README for the design notes and examples in cross-runtime caches.

Metadata

Release files for semantic-cache-key 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for semantic-cache-key 0.1.0
File Size Uploaded Provenance Actions
semantic_cache_key-0.1.0.tar.gz 6.1 kB Without provenance Details for semantic_cache_key-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for semantic-cache-key 0.1.0
File Interpreter ABI Platform Actions
semantic_cache_key-0.1.0-py3-none-any.whl Python 3 none any Details for semantic_cache_key-0.1.0-py3-none-any.whl

Total release size: 12.5 kB

Release files / semantic_cache_key-0.1.0.tar.gz

Download URL semantic_cache_key-0.1.0.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
9ffd02ed8b7daf1bd2056c5d25ac564a5d50f2b8a52a3a38fc647493e88d29ea
BLAKE2b-256 checksum
How to use checksums
5b27f21cbc843f3f4755ebc101aec919c42b45f40b641a1301a86e67f6817632
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / semantic_cache_key-0.1.0-py3-none-any.whl

Download URL semantic_cache_key-0.1.0-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
854c514706adc117b3dee202111ab8fc0e7237eef1fa9fa98a5282975e6bb78e
BLAKE2b-256 checksum
How to use checksums
9320caa4e9879da9dc00b642135c2bffae7440cf3b5e09b825d2f1a5866f2170
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Security for semantic-cache-key 0.1.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A 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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page