Skip to content

Commit 3dfa8d1

Browse files
committed
cache system prompt as well.
1 parent 870cbee commit 3dfa8d1

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

packages/agent/src/core/toolAgent.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
ToolResultPart,
99
ToolSet,
1010
tool as makeTool,
11-
Message
1211
} from 'ai';
1312
import chalk from 'chalk';
1413

@@ -192,40 +191,47 @@ async function executeTools(
192191
};
193192
}
194193

194+
function createCacheControlMessageFromSystemPrompt(
195+
systemPrompt: string,
196+
): CoreMessage {
197+
return {
198+
role: 'system',
199+
content: systemPrompt,
200+
providerOptions: {
201+
anthropic: { cacheControl: { type: 'ephemeral' } },
202+
},
203+
};
204+
}
205+
195206
/**
196207
* Adds cache control to the messages for token caching with the Vercel AI SDK
197208
* This marks the last two messages as ephemeral which allows the conversation up to that
198209
* point to be cached (with a ~5 minute window), reducing token usage when making multiple API calls
199210
*/
200211
function addCacheControlToMessages(messages: CoreMessage[]): CoreMessage[] {
201212
if (messages.length <= 1) return messages;
202-
213+
203214
// Create a deep copy of the messages array to avoid mutating the original
204215
const result = JSON.parse(JSON.stringify(messages)) as CoreMessage[];
205-
216+
206217
// Get the last two messages (if available)
207-
const lastTwoMessageIndices = [
208-
messages.length - 1,
209-
messages.length - 2
210-
];
211-
218+
const lastTwoMessageIndices = [messages.length - 1, messages.length - 2];
219+
212220
// Add providerOptions with anthropic cache control to the last two messages
213-
lastTwoMessageIndices.forEach(index => {
221+
lastTwoMessageIndices.forEach((index) => {
214222
if (index >= 0) {
215223
const message = result[index];
216224
if (message) {
217225
// For the Vercel AI SDK, we need to add the providerOptions.anthropic property
218226
// with cacheControl: 'ephemeral' to enable token caching
219227
message.providerOptions = {
220228
...message.providerOptions,
221-
anthropic: {
222-
cacheControl: 'ephemeral'
223-
}
229+
anthropic: { cacheControl: { type: 'ephemeral' } },
224230
};
225231
}
226232
}
227233
});
228-
234+
229235
return result;
230236
}
231237

@@ -275,13 +281,15 @@ export const toolAgent = async (
275281
});
276282
});
277283
// Apply cache control to messages for token caching
278-
const messagesWithCacheControl = addCacheControlToMessages(messages);
279-
284+
const messagesWithCacheControl = [
285+
createCacheControlMessageFromSystemPrompt(systemPrompt),
286+
...addCacheControlToMessages(messages),
287+
];
288+
280289
const generateTextProps = {
281290
model: config.model,
282291
temperature: config.temperature,
283292
messages: messagesWithCacheControl,
284-
system: systemPrompt,
285293
tools: toolSet,
286294
};
287295
const { text, toolCalls } = await generateText(generateTextProps);

0 commit comments

Comments
 (0)