prompt-cache-optimizer
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_controlbreakpoints 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.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file prompt_cache_optimizer-0.1.0.tar.gz.
File metadata
- Download URL: prompt_cache_optimizer-0.1.0.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2432e7cd696f65957fac4111ae3277aa05c8c19ad1d801c2cfc5956392e8eb06
|
|
| MD5 |
e12afe1cbe8db3e5ce77e83ce613bf6f
|
|
| BLAKE2b-256 |
e9bd4be17e99694c2bcf9d9c189b5864754a3889517cf610eddfe1b818157de8
|
Provenance
The following attestation bundles were made for prompt_cache_optimizer-0.1.0.tar.gz:
Publisher:
publish.yml on Encephos/prompt-cache-optimizer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
prompt_cache_optimizer-0.1.0.tar.gz -
Subject digest:
2432e7cd696f65957fac4111ae3277aa05c8c19ad1d801c2cfc5956392e8eb06 - Sigstore transparency entry: 2684491131
- Sigstore integration time:
-
Permalink:
Encephos/prompt-cache-optimizer@4b05f0e2ecf40bd3871ef5255329b0b7236fc8a4 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/Encephos
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4b05f0e2ecf40bd3871ef5255329b0b7236fc8a4 -
Trigger Event:
release
-
Statement type:
File details
Details for the file prompt_cache_optimizer-0.1.0-py3-none-any.whl.
File metadata
- Download URL: prompt_cache_optimizer-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2d021f99fa1fc8830e5b08fd812063f37ca9748afcad3addb92073788d608afc
|
|
| MD5 |
52fa827ceb826fc272634b8cae1370f2
|
|
| BLAKE2b-256 |
498c64a640f3e84591cc23459cc7c32bf1f24bc183ac0d82590e0d69467ec4f5
|
Provenance
The following attestation bundles were made for prompt_cache_optimizer-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on Encephos/prompt-cache-optimizer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
prompt_cache_optimizer-0.1.0-py3-none-any.whl -
Subject digest:
2d021f99fa1fc8830e5b08fd812063f37ca9748afcad3addb92073788d608afc - Sigstore transparency entry: 2684491230
- Sigstore integration time:
-
Permalink:
Encephos/prompt-cache-optimizer@4b05f0e2ecf40bd3871ef5255329b0b7236fc8a4 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/Encephos
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4b05f0e2ecf40bd3871ef5255329b0b7236fc8a4 -
Trigger Event:
release
-
Statement type: