Skip to content

How Headroom CacheAligner Maximizes LLM Provider KV Cache Hits

Problem

I noticed my LLM API bill was higher than expected even though I send similar prompts repeatedly. I dug into provider caching and found the issue: even a single character difference at the start of a prompt invalidates the entire KV cache.

My system prompt included:

Dynamic system prompt
Today is Monday, June 2, 2026.
Session ID: a1b2-c3d4-e5f6
You are a helpful coding assistant.

The date changes daily. The session ID changes per conversation. Both sit at the top of the prompt, so the prefix is never byte-identical across requests. Result: zero cache hits.

What Is CacheAligner?

CacheAligner detects dynamic patterns in your system prompt, moves them to a [Context: ...] tail block, and keeps the prefix stable. Provider-specific optimizers then insert the right cache markers.

Provider cache discounts are significant:

  • Anthropic cache_control blocks: 90% off cached input tokens (with 25% write premium)
  • OpenAI automatic prefix caching: 50% off when prefixes are byte-identical (min 1024 tokens)
  • Google CachedContent API: 75% off (min 32,768 tokens)

Configuration

cachealigner_config.py
from headroom.transforms import CacheAlignerConfig
aligner_config = CacheAlignerConfig(
enabled=True,
dynamic_patterns=[
r"Today is \w+ \d+, \d{4}",
r"Current time: .*",
r"Session ID: [a-f0-9-]+",
],
)

CacheAligner matches these patterns and moves the matched text to the end of the prompt. The prefix “You are a helpful coding assistant.” stays identical across every request.

Provider-Specific Setup

Anthropic

anthropic_cache.py
from headroom import HeadroomClient, AnthropicProvider
from anthropic import Anthropic
client = HeadroomClient(
original_client=Anthropic(),
provider=AnthropicProvider(enable_cache_control=True),
enable_cache_optimizer=True,
)

Anthropic requires explicit cache_control blocks. Headroom inserts them automatically.

OpenAI and Google

OpenAI’s prefix caching is automatic once prefixes are stable and over 1024 tokens. Google’s CachedContent API requires registration but works similarly.

Compounded Savings

Cache savings stack with compression savings:

Savings stack
100K tokens original
→ 20K via SmartCrusher (80% compression)
→ 18K cached at 10% price = 1.8K equivalent tokens
= 96.2% total cost reduction

Overhead is sub-millisecond. The optimization is essentially free.

Common Mistake

I assumed prefix caching “just works” after I enabled it in the provider dashboard. It does not. Without CacheAligner, dynamic content in system prompts silently prevents cache hits on every request. The provider sees a different prefix each time and builds a new KV cache entry.

Summary

In this post, I showed how CacheAligner stabilizes prompt prefixes for provider KV caching. The key point is that moving dynamic content to a tail block makes the prefix byte-identical across requests, turning provider caching from a passive feature into an active cost-reduction strategy.

Final Words + More Resources

My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

Comments