-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClaudeCodeTokenCounter.js
More file actions
330 lines (283 loc) · 11.2 KB
/
Copy pathClaudeCodeTokenCounter.js
File metadata and controls
330 lines (283 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
* ClaudeCodeTokenCounter - Accurate token counting matching Claude Code's /context command
*
* This module replicates Claude Code's token counting exactly by:
* 1. Caching static token counts (System tools, MCP tools, Custom agents, System prompt)
* 2. Dynamically calculating Memory files tokens
* 3. Adding conversation tokens from session JSONL
* 4. Providing the same breakdown format as Claude Code
*/
const { spawn } = require('child_process');
const fs = require('fs').promises;
const path = require('path');
class ClaudeCodeTokenCounter {
constructor() {
this.staticTokenCache = null;
this.lastCacheTime = null;
this.cacheValidityMs = 24 * 60 * 60 * 1000; // 24 hours
}
/**
* Initialize or refresh static token cache by running Claude Code /context
*/
async refreshStaticTokenCache(workingDirectory = process.cwd()) {
try {
console.log('[ClaudeCodeTokenCounter] Refreshing static token cache...');
const contextOutput = await this.runClaudeCodeContext(workingDirectory);
const parsedTokens = this.parseContextOutput(contextOutput);
this.staticTokenCache = {
systemPrompt: parsedTokens.systemPrompt,
systemTools: parsedTokens.systemTools,
mcpTools: parsedTokens.mcpTools,
customAgents: parsedTokens.customAgents,
contextLimit: parsedTokens.contextLimit,
// Memory files are dynamic, so we don't cache them
breakdown: parsedTokens.breakdown
};
this.lastCacheTime = Date.now();
console.log('[ClaudeCodeTokenCounter] Static cache refreshed:', {
systemPrompt: this.staticTokenCache.systemPrompt,
systemTools: this.staticTokenCache.systemTools,
mcpTools: this.staticTokenCache.mcpTools,
customAgents: this.staticTokenCache.customAgents,
contextLimit: this.staticTokenCache.contextLimit
});
return this.staticTokenCache;
} catch (error) {
console.error('[ClaudeCodeTokenCounter] Failed to refresh static cache:', error.message);
// Fall back to estimated constants based on provided data
this.staticTokenCache = {
systemPrompt: 3000,
systemTools: 13300,
mcpTools: 13400,
customAgents: 1600,
contextLimit: 200000, // 200k for Sonnet 4
breakdown: {
mcpTools: [],
customAgents: []
}
};
this.lastCacheTime = Date.now();
console.log('[ClaudeCodeTokenCounter] Using fallback static token estimates');
return this.staticTokenCache;
}
}
/**
* Run claude-code /context command and capture output
*/
async runClaudeCodeContext(workingDirectory) {
return new Promise((resolve, reject) => {
const child = spawn('claude-code', ['/context'], {
cwd: workingDirectory,
stdio: ['pipe', 'pipe', 'pipe']
});
let stdout = '';
let stderr = '';
child.stdout.on('data', (data) => {
stdout += data.toString();
});
child.stderr.on('data', (data) => {
stderr += data.toString();
});
child.on('close', (code) => {
if (code === 0) {
resolve(stdout);
} else {
reject(new Error(`Claude Code /context failed with code ${code}: ${stderr}`));
}
});
child.on('error', (error) => {
reject(new Error(`Failed to run claude-code: ${error.message}`));
});
// Close stdin to avoid hanging
child.stdin.end();
});
}
/**
* Parse Claude Code /context output to extract token counts
*/
parseContextOutput(output) {
const lines = output.split('\n');
const tokens = {
systemPrompt: 0,
systemTools: 0,
mcpTools: 0,
customAgents: 0,
memoryFiles: 0,
contextLimit: 200000,
totalUsed: 0,
breakdown: {
mcpTools: [],
customAgents: []
}
};
let currentSection = null;
for (const line of lines) {
const trimmed = line.trim();
// Parse main token counts
if (trimmed.includes('System prompt:')) {
const match = trimmed.match(/(\d+\.?\d*)k tokens/);
if (match) tokens.systemPrompt = parseFloat(match[1]) * 1000;
} else if (trimmed.includes('System tools:')) {
const match = trimmed.match(/(\d+\.?\d*)k tokens/);
if (match) tokens.systemTools = parseFloat(match[1]) * 1000;
} else if (trimmed.includes('MCP tools:')) {
const match = trimmed.match(/(\d+\.?\d*)k tokens/);
if (match) tokens.mcpTools = parseFloat(match[1]) * 1000;
} else if (trimmed.includes('Custom agents:')) {
const match = trimmed.match(/(\d+\.?\d*)k tokens/);
if (match) tokens.customAgents = parseFloat(match[1]) * 1000;
} else if (trimmed.includes('Memory files:')) {
const match = trimmed.match(/(\d+\.?\d*)k tokens/);
if (match) tokens.memoryFiles = parseFloat(match[1]) * 1000;
}
// Parse context limit
const contextMatch = trimmed.match(/(\d+)k\/(\d+)k tokens/);
if (contextMatch) {
tokens.totalUsed = parseInt(contextMatch[1]) * 1000;
tokens.contextLimit = parseInt(contextMatch[2]) * 1000;
}
// Parse sections
if (trimmed.includes('MCP tools ·')) {
currentSection = 'mcpTools';
} else if (trimmed.includes('Custom agents ·')) {
currentSection = 'customAgents';
} else if (trimmed.includes('Memory files ·')) {
currentSection = 'memoryFiles';
}
// Parse detailed breakdowns
if (currentSection && trimmed.startsWith('└')) {
const toolMatch = trimmed.match(/└ ([^:]+).*?(\d+) tokens/);
if (toolMatch) {
const [, name, tokenCount] = toolMatch;
tokens.breakdown[currentSection].push({
name: name.trim(),
tokens: parseInt(tokenCount)
});
}
}
}
return tokens;
}
/**
* Calculate memory files tokens dynamically
*/
async calculateMemoryFiles() {
try {
// Calculate CLAUDE.md files tokens
let totalMemoryTokens = 0;
// Global CLAUDE.md
const globalClaudeMd = path.join(process.env.HOME || '', '.claude', 'CLAUDE.md');
try {
const stats = await fs.stat(globalClaudeMd);
totalMemoryTokens += Math.ceil(stats.size / 4); // ~4 chars per token
} catch (err) {
// File doesn't exist, skip
}
// Project CLAUDE.md
const projectClaudeMd = path.join(process.cwd(), 'CLAUDE.md');
try {
const stats = await fs.stat(projectClaudeMd);
totalMemoryTokens += Math.ceil(stats.size / 4); // ~4 chars per token
} catch (err) {
// File doesn't exist, skip
}
return totalMemoryTokens;
} catch (error) {
console.error('[ClaudeCodeTokenCounter] Error calculating memory files:', error);
return 2600; // Fallback based on provided example
}
}
/**
* Calculate conversation tokens from session JSONL
*/
async calculateConversationTokens(sessionId, sessionsDir) {
try {
const jsonlPath = path.join(sessionsDir, `${sessionId}.jsonl`);
const content = await fs.readFile(jsonlPath, 'utf8');
let totalInputTokens = 0;
let totalOutputTokens = 0;
const lines = content.trim().split('\n');
for (const line of lines) {
if (line.trim()) {
try {
const entry = JSON.parse(line);
if (entry.usage) {
totalInputTokens += entry.usage.input_tokens || 0;
totalOutputTokens += entry.usage.output_tokens || 0;
}
} catch (err) {
// Skip malformed lines
}
}
}
return {
inputTokens: totalInputTokens,
outputTokens: totalOutputTokens,
totalTokens: totalInputTokens + totalOutputTokens
};
} catch (error) {
console.error('[ClaudeCodeTokenCounter] Error calculating conversation tokens:', error);
return { inputTokens: 0, outputTokens: 0, totalTokens: 0 };
}
}
/**
* Get complete token breakdown matching Claude Code format
*/
async getAccurateTokenBreakdown(sessionId = null, sessionsDir = null) {
// Refresh cache if needed
if (!this.staticTokenCache ||
!this.lastCacheTime ||
Date.now() - this.lastCacheTime > this.cacheValidityMs) {
await this.refreshStaticTokenCache();
}
// Calculate dynamic components
const memoryFiles = await this.calculateMemoryFiles();
const conversation = sessionId && sessionsDir ?
await this.calculateConversationTokens(sessionId, sessionsDir) :
{ inputTokens: 0, outputTokens: 0, totalTokens: 0 };
// Build complete breakdown
const breakdown = {
systemPrompt: this.staticTokenCache.systemPrompt,
systemTools: this.staticTokenCache.systemTools,
mcpTools: this.staticTokenCache.mcpTools,
customAgents: this.staticTokenCache.customAgents,
memoryFiles: memoryFiles,
conversation: conversation.totalTokens,
// Totals
staticTotal: this.staticTokenCache.systemPrompt +
this.staticTokenCache.systemTools +
this.staticTokenCache.mcpTools +
this.staticTokenCache.customAgents,
dynamicTotal: memoryFiles + conversation.totalTokens,
contextLimit: this.staticTokenCache.contextLimit,
// Details
conversationDetails: conversation,
breakdown: this.staticTokenCache.breakdown
};
breakdown.grandTotal = breakdown.staticTotal + breakdown.dynamicTotal;
breakdown.usagePercentage = (breakdown.grandTotal / breakdown.contextLimit * 100).toFixed(1);
breakdown.freeSpace = breakdown.contextLimit - breakdown.grandTotal;
return breakdown;
}
/**
* Format token breakdown in Claude Code style
*/
formatClaudeCodeStyle(breakdown) {
const { grandTotal, contextLimit, usagePercentage, freeSpace } = breakdown;
let output = '';
output += `Context Usage\n`;
output += `claude-sonnet-4-20250514 • ${Math.round(grandTotal/1000)}k/${Math.round(contextLimit/1000)}k tokens (${usagePercentage}%)\n\n`;
// Component breakdown
output += `⛁ System prompt: ${(breakdown.systemPrompt/1000).toFixed(1)}k tokens (${(breakdown.systemPrompt/contextLimit*100).toFixed(1)}%)\n`;
output += `⛁ System tools: ${(breakdown.systemTools/1000).toFixed(1)}k tokens (${(breakdown.systemTools/contextLimit*100).toFixed(1)}%)\n`;
output += `⛁ MCP tools: ${(breakdown.mcpTools/1000).toFixed(1)}k tokens (${(breakdown.mcpTools/contextLimit*100).toFixed(1)}%)\n`;
output += `⛁ Custom agents: ${(breakdown.customAgents/1000).toFixed(1)}k tokens (${(breakdown.customAgents/contextLimit*100).toFixed(1)}%)\n`;
output += `⛁ Memory files: ${(breakdown.memoryFiles/1000).toFixed(1)}k tokens (${(breakdown.memoryFiles/contextLimit*100).toFixed(1)}%)\n`;
if (breakdown.conversation > 0) {
output += `⛁ Conversation: ${(breakdown.conversation/1000).toFixed(1)}k tokens (${(breakdown.conversation/contextLimit*100).toFixed(1)}%)\n`;
}
output += `⛶ Free space: ${(freeSpace/1000).toFixed(1)}k (${(freeSpace/contextLimit*100).toFixed(1)}%)\n`;
return output;
}
}
module.exports = ClaudeCodeTokenCounter;