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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prompt_cache_optimizer-0.1.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

prompt_cache_optimizer-0.1.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

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

Hashes for prompt_cache_optimizer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2432e7cd696f65957fac4111ae3277aa05c8c19ad1d801c2cfc5956392e8eb06
MD5 e12afe1cbe8db3e5ce77e83ce613bf6f
BLAKE2b-256 e9bd4be17e99694c2bcf9d9c189b5864754a3889517cf610eddfe1b818157de8

See more details on using hashes here.

Provenance

The following attestation bundles were made for prompt_cache_optimizer-0.1.0.tar.gz:

Publisher: publish.yml on Encephos/prompt-cache-optimizer

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file prompt_cache_optimizer-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for prompt_cache_optimizer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2d021f99fa1fc8830e5b08fd812063f37ca9748afcad3addb92073788d608afc
MD5 52fa827ceb826fc272634b8cae1370f2
BLAKE2b-256 498c64a640f3e84591cc23459cc7c32bf1f24bc183ac0d82590e0d69467ec4f5

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

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