Skip to main content

llm-cache

SQLite-backed LLM response cache. Exact match + fuzzy match. Decorator API. Zero mandatory server dependencies.

Tests Dependencies Python License LinkedIn


Why llm-cache?

Every LLM API call costs money and takes time. In development and testing, you hit the same prompts over and over. In production, users ask the same questions. llm-cache stores responses in a local SQLite database and serves them instantly — no Redis, no server, no external service.

Two match layers:

  • Exact match — SHA-256 hash of the normalised prompt. O(1) lookup.
  • Fuzzy match — token overlap similarity for near-identical prompts. Catches rephrasings without embeddings.

Install

pip install llm-cache

Quick start

from llm_cache import LLMCache

cache = LLMCache()  # stores in .llm_cache.db

# Decorator — wraps any LLM call function
@cache.cached(model="claude-opus-4-5")
def ask(prompt: str) -> str:
    return client.messages.create(
        model="claude-opus-4-5",
        messages=[{"role": "user", "content": prompt}],
    ).content[0].text

ask("What is RAG?")   # hits API first time
ask("What is RAG?")   # served from cache instantly — no API call
ask("What is rag?")   # fuzzy match — also served from cache

Direct API

from llm_cache import LLMCache, MatchType

cache = LLMCache("myapp.db", fuzzy_threshold=0.85, ttl_seconds=86400)

# Get
response, match = cache.get("What is RAG?")
if match == MatchType.MISS:
    response = call_api("What is RAG?")
    cache.set("What is RAG?", response, model="claude-opus-4-5", tags=["qa"])

# Match types
# MatchType.EXACT  — identical prompt
# MatchType.FUZZY  — near-match above threshold
# MatchType.MISS   — not cached

# Stats
print(cache.stats())
# CacheStats(entries=142, hit_rate=73%, tokens_saved≈48,200)

# Management
cache.delete(tags=["qa"])              # delete by tag
cache.delete(older_than_seconds=3600)  # expire old entries
cache.delete(model="claude-opus-4-5") # delete by model
cache.clear()                          # wipe everything

Configuration

cache = LLMCache(
    db_path=".llm_cache.db",    # SQLite file path
    fuzzy_threshold=0.85,        # 0-1, how similar prompts must be for fuzzy hit
    ttl_seconds=86400,           # auto-expire entries after N seconds (None = never)
    max_entries=10000,           # evict oldest when cache exceeds this size
)

Cost savings estimate

stats = cache.stats()
print(f"Tokens saved:  {stats.estimated_tokens_saved:,}")
print(f"Estimated saving: ~£{stats.cost_savings_estimate:.2f}")
print(f"Hit rate: {stats.hit_rate:.0%}")

Context manager

with LLMCache("session.db") as cache:
    result, _ = cache.get("my prompt")

Linda Oraegbunam | LinkedIn | Twitter | GitHub

Release files for llm-response-cache 1.0.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 llm-response-cache 1.0.0
File Size Uploaded
llm_response_cache-1.0.0.tar.gz 10.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for llm-response-cache 1.0.0
File Interpreter ABI Platform
llm_response_cache-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 19.9 kB

Release files / llm_response_cache-1.0.0.tar.gz

Download URL llm_response_cache-1.0.0.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
3b7bafabf6c35a8ede1e2339a7b4efb6efd00cfa19770c0a87f96ec454840094
BLAKE2b-256 checksum
How to use checksums
dd6fef83a1a8a21f2e069034ff14ef9b019a9e6d14d93461f869003ab0cbc2b3
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 Apr 12, 2026.

Transparency log

Release files / llm_response_cache-1.0.0-py3-none-any.whl

Download URL llm_response_cache-1.0.0-py3-none-any.whl
Size 9.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c74ef6e51051e0692ca4aae9bdb7cfb22e1bdc302e027b8825feed3cb2e43dc4
BLAKE2b-256 checksum
How to use checksums
e3289158949d1a392fe6a643494c74b45cda61d3366ae971e5d41a3dc57342bd
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 Apr 12, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

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