MCPcopy Create free account
hub / github.com/QodeXcli/QodeX / countTokens

Function countTokens

src/utils/tokenizer.ts:92–106  ·  view source on GitHub ↗
(text: string | null | undefined)

Source from the content-addressed store, hash-verified

90 * heuristic. Never throws.
91 */
92export function countTokens(text: string | null | undefined): number {
93 if (!text) return 0;
94 if (encoder) {
95 try {
96 return encoder(text).length;
97 } catch (err) {
98 if (!warnedEncoderFailed) {
99 warnedEncoderFailed = true;
100 logger.debug('tokenizer encoder failed; falling back to heuristic', { err });
101 }
102 /* fall through */
103 }
104 }
105 return heuristicTokens(text);
106}
107
108/** Count tokens for an arbitrary JSON-serializable value (tool args, etc.). */
109export function countTokensJson(value: unknown): number {

Callers 6

countTokensJsonFunction · 0.85
estimateTokensFunction · 0.85
estimateTokensFunction · 0.85
buildTaskContextFunction · 0.85
estimateNaiveTokensFunction · 0.85

Calls 2

heuristicTokensFunction · 0.85
debugMethod · 0.80

Tested by

no test coverage detected