Commit 8327c0f
Anthropic Prompt Caching: Align CONVERSATION_HISTORY with Anthropic's incremental caching pattern
This commit updates the CONVERSATION_HISTORY cache strategy to align with
Anthropic's official documentation and cookbook examples
(https://github.com/anthropics/claude-cookbooks/blob/main/misc/prompt_caching.ipynb)
for incremental conversation caching.
**Cache breakpoint placement:**
- Before: Cache breakpoint on penultimate (second-to-last) user message
- After: Cache breakpoint on last user message
**Aggregate eligibility:**
- Before: Only considered user messages for min content length check
- After: Considers all message types (user, assistant, tool) within 20-block
lookback window for aggregate eligibility
Anthropic's documentation and cookbook demonstrate incremental caching by
placing cache_control on the LAST user message:
```python
result.append({
"role": "user",
"content": [{
"type": "text",
"text": turn["content"][0]["text"],
"cache_control": {"type": "ephemeral"} # On LAST user message
}]
})
```
This pattern is also shown in their official docs:
https://docs.claude.com/en/docs/build-with-claude/prompt-caching#large-context-caching-example
Anthropic's caching system uses prefix matching to find the longest matching
prefix from the cache. By placing cache_control on the last user message,
we enable the following incremental caching pattern:
```
Turn 1: Cache [System + User1]
Turn 2: Reuse [System + User1], process [Assistant1 + User2],
cache [System + User1 + Assistant1 + User2]
Turn 3: Reuse [System + User1 + Assistant1 + User2],
process [Assistant2 + User3],
cache [System + User1 + Assistant1 + User2 + Assistant2 + User3]
```
The cache grows incrementally with each turn, building a larger prefix that
can be reused. This is the recommended pattern from Anthropic.
The new implementation considers all message types (user, assistant, tool)
within the 20-block lookback window when checking minimum content length.
This ensures that:
- Short user questions don't prevent caching when conversation has long
assistant responses
- The full conversation context is considered for the 1024+ token minimum
- Aligns with Anthropic's note: "The automatic prefix checking only looks
back approximately 20 content blocks from each explicit breakpoint"
None. This is an implementation detail of the CONVERSATION_HISTORY strategy.
The API surface remains unchanged. Users may observe:
- Different cache hit patterns (should be more effective)
- Cache metrics may show higher cache read tokens as conversations grow
- Updated `shouldRespectMinLengthForUserHistoryCaching()` to test aggregate
eligibility with combined message lengths
- Renamed `shouldApplyCacheControlToLastUserMessageForConversationHistory()`
(from `shouldRespectAllButLastUserMessageForUserHistoryCaching`)
- Added `shouldDemonstrateIncrementalCachingAcrossMultipleTurns()` integration
test showing cache growth pattern across 4 conversation turns
- Updated mock test assertions to verify last message has cache_control
Updated anthropic-chat.adoc to clarify:
- CONVERSATION_HISTORY strategy description now mentions incremental prefix
caching
- Code example comments updated to reflect cache breakpoint on last user
message
- Implementation Details section expanded with explanation of prefix matching
and aggregate eligibility checking
- Anthropic Prompt Caching Docs: https://docs.claude.com/en/docs/build-with-claude/prompt-caching
- Anthropic Cookbook: https://github.com/anthropics/claude-cookbooks/blob/main/misc/prompt_caching.ipynb
Signed-off-by: Soby Chacko <[email protected]>1 parent 838801f commit 8327c0f
File tree
4 files changed
+207
-38
lines changed- models/spring-ai-anthropic/src
- main/java/org/springframework/ai/anthropic
- test/java/org/springframework/ai/anthropic
- spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat
4 files changed
+207
-38
lines changedLines changed: 20 additions & 18 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
618 | 618 | | |
619 | 619 | | |
620 | 620 | | |
621 | | - | |
622 | | - | |
| 621 | + | |
| 622 | + | |
623 | 623 | | |
624 | | - | |
625 | | - | |
626 | | - | |
627 | | - | |
628 | | - | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
629 | 629 | | |
630 | | - | |
| 630 | + | |
631 | 631 | | |
632 | 632 | | |
633 | 633 | | |
| |||
676 | 676 | | |
677 | 677 | | |
678 | 678 | | |
679 | | - | |
680 | | - | |
| 679 | + | |
681 | 680 | | |
682 | | - | |
683 | | - | |
684 | | - | |
685 | | - | |
686 | | - | |
687 | | - | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
688 | 692 | | |
689 | 693 | | |
690 | | - | |
691 | | - | |
692 | 694 | | |
693 | 695 | | |
694 | 696 | | |
| |||
Lines changed: 174 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
280 | 280 | | |
281 | 281 | | |
282 | 282 | | |
283 | | - | |
| 283 | + | |
284 | 284 | | |
285 | | - | |
286 | | - | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
287 | 290 | | |
288 | | - | |
| 291 | + | |
289 | 292 | | |
290 | 293 | | |
291 | 294 | | |
292 | 295 | | |
293 | | - | |
| 296 | + | |
294 | 297 | | |
295 | 298 | | |
296 | 299 | | |
| |||
303 | 306 | | |
304 | 307 | | |
305 | 308 | | |
306 | | - | |
| 309 | + | |
307 | 310 | | |
308 | 311 | | |
309 | 312 | | |
310 | 313 | | |
311 | | - | |
| 314 | + | |
312 | 315 | | |
313 | 316 | | |
314 | 317 | | |
| |||
319 | 322 | | |
320 | 323 | | |
321 | 324 | | |
322 | | - | |
| 325 | + | |
323 | 326 | | |
324 | 327 | | |
325 | 328 | | |
326 | 329 | | |
327 | | - | |
328 | | - | |
| 330 | + | |
| 331 | + | |
329 | 332 | | |
330 | 333 | | |
331 | 334 | | |
332 | 335 | | |
333 | 336 | | |
334 | | - | |
335 | | - | |
| 337 | + | |
| 338 | + | |
336 | 339 | | |
337 | 340 | | |
338 | 341 | | |
| |||
450 | 453 | | |
451 | 454 | | |
452 | 455 | | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
453 | 615 | | |
Lines changed: 5 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
331 | 331 | | |
332 | 332 | | |
333 | 333 | | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
339 | 339 | | |
340 | 340 | | |
341 | 341 | | |
| |||
Lines changed: 8 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
213 | 213 | | |
214 | 214 | | |
215 | 215 | | |
216 | | - | |
| 216 | + | |
217 | 217 | | |
218 | | - | |
| 218 | + | |
219 | 219 | | |
220 | 220 | | |
221 | 221 | | |
| |||
272 | 272 | | |
273 | 273 | | |
274 | 274 | | |
275 | | - | |
| 275 | + | |
276 | 276 | | |
277 | 277 | | |
278 | 278 | | |
| |||
620 | 620 | | |
621 | 621 | | |
622 | 622 | | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
623 | 626 | | |
624 | 627 | | |
625 | 628 | | |
626 | 629 | | |
627 | 630 | | |
628 | 631 | | |
629 | 632 | | |
| 633 | + | |
| 634 | + | |
630 | 635 | | |
631 | 636 | | |
632 | 637 | | |
| |||
0 commit comments