fix: anthropic optimized caching strategy

This commit is contained in:
Dallin Romney 2025-08-19 14:56:06 -07:00
parent 1ce60a0091
commit f1afd14202
2 changed files with 13 additions and 2 deletions

View file

@ -219,14 +219,16 @@ describe("AnthropicCachingStrategies", () => {
expect(result.messages).toEqual([{ content: smallContent }]);
});
it("should cache large text items in array content", () => {
it("should cache large text items in array content but only once per message", () => {
const largeText = "a".repeat(2100); // ~525 tokens
const anotherLargeText = "b".repeat(2100); // Also ~525 tokens
const smallText = "a".repeat(100); // ~25 tokens
const body = {
messages: [
{
content: [
{ type: "text", text: largeText },
{ type: "text", text: anotherLargeText },
{ type: "text", text: smallText },
{ type: "image", data: "image_data" },
],
@ -236,6 +238,7 @@ describe("AnthropicCachingStrategies", () => {
const result = CACHING_STRATEGIES.optimized(body);
// Only the first large text item should get cache_control
expect(result.messages).toEqual([
{
content: [
@ -244,6 +247,7 @@ describe("AnthropicCachingStrategies", () => {
text: largeText,
cache_control: { type: "ephemeral" },
},
{ type: "text", text: anotherLargeText },
{ type: "text", text: smallText },
{ type: "image", data: "image_data" },
],

View file

@ -118,11 +118,18 @@ const optimizedStrategy: CachingStrategy = (body) => {
};
}
} else if (message.content && Array.isArray(message.content)) {
// Only add one cache control per message with array content
let addedCacheControl = false;
const updatedContent = message.content.map((item: any) => {
if (item.type === "text" && item.text) {
const tokens = estimateTokenCount(item.text);
if (tokens > 500 && availableCacheMessages > 0) {
if (
tokens > 500 &&
availableCacheMessages > 0 &&
!addedCacheControl
) {
availableCacheMessages -= 1;
addedCacheControl = true;
return {
...item,
cache_control: { type: "ephemeral" },