Skip to content

Commit 4b51607

Browse files
Merge pull request #7245 from continuedev/dallin/anthropic-caching-fix
fix: HOTFIX anthropic optimized caching strategy
2 parents e39e672 + f1afd14 commit 4b51607

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

packages/openai-adapters/src/apis/AnthropicCachingStrategies.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,16 @@ describe("AnthropicCachingStrategies", () => {
219219
expect(result.messages).toEqual([{ content: smallContent }]);
220220
});
221221

222-
it("should cache large text items in array content", () => {
222+
it("should cache large text items in array content but only once per message", () => {
223223
const largeText = "a".repeat(2100); // ~525 tokens
224+
const anotherLargeText = "b".repeat(2100); // Also ~525 tokens
224225
const smallText = "a".repeat(100); // ~25 tokens
225226
const body = {
226227
messages: [
227228
{
228229
content: [
229230
{ type: "text", text: largeText },
231+
{ type: "text", text: anotherLargeText },
230232
{ type: "text", text: smallText },
231233
{ type: "image", data: "image_data" },
232234
],
@@ -236,6 +238,7 @@ describe("AnthropicCachingStrategies", () => {
236238

237239
const result = CACHING_STRATEGIES.optimized(body);
238240

241+
// Only the first large text item should get cache_control
239242
expect(result.messages).toEqual([
240243
{
241244
content: [
@@ -244,6 +247,7 @@ describe("AnthropicCachingStrategies", () => {
244247
text: largeText,
245248
cache_control: { type: "ephemeral" },
246249
},
250+
{ type: "text", text: anotherLargeText },
247251
{ type: "text", text: smallText },
248252
{ type: "image", data: "image_data" },
249253
],

packages/openai-adapters/src/apis/AnthropicCachingStrategies.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,18 @@ const optimizedStrategy: CachingStrategy = (body) => {
118118
};
119119
}
120120
} else if (message.content && Array.isArray(message.content)) {
121+
// Only add one cache control per message with array content
122+
let addedCacheControl = false;
121123
const updatedContent = message.content.map((item: any) => {
122124
if (item.type === "text" && item.text) {
123125
const tokens = estimateTokenCount(item.text);
124-
if (tokens > 500 && availableCacheMessages > 0) {
126+
if (
127+
tokens > 500 &&
128+
availableCacheMessages > 0 &&
129+
!addedCacheControl
130+
) {
125131
availableCacheMessages -= 1;
132+
addedCacheControl = true;
126133
return {
127134
...item,
128135
cache_control: { type: "ephemeral" },

0 commit comments

Comments
 (0)