|
1 | 1 | import Anthropic from '@anthropic-ai/sdk'; |
2 | 2 |
|
3 | | -export type TokenUsage = { |
4 | | - input: number; |
5 | | - inputCacheWrites: number; |
6 | | - inputCacheReads: number; |
7 | | - output: number; |
8 | | -}; |
9 | | - |
10 | | -export const getTokenUsage = (response: Anthropic.Message): TokenUsage => { |
11 | | - return { |
12 | | - input: response.usage.input_tokens, |
13 | | - inputCacheWrites: response.usage.cache_creation_input_tokens ?? 0, |
14 | | - inputCacheReads: response.usage.cache_read_input_tokens ?? 0, |
15 | | - output: response.usage.output_tokens, |
16 | | - }; |
17 | | -}; |
18 | | - |
19 | | -export const addTokenUsage = (a: TokenUsage, b: TokenUsage): TokenUsage => { |
20 | | - return { |
21 | | - input: a.input + b.input, |
22 | | - inputCacheWrites: a.inputCacheWrites + b.inputCacheWrites, |
23 | | - inputCacheReads: a.inputCacheReads + b.inputCacheReads, |
24 | | - output: a.output + b.output, |
25 | | - }; |
26 | | -}; |
| 3 | +import { LogLevel } from '../utils/logger.js'; |
27 | 4 |
|
28 | 5 | const PER_MILLION = 1 / 1000000; |
29 | | -const TOKEN_COST: TokenUsage = { |
| 6 | +const TOKEN_COST = { |
30 | 7 | input: 3 * PER_MILLION, |
31 | | - inputCacheWrites: 3.75 * PER_MILLION, |
32 | | - inputCacheReads: 0.3 * PER_MILLION, |
| 8 | + cacheWrites: 3.75 * PER_MILLION, |
| 9 | + cacheReads: 0.3 * PER_MILLION, |
33 | 10 | output: 15 * PER_MILLION, |
34 | 11 | }; |
35 | 12 |
|
36 | | -const formatter = new Intl.NumberFormat('en-US', { |
37 | | - style: 'currency', |
38 | | - currency: 'USD', |
39 | | - minimumFractionDigits: 2, |
40 | | -}); |
41 | | - |
42 | | -export const getTokenCost = (usage: TokenUsage): string => { |
43 | | - return formatter.format( |
44 | | - usage.input * TOKEN_COST.input + |
45 | | - usage.inputCacheWrites * TOKEN_COST.inputCacheWrites + |
46 | | - usage.inputCacheReads * TOKEN_COST.inputCacheReads + |
47 | | - usage.output * TOKEN_COST.output, |
48 | | - ); |
49 | | -}; |
| 13 | +export class TokenUsage { |
| 14 | + public input: number = 0; |
| 15 | + public cacheWrites: number = 0; |
| 16 | + public cacheReads: number = 0; |
| 17 | + public output: number = 0; |
50 | 18 |
|
51 | | -export const getTokenString = (usage: TokenUsage): string => { |
52 | | - return `input: ${usage.input} input-cache-writes: ${usage.inputCacheWrites} input-cache-reads: ${usage.inputCacheReads} output: ${usage.output} COST: ${getTokenCost(usage)}`; |
53 | | -}; |
| 19 | + constructor() {} |
| 20 | + |
| 21 | + add(usage: TokenUsage) { |
| 22 | + this.input += usage.input; |
| 23 | + this.cacheWrites += usage.cacheWrites; |
| 24 | + this.cacheReads += usage.cacheReads; |
| 25 | + this.output += usage.output; |
| 26 | + } |
| 27 | + |
| 28 | + clone() { |
| 29 | + const usage = new TokenUsage(); |
| 30 | + usage.input = this.input; |
| 31 | + usage.cacheWrites = this.cacheWrites; |
| 32 | + usage.cacheReads = this.cacheReads; |
| 33 | + usage.output = this.output; |
| 34 | + return usage; |
| 35 | + } |
| 36 | + |
| 37 | + static fromMessage(message: Anthropic.Message) { |
| 38 | + const usage = new TokenUsage(); |
| 39 | + usage.input = message.usage.input_tokens; |
| 40 | + usage.cacheWrites = message.usage.cache_creation_input_tokens ?? 0; |
| 41 | + usage.cacheReads = message.usage.cache_read_input_tokens ?? 0; |
| 42 | + usage.output = message.usage.output_tokens; |
| 43 | + return usage; |
| 44 | + } |
| 45 | + |
| 46 | + static sum(usages: TokenUsage[]) { |
| 47 | + const usage = new TokenUsage(); |
| 48 | + usages.forEach((u) => usage.add(u)); |
| 49 | + return usage; |
| 50 | + } |
| 51 | + |
| 52 | + getCost() { |
| 53 | + const formatter = new Intl.NumberFormat('en-US', { |
| 54 | + style: 'currency', |
| 55 | + currency: 'USD', |
| 56 | + minimumFractionDigits: 2, |
| 57 | + }); |
| 58 | + |
| 59 | + return formatter.format( |
| 60 | + this.input * TOKEN_COST.input + |
| 61 | + this.cacheWrites * TOKEN_COST.cacheWrites + |
| 62 | + this.cacheReads * TOKEN_COST.cacheReads + |
| 63 | + this.output * TOKEN_COST.output, |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + toString() { |
| 68 | + return `input: ${this.input} cache-writes: ${this.cacheWrites} cache-reads: ${this.cacheReads} output: ${this.output} COST: ${this.getCost()}`; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +export class TokenTracker { |
| 73 | + public tokenUsage = new TokenUsage(); |
| 74 | + public children: TokenTracker[] = []; |
| 75 | + |
| 76 | + constructor( |
| 77 | + public readonly name: string = 'unnamed', |
| 78 | + public readonly parent: TokenTracker | undefined = undefined, |
| 79 | + public readonly logLevel: LogLevel = parent?.logLevel ?? LogLevel.debug, |
| 80 | + ) { |
| 81 | + if (parent) { |
| 82 | + parent.children.push(this); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + getTotalUsage() { |
| 87 | + const usage = this.tokenUsage.clone(); |
| 88 | + this.children.forEach((child) => usage.add(child.getTotalUsage())); |
| 89 | + return usage; |
| 90 | + } |
| 91 | + |
| 92 | + getTotalCost() { |
| 93 | + const usage = this.getTotalUsage(); |
| 94 | + return usage.getCost(); |
| 95 | + } |
| 96 | + |
| 97 | + toString() { |
| 98 | + return `${this.name}: ${this.getTotalUsage().toString()}`; |
| 99 | + } |
| 100 | +} |
0 commit comments