ReviseAlgo Logo

LLM Inference Engineering

Prompt Caching & Speculative Decoding

Anthropic/Gemini prefix caching, 90% cost reduction, and speculative token generation.

Interview: High - Directly reduces costs and latency for production systems.

Stop Paying to Process the Same System Prompt 10,000 Times

Prompt Caching Flow

If your system prompt is 2,000 tokens and you have 100,000 requests/day, you are spending 6/day (2,160/year) just on re-processing the same static context. Prompt Prefix Caching lets Anthropic and Google compute and cache your static context on their infrastructure, so you only pay for input tokens once and cache reads at 90% discount.

How Prefix Caching Works

The model provider hashes the prefix (your system prompt + any static context). On subsequent requests with the same prefix, the KV cache for those tokens is served from their GPU memory instead of recomputed. The constraint is that the prefix must be identical byte-for-byte. Even a single character change invalidates the cache. This is why dynamic content (user names, timestamps) must always be appended after the cached prefix, never injected inside it.

Speculative Decoding

Standard decoding is sequential and slow: 1 token per compute step. Speculative Decoding uses a small, fast "draft" model (e.g. Llama 3 8B) to generate 4-8 tokens in parallel, then uses the large "oracle" model (e.g. Llama 3 70B) to verify them all in a single forward pass. If the draft tokens match what the large model would have generated, all are accepted simultaneously. This achieves 2-3x throughput improvement for free with no quality loss.

Use Cases

Reducing cost by 80-90% for chatbots where the system prompt and knowledge base are static

Enabling large context windows (200K tokens) economically for document analysis apps

Video/code analysis where the media is the same across many analysis requests

Common Mistakes

Injecting the current timestamp or user ID inside the cached prefix block — this invalidates the cache on every single request

Expecting the cache to persist indefinitely — Anthropic ephemeral cache expires after 5 minutes of no activity

Not checking cache_read_input_tokens in the response to verify the cache is actually being hit