Skip to content

Conversation

sobychacko
Copy link
Contributor

…en efficiency

  • Adds cache control support in AnthropicApi and AnthropicChatModel
  • Creates AnthropicCacheType enum with EPHEMERAL cache type
  • Extends AbstractMessage and UserMessage to support cache parameters
  • Updates Usage tracking to include cache-related token metrics (cacheCreationInputTokens, cacheReadInputTokens)
  • Adds integration test to verify prompt caching functionality and token usage patterns
  • Updates ContentBlock to support CacheControl parameter for low-level API usage
  • Adds comprehensive reference documentation with usage examples and best practices

This implementation follows Anthropic's prompt caching API which allows for more efficient token usage by caching frequently used prompts.

Original implementation provided by @Claudio-code (Claudio Silva Junior) See 15e5026

Fixes #1403

@markpollack
Copy link
Member

Why add this to usermessage/abstract message? I though a chatoption would be sufficient. I'm concerned about having functionality in the message hiearchy that isn't universal.

…tOptions

- Add cacheControl field to AnthropicChatOptions with builder method
- Create AnthropicCacheType enum with EPHEMERAL type for type-safe cache creation
- Update AnthropicChatModel.createRequest() to apply cache control from options to user message ContentBlocks
- Extend ContentBlock record with cacheControl parameter and constructor for API compatibility
- Update Usage record to include cacheCreationInputTokens and cacheReadInputTokens fields
- Update StreamHelper to handle new Usage constructor with cache token parameters
- Add AnthropicApiIT.chatWithPromptCache() test for low-level API validation
- Add AnthropicChatModelIT.chatWithPromptCacheViaOptions() integration test
- Add comprehensive unit tests for AnthropicChatOptions cache control functionality
- Update documentation with cacheControl() method examples and usage patterns

Cache control is configured through AnthropicChatOptions rather than message classes
to maintain provider portability. The cache control gets applied during request creation
in AnthropicChatModel when building ContentBlocks for user messages.

Original implementation provided by @Claudio-code (Claudio Silva Junior)
See spring-projects@15e5026

Fixes spring-projects#1403

Signed-off-by: Soby Chacko <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add suport to anthropic prompt caching
2 participants