跪拜 Guibai
← All articles
Deep Learning

Prompt Caching in Practice: Stop Paying Twice for the Same System Prompt, Token Costs Drop 80%

By Jvav练习生 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Input token costs can dominate the bill for any LLM application, and prompt caching is the most direct lever to pull. This guide shows how a structural change to the prompt yields a 92% cache hit rate, cutting costs by 80% and halving latency, without touching the model.

Summary

A customer service agent's input token bill ballooned to three times the budget in a week because every request sent the same 3000-character system prompt and 20 tool definitions, billed at full price each time. Enabling prompt caching brought the hit rate to 92%, cut input costs to about 20% of the original, and reduced time-to-first-token from 1.8 seconds to 0.9 seconds.

The underlying mechanism is prefix reuse: a request only hits the cache if its initial tokens are byte-for-byte identical to a previously computed prefix. Timestamps in the first line of the system prompt, randomly sampled few-shot examples, and unsorted JSON serialization all silently break that prefix, causing every request to miss. The fix is simple in principle: keep stable content (system prompt, tool definitions, fixed examples) at the front and append volatile parts (session history, user input) after.

A complete Python snippet demonstrates a cache-friendly prompt builder and a hit-rate meter, with the exact fields to read from DeepSeek, OpenAI, and Anthropic responses. In a 1,000-request benchmark, restructuring the prompt lifted the hit rate from 0% to 92%, lowered input cost to 21% of the original, and trimmed latency to 0.9 seconds. The article ends with a six-point checklist for maintaining cache stability in production.

Takeaways
— Cache hits require a byte-for-byte identical prefix from the first token; a single extra space or timestamp invalidates everything after it.
— Placements of volatile content like timestamps or UUIDs should go after the stable prefix, ideally at the end of the user message or via a tool call.
— OpenAI auto-caches prefixes over 1024 tokens and bills cached input at 50%.
— Anthropic uses cache_control markers, with writes at 1.25x and reads at 0.1x, and a default 5-minute expiry.
— DeepSeek auto-caches and charges about one-quarter of the miss price for hits.
— Recommended prefix order: system prompt, tool definitions, fixed examples, then session history and current user input.
— Set sort_keys=True on JSON serialization to keep tool definitions and parameters deterministic.
— With a 1,000-request test, removing a timestamp alone raised hit rate to 61%; adding stable prefixes and sorted JSON pushed it to 92%.
— At 92% hit rate, input cost dropped to 21% and time-to-first-token fell from 1.8s to 0.9s compared to the original pattern.
— Monitor hit rate daily; persistently below 70% indicates a polluted prefix.
— Cache typically covers only input tokens; output tokens are still billed at full price.
— Low-frequency usage can expire the cache, so don't count on overnight hits.
Conclusions

Prompt caching rewards structural prompt design over clever wording—separating stable from volatile content yields outsize cost and latency wins.

The biggest practical gain may be the latency reduction, not just the cost saving, because time-to-first-token directly affects perceived agent responsiveness.

Hit rate is a measurable, actionable metric that forces you to treat prompt engineering as an engineering discipline with observability, not guesswork.

Provider pricing differences mean the same caching principle leads to different cost structures; teams need to read each vendor's current terms before committing.

Concepts & terms
Prefix cache
A cache that reuses key/value vectors for a byte-identical starting sequence of tokens in a prompt. If the prefix changes at any point, everything after that point must be recomputed.
KV cache
The stored key and value vectors learned during inference for each token. Reusing them for a repeated prefix avoids recomputation and lowers latency and cost.
Hit rate
The proportion of input tokens served from the cache versus those that miss. Above 70% generally indicates a well-structured stable prefix; below that usually means the prefix is being polluted by volatile content.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗