fix: add maximum 4 cache control blocks for anthropic (#6937)
* add max 5 cache controls * add tests * use MAX_CACHING_MESSAGES instead
This commit is contained in:
parent
b11f5cab3a
commit
217b463ede
2 changed files with 131 additions and 16 deletions
|
|
@ -59,6 +59,30 @@ describe("AnthropicCachingStrategies", () => {
|
|||
const result = CACHING_STRATEGIES.systemOnly(body);
|
||||
expect(result).toEqual(body);
|
||||
});
|
||||
|
||||
it("should not add more than 5 cached messages", () => {
|
||||
const body = {
|
||||
system: [
|
||||
{ text: "system message 1" },
|
||||
{ text: "system message 2" },
|
||||
{ text: "system message 3" },
|
||||
{ text: "system message 4" },
|
||||
{ text: "system message 5" },
|
||||
],
|
||||
messages: [{ content: "user message" }],
|
||||
};
|
||||
|
||||
const result = CACHING_STRATEGIES.systemOnly(body);
|
||||
|
||||
expect(result.system).toEqual([
|
||||
{ text: "system message 1", cache_control: { type: "ephemeral" } },
|
||||
{ text: "system message 2", cache_control: { type: "ephemeral" } },
|
||||
{ text: "system message 3", cache_control: { type: "ephemeral" } },
|
||||
{ text: "system message 4", cache_control: { type: "ephemeral" } },
|
||||
{ text: "system message 5" },
|
||||
]);
|
||||
expect(result.messages).toEqual(body.messages);
|
||||
});
|
||||
});
|
||||
|
||||
describe("systemAndToolsStrategy", () => {
|
||||
|
|
@ -119,6 +143,30 @@ describe("AnthropicCachingStrategies", () => {
|
|||
const result = CACHING_STRATEGIES.systemAndTools(body);
|
||||
expect(result).toEqual(body);
|
||||
});
|
||||
|
||||
it("should add only 4 cache controls in total to both system and tools", () => {
|
||||
const body = {
|
||||
system: [
|
||||
{ text: "system message 1" },
|
||||
{ text: "system message 2" },
|
||||
{ text: "system message 3" },
|
||||
{ text: "system message 4" },
|
||||
],
|
||||
tools: [{ name: "tool1" }, { name: "tool2" }],
|
||||
messages: [{ content: "user message" }],
|
||||
};
|
||||
|
||||
const result = CACHING_STRATEGIES.systemAndTools(body);
|
||||
|
||||
expect(result.system).toEqual([
|
||||
{ text: "system message 1", cache_control: { type: "ephemeral" } },
|
||||
{ text: "system message 2", cache_control: { type: "ephemeral" } },
|
||||
{ text: "system message 3", cache_control: { type: "ephemeral" } },
|
||||
{ text: "system message 4", cache_control: { type: "ephemeral" } },
|
||||
]);
|
||||
expect(result.tools).toEqual(body.tools);
|
||||
expect(result.messages).toEqual(body.messages);
|
||||
});
|
||||
});
|
||||
|
||||
describe("optimizedStrategy", () => {
|
||||
|
|
@ -203,6 +251,46 @@ describe("AnthropicCachingStrategies", () => {
|
|||
]);
|
||||
});
|
||||
|
||||
it("should add maximum 4 cache control blocks", () => {
|
||||
const body = {
|
||||
system: [{ text: "system message 1" }, { text: "system message 2" }],
|
||||
tools: [{ name: "tool" }],
|
||||
messages: [
|
||||
{ content: "small content" },
|
||||
{ content: "a".repeat(2100) },
|
||||
{ content: "b".repeat(2100) },
|
||||
{
|
||||
content: [
|
||||
{ type: "text", text: "c".repeat(2100) },
|
||||
{ type: "image", data: "image" },
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const result = CACHING_STRATEGIES.optimized(body);
|
||||
|
||||
expect(result.system).toEqual([
|
||||
{ text: "system message 1", cache_control: { type: "ephemeral" } },
|
||||
{ text: "system message 2", cache_control: { type: "ephemeral" } },
|
||||
]);
|
||||
expect(result.tools).toEqual([
|
||||
{ name: "tool", cache_control: { type: "ephemeral" } },
|
||||
]);
|
||||
expect(result.messages[0]).toEqual({ content: "small content" });
|
||||
expect(result.messages[1]).toEqual({
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: "a".repeat(2100),
|
||||
cache_control: { type: "ephemeral" },
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(result.messages[2].content).toEqual(body.messages[2].content);
|
||||
expect(result.messages[3].content).toEqual(body.messages[3].content);
|
||||
});
|
||||
|
||||
it("should handle complex message structure", () => {
|
||||
const body = {
|
||||
system: [{ text: "system" }],
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
const MAX_CACHING_MESSAGES = 4;
|
||||
|
||||
// Caching strategy type - transforms a clean Anthropic body by adding cache_control
|
||||
export type CachingStrategy = (anthropicBody: any) => any;
|
||||
|
||||
|
|
@ -9,13 +11,20 @@ const noCachingStrategy: CachingStrategy = (body) => body;
|
|||
|
||||
// Strategy 2: System Messages Only (Current Implementation)
|
||||
const systemOnlyStrategy: CachingStrategy = (body) => {
|
||||
let availableCacheMessages = MAX_CACHING_MESSAGES;
|
||||
if (body.system && Array.isArray(body.system)) {
|
||||
return {
|
||||
...body,
|
||||
system: body.system.map((item: any) => ({
|
||||
...item,
|
||||
cache_control: { type: "ephemeral" },
|
||||
})),
|
||||
system: body.system.map((item: any) => {
|
||||
if (availableCacheMessages > 0) {
|
||||
availableCacheMessages -= 1;
|
||||
return {
|
||||
...item,
|
||||
cache_control: { type: "ephemeral" },
|
||||
};
|
||||
}
|
||||
return item;
|
||||
}),
|
||||
};
|
||||
}
|
||||
return body;
|
||||
|
|
@ -24,19 +33,27 @@ const systemOnlyStrategy: CachingStrategy = (body) => {
|
|||
// Strategy 3: System and Tools (High Impact)
|
||||
const systemAndToolsStrategy: CachingStrategy = (body) => {
|
||||
const result = { ...body };
|
||||
let availableCacheMessages = MAX_CACHING_MESSAGES;
|
||||
|
||||
// Cache system messages
|
||||
if (result.system && Array.isArray(result.system)) {
|
||||
result.system = result.system.map((item: any) => ({
|
||||
...item,
|
||||
cache_control: { type: "ephemeral" },
|
||||
}));
|
||||
result.system = result.system.map((item: any) => {
|
||||
if (availableCacheMessages > 0) {
|
||||
availableCacheMessages -= 1;
|
||||
return {
|
||||
...item,
|
||||
cache_control: { type: "ephemeral" },
|
||||
};
|
||||
}
|
||||
return item;
|
||||
});
|
||||
}
|
||||
|
||||
// Cache tool definitions
|
||||
if (result.tools && Array.isArray(result.tools) && result.tools.length > 0) {
|
||||
result.tools = result.tools.map((tool: any, index: number) => {
|
||||
if (index === result.tools.length - 1) {
|
||||
if (index === result.tools.length - 1 && availableCacheMessages > 0) {
|
||||
availableCacheMessages -= 1;
|
||||
return {
|
||||
...tool,
|
||||
cache_control: { type: "ephemeral" },
|
||||
|
|
@ -52,19 +69,27 @@ const systemAndToolsStrategy: CachingStrategy = (body) => {
|
|||
// Strategy 4: Optimized (Intelligent Caching)
|
||||
const optimizedStrategy: CachingStrategy = (body) => {
|
||||
const result = { ...body };
|
||||
let availableCacheMessages = MAX_CACHING_MESSAGES;
|
||||
|
||||
// Always cache system messages
|
||||
if (result.system && Array.isArray(result.system)) {
|
||||
result.system = result.system.map((item: any) => ({
|
||||
...item,
|
||||
cache_control: { type: "ephemeral" },
|
||||
}));
|
||||
result.system = result.system.map((item: any) => {
|
||||
if (availableCacheMessages > 0) {
|
||||
availableCacheMessages -= 1;
|
||||
return {
|
||||
...item,
|
||||
cache_control: { type: "ephemeral" },
|
||||
};
|
||||
}
|
||||
return item;
|
||||
});
|
||||
}
|
||||
|
||||
// Cache tool definitions
|
||||
if (result.tools && Array.isArray(result.tools) && result.tools.length > 0) {
|
||||
result.tools = result.tools.map((tool: any, index: number) => {
|
||||
if (index === result.tools.length - 1) {
|
||||
if (index === result.tools.length - 1 && availableCacheMessages > 0) {
|
||||
availableCacheMessages -= 1;
|
||||
return {
|
||||
...tool,
|
||||
cache_control: { type: "ephemeral" },
|
||||
|
|
@ -79,7 +104,8 @@ const optimizedStrategy: CachingStrategy = (body) => {
|
|||
result.messages = result.messages.map((message: any) => {
|
||||
if (message.content && typeof message.content === "string") {
|
||||
const tokens = estimateTokenCount(message.content);
|
||||
if (tokens > 500) {
|
||||
if (tokens > 500 && availableCacheMessages > 0) {
|
||||
availableCacheMessages -= 1;
|
||||
return {
|
||||
...message,
|
||||
content: [
|
||||
|
|
@ -95,7 +121,8 @@ const optimizedStrategy: CachingStrategy = (body) => {
|
|||
const updatedContent = message.content.map((item: any) => {
|
||||
if (item.type === "text" && item.text) {
|
||||
const tokens = estimateTokenCount(item.text);
|
||||
if (tokens > 500) {
|
||||
if (tokens > 500 && availableCacheMessages > 0) {
|
||||
availableCacheMessages -= 1;
|
||||
return {
|
||||
...item,
|
||||
cache_control: { type: "ephemeral" },
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue