Skip to content

Commit 72b29ea

Browse files
committed
feat(js/plugins/vertexai): add support to anthropic prompt caching
1 parent 2b8df79 commit 72b29ea

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

js/plugins/vertexai/src/modelgarden/anthropic.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,15 @@ export function fromAnthropicResponse(
309309
role: 'model',
310310
content: parts.map(fromAnthropicPart),
311311
};
312+
let usageCustom: Record<string, number> | undefined = undefined;
313+
if ( response.usage.cache_creation_input_tokens) {
314+
usageCustom = {};
315+
usageCustom['cacheCreationInputTokens']= response.usage.cache_creation_input_tokens;
316+
}
317+
if ( response.usage.cache_read_input_tokens) {
318+
usageCustom = usageCustom || {};
319+
usageCustom['cacheReadInputTokens']= response.usage.cache_read_input_tokens;
320+
}
312321
return {
313322
message,
314323
finishReason: toGenkitFinishReason(
@@ -328,6 +337,7 @@ export function fromAnthropicResponse(
328337
...getBasicUsageStats(input.messages, message),
329338
inputTokens: response.usage.input_tokens,
330339
outputTokens: response.usage.output_tokens,
340+
custom: usageCustom
331341
},
332342
};
333343
}

js/plugins/vertexai/tests/modelgarden/anthropic_test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,12 @@ describe('toAnthropicRequest', () => {
134134
messages: [
135135
{
136136
role: 'user',
137-
content: {
138-
type: 'text',
139-
text: 'What are the key terms and conditions in this agreement?',
140-
},
137+
content: [
138+
{
139+
type: 'text',
140+
text: 'What are the key terms and conditions in this agreement?',
141+
},
142+
],
141143
},
142144
],
143145
},
@@ -218,6 +220,8 @@ describe('fromAnthropicResponse', () => {
218220
usage: {
219221
input_tokens: 123,
220222
output_tokens: 234,
223+
cache_creation_input_tokens: 0,
224+
cache_read_input_tokens: 0,
221225
},
222226
stop_sequence: null,
223227
type: 'message',
@@ -300,6 +304,8 @@ describe('fromAnthropicResponse', () => {
300304
usage: {
301305
input_tokens: 123,
302306
output_tokens: 234,
307+
cache_creation_input_tokens: 0,
308+
cache_read_input_tokens: 0,
303309
},
304310
content: [
305311
{

0 commit comments

Comments
 (0)