Skip to main content

bedrockcache

ci pypi python

Audit and fix Anthropic prompt caching when calling Claude on AWS Bedrock through any abstraction stack.

Why

Anthropic prompt caching cuts cost by ~90% on stable prefixes. On the native Anthropic API the SDK applies it automatically. But if you call Claude through Bedrock — directly via boto3, indirectly via LiteLLM, via AnthropicBedrock, as a Strands or pydantic-ai backend — caching breaks silently at one of the abstraction boundaries, and you find out via the bill.

A real Bedrock+LiteLLM stack ran up $37,901 in 9 days on uncached input tokens because each layer "nominally supported" caching but caching was not actually applying. bedrockcache catches that class of bug at request time.

Install

pip install bedrockcache

Audit a request before sending

from bedrockcache import audit, Backend

request = {
    "model": "bedrock/anthropic.claude-sonnet-4-5-v1:0",
    "messages": [{"role": "user", "content": "hi"}],
}
report = audit(request, Backend.LITELLM)
print(report.will_cache)        # False
print(report.errors)            # ['no `cache_control` on any message; LiteLLM has nothing to translate']
print(report.recommendations)   # ['add cache_control={...} either on the message dict or …']

Lock it in CI

def test_my_rag_caches():
    request = build_my_rag_request(query="...")
    audit(request, Backend.BEDROCK_CONVERSE).assert_caches()

assert_caches() raises AssertionError with the specific reason if any layer is dropping the directive.

CLI

$ bedrockcache audit request.json --backend litellm --strict
backend:           litellm
will_cache:        False
breakpoint_count:  0
reasons:
  [error] no `cache_control` on any message; LiteLLM has nothing to translate
recommendations:
  - add `cache_control={'type': 'ephemeral'}` either on the message dict or on the last content sub-item of stable prefixes.
$ echo $?
1

Supported backends

Backend Identifier What it audits
Bedrock Converse API Backend.BEDROCK_CONVERSE cachePoint blocks across system / messages / toolConfig, ≥1024 token segment rule, 4-breakpoint cap
Bedrock InvokeModel + Anthropic body Backend.BEDROCK_INVOKE_ANTHROPIC cache_control blocks, missing anthropic-beta: prompt-caching-2024-07-31 for older Claude families
anthropic.AnthropicBedrock Backend.ANTHROPIC_BEDROCK same as InvokeModel
LiteLLM completion() Backend.LITELLM cache_control placement (message dict vs content), Bedrock-only model check, dual-placement warning
Strands BedrockModel Backend.STRANDS cache_points parameter present; rejects misuse of Anthropic-shape cache_control
pydantic-ai bedrock Backend.PYDANTIC_AI_BEDROCK model_settings.extra_body passthrough; recursive audit of inner Anthropic body

What it explicitly is not

  • Not a router. Not a proxy. Not a generic LLM cost tracker.
  • Bedrock-only by design.
  • Not a replacement for Phoenix or Langfuse.
  • Not coupled to any framework — zero runtime dependencies.

Sibling libraries

Three independent libraries that compose around Anthropic-on-Bedrock:

  • bedrockcache (this) — static auditor for prompt caching.
  • bedrockstack — Bedrock-aware retry policy, cost ledger, streaming-error normalization.
  • ragvitals — 5-dim drift detection for the RAG pipelines these models live inside.

Roadmap

  • v0.2: real Anthropic tokenizer in place of the char-based heuristic.
  • v0.3: Reporter.from_cloudwatch(log_group=...) — sample CloudWatch InvokeModel logs and dollar-ize the cache miss rate.
  • v0.4: suggest_breakpoints(system_prompt, model=...) — recommend optimal placement for long prefixes.

Develop

pip install -e ".[dev]"
pytest -v

License

MIT

Metadata

Release files for bedrockcache 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 bedrockcache 0.1.0
File Size Uploaded Provenance
bedrockcache-0.1.0.tar.gz 10.7 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 21.4 kB

Release files / bedrockcache-0.1.0.tar.gz

Download URL bedrockcache-0.1.0.tar.gz
Size 10.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f5f7cbcecc7548e007bc93b834497c9952e543b5b28e8977a654c7faf7feefbe
BLAKE2b-256 checksum
How to use checksums
44cc3a401d881a147bc6d84685f1c5576b908becfaf26a3deba76bd0e7756df1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

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

Download URL bedrockcache-0.1.0-py3-none-any.whl
Size 10.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5fbdbd49a1105070e618ad791e2b6be62542031547ad4f215ad7e75a447d71b3
BLAKE2b-256 checksum
How to use checksums
e00af25c57e588b692a1ee7d19c0de5f386974a8cd1a6aee9aa61f17a3f5bb03
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Security for bedrockcache 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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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