| 1 | import { MessageCreateParams } from "@anthropic-ai/sdk/resources"; |
| 2 | |
| 3 | const MAX_CACHING_MESSAGES = 4; |
| 4 | |
| 5 | // Caching strategy type - transforms a clean Anthropic body by adding cache_control |
| 6 | export type CachingStrategy = ( |
| 7 | anthropicBody: MessageCreateParams, |
| 8 | ) => MessageCreateParams; |
| 9 | |
| 10 | // Utility function to estimate token count |
| 11 | const estimateTokenCount = (text: string): number => Math.ceil(text.length / 4); |
| 12 | |
| 13 | // Strategy 1: No Caching |
| 14 | const noCachingStrategy: CachingStrategy = (body) => body; |
| 15 | |
| 16 | // Strategy 2: System Messages Only |
| 17 | const systemOnlyStrategy: CachingStrategy = (body) => { |
| 18 | let availableCacheMessages = MAX_CACHING_MESSAGES; |
| 19 | if (body.system && Array.isArray(body.system)) { |
| 20 | return { |
| 21 | ...body, |
| 22 | system: body.system.map((item) => { |
| 23 | if (availableCacheMessages > 0) { |
| 24 | availableCacheMessages -= 1; |
| 25 | return { |
| 26 | ...item, |
| 27 | cache_control: { type: "ephemeral" }, |
| 28 | }; |
| 29 | } |
| 30 | return item; |
| 31 | }), |
| 32 | }; |
| 33 | } |
| 34 | return body; |
| 35 | }; |
| 36 | |
| 37 | // Strategy 3: System and Tools (High Impact) |
| 38 | const systemAndToolsStrategy: CachingStrategy = (body) => { |
| 39 | const result = { ...body }; |
| 40 | let availableCacheMessages = MAX_CACHING_MESSAGES; |
| 41 | |
| 42 | // Cache system messages |
| 43 | if (result.system && Array.isArray(result.system)) { |
| 44 | result.system = result.system.map((item) => { |
| 45 | if (availableCacheMessages > 0) { |
| 46 | availableCacheMessages -= 1; |
| 47 | return { |
| 48 | ...item, |
| 49 | cache_control: { type: "ephemeral" }, |
| 50 | }; |
| 51 | } |
| 52 | return item; |
| 53 | }); |
| 54 | } |
| 55 | |
| 56 | // Cache tool definitions |
| 57 | if (result.tools && Array.isArray(result.tools) && result.tools.length > 0) { |
| 58 | result.tools = result.tools.map((tool, index: number) => { |
| 59 | if (index === result.tools!.length - 1 && availableCacheMessages > 0) { |
| 60 | availableCacheMessages -= 1; |
| 61 | return { |
| 62 | ...tool, |
| 63 | cache_control: { type: "ephemeral" }, |
| 64 | }; |
| 65 | } |
| 66 | return tool; |
| 67 | }); |
| 68 | } |
| 69 | |
| 70 | return result; |
| 71 | }; |
| 72 | |
| 73 | // Strategy 4: Optimized (Intelligent Caching) |
| 74 | const optimizedStrategy: CachingStrategy = (body) => { |
| 75 | const result = { ...body }; |
| 76 | let availableCacheMessages = MAX_CACHING_MESSAGES; |
| 77 | |
| 78 | // Always cache system messages |
| 79 | if (result.system && Array.isArray(result.system)) { |
| 80 | result.system = result.system.map((item) => { |
| 81 | if (availableCacheMessages > 0) { |
| 82 | availableCacheMessages -= 1; |
| 83 | return { |
| 84 | ...item, |
| 85 | cache_control: { type: "ephemeral" }, |
| 86 | }; |
| 87 | } |
| 88 | return item; |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | // Cache tool definitions |
| 93 | if (result.tools && Array.isArray(result.tools) && result.tools.length > 0) { |
| 94 | result.tools = result.tools.map((tool, index: number) => { |
| 95 | if (index === result.tools!.length - 1 && availableCacheMessages > 0) { |
| 96 | availableCacheMessages -= 1; |
| 97 | return { |
| 98 | ...tool, |
| 99 | cache_control: { type: "ephemeral" }, |
| 100 | }; |
| 101 | } |
| 102 | return tool; |
| 103 | }); |
| 104 | } |
| 105 | |
| 106 | // Cache large messages (>500 tokens) |
| 107 | if (result.messages && Array.isArray(result.messages)) { |
| 108 | result.messages = result.messages.map((message) => { |
| 109 | if (message.content && typeof message.content === "string") { |
| 110 | const tokens = estimateTokenCount(message.content); |
| 111 | if (tokens > 500 && availableCacheMessages > 0) { |
| 112 | availableCacheMessages -= 1; |
| 113 | return { |
| 114 | ...message, |
| 115 | content: [ |
| 116 | { |
| 117 | type: "text", |
| 118 | text: message.content, |
| 119 | cache_control: { type: "ephemeral" as const }, |
| 120 | }, |
| 121 | ], |
| 122 | }; |
| 123 | } |
| 124 | } else if (message.content && Array.isArray(message.content)) { |
| 125 | // Only add one cache control per message with array content |
| 126 | let addedCacheControl = false; |
| 127 | const updatedContent = message.content.map((item) => { |
| 128 | if (item.type === "text" && item.text) { |
| 129 | const tokens = estimateTokenCount(item.text); |
| 130 | if ( |
| 131 | tokens > 500 && |
| 132 | availableCacheMessages > 0 && |
| 133 | !addedCacheControl |
| 134 | ) { |
| 135 | availableCacheMessages -= 1; |
| 136 | addedCacheControl = true; |
| 137 | return { |
| 138 | ...item, |
| 139 | cache_control: { type: "ephemeral" as const }, |
| 140 | }; |
| 141 | } |
| 142 | } |
| 143 | return item; |
| 144 | }); |
| 145 | |
| 146 | return { |
| 147 | ...message, |
| 148 | content: updatedContent, |
| 149 | }; |
| 150 | } |
| 151 | return message; |
| 152 | }); |
| 153 | } |
| 154 | |
| 155 | return result; |
| 156 | }; |
| 157 | |
| 158 | // Available caching strategies |
| 159 | export const CACHING_STRATEGIES = { |
| 160 | none: noCachingStrategy, |
| 161 | systemOnly: systemOnlyStrategy, |
| 162 | systemAndTools: systemAndToolsStrategy, |
| 163 | optimized: optimizedStrategy, |
| 164 | } as const; |
| 165 | |
| 166 | export type CachingStrategyName = keyof typeof CACHING_STRATEGIES; |
| 167 | |
| 168 | // Helper function to get available strategies |
| 169 | export const getAvailableStrategies = () => { |
| 170 | return Object.keys(CACHING_STRATEGIES) as CachingStrategyName[]; |
| 171 | }; |
| 172 | |
| 173 | // Helper function to get strategy description |
| 174 | export const getStrategyDescription = ( |
| 175 | strategy: CachingStrategyName, |
| 176 | ): string => { |
| 177 | const descriptions = { |
| 178 | none: "No caching - baseline for comparison", |
| 179 | systemOnly: "Cache only system messages (current implementation)", |
| 180 | systemAndTools: "Cache system messages and tool definitions (high impact)", |
| 181 | optimized: |
| 182 | "Intelligent caching - system, tools, and large content (best performance)", |
| 183 | }; |
| 184 | return descriptions[strategy]; |
| 185 | }; |