(text: string)
| 24 | * monotonic between LLM round-trips without paying for a tokenizer. |
| 25 | */ |
| 26 | export function estimateTokens(text: string): number { |
| 27 | let asciiCount = 0; |
| 28 | let nonAsciiCount = 0; |
| 29 | for (const char of text) { |
| 30 | if (char.codePointAt(0)! <= 127) { |
| 31 | asciiCount++; |
| 32 | } else { |
| 33 | nonAsciiCount++; |
| 34 | } |
| 35 | } |
| 36 | return Math.ceil(asciiCount / 4) + nonAsciiCount; |
| 37 | } |
| 38 | |
| 39 | export function estimateTokensForMessages(messages: readonly Message[]): number { |
| 40 | let total = 0; |
no outgoing calls
no test coverage detected