Skip to main content

cached-response

Your test suite asks the model the same questions every run. This replays the answers it already has — and refuses to when anything that matters changed.

No cache server, no required dependencies, one decorator. Python 3.11+.

python -m pip install cached-response
from cached_response import cached_llm_response

@cached_llm_response(mode="testing")
def ask_llm(request):
    return your_model_call(request)

Caching is off unless you turn it on (mode or CACHED_RESPONSE_MODE), so importing this package cannot change production behavior. Sync and async both work, and the answer is stored in a local SQLite file.

Start here: integration guide and rule recipes.

The one idea: finding is clever, approving is not

Two steps, deliberately unequal.

Finding a candidate is allowed to be fuzzy. Ids, timestamps and generated tokens are stripped out, and the remaining skeleton is matched against stored requests. A wrong guess here costs one model call.

Approving it is not fuzzy at all. Every byte outside the fields you declared irrelevant must match exactly. A wrong approval corrupts an answer, so there is no confidence threshold — only an exact guarantee.

That asymmetry is the whole design. Similarity never authorizes reuse.

What it matches, with real examples

These pairs are two runs of one agent test. The cache treats them as the same question:

run 1   "report their current status: t_41dfe03f"
run 2   "report their current status: t_217deef9"

Ids that appear in questions and answers are handled by aliases, not by ignore-rules: each run's ids map to stable handles, and the replayed answer is rendered back with the caller's own ids — so a reused answer talks about your task, never last run's. See test-ID aliases.

run 1   "created_at": 1788907973    "pid": 248801    "current_run_id": 32
run 2   "created_at": 1788908055    "pid": 249064    "current_run_id": 33

Bookkeeping fields are handled by declared metadata — you list the exact paths that cannot affect the answer. Nothing is declared by default, and a recognizable format is never itself a reason: a UUID may be a trace id or a resource target, and only you know which.

And a pair from the same capture that the cache correctly refuses:

run 1   events: [created, claimed, spawned, heartbeat]
run 2   events: [created, claimed, spawned, heartbeat, heartbeat]

One more event arrived. The world changed, so that call runs live. Quantities are the same story: "Listed 4 clusters" is an answer, not noise, and no number-shaped rule will ever be allowed to blur it.

Thinking tokens (Gemini and friends)

Providers that return a thought signature attach a fresh one to every generation and require it echoed back:

run 1   call_346990__thought__EosRCogRARFNMg+G9bvw4j1yvTBrza0H…
run 2   call_470754__thought__ErARCq0RARFNMg/wlv0d7ieH8gwq3Wfc…

Two runs of one conversation are therefore never byte-equal past their first tool call, and a single live turn used to make every later turn in that conversation miss. signed_call_handles=True keys those tokens as stable positional handles for lookup only — the request sent to your provider and the response handed back always carry the real bytes. Signature bytes are never rewritten, never invented, and never matched across.

Measured on a production agent's CI suite: 445 of 2199 model calls served from cache (20%), all exact matches, with fresh task ids and fresh signatures on every repetition.

Self-learning, and its limits

With learning=True, near-miss pairs go to a model judge that answers SAFE, UNSAFE, or UNCERTAIN — and uncertain means miss. A SAFE verdict mints a narrow rule; UNSAFE is remembered too, so the same junk pair is not re-judged forever.

The judge never writes the pattern. Python derives it from the observed difference, bounded to anchored character classes, scoped to declared metadata, and pinned by a hash of everything outside the approved span. A model asked to write a regex will happily generalize Listed 4 clusters into Listed \d+; this design makes that impossible rather than unlikely. Start with learning=False and explicit rules; add the judge once you have measured what it costs. See learning from misses.

Diagnose a miss

Every miss records why. Counters are process-local and content-free; excerpts are redacted unless you explicitly ask for raw text.

from cached_response import cache_misses, cache_stats

print(cache_stats())    # hits, misses, reasons, candidate rejections
print(cache_misses(3))  # newest misses, with the closest candidate and the diff

The package also ships an agent skill for this workflow — inspect real misses, propose narrow rules, measure the result:

cached-response --skill
cached-response --install-skill .agents/skills

Modes

Mode Behavior
disabled (default) Always calls your function. Keep this in production.
conservative Exact input matches only.
testing Exact matches, plus test-ID aliases and declared-metadata review.
risky Same safeguards as testing; retained for compatibility.

A cached answer measures the cache, not the model. Never enable this on a run whose results you intend to trust as a measurement of the model itself — the honest uses are dev loops, retries, and re-runs while you iterate on the code around the model.

cached_staticmethod

Exact-argument caching for pure functions. Always enabled, no mode, no word minimum:

from cached_response import cached_staticmethod

@cached_staticmethod
def square(number):
    return number * number

Caching skips execution, so never wrap something whose side effect or freshness check must happen every call.

Settings

Set on either decorator, or globally with configure() (decorator options win).

Option Meaning
path Where the SQLite database lives. It holds your inputs and results.
namespace, version Separate caches; bump version when hidden behavior changes.
min_words=100 Minimum input length for the LLM decorator.
refresh_start="1d", refresh_force="7d" Age window; refresh chance grows evenly between them (50% at day 4).
metadata_paths, metadata_rules Declare irrelevant fields — reviewed by the judge, or by you.
test_aliases, alias_version Stable handles for fresh per-run ids (guide).
signed_call_handles=False Key provider thought-signature tokens as positional handles.
learning=False Turn off model review; explicit rules still apply.
verifier_model, verifier_timeout, verifier_overrider Who reviews, how long, or replace it entirely.
signature_matching, structural_matching Broaden candidate search only; neither authorizes a hit.
report, diagnostics, diagnostic_text, diagnostic_raw_inputs Reporting and how much miss detail is kept.

Call any wrapped function with use_cache=False to bypass for one call. Storage defaults to a temporary directory the OS may clear — set path to keep it. The database stores original inputs and responses in plain text; redaction applies to diagnostics, not to the cache itself.

Runnable examples · All settings and contracts · Numeric test metadata

Metadata

Release files for cached-response 0.2.1

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

Source distribution (sdist)

Source distribution for cached-response 0.2.1
File Size Uploaded Provenance Actions
cached_response-0.2.1.tar.gz 105.2 kB With provenance Details for cached_response-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cached-response 0.2.1
File Interpreter ABI Platform Actions
cached_response-0.2.1-py3-none-any.whl Python 3 none any Details for cached_response-0.2.1-py3-none-any.whl

Total release size: 158.5 kB

Release files / cached_response-0.2.1.tar.gz

Download URL cached_response-0.2.1.tar.gz
Size 105.2 kB
Tags Source
SHA-256 checksum
How to use checksums
5bab57ae03e82d2e3685136585fa9c9125bf583e6f7a288eecb795b9956c2cb1
BLAKE2b-256 checksum
How to use checksums
d75878d90e4b0f80f743abd9f5ea0a266085321e7e4a035fb65574a31aa0232f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 14, 2026.

Transparency log

Release files / cached_response-0.2.1-py3-none-any.whl

Download URL cached_response-0.2.1-py3-none-any.whl
Size 53.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f4b687699b9673a44bf0f9b2df3c2d119a42abea68c0c300b40884c6a15e8e51
BLAKE2b-256 checksum
How to use checksums
993f85ed35b163a8061604b10f0c57c275b8c2add1cb75a51b963d0dd294bcd6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Sep 14, 2026.

Transparency log

Security for cached-response 0.2.1

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

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for cached_response-0.2.1.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 Sep 14, 2026.

Transparency log

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.

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