cachedrift
Your cache hit rate died on a Tuesday. Nobody noticed until the invoice.
Static analyzer for the code patterns that silently kill LLM prompt caching.
pip install cachedrift
Prompt caching cuts LLM input costs by up to 90% — if the request prefix is byte-identical between calls. One nondeterministic byte and every call silently pays full price. No error. HTTP 200. Perfect answer. Just a bigger invoice.
cachedrift scans your source for the recurring shapes of that bug — before you deploy, not after the bill:
$ cachedrift src/
cachedrift 0.1.0 — 42 file(s) scanned
findings: 2 (fail=2 warn=0 info=0)
[FAIL] CD01 src/agent.py:42 — Time call feeding a prompt — the prefix changes every request
system = f"You are helpful. Today is {datetime.now()}"
[FAIL] CD03 src/tools.py:87 — Tools/prompt built from a set — iteration order is not deterministic
for t in {parse, search, write}:
The problem nobody measures
In March 2026 Anthropic quietly changed the default prompt-cache TTL from one
hour to five minutes. Spend reports of 15–20× showed up for weeks before
anyone understood why. The postmortem's list of "the four prefix drifts" reads
like a bug catalog: re-encoded images, tool_choice flips, session-id reseeding,
dict-ordered system prompts. Every one of them is findable statically. None
of them were — until now.
Runtime tools (cachebench, tracer wrappers) tell you your hit rate after it collapsed. cachedrift flags the drift in the code that causes it.
The rules
| Rule | Severity | Pattern | Why the cache dies |
|---|---|---|---|
| CD01 | fail | datetime.now() / time.time() / Date.now in a prompt |
New timestamp = new prefix = new cache entry, every call |
| CD02 | fail | uuid4() / random() / Math.random in a prompt |
Unique id per call — guaranteed miss |
| CD03 | fail | Tool loop over a set literal | Set iteration order is not stable across runs |
| CD04 | warn | Tool list iterated without sorted() |
If ordering ever changes, the prefix breaks |
| CD05 | fail | tool_choice assigned a variable |
Flipping auto/any/forced rewrites the cache key |
| CD06 | warn | b64encode(open(...)) inline |
Re-encoding may produce different bytes per read |
| CD07 | fail | strftime/ISO stamp concatenated into a string |
Cache-buster baked into the template |
| CD08 | warn | json.dumps(...) without sort_keys=True |
Dict order shifts across processes → prefix shifts |
| CD09 | info | .strip() / .rstrip() on a prompt |
Whitespace churn is a prefix change |
| CD10 | info | model = os.environ.get(...) |
Silent model swaps move you to a different cache pool |
Exit codes: 0 clean · 1 warnings · 2 failures · 3 usage error.
Install & use
pip install cachedrift
cachedrift . # scan cwd
cachedrift src/ --format json # CI-ready JSON
cachedrift src/ --min-severity fail # only the hard failures
cachedrift src/ --rules CD01 CD07 # pick rules
cachedrift src/ --fail-on warn # stricter CI gate
CI gate that blocks cache-killers:
- run: pip install cachedrift
- run: cachedrift src/ --min-severity warn --fail-on warn
Why static, why zero-dep
- Deterministic — same tree, same findings. No model decides anything.
- Offline — no API keys, no network, no telemetry.
- Fast — 400k-character adversarial lines scan in milliseconds; the regexes are bounded and regression-tested against catastrophic backtracking.
- Auditable — one file, ~350 lines. Read it in fifteen minutes.
API
import cachedrift as cd
findings = cd.scan_text("agent.py", 'msg = f"now {datetime.now()}"')
for f in findings:
print(f.rule, f.severity, f.line, f.message)
Honest limits
Static analysis sees shapes, not intent. A timestamp that genuinely belongs in the prompt is a finding you suppress, not a false positive to debug. CD04/CD08/CD09 are heuristics — verify, don't panic. Pair cachedrift (prevention) with a runtime hit-ratio tracker (detection) for full coverage.
If cachedrift saved you from a silent 10× bill, drop a star — it's the only metric that keeps this free.
Part of the ZidoCode agent-audit suite — zero-dependency, offline, deterministic tools that audit what AI agents actually do.
Metadata
Release files for cachedrift 0.1.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 |
|---|---|---|---|---|
| cachedrift-0.1.0.tar.gz | 11.0 kB | Without provenance | Details for cachedrift-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cachedrift-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for cachedrift-0.1.0-py3-none-any.whl |
Total release size: 19.8 kB
Release files / cachedrift-0.1.0.tar.gz
| Download URL | cachedrift-0.1.0.tar.gz |
|---|---|
| Size | 11.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
62b3981ba24a321078379f543ecb384189c07b8b7bf8a7ea9e1b059179bc617a
|
|
BLAKE2b-256 checksum How to use checksums |
b757013ae5afe2213f8331b6eac51de79a5fdf27e28fc5e2608f657ed2e387a1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.18 {"installer":{"name":"uv","version":"0.12.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / cachedrift-0.1.0-py3-none-any.whl
| Download URL | cachedrift-0.1.0-py3-none-any.whl |
|---|---|
| Size | 8.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
88956d9f5baf8fe68753661162f9a842af5e427b6a0d4c8a0228ec89e2dd5c10
|
|
BLAKE2b-256 checksum How to use checksums |
7c2749b2278e7376b0f72a19602d6c6b653b91d926daa84a0102ed08b6bcd928
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.18 {"installer":{"name":"uv","version":"0.12.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for cachedrift 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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.