Skip to main content

prompt-cache-optimizer

PyPI version License: MIT

A zero-dependency Python micro-tool to structure LLM payloads for maximum prompt caching hits (OpenAI & Anthropic). Save up to 90% on API costs by ensuring your static contexts are perfectly formatted for caching.

The Problem

LLM providers like Anthropic and OpenAI now offer Prompt Caching, which drastically reduces costs for long prompts (like RAG documents or large system instructions). However, to trigger the cache, your payload must be strictly organized:

  • Static content must be grouped perfectly at the front (Prefix Caching).
  • Anthropic requires explicit cache_control breakpoints injected into specific blocks.

Manually managing this array structure in your code leads to messy boilerplate and missed cache hits.

The Solution

prompt-cache-optimizer is a pure standard library tool that takes your system prompt, your RAG documents, and your chat history, and automatically formats them into the perfect JSON payload required by either OpenAI or Anthropic to guarantee maximum cache utilization.

Installation

pip install prompt-cache-optimizer

Usage

from prompt_cache_optimizer import build_optimized_prompt
import anthropic

# Your heavy, static RAG context
docs = ["Long document 1...", "Long document 2..."]
history = [{"role": "user", "content": "What is in the docs?"}]

# Build the cache-optimized payload
payload = build_optimized_prompt(
    system_instruction="You are a helpful RAG assistant.",
    rag_documents=docs,
    chat_history=history,
    provider="anthropic" # or "openai"
)

# Unpack directly into the official SDK!
client = anthropic.Anthropic()
response = client.messages.create(
    model="claude-3-5-sonnet-20240620",
    max_tokens=1024,
    **payload  # Automatically handles the `system` and `messages` arrays
)

Why zero-dependency?

If you just want to save API costs, you shouldn't have to install heavy frameworks like LangChain or LlamaIndex. This micro-tool operates in under 1ms, adds zero bloat to your deployment, and integrates seamlessly with the official openai and anthropic Python SDKs.

Metadata

Release files for prompt-cache-optimizer 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 prompt-cache-optimizer 0.1.0
File Size Uploaded Provenance Actions
prompt_cache_optimizer-0.1.0.tar.gz 4.5 kB With provenance Details for prompt_cache_optimizer-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for prompt-cache-optimizer 0.1.0
File Interpreter ABI Platform Actions
prompt_cache_optimizer-0.1.0-py3-none-any.whl Python 3 none any Details for prompt_cache_optimizer-0.1.0-py3-none-any.whl

Total release size: 9.0 kB

Release files / prompt_cache_optimizer-0.1.0.tar.gz

Download URL prompt_cache_optimizer-0.1.0.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
2432e7cd696f65957fac4111ae3277aa05c8c19ad1d801c2cfc5956392e8eb06
BLAKE2b-256 checksum
How to use checksums
e9bd4be17e99694c2bcf9d9c189b5864754a3889517cf610eddfe1b818157de8
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 2, 2026.

Transparency log

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

Download URL prompt_cache_optimizer-0.1.0-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2d021f99fa1fc8830e5b08fd812063f37ca9748afcad3addb92073788d608afc
BLAKE2b-256 checksum
How to use checksums
498c64a640f3e84591cc23459cc7c32bf1f24bc183ac0d82590e0d69467ec4f5
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 2, 2026.

Transparency log

Security for prompt-cache-optimizer 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

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for prompt_cache_optimizer-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 2, 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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full 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