Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b4gvXXQgw1eTk5EoTT
3.5 KiB
prompt-caching
Provider-side prompt caching with Spring AI 2.1: where AnthropicCacheStrategy puts cache breakpoints on the request, what a hit, a miss and a write look like in Usage, what one changed word invalidates, the 5-minute and 1-hour lifetimes, the 20-block lookback in a long chat, OpenAI's automatic prefix caching and prompt_cache_key, and a cost table with the traps in it.
No vendor API was called, and no claim is made about any vendor's behaviour. FakeProviders is a local HTTP server that answers in the Anthropic Messages and OpenAI chat-completions formats. The real AnthropicChatModel and OpenAiChatModel talk to it, so the requests it records are the requests those classes build. Everything on the vendor's side is simulation: a token is ceil(characters / 4), the answer is fixed text, and cache hits follow the rules the vendors document (read on 2026-11-24) applied to the request that arrived. Not simulated: concurrent first requests, OpenAI's routing by prompt_cache_key, hidden system tokens, rounding of OpenAI cached counts, latency. So the cost table is a worked example of the price sheets, not a bill.
| Spring Boot | 4.2.0 (GA 2026-11-19) |
| Spring AI | 2.1.0 (GA 2026-11-19) |
| Java | 25 (Temurin 25.0.5.1) |
| Prices | read 2026-11-24 from the Anthropic and OpenAI pricing pages |
Run it
export JAVA_HOME=/path/to/jdk-25
./scripts/run-all.sh # runs the tests; they write every file under output/
Every figure in output/ is also an assertion in a test, so a number that stops being true fails the build. No API key is needed.
Layout
| Path | What it is |
|---|---|
src/main/.../CallUsage.java |
Turns Spring AI's Usage into plain / read / write / output tokens |
src/main/.../Cost.java, Rates.java, Prices.java |
Dollars from tokens; list prices with the date they were read |
src/main/.../Models.java |
Builds the real Anthropic and OpenAI chat models with cache options |
src/main/.../SupportPrompts.java, SupportTools.java |
Deterministic prompt, retrieved chunks and four tools |
src/test/.../support/FakeProviders.java |
The local stand-in for both vendors (the simulation) |
src/test/.../*Test.java |
One test per transcript |
Captured output
| File | Test | What it shows |
|---|---|---|
01-breakpoints-on-the-wire.txt |
WireTest |
Where each AnthropicCacheStrategy puts cache_control |
02-hits-and-invalidation.txt |
HitsTest |
A hit in Usage, and what one changed word costs |
03-clock-in-the-system-prompt.txt |
HitsTest |
multiBlockSystemCaching and a clock in the system prompt |
04-minimum-length.txt |
MinimumLengthTest |
A prompt below the minimum: two zeros and no error |
05-expiry.txt |
ExpiryTest |
5-minute and 1-hour lifetimes on a virtual clock |
06-conversation.txt |
ConversationTest |
CONVERSATION_HISTORY over 8 turns, and the 20-block lookback |
07-openai-automatic-caching.txt |
OpenAiTest |
prompt_cache_key, cached tokens in Usage, where a message boundary falls |
08-cost.txt |
CostTest |
Cost of 1,000 requests by strategy, gap, model; break-even hits |
The article walks through all of this in one page: Prompt Caching in Spring AI.