Add prompt caching support for AWS Bedrock Converse API #4614
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Implements prompt caching to reduce costs on repeated content and improve
response times. Applications with large system prompts, extensive tool
definitions, or multi-turn conversations can see significant savings, as
cached content costs ~90% less to process than uncached content.
Adds five caching strategies to address different use cases:
SYSTEM_ONLY
: Cache system messages (most common - stable instructions)TOOLS_ONLY
: Cache tool definitions (when tools are stable but system varies)SYSTEM_AND_TOOLS
: Cache both (when both are large and stable)CONVERSATION_HISTORY
: Cache conversation history (for chatbots and assistants)NONE
: Default, no cachingImplementation:
BedrockCacheStrategy
enum withBedrockCacheOptions
configuration classBedrockChatOptions
(equals
/hashCode
/copy
support)CONVERSATION_HISTORY
enables incremental caching where each turn builds on the previous cached prefixMap
to maintain provider independence without adding Bedrock-specific fields to shared interfacesModel compatibility:
SYSTEM_ONLY
andCONVERSATION_HISTORY
only (AWS limitation on tool caching for Nova models)Testing:
Documentation includes usage examples, real-world use cases (legal document
analysis, code review, customer support, multi-tenant SaaS), best practices,
cache invalidation behavior, and cost considerations. Break-even occurs after
one cache hit since cache reads cost ~90% less than base input tokens while
cache writes cost ~25% more.
Thank you for taking time to contribute this pull request!
You might have already read the contributor guide, but as a reminder, please make sure to:
git commit -s
) per the DCOmain
branch and squash your commitsFor more details, please check the contributor guide.
Thank you upfront!