Add prompt-caching module: Spring AI 2.1 Anthropic cache strategies on the real request (breakpoints per strategy, hit/miss/write in Usage, one changed word, multi-block system caching, minimum length, 5-minute and 1-hour lifetimes, the 20-block lookback), OpenAI automatic caching with prompt_cache_key, and a cost table. The vendors are a local server applying their documented rules; no vendor API called. Spring Boot 4.2.0, Spring AI 2.1.0, Java 25.
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b4gvXXQgw1eTk5EoTT
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
# Where each AnthropicCacheStrategy puts cache_control (request: 4 tools, a system prompt, one user turn)
|
||||
|
||||
Request shape: tools[0..3], system (one string), messages[0] = retrieved context + question
|
||||
|
||||
NONE breakpoints=0 []
|
||||
TOOLS_ONLY breakpoints=1 [tools[3]] {"type":"ephemeral","ttl":"5m"}
|
||||
SYSTEM_ONLY breakpoints=1 [system[0]] {"type":"ephemeral","ttl":"5m"}
|
||||
SYSTEM_AND_TOOLS breakpoints=2 [tools[3], system[0]] {"type":"ephemeral","ttl":"5m"}
|
||||
CONVERSATION_HISTORY breakpoints=3 [tools[3], system[0], messages[0].content[0]] {"type":"ephemeral","ttl":"5m"}
|
||||
@@ -0,0 +1,14 @@
|
||||
# A cache hit in Spring AI's Usage, and what one changed word costs (SYSTEM_AND_TOOLS, 4 tools, 24,000-character system prompt)
|
||||
|
||||
A. The same tools and system prompt, a different question each time
|
||||
call 1 getPromptTokens=6959 getCacheReadInputTokens=0 getCacheWriteInputTokens=6628
|
||||
call 2 getPromptTokens=6957 getCacheReadInputTokens=6628 getCacheWriteInputTokens=0
|
||||
call 2: 6628 of 6957 prompt tokens came from the cache; 329 were billed at the plain input price
|
||||
(getPromptTokens() includes the cached tokens in 2.1.0)
|
||||
|
||||
B. After a warm-up call, what does each change do? (read = served from cache, write = stored again)
|
||||
(the four tool definitions alone are 550 tokens; the whole prefix is 6628)
|
||||
nothing changed read=6628 write=0 uncached=326
|
||||
only the retrieved context changed read=6628 write=0 uncached=1007
|
||||
one word of the system prompt changed read=550 write=6079 uncached=12
|
||||
one word of one tool description changed read=0 write=6628 uncached=12
|
||||
@@ -0,0 +1,8 @@
|
||||
# A clock in the system prompt: one joined block, or the static prefix pattern (SYSTEM_ONLY)
|
||||
|
||||
multiBlockSystemCaching=false system blocks sent=1 breakpoints=[system[0]]
|
||||
call 1: cacheWrite=6085 cacheRead=0
|
||||
call 2: cacheWrite=6085 cacheRead=0 (the clock changed between the calls)
|
||||
multiBlockSystemCaching=true system blocks sent=2 breakpoints=[system[0]]
|
||||
call 1: cacheWrite=6078 cacheRead=0
|
||||
call 2: cacheWrite=0 cacheRead=6078 (the clock changed between the calls)
|
||||
@@ -0,0 +1,9 @@
|
||||
# Below the vendor minimum: nothing is cached and nothing says so (SYSTEM_ONLY, no tools, simulated minimum 512 tokens)
|
||||
|
||||
A. The breakpoint is sent either way; the vendor decides
|
||||
system prompt 1,800 characters: breakpoint sent=true call 2 read=0 write=0
|
||||
system prompt 2,400 characters: breakpoint sent=true call 2 read=625 write=0
|
||||
|
||||
B. messageTypeMinContentLength(SYSTEM, 2000) keeps the breakpoint off short prompts, client side
|
||||
system prompt 1,800 characters: breakpoint sent=false []
|
||||
system prompt 2,400 characters: breakpoint sent=true [system[0]]
|
||||
@@ -0,0 +1,18 @@
|
||||
# Cache lifetime: 5 minutes refreshed by every read, or 1 hour (SYSTEM_ONLY, virtual clock)
|
||||
|
||||
TTL FIVE_MINUTES (cache_control sent: {"type":"ephemeral","ttl":"5m"})
|
||||
t= 0s (gap 0s) MISS write=6078
|
||||
t= 240s (gap 240s) HIT read=6078
|
||||
t= 530s (gap 290s) HIT read=6078
|
||||
t= 831s (gap 301s) MISS write=6078
|
||||
t= 931s (gap 100s) HIT read=6078
|
||||
t= 2731s (gap 1800s) MISS write=6078
|
||||
|
||||
TTL ONE_HOUR (cache_control sent: {"type":"ephemeral","ttl":"1h"})
|
||||
t= 0s (gap 0s) MISS write=6078
|
||||
t= 240s (gap 240s) HIT read=6078
|
||||
t= 530s (gap 290s) HIT read=6078
|
||||
t= 831s (gap 301s) HIT read=6078
|
||||
t= 931s (gap 100s) HIT read=6078
|
||||
t= 2731s (gap 1800s) HIT read=6078
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
# An 8-turn chat, each user turn about 1,600 characters of pasted log (claude-sonnet-5-5 prices, 5-minute cache)
|
||||
|
||||
strategy NONE
|
||||
turn 1 breakpoints=[] plain=6403 read=0 write=0
|
||||
turn 2 breakpoints=[] plain=6773 read=0 write=0
|
||||
turn 3 breakpoints=[] plain=7143 read=0 write=0
|
||||
turn 4 breakpoints=[] plain=7543 read=0 write=0
|
||||
turn 5 breakpoints=[] plain=7909 read=0 write=0
|
||||
turn 6 breakpoints=[] plain=8304 read=0 write=0
|
||||
turn 7 breakpoints=[] plain=8699 read=0 write=0
|
||||
turn 8 breakpoints=[] plain=9077 read=0 write=0
|
||||
8 turns: plain=61851 read=0 write=0 input+output cost $0.1269
|
||||
|
||||
strategy SYSTEM_ONLY
|
||||
turn 1 breakpoints=[system[0]] plain=325 read=0 write=6078
|
||||
turn 2 breakpoints=[system[0]] plain=695 read=6078 write=0
|
||||
turn 3 breakpoints=[system[0]] plain=1065 read=6078 write=0
|
||||
turn 4 breakpoints=[system[0]] plain=1465 read=6078 write=0
|
||||
turn 5 breakpoints=[system[0]] plain=1831 read=6078 write=0
|
||||
turn 6 breakpoints=[system[0]] plain=2226 read=6078 write=0
|
||||
turn 7 breakpoints=[system[0]] plain=2621 read=6078 write=0
|
||||
turn 8 breakpoints=[system[0]] plain=2999 read=6078 write=0
|
||||
8 turns: plain=13227 read=42546 write=6078 input+output cost $0.0491
|
||||
|
||||
strategy CONVERSATION_HISTORY
|
||||
turn 1 breakpoints=[system[0], messages[0].content[0]] plain=0 read=0 write=6403
|
||||
turn 2 breakpoints=[system[0], messages[2].content[0]] plain=0 read=6403 write=370
|
||||
turn 3 breakpoints=[system[0], messages[4].content[0]] plain=0 read=6773 write=370
|
||||
turn 4 breakpoints=[system[0], messages[6].content[0]] plain=0 read=7143 write=400
|
||||
turn 5 breakpoints=[system[0], messages[8].content[0]] plain=0 read=7543 write=366
|
||||
turn 6 breakpoints=[system[0], messages[10].content[0]] plain=0 read=7909 write=395
|
||||
turn 7 breakpoints=[system[0], messages[12].content[0]] plain=0 read=8304 write=395
|
||||
turn 8 breakpoints=[system[0], messages[14].content[0]] plain=0 read=8699 write=378
|
||||
8 turns: plain=0 read=52774 write=9077 input+output cost $0.0312
|
||||
|
||||
The 20-block lookback: the same chat, but 24 messages are added between two turns
|
||||
next turn, 2 messages added since the last breakpoint: read=6773 write=370
|
||||
next turn, 25 messages added since the last breakpoint: read=6078 write=1339
|
||||
@@ -0,0 +1,20 @@
|
||||
# OpenAI through Spring AI 2.1.0: no breakpoints to place, where a message boundary falls, and a cached count in Usage
|
||||
|
||||
A. Same system prompt, different question (simulated rule: longest shared message-boundary prefix of at least 1,024 tokens)
|
||||
request carried prompt_cache_key=support-assistant-v1 keys sent: [messages, model, prompt_cache_key]
|
||||
call 1: getPromptTokens=6410 getCacheReadInputTokens=0 getCacheWriteInputTokens=null
|
||||
call 2: getPromptTokens=6408 getCacheReadInputTokens=6080 getCacheWriteInputTokens=null
|
||||
as a price-sheet bill: plain=328 read=6080 write=0
|
||||
|
||||
B. A clock inside the one big system message (the simulation caches whole messages, the GPT-5.6-and-later rule)
|
||||
call 2: 0 of 6097 prompt tokens cached
|
||||
|
||||
C. The clock moved to its own message after the stable one, so a message boundary falls where the text starts to change
|
||||
call 2: 6080 of 6097 prompt tokens cached
|
||||
|
||||
D. A prompt under 1,024 tokens (simulated minimum): no error, no caching
|
||||
call 2: 0 of 635 prompt tokens cached
|
||||
|
||||
E. Options that exist in Spring AI 2.1.0, and one that has to travel through extraBody
|
||||
keys on the wire: [messages, model, prompt_cache_key, prompt_cache_retention]
|
||||
prompt_cache_retention=24h
|
||||
@@ -0,0 +1,36 @@
|
||||
# Cost of 1,000 requests: a 24,000-character policy + 4 tools + 2 retrieved chunks + a question, 150 output tokens (list prices 2026-11-24)
|
||||
|
||||
A. claude-sonnet-5-5: the same 300 requests, arriving every N seconds (cost per 1,000 requests)
|
||||
gap setup plain read write dollars vs none
|
||||
20 s no caching 2,187k 0k 0k $16.08
|
||||
SYSTEM_AND_TOOLS, 5 minutes 198k 1,982k 7k $3.54 -78%
|
||||
SYSTEM_AND_TOOLS, 1 hour 198k 1,982k 7k $3.57 -78%
|
||||
CONVERSATION_HISTORY, 5 minutes 0k 1,982k 205k $3.87 -76%
|
||||
|
||||
240 s no caching 2,187k 0k 0k $16.08
|
||||
SYSTEM_AND_TOOLS, 5 minutes 198k 1,982k 7k $3.54 -78%
|
||||
SYSTEM_AND_TOOLS, 1 hour 198k 1,982k 7k $3.57 -78%
|
||||
CONVERSATION_HISTORY, 5 minutes 0k 1,982k 205k $3.87 -76%
|
||||
|
||||
301 s no caching 2,187k 0k 0k $16.08
|
||||
SYSTEM_AND_TOOLS, 5 minutes 198k 0k 1,988k $19.39 +21%
|
||||
SYSTEM_AND_TOOLS, 1 hour 198k 1,982k 7k $3.57 -78%
|
||||
CONVERSATION_HISTORY, 5 minutes 0k 0k 2,187k $19.72 +23%
|
||||
|
||||
3700 s no caching 2,187k 0k 0k $16.08
|
||||
SYSTEM_AND_TOOLS, 5 minutes 198k 0k 1,988k $19.39 +21%
|
||||
SYSTEM_AND_TOOLS, 1 hour 198k 0k 1,988k $29.33 +82%
|
||||
CONVERSATION_HISTORY, 5 minutes 0k 0k 2,187k $19.72 +23%
|
||||
|
||||
B. The same requests every 20 seconds, per model (SYSTEM_AND_TOOLS with the 5-minute lifetime for Anthropic)
|
||||
model no caching cached saving
|
||||
claude-haiku-5-5 $0.80 $0.21 -74%
|
||||
claude-sonnet-5-5 $16.08 $3.54 -78%
|
||||
claude-opus-5-5 $32.15 $7.07 -78%
|
||||
gpt-6.2-sol $16.35 $3.54 -78% (OpenAI: automatic; 'no caching' = every request sent with a changing first message)
|
||||
|
||||
C. How many hits inside the lifetime does a write pay back? hits = (write multiple - 1) / (1 - read multiple)
|
||||
model write 5m read hits (5m) hits (1h)
|
||||
claude-haiku-5-5 1.25x 0.100x 0.28 1.11
|
||||
claude-sonnet-5-5 1.25x 0.050x 0.26 1.05
|
||||
claude-opus-5-5 1.25x 0.050x 0.26 1.05
|
||||
Reference in New Issue
Block a user