-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathtoken-tracker.js
More file actions
581 lines (520 loc) · 18.7 KB
/
Copy pathtoken-tracker.js
File metadata and controls
581 lines (520 loc) · 18.7 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/**
* Token usage tracking for AWF API Proxy.
*
* Intercepts LLM API responses (both streaming SSE and non-streaming JSON)
* to extract token usage data without adding latency to the client.
*
* Architecture:
* proxyRes (LLM response) → res (client)
* ├─ on('data'): buffer/inspect chunks for usage extraction
* └─ on('end'): finalize parsing → log to file + metrics
*
* For non-streaming responses: buffer the JSON body (up to MAX_BUFFER_SIZE),
* then parse it on 'end' to extract usage fields.
* For streaming (SSE) responses: scan each chunk for usage events as they
* are received, accumulate usage from message_start / message_delta / final
* data events, and log the aggregated result on 'end'.
*
* Zero external dependencies — uses Node.js built-in streams and fs.
*/
'use strict';
const fs = require('fs');
const path = require('path');
const { logRequest } = require('./logging');
// Max response body to buffer for non-streaming usage extraction (5 MB).
// Responses larger than this are still forwarded but usage is not extracted.
const MAX_BUFFER_SIZE = 5 * 1024 * 1024;
// Token usage log file path (inside the mounted log volume)
const TOKEN_LOG_DIR = process.env.AWF_TOKEN_LOG_DIR || '/var/log/api-proxy';
const TOKEN_LOG_FILE = path.join(TOKEN_LOG_DIR, 'token-usage.jsonl');
let logStream = null;
/**
* Get or create the JSONL append stream for token usage logs.
* Uses a lazy singleton — created on first write.
*/
function getLogStream() {
if (logStream) return logStream;
try {
// Ensure directory exists
fs.mkdirSync(TOKEN_LOG_DIR, { recursive: true });
logStream = fs.createWriteStream(TOKEN_LOG_FILE, { flags: 'a' });
logStream.on('error', (err) => {
logRequest('warn', 'token_log_error', { error: err.message });
logStream = null;
});
return logStream;
} catch (err) {
logRequest('warn', 'token_log_init_error', { error: err.message });
return null;
}
}
/**
* Write a token usage record to the JSONL log file.
* Handles backpressure by dropping writes when the stream buffer is full.
*/
function writeTokenUsage(record) {
const stream = getLogStream();
if (stream && !stream.writableEnded) {
const ok = stream.write(JSON.stringify(record) + '\n');
if (!ok) {
// Backpressure — stream buffer full. Drop this write rather than
// accumulating unbounded memory. The 'drain' event will unblock
// future writes naturally.
logRequest('warn', 'token_log_backpressure', { request_id: record.request_id });
}
}
}
/**
* Check if a response is SSE (Server-Sent Events) streaming.
*/
function isStreamingResponse(headers) {
const ct = headers['content-type'] || '';
return ct.includes('text/event-stream');
}
/**
* Extract token usage from a non-streaming JSON response body.
*
* Supports:
* - OpenAI/Copilot: { usage: { prompt_tokens, completion_tokens, total_tokens } }
* - Anthropic: { usage: { input_tokens, output_tokens, cache_creation_input_tokens, cache_read_input_tokens } }
*
* Also extracts the model field if present.
*
* @param {Buffer} body - Response body
* @returns {{ usage: object|null, model: string|null }}
*/
function extractUsageFromJson(body) {
try {
const text = body.toString('utf8');
const json = JSON.parse(text);
const result = { usage: null, model: json.model || null };
if (json.usage && typeof json.usage === 'object') {
const usage = {};
let hasField = false;
// Anthropic fields
if (typeof json.usage.input_tokens === 'number') {
usage.input_tokens = json.usage.input_tokens;
hasField = true;
}
if (typeof json.usage.output_tokens === 'number') {
usage.output_tokens = json.usage.output_tokens;
hasField = true;
}
if (typeof json.usage.cache_creation_input_tokens === 'number') {
usage.cache_creation_input_tokens = json.usage.cache_creation_input_tokens;
hasField = true;
}
if (typeof json.usage.cache_read_input_tokens === 'number') {
usage.cache_read_input_tokens = json.usage.cache_read_input_tokens;
hasField = true;
}
// OpenAI/Copilot fields
if (typeof json.usage.prompt_tokens === 'number') {
usage.prompt_tokens = json.usage.prompt_tokens;
hasField = true;
}
if (typeof json.usage.completion_tokens === 'number') {
usage.completion_tokens = json.usage.completion_tokens;
hasField = true;
}
if (typeof json.usage.total_tokens === 'number') {
usage.total_tokens = json.usage.total_tokens;
hasField = true;
}
if (hasField) {
result.usage = usage;
}
}
return result;
} catch {
return { usage: null, model: null };
}
}
/**
* Extract token usage from a single SSE data line.
*
* SSE format: "data: {json}\n\n"
*
* Anthropic streaming events with usage:
* - message_start: { type: "message_start", message: { usage: { input_tokens, cache_creation_input_tokens, cache_read_input_tokens } } }
* - message_delta: { type: "message_delta", usage: { output_tokens } }
*
* OpenAI/Copilot streaming events with usage:
* - Final chunk: { usage: { prompt_tokens, completion_tokens, total_tokens } }
*
* @param {string} line - A single SSE data line (without "data: " prefix)
* @returns {{ usage: object|null, model: string|null }}
*/
function extractUsageFromSseLine(line) {
if (!line || line === '[DONE]') return { usage: null, model: null };
try {
const json = JSON.parse(line);
const result = { usage: null, model: json.model || null };
// Anthropic message_start: usage is inside message object
if (json.type === 'message_start' && json.message && json.message.usage) {
result.usage = {};
const u = json.message.usage;
if (typeof u.input_tokens === 'number') result.usage.input_tokens = u.input_tokens;
if (typeof u.cache_creation_input_tokens === 'number') result.usage.cache_creation_input_tokens = u.cache_creation_input_tokens;
if (typeof u.cache_read_input_tokens === 'number') result.usage.cache_read_input_tokens = u.cache_read_input_tokens;
result.model = (json.message && json.message.model) || result.model;
return result;
}
// Anthropic message_delta: usage at top level
if (json.type === 'message_delta' && json.usage) {
result.usage = {};
if (typeof json.usage.output_tokens === 'number') result.usage.output_tokens = json.usage.output_tokens;
return result;
}
// OpenAI/Copilot: usage at top level in final chunk
if (json.usage && typeof json.usage === 'object') {
result.usage = {};
if (typeof json.usage.prompt_tokens === 'number') result.usage.prompt_tokens = json.usage.prompt_tokens;
if (typeof json.usage.completion_tokens === 'number') result.usage.completion_tokens = json.usage.completion_tokens;
if (typeof json.usage.total_tokens === 'number') result.usage.total_tokens = json.usage.total_tokens;
return result;
}
return result;
} catch {
return { usage: null, model: null };
}
}
/**
* Extract all SSE data lines from a text chunk.
* Lines are prefixed with "data: " in the SSE protocol.
*/
function parseSseDataLines(text) {
const lines = [];
const parts = text.split('\n');
for (const part of parts) {
const trimmed = part.trim();
if (trimmed.startsWith('data: ')) {
lines.push(trimmed.slice(6));
} else if (trimmed === 'data:') {
// empty data line
}
}
return lines;
}
/**
* Normalize extracted usage into a unified format.
*
* Output fields:
* - input_tokens: number (from Anthropic input_tokens or OpenAI prompt_tokens)
* - output_tokens: number (from Anthropic output_tokens or OpenAI completion_tokens)
* - cache_read_tokens: number (Anthropic only, 0 for others)
* - cache_write_tokens: number (Anthropic only, 0 for others)
*/
function normalizeUsage(usage) {
if (!usage) return null;
return {
input_tokens: usage.input_tokens ?? usage.prompt_tokens ?? 0,
output_tokens: usage.output_tokens ?? usage.completion_tokens ?? 0,
cache_read_tokens: usage.cache_read_input_tokens ?? 0,
cache_write_tokens: usage.cache_creation_input_tokens ?? 0,
};
}
/**
* Attach token usage tracking to an upstream response.
*
* This function listens on the proxyRes 'data' and 'end' events to extract
* token usage. It does NOT modify the response stream — the caller still
* does proxyRes.pipe(res) as before.
*
* @param {http.IncomingMessage} proxyRes - Upstream response
* @param {object} opts
* @param {string} opts.requestId - Request ID for correlation
* @param {string} opts.provider - Provider name (openai, anthropic, copilot, opencode)
* @param {string} opts.path - Request path
* @param {number} opts.startTime - Request start time (Date.now())
* @param {object} opts.metrics - Metrics module reference
*/
function trackTokenUsage(proxyRes, opts) {
const { requestId, provider, path: reqPath, startTime, metrics: metricsRef } = opts;
const streaming = isStreamingResponse(proxyRes.headers);
// Accumulate response body for usage extraction
const chunks = [];
let totalBytes = 0;
let overflow = false;
// For streaming: accumulate usage across SSE events
let streamingUsage = {};
let streamingModel = null;
let partialLine = '';
proxyRes.on('data', (chunk) => {
totalBytes += chunk.length;
if (streaming) {
// Parse SSE data lines from this chunk to extract usage events
const text = partialLine + chunk.toString('utf8');
// Keep any incomplete line at the end for next chunk
const lastNewline = text.lastIndexOf('\n');
if (lastNewline >= 0) {
const complete = text.slice(0, lastNewline);
partialLine = text.slice(lastNewline + 1);
const dataLines = parseSseDataLines(complete);
for (const line of dataLines) {
const { usage, model } = extractUsageFromSseLine(line);
if (model && !streamingModel) streamingModel = model;
if (usage) {
// Merge usage fields (Anthropic sends input in message_start, output in message_delta)
for (const [k, v] of Object.entries(usage)) {
streamingUsage[k] = v;
}
}
}
} else {
partialLine = text;
}
} else if (!overflow) {
if (totalBytes <= MAX_BUFFER_SIZE) {
chunks.push(chunk);
} else {
overflow = true;
chunks.length = 0; // free memory
}
}
});
proxyRes.on('end', () => {
// Only process successful responses (2xx)
if (proxyRes.statusCode < 200 || proxyRes.statusCode >= 300) return;
const duration = Date.now() - startTime;
let usage = null;
let model = null;
if (streaming) {
// Process any remaining partial line
if (partialLine.trim()) {
const dataLines = parseSseDataLines(partialLine);
for (const line of dataLines) {
const { usage: u, model: m } = extractUsageFromSseLine(line);
if (m && !streamingModel) streamingModel = m;
if (u) {
for (const [k, v] of Object.entries(u)) {
streamingUsage[k] = v;
}
}
}
}
if (Object.keys(streamingUsage).length > 0) {
usage = streamingUsage;
model = streamingModel;
}
} else if (!overflow && chunks.length > 0) {
const body = Buffer.concat(chunks);
const result = extractUsageFromJson(body);
usage = result.usage;
model = result.model;
}
const normalized = normalizeUsage(usage);
if (!normalized) return;
// Update metrics
if (metricsRef) {
metricsRef.increment('input_tokens_total', { provider }, normalized.input_tokens);
metricsRef.increment('output_tokens_total', { provider }, normalized.output_tokens);
}
// Build log record
const record = {
timestamp: new Date().toISOString(),
request_id: requestId,
provider,
model: model || 'unknown',
path: reqPath,
status: proxyRes.statusCode,
streaming,
input_tokens: normalized.input_tokens,
output_tokens: normalized.output_tokens,
cache_read_tokens: normalized.cache_read_tokens,
cache_write_tokens: normalized.cache_write_tokens,
duration_ms: duration,
response_bytes: totalBytes,
};
// Write to JSONL log file
writeTokenUsage(record);
// Log summary to stdout
logRequest('info', 'token_usage', {
request_id: requestId,
provider,
model: model || 'unknown',
input_tokens: normalized.input_tokens,
output_tokens: normalized.output_tokens,
cache_read_tokens: normalized.cache_read_tokens,
cache_write_tokens: normalized.cache_write_tokens,
streaming,
});
});
}
/**
* Parse WebSocket frames from a buffer (server→client direction, unmasked).
*
* Returns an object with:
* - messages: Array of decoded text frame payloads (strings)
* - consumed: Number of bytes consumed from the buffer
*
* Only handles non-fragmented text frames (FIN=1, opcode=1).
* Other frame types (binary, ping, pong, close, continuation) are consumed
* but their payloads are not returned.
*
* @param {Buffer} buf - Buffer containing WebSocket frame data
* @returns {{ messages: string[], consumed: number }}
*/
function parseWebSocketFrames(buf) {
const messages = [];
let pos = 0;
while (pos + 2 <= buf.length) {
const firstByte = buf[pos];
const secondByte = buf[pos + 1];
const fin = (firstByte & 0x80) !== 0;
const opcode = firstByte & 0x0F;
const masked = (secondByte & 0x80) !== 0;
let payloadLength = secondByte & 0x7F;
let headerSize = 2;
if (payloadLength === 126) {
if (pos + 4 > buf.length) break;
payloadLength = buf.readUInt16BE(pos + 2);
headerSize = 4;
} else if (payloadLength === 127) {
if (pos + 10 > buf.length) break;
payloadLength = Number(buf.readBigUInt64BE(pos + 2));
headerSize = 10;
}
if (masked) headerSize += 4; // skip masking key
const frameEnd = pos + headerSize + payloadLength;
if (frameEnd > buf.length) break;
// Extract text frames (opcode 1) with FIN set
if (opcode === 1 && fin) {
messages.push(buf.slice(pos + headerSize, frameEnd).toString('utf8'));
}
pos = frameEnd;
}
return { messages, consumed: pos };
}
/**
* Attach token usage tracking to a WebSocket upstream connection.
*
* Claude Code CLI uses WebSocket streaming to the Anthropic API. The
* api-proxy relays this as a raw socket pipe (tlsSocket ↔ clientSocket).
* This function adds a non-blocking 'data' listener on the upstream socket
* to parse WebSocket frames and extract token usage from JSON text messages.
*
* The upstream stream starts with an HTTP 101 response header, followed by
* WebSocket frames. This function skips the HTTP header before parsing frames.
*
* @param {import('tls').TLSSocket} upstreamSocket - Upstream TLS socket
* @param {object} opts
* @param {string} opts.requestId - Request ID for correlation
* @param {string} opts.provider - Provider name (anthropic, copilot, etc.)
* @param {string} opts.path - Request path
* @param {number} opts.startTime - Request start time (Date.now())
* @param {object} opts.metrics - Metrics module reference
*/
function trackWebSocketTokenUsage(upstreamSocket, opts) {
const { requestId, provider, path: reqPath, startTime, metrics: metricsRef } = opts;
let httpHeaderParsed = false;
let buffer = Buffer.alloc(0);
let totalBytes = 0;
let streamingUsage = {};
let streamingModel = null;
let finalized = false;
// Max buffer to prevent unbounded memory growth (1 MB)
const MAX_WS_BUFFER = 1 * 1024 * 1024;
upstreamSocket.on('data', (chunk) => {
totalBytes += chunk.length;
buffer = Buffer.concat([buffer, chunk]);
// Safety: drop buffer if it grows too large (malformed frames)
if (buffer.length > MAX_WS_BUFFER) {
buffer = Buffer.alloc(0);
httpHeaderParsed = true; // skip header parsing
return;
}
// Skip the HTTP 101 Switching Protocols response header
if (!httpHeaderParsed) {
const headerEnd = buffer.indexOf('\r\n\r\n');
if (headerEnd === -1) return; // need more data for full header
buffer = buffer.slice(headerEnd + 4);
httpHeaderParsed = true;
}
// Parse any complete WebSocket frames
const { messages, consumed } = parseWebSocketFrames(buffer);
if (consumed > 0) {
buffer = buffer.slice(consumed);
}
for (const text of messages) {
const { usage, model } = extractUsageFromSseLine(text);
if (model && !streamingModel) streamingModel = model;
if (usage) {
for (const [k, v] of Object.entries(usage)) {
streamingUsage[k] = v;
}
}
}
});
function doFinalize() {
if (finalized) return;
finalized = true;
if (Object.keys(streamingUsage).length === 0) return;
const duration = Date.now() - startTime;
const normalized = normalizeUsage(streamingUsage);
if (!normalized) return;
if (metricsRef) {
metricsRef.increment('input_tokens_total', { provider }, normalized.input_tokens);
metricsRef.increment('output_tokens_total', { provider }, normalized.output_tokens);
}
const record = {
timestamp: new Date().toISOString(),
request_id: requestId,
provider,
model: streamingModel || 'unknown',
path: reqPath,
status: 200,
streaming: true,
input_tokens: normalized.input_tokens,
output_tokens: normalized.output_tokens,
cache_read_tokens: normalized.cache_read_tokens,
cache_write_tokens: normalized.cache_write_tokens,
duration_ms: duration,
response_bytes: totalBytes,
};
writeTokenUsage(record);
logRequest('info', 'token_usage', {
request_id: requestId,
provider,
model: streamingModel || 'unknown',
input_tokens: normalized.input_tokens,
output_tokens: normalized.output_tokens,
cache_read_tokens: normalized.cache_read_tokens,
cache_write_tokens: normalized.cache_write_tokens,
streaming: true,
transport: 'websocket',
});
}
upstreamSocket.on('close', doFinalize);
upstreamSocket.on('end', doFinalize);
}
/**
* Close the log stream (for graceful shutdown).
* Returns a Promise that resolves once the stream has flushed.
*/
function closeLogStream() {
return new Promise((resolve) => {
if (logStream) {
logStream.end(() => {
logStream = null;
resolve();
});
} else {
resolve();
}
});
}
module.exports = {
trackTokenUsage,
trackWebSocketTokenUsage,
closeLogStream,
// Exported for testing
extractUsageFromJson,
extractUsageFromSseLine,
parseSseDataLines,
parseWebSocketFrames,
normalizeUsage,
isStreamingResponse,
writeTokenUsage,
TOKEN_LOG_FILE,
};