MCPcopy
hub / github.com/continuedev/continue / countTokens

Function countTokens

core/llm/countTokens.ts:112–132  ·  view source on GitHub ↗
(
  content: MessageContent,
  // defaults to llama2 because the tokenizer tends to produce more tokens
  modelName = "llama2",
)

Source from the content-addressed store, hash-verified

110}
111
112function countTokens(
113 content: MessageContent,
114 // defaults to llama2 because the tokenizer tends to produce more tokens
115 modelName = "llama2",
116): number {
117 const encoding = encodingForModel(modelName);
118 let baseTokens = 0;
119 if (Array.isArray(content)) {
120 baseTokens = content.reduce((acc, part) => {
121 return (
122 acc +
123 (part.type === "text"
124 ? encoding.encode(part.text ?? "", "all", []).length
125 : countImageTokens(part))
126 );
127 }, 0);
128 } else {
129 baseTokens = encoding.encode(content ?? "", "all", []).length;
130 }
131 return getAdjustedTokenCountFromModel(baseTokens, modelName);
132}
133
134// https://community.openai.com/t/how-to-calculate-the-tokens-when-using-function-call/266573/10
135function countToolsTokens(tools: Tool[], modelName: string): number {

Callers 14

isItemTooBigMethod · 0.90
markdownChunkerFunction · 0.90
fillPromptWithSnippetsFunction · 0.90
pruneLengthFunction · 0.90
formatOpenedFilesContextFunction · 0.90
trimSnippetForContextFunction · 0.90
getRemainingTokenCountFunction · 0.90
getSnippetsFunction · 0.90
prunePrefixSuffixMethod · 0.90
recursiveStreamFunction · 0.90
countChatMessageTokensFunction · 0.70

Calls 4

encodingForModelFunction · 0.85
countImageTokensFunction · 0.85
encodeMethod · 0.65

Tested by

no test coverage detected