MCPcopy Create free account
hub / github.com/SethGammon/Citadel / readTokenEconomics

Function readTokenEconomics

scripts/telemetry-stats.js:269–346  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

267// ── Token economics ───────────────────────────────────────────────────────────
268
269function readTokenEconomics() {
270 const agentRuns = readJsonl(path.join(TELEMETRY_DIR, 'agent-runs.jsonl'));
271 const hookTiming = readJsonl(path.join(TELEMETRY_DIR, 'hook-timing.jsonl'));
272 const audit = readJsonl(path.join(TELEMETRY_DIR, 'audit.jsonl'));
273
274 // Routing savings: look for meta.tier in agent-run events
275 let tier0 = 0, tier1 = 0, tier2 = 0;
276 let tierDataAvailable = false;
277
278 for (const e of agentRuns) {
279 if (e.meta && typeof e.meta.tier === 'number') {
280 tierDataAvailable = true;
281 if (e.meta.tier === 0) tier0++;
282 else if (e.meta.tier === 1) tier1++;
283 else if (e.meta.tier === 2) tier2++;
284 }
285 }
286
287 const tierResolutions = tier0 + tier1 + tier2;
288 const routingSavings = tierDataAvailable ? {
289 tier0_resolutions: tier0,
290 tier1_resolutions: tier1,
291 tier2_resolutions: tier2,
292 tokens_saved_estimate: tierResolutions * TOKENS_PER_TIER_RESOLUTION,
293 methodology: 'Tier 0-2 resolutions * 500 tokens (avg Tier 3 cost)',
294 } : null;
295
296 // Circuit breaker trips from hook-timing counters (metric: trips)
297 const circuitTrips = hookTiming.filter(
298 e => (e.hook === 'circuit-breaker' || (typeof e.hook === 'string' && e.hook.includes('circuit')))
299 && e.metric === 'trips'
300 ).length;
301
302 // Also count from audit log entries mentioning circuit-breaker trip
303 const auditTrips = audit.filter(
304 e => typeof e.event === 'string' &&
305 (e.event.includes('circuit-breaker') || e.event.includes('circuit_breaker')) &&
306 (e.event.includes('trip') || e.event.includes('blocked'))
307 ).length;
308
309 const totalTrips = circuitTrips + auditTrips;
310
311 const circuitBreakerSaves = {
312 total_trips: totalTrips,
313 tokens_saved_estimate: totalTrips * TOKENS_PER_CIRCUIT_TRIP,
314 methodology: 'trips * 15000 tokens (avg spiral before intervention)',
315 };
316
317 // Quality gate violations from hook-timing counters (metric: violations) or audit
318 const timingViolations = hookTiming.filter(
319 e => (e.hook === 'quality-gate' || (typeof e.hook === 'string' && e.hook.includes('quality')))
320 && e.metric === 'violations'
321 ).length;
322
323 const auditViolations = audit.filter(
324 e => typeof e.event === 'string' &&
325 (e.event.includes('quality-gate') || e.event.includes('quality_gate') ||
326 e.event.includes('violation'))

Callers 3

tokenReportFunction · 0.85
mainFunction · 0.85
readCitadelValueMetricsFunction · 0.85

Calls 1

readJsonlFunction · 0.70

Tested by

no test coverage detected