@adminforth/completion-adapter-anthropic-messages 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -26,6 +26,11 @@ type LangChainModelCallRequest = {
26
26
  };
27
27
  messages: LangChainMessageLike[];
28
28
  };
29
+ type UsedTokens = {
30
+ input_uncached: number;
31
+ input_cached: number;
32
+ output: number;
33
+ };
29
34
  export default class CompletionAdapterAnthropicMessages implements CompletionAdapter {
30
35
  options: AdapterOptions;
31
36
  private client?;
@@ -47,5 +52,6 @@ export default class CompletionAdapterAnthropicMessages implements CompletionAda
47
52
  content?: string;
48
53
  finishReason?: string;
49
54
  error?: string;
55
+ used_tokens?: UsedTokens;
50
56
  }>;
51
57
  }
package/dist/index.js CHANGED
@@ -57,6 +57,20 @@ function extractReasoning(data) {
57
57
  }
58
58
  return reasoning || undefined;
59
59
  }
60
+ function extractUsedTokens(data) {
61
+ var _a, _b, _c, _d;
62
+ const usage = data.usage;
63
+ if (!usage)
64
+ return undefined;
65
+ const inputTokens = (_a = usage.input_tokens) !== null && _a !== void 0 ? _a : 0;
66
+ const cacheWriteTokens = (_b = usage.cache_creation_input_tokens) !== null && _b !== void 0 ? _b : 0;
67
+ const cacheReadTokens = (_c = usage.cache_read_input_tokens) !== null && _c !== void 0 ? _c : 0;
68
+ return {
69
+ input_uncached: inputTokens + cacheWriteTokens,
70
+ input_cached: cacheReadTokens,
71
+ output: (_d = usage.output_tokens) !== null && _d !== void 0 ? _d : 0,
72
+ };
73
+ }
60
74
  function extractToolUse(data) {
61
75
  var _a;
62
76
  for (const block of (_a = data.content) !== null && _a !== void 0 ? _a : []) {
@@ -200,6 +214,7 @@ export default class CompletionAdapterAnthropicMessages {
200
214
  ? extractOutputText(parsedMessage)
201
215
  : JSON.stringify(parsedMessage.parsed_output);
202
216
  const parsedReasoning = extractReasoning(parsedMessage);
217
+ const usedTokens = extractUsedTokens(parsedMessage);
203
218
  if (parsedReasoning && isStreaming) {
204
219
  yield (streamChunkCallback === null || streamChunkCallback === void 0 ? void 0 : streamChunkCallback(parsedReasoning, {
205
220
  type: "reasoning",
@@ -228,22 +243,26 @@ export default class CompletionAdapterAnthropicMessages {
228
243
  return {
229
244
  content: toolResult,
230
245
  finishReason: "tool_call",
246
+ used_tokens: usedTokens,
231
247
  };
232
248
  }
233
249
  catch (error) {
234
250
  return {
235
251
  error: getErrorMessage(error),
236
252
  finishReason: "tool_call",
253
+ used_tokens: usedTokens,
237
254
  };
238
255
  }
239
256
  }
240
257
  return {
241
258
  content: parsedOutput || undefined,
242
259
  finishReason: parsedMessage.stop_reason || undefined,
260
+ used_tokens: usedTokens,
243
261
  };
244
262
  }
245
263
  if (!isStreaming) {
246
264
  const message = (yield this.getClient().messages.create(body));
265
+ const usedTokens = extractUsedTokens(message);
247
266
  const toolUse = extractToolUse(message);
248
267
  if (toolUse) {
249
268
  try {
@@ -251,18 +270,21 @@ export default class CompletionAdapterAnthropicMessages {
251
270
  return {
252
271
  content: toolResult,
253
272
  finishReason: "tool_call",
273
+ used_tokens: usedTokens,
254
274
  };
255
275
  }
256
276
  catch (error) {
257
277
  return {
258
278
  error: getErrorMessage(error),
259
279
  finishReason: "tool_call",
280
+ used_tokens: usedTokens,
260
281
  };
261
282
  }
262
283
  }
263
284
  return {
264
285
  content: extractOutputText(message) || undefined,
265
286
  finishReason: message.stop_reason || undefined,
287
+ used_tokens: usedTokens,
266
288
  };
267
289
  }
268
290
  const stream = this.getClient().messages.stream(body);
@@ -305,6 +327,7 @@ export default class CompletionAdapterAnthropicMessages {
305
327
  const message = (yield stream.finalMessage());
306
328
  const finalContent = extractOutputText(message);
307
329
  const finalReasoning = extractReasoning(message) || "";
330
+ const usedTokens = extractUsedTokens(message);
308
331
  if (finalReasoning && finalReasoning !== fullReasoning) {
309
332
  const delta = finalReasoning.startsWith(fullReasoning)
310
333
  ? finalReasoning.slice(fullReasoning.length)
@@ -350,6 +373,7 @@ export default class CompletionAdapterAnthropicMessages {
350
373
  return {
351
374
  content: toolResult,
352
375
  finishReason: "tool_call",
376
+ used_tokens: usedTokens,
353
377
  };
354
378
  }
355
379
  catch (error) {
@@ -357,12 +381,14 @@ export default class CompletionAdapterAnthropicMessages {
357
381
  error: getErrorMessage(error),
358
382
  content: fullContent || undefined,
359
383
  finishReason: "tool_call",
384
+ used_tokens: usedTokens,
360
385
  };
361
386
  }
362
387
  }
363
388
  return {
364
389
  content: fullContent || undefined,
365
390
  finishReason: message.stop_reason || undefined,
391
+ used_tokens: usedTokens,
366
392
  };
367
393
  }
368
394
  catch (error) {
package/index.ts CHANGED
@@ -43,6 +43,7 @@ type AnthropicMessageLike = {
43
43
  content?: Array<any>;
44
44
  stop_reason?: string | null;
45
45
  parsed_output?: unknown;
46
+ usage?: AnthropicUsage;
46
47
  };
47
48
 
48
49
  type AnthropicToolUseBlock = {
@@ -67,6 +68,19 @@ type LangChainModelCallRequest = {
67
68
  messages: LangChainMessageLike[];
68
69
  };
69
70
 
71
+ type AnthropicUsage = {
72
+ input_tokens?: number;
73
+ cache_creation_input_tokens?: number | null;
74
+ cache_read_input_tokens?: number | null;
75
+ output_tokens?: number;
76
+ };
77
+
78
+ type UsedTokens = {
79
+ input_uncached: number;
80
+ input_cached: number;
81
+ output: number;
82
+ };
83
+
70
84
  function getApiKey(options: AdapterOptions): string | undefined {
71
85
  return options.anthropicApiKey || options.antropicApiKey;
72
86
  }
@@ -112,6 +126,21 @@ function extractReasoning(data: AnthropicMessageLike): string | undefined {
112
126
  return reasoning || undefined;
113
127
  }
114
128
 
129
+ function extractUsedTokens(data: AnthropicMessageLike): UsedTokens | undefined {
130
+ const usage = data.usage;
131
+ if (!usage) return undefined;
132
+
133
+ const inputTokens = usage.input_tokens ?? 0;
134
+ const cacheWriteTokens = usage.cache_creation_input_tokens ?? 0;
135
+ const cacheReadTokens = usage.cache_read_input_tokens ?? 0;
136
+
137
+ return {
138
+ input_uncached: inputTokens + cacheWriteTokens,
139
+ input_cached: cacheReadTokens,
140
+ output: usage.output_tokens ?? 0,
141
+ };
142
+ }
143
+
115
144
  function extractToolUse(data: AnthropicMessageLike): AnthropicToolUseBlock | undefined {
116
145
  for (const block of data.content ?? []) {
117
146
  if (block?.type === "tool_use") {
@@ -323,6 +352,7 @@ export default class CompletionAdapterAnthropicMessages
323
352
  content?: string;
324
353
  finishReason?: string;
325
354
  error?: string;
355
+ used_tokens?: UsedTokens;
326
356
  }> => {
327
357
  const request =
328
358
  typeof requestOrContent === "string"
@@ -386,6 +416,7 @@ export default class CompletionAdapterAnthropicMessages
386
416
  ? extractOutputText(parsedMessage)
387
417
  : JSON.stringify(parsedMessage.parsed_output);
388
418
  const parsedReasoning = extractReasoning(parsedMessage);
419
+ const usedTokens = extractUsedTokens(parsedMessage);
389
420
 
390
421
  if (parsedReasoning && isStreaming) {
391
422
  await streamChunkCallback?.(parsedReasoning, {
@@ -417,11 +448,13 @@ export default class CompletionAdapterAnthropicMessages
417
448
  return {
418
449
  content: toolResult,
419
450
  finishReason: "tool_call",
451
+ used_tokens: usedTokens,
420
452
  };
421
453
  } catch (error) {
422
454
  return {
423
455
  error: getErrorMessage(error),
424
456
  finishReason: "tool_call",
457
+ used_tokens: usedTokens,
425
458
  };
426
459
  }
427
460
  }
@@ -429,6 +462,7 @@ export default class CompletionAdapterAnthropicMessages
429
462
  return {
430
463
  content: parsedOutput || undefined,
431
464
  finishReason: parsedMessage.stop_reason || undefined,
465
+ used_tokens: usedTokens,
432
466
  };
433
467
  }
434
468
 
@@ -436,6 +470,7 @@ export default class CompletionAdapterAnthropicMessages
436
470
  const message = (await this.getClient().messages.create(
437
471
  body as any,
438
472
  )) as AnthropicMessageLike;
473
+ const usedTokens = extractUsedTokens(message);
439
474
 
440
475
  const toolUse = extractToolUse(message);
441
476
  if (toolUse) {
@@ -444,11 +479,13 @@ export default class CompletionAdapterAnthropicMessages
444
479
  return {
445
480
  content: toolResult,
446
481
  finishReason: "tool_call",
482
+ used_tokens: usedTokens,
447
483
  };
448
484
  } catch (error) {
449
485
  return {
450
486
  error: getErrorMessage(error),
451
487
  finishReason: "tool_call",
488
+ used_tokens: usedTokens,
452
489
  };
453
490
  }
454
491
  }
@@ -456,6 +493,7 @@ export default class CompletionAdapterAnthropicMessages
456
493
  return {
457
494
  content: extractOutputText(message) || undefined,
458
495
  finishReason: message.stop_reason || undefined,
496
+ used_tokens: usedTokens,
459
497
  };
460
498
  }
461
499
 
@@ -492,6 +530,7 @@ export default class CompletionAdapterAnthropicMessages
492
530
  const message = (await stream.finalMessage()) as AnthropicMessageLike;
493
531
  const finalContent = extractOutputText(message);
494
532
  const finalReasoning = extractReasoning(message) || "";
533
+ const usedTokens = extractUsedTokens(message);
495
534
 
496
535
  if (finalReasoning && finalReasoning !== fullReasoning) {
497
536
  const delta = finalReasoning.startsWith(fullReasoning)
@@ -541,12 +580,14 @@ export default class CompletionAdapterAnthropicMessages
541
580
  return {
542
581
  content: toolResult,
543
582
  finishReason: "tool_call",
583
+ used_tokens: usedTokens,
544
584
  };
545
585
  } catch (error) {
546
586
  return {
547
587
  error: getErrorMessage(error),
548
588
  content: fullContent || undefined,
549
589
  finishReason: "tool_call",
590
+ used_tokens: usedTokens,
550
591
  };
551
592
  }
552
593
  }
@@ -554,6 +595,7 @@ export default class CompletionAdapterAnthropicMessages
554
595
  return {
555
596
  content: fullContent || undefined,
556
597
  finishReason: message.stop_reason || undefined,
598
+ used_tokens: usedTokens,
557
599
  };
558
600
  } catch (error) {
559
601
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adminforth/completion-adapter-anthropic-messages",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",