Skip to main content

Stable semantic cache keys for LLM requests. Invariant to whitespace, casing, and key ordering; sensitive to model swaps, tool list, and retrieval context. Python port of @mukundakatta/semantic-cache-key.

Project description

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.

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

semantic_cache_key-0.1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

semantic_cache_key-0.1.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file semantic_cache_key-0.1.0.tar.gz.

File metadata

  • Download URL: semantic_cache_key-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for semantic_cache_key-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9ffd02ed8b7daf1bd2056c5d25ac564a5d50f2b8a52a3a38fc647493e88d29ea
MD5 f2f4b931613b5087e10231749ec3d0db
BLAKE2b-256 5b27f21cbc843f3f4755ebc101aec919c42b45f40b641a1301a86e67f6817632

See more details on using hashes here.

File details

Details for the file semantic_cache_key-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for semantic_cache_key-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 854c514706adc117b3dee202111ab8fc0e7237eef1fa9fa98a5282975e6bb78e
MD5 0b4480d237f4638602f2ff37827001c7
BLAKE2b-256 9320caa4e9879da9dc00b642135c2bffae7440cf3b5e09b825d2f1a5866f2170

See more details on using hashes here.

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