Elffuss BraidHub

kikocis/continue public

fix: anthropic optimized caching strategy
Changeset93a8eb04e36cf8b79853c6fa8a0fa1d035d4a899a078047bb3506446414e482e
AuthorDallin Romney <dallinromney@gmail.com> · 2025-08-19 14:56 -07:00
CommitterDallin Romney <dallinromney@gmail.com> · 2025-08-19 14:56 -07:00
Parentseca53d6fa452

Browse the files at this changeset

2 files changed

modified packages/openai-adapters/src/apis/AnthropicCachingStrategies.test.ts +5 −1
@@ -219,14 +219,16 @@
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 @@
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 @@
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 ],
modified packages/openai-adapters/src/apis/AnthropicCachingStrategies.ts +8 −1
@@ -118,11 +118,18 @@
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" },