- Newest
- Most votes
- Most comments
To enable explicit prompt caching with the Bedrock Converse API in Java for Amazon Nova Pro, you need to add a cache checkpoint to your message content. Your current code doesn't include this cache checkpoint, which is why you're not seeing any cache usage in the response.
Here's how you can modify your code to include a cache checkpoint:
var message = Message.builder() .role(ConversationRole.USER) .content(List.of( ContentBlock.fromText("some text"), ContentBlock.builder() .cachePoint(CachePoint.builder() .type("default") .build()) .build() )) .build(); ConverseResponse response = client.converse(request -> request .modelId(modelId) .messages(message) .inferenceConfig(config -> config .maxTokens(512) .temperature(0.5F) .topP(0.9F)));
The key change is adding a cachePoint ContentBlock after your text content. This tells Bedrock to cache everything before this point.
You can also add cache checkpoints to the system prompt if you're using one:
var systemPrompt = List.of( ContentBlock.fromText("Your system instructions here"), ContentBlock.builder() .cachePoint(CachePoint.builder() .type("default") .build()) .build() ); ConverseResponse response = client.converse(request -> request .modelId(modelId) .messages(message) .system(systemPrompt) .inferenceConfig(config -> config .maxTokens(512) .temperature(0.5F) .topP(0.9F)));
To verify that caching is working, check the usage field in the response. When prompt caching is active, you should see values for cacheReadInputTokens and cacheWriteInputTokens. On the first call, you'll see tokens being written to the cache, and on subsequent identical calls (within the 5-minute TTL period), you should see tokens being read from the cache.
Remember that the cache has a 5-minute Time To Live (TTL), so identical requests need to be made within this timeframe to benefit from cache hits.
Sources
Clarification on Prompt Caching Usage with converse API in Amazon Bedrock | AWS re:Post
Nova Pro Bedrock Prompt Cache Usage | AWS re:Post
Using the Converse API - Amazon Bedrock
Effectively use prompt caching on Amazon Bedrock | AWS Machine Learning Blog
Relevant content
asked a year ago

Thank you for your answer,
After updating the Bedrock dependencies to latest versions, the JAVA code looked like below and ran succesfully with Nova Pro cache.
Cache activity:
TokenUsage(InputTokens=1741, OutputTokens=120, TotalTokens=2140, CacheReadInputTokens=0, CacheWriteInputTokens=279)TokenUsage(InputTokens=1741, OutputTokens=91, TotalTokens=2111, CacheReadInputTokens=279, CacheWriteInputTokens=0)