MCPcopy Create free account
hub / github.com/Noumena-Network/code / countSystemTokens

Function countSystemTokens

src/utils/analyzeContext.ts:273–319  ·  view source on GitHub ↗
(
  effectiveSystemPrompt: readonly string[],
)

Source from the content-addressed store, hash-verified

271}
272
273async function countSystemTokens(
274 effectiveSystemPrompt: readonly string[],
275): Promise<{
276 systemPromptTokens: number
277 systemPromptSections: SystemPromptSectionDetail[]
278}> {
279 // Get system context (gitStatus, etc.) which is always included
280 const systemContext = await getSystemContext()
281
282 // Build named entries: system prompt parts + system context values
283 // Skip empty strings and the global-cache boundary marker
284 const namedEntries: Array<{ name: string; content: string }> = [
285 ...effectiveSystemPrompt
286 .filter(
287 content =>
288 content.length > 0 && content !== SYSTEM_PROMPT_DYNAMIC_BOUNDARY,
289 )
290 .map(content => ({ name: extractSectionName(content), content })),
291 ...Object.entries(systemContext)
292 .filter(([, content]) => content.length > 0)
293 .map(([name, content]) => ({ name, content })),
294 ]
295
296 if (namedEntries.length < 1) {
297 return { systemPromptTokens: 0, systemPromptSections: [] }
298 }
299
300 const systemTokenCounts = await Promise.all(
301 namedEntries.map(({ content }) =>
302 countTokensWithFallback([{ role: 'user', content }], []),
303 ),
304 )
305
306 const systemPromptSections: SystemPromptSectionDetail[] = namedEntries.map(
307 (entry, i) => ({
308 name: entry.name,
309 tokens: systemTokenCounts[i] || 0,
310 }),
311 )
312
313 const systemPromptTokens = systemTokenCounts.reduce(
314 (sum: number, tokens) => sum + (tokens || 0),
315 0,
316 )
317
318 return { systemPromptTokens, systemPromptSections }
319}
320
321async function countMemoryFileTokens(): Promise<{
322 memoryFileDetails: MemoryFile[]

Callers 1

analyzeContextUsageFunction · 0.85

Calls 3

extractSectionNameFunction · 0.85
countTokensWithFallbackFunction · 0.85
entriesMethod · 0.80

Tested by

no test coverage detected