Skip to content

🪨 feat: Add Bedrock Prompt Caching Support - #8271

Merged
danny-avila merged 10 commits into
devfrom
feat/bedrock-prompt-caching
Nov 13, 2025
Merged

danny-avila merged 10 commits into
devfrom
feat/bedrock-prompt-caching

Conversation

@dustinhealy

@dustinhealy dustinhealy commented Jul 6, 2025 •

Copy link
Copy Markdown
Collaborator

Summary

Closes #7109

Adds a new parameter, Use Prompt Caching, for Bedrock endpoints, enabling cache points to be automatically embedded in messages sent to supported models

Note: actual insertion logic relies on LibreChat-AI/agents#28 (as well as a bug-fix around tool calling in LibreChat-AI/agents#30). This PR now mainly concerns the addition of the Use Prompt Caching toggle itself as a parameter. Previous implementation details which have been moved and refactored for the agents repo (or are no longer necessary as a result of the refactor) are left with strikethroughs for the sake of historical tracking.

Change Type

  • New feature (non-breaking change which adds functionality)

Details

  • Introduced addBedrockCacheControl to insert cache points when Use Prompt Caching is enabled (api/app/clients/prompts/addBedrockCacheControl.js).
  • Integrated logic into the Bedrock agent client so cache control is applied as appropriate (api/server/controllers/agents/client.js).
  • Updated exports to make addBedrockCacheControl available (api/app/clients/prompts/index.js).
  • Added promptCache (shown in UI as Use Prompt Caching) to Bedrock parameter settings and schema for configuration and validation (packages/data-provider/src/parameterSettings.ts, packages/data-provider/src/bedrock.ts).

Testing

  • Enabled and disabled Use Prompt Caching via parameter panel and verified the presence or absence of cache points in messages sent to Bedrock endpoints.
  • Using LangFuse, verified traces contained expected cache read and cache write usage metadata when prompt caching turned on for an agent using the Bedrock endpoint.

Test Configuration

Agent using AWS Bedrock endpoint with model: us.anthropic.claude-sonnet-4-20250514-v1:0 with Use Prompt Caching toggled on

Three message chain with high-token initial message (1000+ token) in order to trigger cache reads and cache writes.

Initial Input

User:
Use the Calculator tool, what is 5123043/39201? This is a long input test to see if prompt caching works. Lorem ipsum Lorem ipsum Lorem ipsum... <Continues for around ~1300 more tokens>

AWS Bedrock Prompt Caching (Tool Use) Test Agent:
I'll calculate 5123043/39201 for you using the calculator tool.
<Ran calculator>
The result of 5123043/39201 is approximately 130.687.

Usage Metadata:

{
    "inputTokens": 1794,
    "outputTokens": 26,
    "serverToolUsage": {},
    "totalTokens": 1820
}

Follow-up (triggers write to cache)

User:
This is a follow-up to check for cache_write

AWS Bedrock Prompt Caching (Tool Use) Test Agent:
I understand you're testing the cache functionality with this follow-up message. The previous calculation of 5123043/39201 gave us the result of approximately 130.687.

Is there anything else you'd like me to calculate or help you with?

Usage Metadata:

{
    "cacheReadInputTokenCount": 0,
    "cacheReadInputTokens": 0,
    "cacheWriteInputTokenCount": 1844,
    "cacheWriteInputTokens": 1844,
    "inputTokens": 4,
    "outputTokens": 58,
    "serverToolUsage": {},
    "totalTokens": 1906
}

Final follow-up (triggers read from cache)

User:
This is a follow-up to check for cache_read

AWS Bedrock Prompt Caching (Tool Use) Test Agent:
I see you're continuing to test the cache functionality. The calculation we performed earlier (5123043/39201 ≈ 130.687) should be available from the cached context along with all the Lorem ipsum text from your original message.

Is there anything specific you'd like me to help you with or any other calculations you need?

Usage Metadata:

{
    "cacheReadInputTokenCount": 1735,
    "cacheReadInputTokens": 1735,
    "cacheWriteInputTokenCount": 182,
    "cacheWriteInputTokens": 182,
    "inputTokens": 4,
    "outputTokens": 75,
    "serverToolUsage": {},
    "totalTokens": 1996
}

Checklist

  • My code adheres to this project's style guidelines
  • I have performed a self-review of my own code
  • I have commented in any complex areas of my code
  • I have made pertinent documentation changes
  • My changes do not introduce new warnings
  • I have written tests demonstrating that my changes are effective or that my feature works
  • Local unit tests pass with my changes
  • A pull request for updating the documentation has been submitted.

@KiGamji

KiGamji commented Jul 12, 2025

Copy link
Copy Markdown
Contributor

@dustinhealy Hi there! Do you have any due dates for this? Thanks in advance

@dustinhealy

Copy link
Copy Markdown
Collaborator Author

@dustinhealy Hi there! Do you have any due dates for this? Thanks in advance

Hi KiGamji, still need to write tests but planning to get this ready for review within the next couple days.

@KiGamji

KiGamji commented Jul 18, 2025

Copy link
Copy Markdown
Contributor

Hopefully we'll see this merged in the next week 👍

@danny-avila

Copy link
Copy Markdown
Collaborator

Hopefully we'll see this merged in the next week 👍

It requires an upstream change for it to fully work:

langchain-ai/langchainjs#8524

So we have to wait when that PR is tested/merged

@mikhailmokhov

mikhailmokhov commented Jul 21, 2025 •

Copy link
Copy Markdown

Hopefully we'll see this merged in the next week 👍

It requires an upstream change for it to fully work:

langchain-ai/langchainjs#8524

So we have to wait when that PR is tested/merged

Note that the mentioned PR is adding cache point support only to @langchain/aws package, make sure you're using it. Also, cachePoint must be a separate content block in the content array, see example: https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html#prompt-caching-get-started. You have it as a text block property.

@KiGamji

KiGamji commented Aug 4, 2025

Copy link
Copy Markdown
Contributor

@danny-avila @dustinhealy Hi there! Hope you consider this in any near time, as the mentioned PR was finally merged, and we would really like to see Bedrock with Prompt Caching! 👍

@danny-avila

Copy link
Copy Markdown
Collaborator

@danny-avila @dustinhealy Hi there! Hope you consider this in any near time, as the mentioned PR was finally merged, and we would really like to see Bedrock with Prompt Caching! 👍

still waiting on package release for @langchain/aws as the latest version 0.1.13 at the time of writing doesn't include prompt caching support:

https://www.npmjs.com/package/@langchain/aws

Comment thread api/app/clients/prompts/addBedrockCacheControl.js Outdated
Comment thread api/app/clients/prompts/index.js Outdated
@danny-avila

Copy link
Copy Markdown
Collaborator

Lastly, rebase branch to latest (to resolve conflicts)

@dustinhealy
dustinhealy force-pushed the feat/bedrock-prompt-caching branch 2 times, most recently from c54a617 to 04e4335 Compare August 8, 2025 19:42
@Pfaufisch

Pfaufisch commented Aug 19, 2025 •

Copy link
Copy Markdown

@langchain/aws 0.1.14 has been released and includes prompt caching support: langchain-ai/langchainjs#8524 (comment)

@KiGamji

KiGamji commented Aug 19, 2025

Copy link
Copy Markdown
Contributor

@danny-avila @dustinhealy tagging for visibility too. We would like to see this, leading to huge cost savings! Thanks in advance for your work!

@KiGamji

KiGamji commented Aug 24, 2025

Copy link
Copy Markdown
Contributor

any reason for this not being merged?

@danny-avila

Copy link
Copy Markdown
Collaborator

any reason for this not being merged?

not fully tested, will be part of next release a week from now. needs to be rebased with latest upcoming changes to dev branch

@ldavis9000aws

Copy link
Copy Markdown

@dustinhealy @danny-avila So this will be in the next release, right? :)

@Defiect

Defiect commented Sep 5, 2025

Copy link
Copy Markdown

@danny-avila Are there specific tests currently blocking this from being in the next release?

Really looking forward to this feature, as I'm sure many others are. Anthropic has been severely tightening the belt on usage limits via subscriptions; general user sentiment seems to indicate that users are increasingly switching to Bedrock to access their models

@KiGamji

KiGamji commented Sep 5, 2025

Copy link
Copy Markdown
Contributor

yeah, we are waiting for this too, as much as new Bedrock keys format support, and fixes for the PROXY env!

@AetherMagee

Copy link
Copy Markdown

Are there any blockers for this besides needing a rebase?

@dustinhealy

dustinhealy commented Sep 5, 2025 •

Copy link
Copy Markdown
Collaborator Author

Are there any blockers for this besides needing a rebase?

The agents repo still needs a version bump for @langchain/aws in order for this feature to actually work + we still have to do some testing on the Bedrock side of things to make sure the prompt caching points are working as intended. I will rebase later today after I finish my current PR just to resolve the merge conflicts. Thanks all for your interest, we know this is highly anticipated feature, so we appreciate your patience in the meantime!

@Defiect

Defiect commented Sep 6, 2025

Copy link
Copy Markdown

Are there any blockers for this besides needing a rebase?

The agents repo still needs a version bump for @langchain/aws in order for this feature to actually work + we still have to do some testing on the Bedrock side of things to make sure the prompt caching points are working as intended. I will rebase later today after I finish my current PR just to resolve the merge conflicts. Thanks all for your interest, we know this is highly anticipated feature, so we appreciate your patience in the meantime!

Awesome, thank you. Your effort is greatly appreciated!

@matthill33

Copy link
Copy Markdown

Are there any blockers for this besides needing a rebase?

The agents repo still needs a version bump for @langchain/aws in order for this feature to actually work + we still have to do some testing on the Bedrock side of things to make sure the prompt caching points are working as intended. I will rebase later today after I finish my current PR just to resolve the merge conflicts. Thanks all for your interest, we know this is highly anticipated feature, so we appreciate your patience in the meantime!

Awesome, thank you. Your effort is greatly appreciated!

I'm also very much looking forward to this feature being implemented, thank you @dustinhealy for your work on it!

@dustinhealy
dustinhealy force-pushed the feat/bedrock-prompt-caching branch from 0daea04 to cf0f9e8 Compare September 8, 2025 15:30
@KiGamji

KiGamji commented Sep 12, 2025 •

Copy link
Copy Markdown
Contributor

@danny-avila Any chance to get this merged until October? I appreciate your time and effort in maintaining this huge project, but this PR is already two months old and it would need more effort for @dustinhealy to rebase and maintain it each time, unless it gets merged. Also, this is one of the mostly anticipated features by activity, along with conversation cost tracking (#9190, #8531), conversation pinning (#7550), PassKeys & E2EE, so I really hope you will give it some attention in the neartime. Thanks in advance!

@dvejsada

Copy link
Copy Markdown

@danny-avila Any chance to get this merged until October? I appreciate your time and effort in maintaining this huge project, but this PR is already two months old and it would need more effort for @dustinhealy to rebase and maintain it each time, unless it gets merged. Also, this is one of the mostly anticipated features by activity, along with conversation cost tracking (#9190, #8531), conversation pinning (#7550), PassKeys & E2EE, so I really hope you will give it some attention in the neartime. Thanks in advance!

Very anticipated indeed!;)

@Ziyann

Ziyann commented Sep 20, 2025 •

Copy link
Copy Markdown
Contributor

Would it be possible to also add this as an option for custom endpoints?
LiteLLM, OpenRouter and some others also need it, with the same logic, except instead of cachePoint: { type: 'default' }, they require cache_control: { type: 'ephemeral' }.

@ldavis9000aws

Copy link
Copy Markdown

Ok...I'm sure there are more important things the project is focused on, but just a reminder:

image

@KiGamji

KiGamji commented Oct 4, 2025

Copy link
Copy Markdown
Contributor

With all the respect to Danny, I absolutely agree with this :/
It has been 3 months...

@KiGamji

KiGamji commented Oct 4, 2025

Copy link
Copy Markdown
Contributor

#9798 seems to be dangling too

@KiGamji

KiGamji commented Nov 9, 2025 •

Copy link
Copy Markdown
Contributor

With all due respect, dear Danny and the LibreChat Team, I believe this is not a serious approach for user-made contributions to such a project...

@KiGamji

KiGamji commented Nov 9, 2025

Copy link
Copy Markdown
Contributor

A huge number of people and their users are waiting while being fed false promises.

@ldavis9000aws

ldavis9000aws commented Nov 10, 2025 •

Copy link
Copy Markdown

Hey @dustinhealy - thanks for updating the feature branch. Hopefully we'll start to get some traction on this.

@danny-avila
danny-avila force-pushed the feat/bedrock-prompt-caching branch from 415a652 to d8bf695 Compare November 11, 2025 00:07
@danny-avila

Copy link
Copy Markdown
Collaborator

Sorry for the wait on this. It's currently working but not for tool calling. @dustinhealy is looking into it

@matthill33

Copy link
Copy Markdown

Sorry for the wait on this. It's currently working but not for tool calling. @dustinhealy is looking into it

Thank you for your work on this!

@dustinhealy
dustinhealy force-pushed the feat/bedrock-prompt-caching branch from cdb3cea to 83b76e0 Compare November 12, 2025 14:30
@KiGamji

KiGamji commented Nov 12, 2025

Copy link
Copy Markdown
Contributor

It would be really amazing if you also review this, Danny: #8871
Appreciate it!

@danny-avila
danny-avila force-pushed the feat/bedrock-prompt-caching branch from 53cfea4 to fbb9caa Compare November 13, 2025 15:16
@danny-avila
danny-avila merged commit c9ee0f1 into dev Nov 13, 2025
6 checks passed
@danny-avila
danny-avila deleted the feat/bedrock-prompt-caching branch November 13, 2025 15:21
@KiGamji

KiGamji commented Nov 13, 2025

Copy link
Copy Markdown
Contributor

absolute-cinema-v0-nguwobt3q5be1

Guiraud pushed a commit to Guiraud/LibreChat that referenced this pull request Nov 21, 2025
* feat: Add Bedrock Cache Control Functionality

- fix: Update Bedrock Cache Control to Require cachePoint as a Separate Content Block

- Modified the addBedrockCacheControl function to ensure cachePoint is added as a separate content block in the content array, rather than as a property of text objects.

- refactor: move addBedrockCacheControl over to packages/api

- ci: add tests for addBedrockCacheControl until full coverage reached

* ci: add test similar to example from the langchain PR

* refactor: move addBedrockCacheControl logic and tests to agents repository

* chore: remove extraneous comment

* chore: update @librechat/agents dependency to version 3.0.12

* chore: update @librechat/agents dependency to version 3.0.13

* chore: update @librechat/agents dependency to version 3.0.14

* chore: update @librechat/agents to v3.0.15

* chore: update default value for prompt cache setting to true

* refactor: set default promptCache to true for claude and nova models

---------

Co-authored-by: Danny Avila <danny@librechat.ai>
jcbartle pushed a commit to jcbartle/LibreChat that referenced this pull request May 11, 2026
* feat: Add Bedrock Cache Control Functionality

- fix: Update Bedrock Cache Control to Require cachePoint as a Separate Content Block

- Modified the addBedrockCacheControl function to ensure cachePoint is added as a separate content block in the content array, rather than as a property of text objects.

- refactor: move addBedrockCacheControl over to packages/api

- ci: add tests for addBedrockCacheControl until full coverage reached

* ci: add test similar to example from the langchain PR

* refactor: move addBedrockCacheControl logic and tests to agents repository

* chore: remove extraneous comment

* chore: update @librechat/agents dependency to version 3.0.12

* chore: update @librechat/agents dependency to version 3.0.13

* chore: update @librechat/agents dependency to version 3.0.14

* chore: update @librechat/agents to v3.0.15

* chore: update default value for prompt cache setting to true

* refactor: set default promptCache to true for claude and nova models

---------

Co-authored-by: Danny Avila <danny@librechat.ai>
ThomasVuNguyen pushed a commit to ThomasVuNguyen/LibreChat that referenced this pull request Jul 15, 2026
* feat: Add Bedrock Cache Control Functionality

- fix: Update Bedrock Cache Control to Require cachePoint as a Separate Content Block

- Modified the addBedrockCacheControl function to ensure cachePoint is added as a separate content block in the content array, rather than as a property of text objects.

- refactor: move addBedrockCacheControl over to packages/api

- ci: add tests for addBedrockCacheControl until full coverage reached

* ci: add test similar to example from the langchain PR

* refactor: move addBedrockCacheControl logic and tests to agents repository

* chore: remove extraneous comment

* chore: update @librechat/agents dependency to version 3.0.12

* chore: update @librechat/agents dependency to version 3.0.13

* chore: update @librechat/agents dependency to version 3.0.14

* chore: update @librechat/agents to v3.0.15

* chore: update default value for prompt cache setting to true

* refactor: set default promptCache to true for claude and nova models

---------

Co-authored-by: Danny Avila <danny@librechat.ai>
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.