Skip to content

Add ChatMessageExtensions with UsePromptCaching #4340

Closed
gcarney-ai wants to merge 4 commits intoaws:developmentfrom
gcarney-ai:gcarney-ai/add-chat-message-extensions
Closed

Add ChatMessageExtensions with UsePromptCaching #4340
gcarney-ai wants to merge 4 commits intoaws:developmentfrom
gcarney-ai:gcarney-ai/add-chat-message-extensions

Conversation

@gcarney-ai
Copy link
Copy Markdown

Description

I have added a new file named ChatMessageExtensions with a single extension: UsePromptCaching. This change creates AdditionalProperties on the message if it is not already populated, and adds CachePoint ContentBlock. I used the Prompt caching for faster model inference for reference. The UsePrompCaching method takes an optional parameter CacheTTL which allows users to pass in either 5Min or 1Hr...defaulting to 5 min.

Motivation and Context

Prompt caching is a crucial piece for both latency and cost optimization, by placing it in a public extension, it enables discovery and reduces duplicate code written by consumers.

Testing

No existing test fixtures for this project. But I manually tested each case.

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project
  • My change requires a change to the documentation
  • I have updated the documentation accordingly
  • I have read the README document
  • I have added tests to cover my changes
  • All new and existing tests passed

License

  • I confirm that this pull request can be released under the Apache 2 license

@boblodgett boblodgett requested a review from normj February 25, 2026 18:31
@dscpinheiro dscpinheiro changed the base branch from main to development February 26, 2026 16:04
@boblodgett boblodgett requested review from jnunn-aws and removed request for normj March 3, 2026 18:32
@peterrsongg peterrsongg requested a review from Copilot March 19, 2026 02:14
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a public convenience extension to enable Bedrock prompt caching on Microsoft.Extensions.AI.ChatMessage instances in the Bedrock MEAI extensions package, along with the required DevConfig entry for release automation.

Changes:

  • Add ChatMessageExtensions.UsePromptCaching to stamp a cache point into ChatMessage.AdditionalProperties.
  • Add a DevConfig entry marking a minor extension update and providing a changelog message.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
generator/.DevConfigs/a1508acc-bb92-4ee6-a356-9b1a82d506b2.json Declares a minor extension change and changelog message for release automation.
extensions/src/AWSSDK.Extensions.Bedrock.MEAI/ChatMessageExtensions.cs Introduces the UsePromptCaching extension that adds a cache point marker to chat messages.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +33 to +35
public static ChatMessage UsePromptCaching(this ChatMessage message, CacheTTL? cacheTTL = null)
{
cacheTTL ??= CacheTTL.FIVE_MINUTES;
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds great. Updated

cacheTTL ??= CacheTTL.FIVE_MINUTES;
message.AdditionalProperties ??= [];

message.AdditionalProperties.Add(nameof(ContentBlock.CachePoint), new CachePointBlock { Type = CachePointType.Default, Ttl = cacheTTL });
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch. Converted to indexing directly

/// <remarks>
/// This method adds a cache point marker to the message's additional properties. Prompt caching
/// allows Bedrock to cache the prompt tokens for reuse in subsequent requests, reducing costs
/// and latency. The model must support prompt caching and the message must be within the minimum token threshold for caching.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect....reworded

@dscpinheiro
Copy link
Copy Markdown
Contributor

@gcarney-ai Other than the testing, can you review Copilot's comments? I think the unresolved ones make sense.

@dscpinheiro
Copy link
Copy Markdown
Contributor

I'm going to close this PR as we haven't had any activity. If you'd like, please feel free to reopen and address Copilot's feedback.

@dscpinheiro dscpinheiro closed this Apr 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants