forked from acho-dev/aden-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualize-metrics.js
More file actions
160 lines (133 loc) · 5.63 KB
/
visualize-metrics.js
File metadata and controls
160 lines (133 loc) · 5.63 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
#!/usr/bin/env node
/**
* Visualize LLM metrics from JSONL file
* Usage: node visualize-metrics.js <path-to-jsonl>
*/
const fs = require("fs");
const filePath = process.argv[2] || "/home/timothy/aden/aden-mcp/packages/api-server/llm-metrics.jsonl";
// Parse JSONL
const lines = fs.readFileSync(filePath, "utf-8").trim().split("\n");
const events = lines.map(line => JSON.parse(line));
// Build span map
const spanMap = new Map();
events.forEach(e => spanMap.set(e.span_id, e));
// Find root spans (no parent)
const roots = events.filter(e => !e.parent_span_id);
// Extract function name from call stack entry
function extractFn(stackEntry) {
const match = stackEntry.match(/:(\d+):(.+)$/);
if (match) return match[2].replace("async ", "");
return stackEntry.split(":").pop() || "unknown";
}
// Build simplified call path from call_stack
function getCallPath(event) {
if (!event.call_stack || event.call_stack.length === 0) return "unknown";
// Skip the first entry (gemini-llm.js) and get the meaningful caller
const meaningful = event.call_stack.slice(1, 4).map(extractFn);
return meaningful.join(" → ");
}
// Print tree structure
function printTree(spanId, depth = 0, isLast = true, prefix = "") {
const event = spanMap.get(spanId);
if (!event) return;
const connector = depth === 0 ? "" : (isLast ? "└── " : "├── ");
const childPrefix = depth === 0 ? "" : (isLast ? " " : "│ ");
// Format model name
const model = event.model.replace("models/", "");
// Get call path
const callPath = getCallPath(event);
// Token bar
const tokenBar = "█".repeat(Math.min(20, Math.round(event.total_tokens / 1000)));
const tokenPct = ((event.total_tokens / totalTokens) * 100).toFixed(1);
console.log(
`${prefix}${connector}[#${event.call_sequence}] ${event.agent_stack?.join("→") || "?"} | ${model}` +
`\n${prefix}${childPrefix} ${callPath}` +
`\n${prefix}${childPrefix} ${tokenBar} ${event.total_tokens.toLocaleString()} tokens (${tokenPct}%) | ${event.latency_ms}ms`
);
// Find children
const children = events.filter(e => e.parent_span_id === spanId);
children.forEach((child, i) => {
const isChildLast = i === children.length - 1;
printTree(child.span_id, depth + 1, isChildLast, prefix + childPrefix);
});
}
// Calculate totals
const totalTokens = events.reduce((sum, e) => sum + e.total_tokens, 0);
const totalLatency = events.reduce((sum, e) => sum + e.latency_ms, 0);
const totalCalls = events.length;
console.log("\n" + "═".repeat(80));
console.log("LLM USAGE VISUALIZATION");
console.log("═".repeat(80));
console.log(`Trace ID: ${events[0]?.trace_id}`);
console.log(`Total Calls: ${totalCalls} | Total Tokens: ${totalTokens.toLocaleString()} | Total Latency: ${(totalLatency/1000).toFixed(1)}s`);
console.log("─".repeat(80) + "\n");
console.log("CALL TREE (by parent-child relationship)");
console.log("─".repeat(80));
roots.forEach((root, i) => printTree(root.span_id, 0, i === roots.length - 1, ""));
// Aggregate by agent
console.log("\n" + "─".repeat(80));
console.log("USAGE BY AGENT");
console.log("─".repeat(80));
const byAgent = {};
events.forEach(e => {
const agent = e.agent_stack?.[0] || "Unknown";
if (!byAgent[agent]) {
byAgent[agent] = { calls: 0, tokens: 0, latency: 0 };
}
byAgent[agent].calls++;
byAgent[agent].tokens += e.total_tokens;
byAgent[agent].latency += e.latency_ms;
});
Object.entries(byAgent)
.sort((a, b) => b[1].tokens - a[1].tokens)
.forEach(([agent, stats]) => {
const bar = "█".repeat(Math.round((stats.tokens / totalTokens) * 30));
const pct = ((stats.tokens / totalTokens) * 100).toFixed(1);
console.log(`${agent.padEnd(20)} ${bar.padEnd(30)} ${stats.tokens.toLocaleString().padStart(8)} tokens (${pct}%) | ${stats.calls} calls | ${(stats.latency/1000).toFixed(1)}s`);
});
// Aggregate by call path
console.log("\n" + "─".repeat(80));
console.log("USAGE BY CALL PATH (what function initiated the LLM call)");
console.log("─".repeat(80));
const byPath = {};
events.forEach(e => {
const path = getCallPath(e);
if (!byPath[path]) {
byPath[path] = { calls: 0, tokens: 0, latency: 0, models: new Set() };
}
byPath[path].calls++;
byPath[path].tokens += e.total_tokens;
byPath[path].latency += e.latency_ms;
byPath[path].models.add(e.model.replace("models/", ""));
});
Object.entries(byPath)
.sort((a, b) => b[1].tokens - a[1].tokens)
.forEach(([path, stats]) => {
const bar = "█".repeat(Math.round((stats.tokens / totalTokens) * 30));
const pct = ((stats.tokens / totalTokens) * 100).toFixed(1);
console.log(`\n${path}`);
console.log(` ${bar} ${stats.tokens.toLocaleString()} tokens (${pct}%) | ${stats.calls} calls | ${(stats.latency/1000).toFixed(1)}s`);
console.log(` Models: ${[...stats.models].join(", ")}`);
});
// Model breakdown
console.log("\n" + "─".repeat(80));
console.log("USAGE BY MODEL");
console.log("─".repeat(80));
const byModel = {};
events.forEach(e => {
const model = e.model.replace("models/", "");
if (!byModel[model]) {
byModel[model] = { calls: 0, tokens: 0, latency: 0 };
}
byModel[model].calls++;
byModel[model].tokens += e.total_tokens;
byModel[model].latency += e.latency_ms;
});
Object.entries(byModel)
.sort((a, b) => b[1].tokens - a[1].tokens)
.forEach(([model, stats]) => {
const bar = "█".repeat(Math.round((stats.tokens / totalTokens) * 30));
const pct = ((stats.tokens / totalTokens) * 100).toFixed(1);
console.log(`${model.padEnd(25)} ${bar.padEnd(30)} ${stats.tokens.toLocaleString().padStart(8)} tokens (${pct}%) | ${stats.calls} calls | ${(stats.latency/1000).toFixed(1)}s`);
});
console.log("\n" + "═".repeat(80) + "\n");