MCPcopy Index your code
hub / github.com/simstudioai/sim / getAccurateTokenCount

Function getAccurateTokenCount

apps/sim/lib/tokenization/estimators.ts:45–58  ·  view source on GitHub ↗
(text: string, modelName = 'text-embedding-3-small')

Source from the content-addressed store, hash-verified

43 * This is the exact count OpenAI's API will use
44 */
45export function getAccurateTokenCount(text: string, modelName = 'text-embedding-3-small'): number {
46 if (!text || text.length === 0) {
47 return 0
48 }
49
50 try {
51 const encoding = getEncoding(modelName)
52 const tokens = encoding.encode(text)
53 return tokens.length
54 } catch (error) {
55 logger.error('Error counting tokens with tiktoken:', error)
56 return Math.ceil(text.length / 4)
57 }
58}
59
60/**
61 * Get individual tokens as strings for visualization

Callers 3

ChunkEditorFunction · 0.90
applyTokenWindowMethod · 0.90
batchByTokenLimitFunction · 0.85

Calls 2

getEncodingFunction · 0.85
errorMethod · 0.80

Tested by

no test coverage detected