Skip to main content

cached-response

Save function results on disk and reuse them. No cache server or required dependencies. Python 3.11+.

python -m pip install cached-response

cached_llm_response

Reuse LLM answers during repeated tests. Add an import and a decorator to your existing function:

from cached_response import cached_llm_response

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

Return text or JSON data. Regular and async functions work. Inputs need at least 100 words; matching calls reuse the answer and print the cache-hit percentage.

Mode What it does
disabled (default) Always calls your function. Keep this in production.
conservative Matches some changing UUIDs, timestamps, and generated IDs.
testing Matches more ID formats and supports LLM-approved matching rules.
risky Also learns patterns from rare generated-looking words.

Learning and optional override

Testing mode includes a verification prompt. For a function taking one chat-request dictionary with messages, the package uses your existing model function to check whether a proposed matching rule is safe. Supported HTTP handlers work too. Approved rules are saved; later matches need no extra judge call. No prompt or hook is required. Unusable replies fall back to a fresh answer.

To replace the built-in check, optionally use:

@cached_llm_response(mode="testing", verifier_overrider=my_verifier)

Your override receives the evidence and default instructions. Return a dictionary with safe_to_reuse (true or false) and reason (text). You can change the prompt or use another model inside it. Override example.

Matching can be wrong, and old answers can be stale—such as yesterday's weather. Learning also costs model calls. A replay does not test the model again.

cached_staticmethod

Reuse a result only when the arguments match exactly. Always enabled; no mode or word minimum. No class is needed.

from cached_response import cached_staticmethod

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

Works with regular and async functions. Inside a class, put @staticmethod above it. Use it for pure functions: caching skips execution, so it must not skip a needed action or a check for fresh data.

Storage, expiry, and settings

Both decorators store inputs and results in a local SQLite file. On Unix this is usually /tmp/cached-response-<uid>/cache.sqlite3; on Windows it is cached-response/cache.sqlite3 inside your user's temporary directory. Temporary files may be deleted by the operating system. Use path for persistent storage.

Set options directly on either decorator:

@cached_staticmethod(path="cache.sqlite3", refresh_start="1d", refresh_force="7d")
Option Meaning
path Where to store the database. It contains your inputs and results.
refresh_start="1d" Start occasionally running the function again after one day.
refresh_force="7d" Always run it again once the saved result is seven days old.
report=False Hide cache statistics. Reporting is on by default.
namespace="my-app" Keep separate applications or users' caches apart.
version="2" Stop reusing old results when a hidden dependency changes.
min_words=100 Minimum input length for the LLM decorator only.
learning=False Turn off rule learning for the LLM decorator.
verifier_overrider=... Replace the built-in LLM verification function.

Between days 1 and 7, the chance of refresh grows evenly: at day 4 it is 50%. Age starts from when the result was generated; a cache hit does not reset it. Durations accept seconds or strings such as "30m", "12h", and "7d". Call either function with use_cache=False to skip caching for that call. There is no automatic limit on total disk usage.

Runnable examples · All settings

Metadata

Release files for cached-response 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 cached-response 0.1.0
File Size Uploaded Provenance Actions
cached_response-0.1.0.tar.gz 48.5 kB With provenance Details for cached_response-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 76.9 kB

Release files / cached_response-0.1.0.tar.gz

Download URL cached_response-0.1.0.tar.gz
Size 48.5 kB
Tags Source
SHA-256 checksum
How to use checksums
db4306d20dba1feb4f255469ac891133eabf7b1fd49d6ba827605f1c1557e898
BLAKE2b-256 checksum
How to use checksums
65dea928d0dccf98905156aa8f9581ca7b1166c5eb6c6600a29a180f524b545a
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 7, 2026.

Transparency log

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

Download URL cached_response-0.1.0-py3-none-any.whl
Size 28.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fe2118779dd99f3ddb3efe4c6ea22bee807544df509dcd995f0d78d79e701270
BLAKE2b-256 checksum
How to use checksums
339f4eb5baa2b4cf9415b03500182247249acac29626cd749643c9a888c7e2f3
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 7, 2026.

Transparency log

Security for cached-response 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.

Full provenance

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

Select from 2 files

Attestation for cached_response-0.1.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 7, 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