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.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cached_response-0.2.0.tar.gz | 104.0 kB | With provenance | Details for cached_response-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cached_response-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for cached_response-0.2.0-py3-none-any.whl |
Total release size: 156.8 kB
Release files / cached_response-0.2.0.tar.gz
| Download URL | cached_response-0.2.0.tar.gz |
|---|---|
| Size | 104.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
144cc31388da0a699a5508492e79b16a49ebf74cabb098b1bfcc57a8a5993929
|
|
BLAKE2b-256 checksum How to use checksums |
a44b556e5c51584beff2abf29f88e6ac5da90aa46d4d2ae02bf81ce85e21a2e5
|
| 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 logRelease files / cached_response-0.2.0-py3-none-any.whl
| Download URL | cached_response-0.2.0-py3-none-any.whl |
|---|---|
| Size | 52.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
433550f1009e491c3962b144bc1e587dea1f3302f42b39065e7f2cf887dafe20
|
|
BLAKE2b-256 checksum How to use checksums |
b496842f6bf11aaf2ee036735f99257d787a6301ceb1677e9565e2fd1bc60e2a
|
| 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 logSecurity for cached-response 0.2.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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for cached_response-0.2.0.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 logA 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.