1
0

offload-eval-cost.mjs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/env node
  2. // Cost/token analysis for the 3-arm offload eval, with a MAIN-vs-SUBAGENT split.
  3. //
  4. // The explore-subagent question. With delegation ALLOWED, the nocg arm spawns a
  5. // Claude Code Explore subagent; the codegraph arms do all work in the main agent.
  6. // Two facts make naive accounting wrong:
  7. // 1. The Explore subagent runs on HAIKU 4.5; the main agent on SONNET 4.6.
  8. // So per-token cost differs ~3x between them — you cannot price both the same.
  9. // 2. The subagent's consumption is ~95% cache-reads. At Haiku's $0.10/MTok
  10. // cache-read rate, a huge TOKEN volume is a small DOLLAR cost.
  11. //
  12. // Rather than re-derive cost from raw token counts (and guess the cache TTL —
  13. // Claude Code uses 1-hour ephemeral cache here, 2x write, not 5-min), we read
  14. // Claude Code's OWN authoritative accounting from the `result` event:
  15. // result.modelUsage[model].costUSD — per-model cost CC itself billed
  16. // result.total_cost_usd — their sum (INCLUDES the Haiku subagent;
  17. // the handoff's "excludes subagent" was wrong)
  18. // The model split IS the agent split here: sonnet => main, haiku => Explore subagent
  19. // (only nocg spawns one, and only nocg shows haiku usage). Token volume is still
  20. // summed per-model from modelUsage for the separate "tokens" story.
  21. //
  22. // Usage: offload-eval-cost.mjs <runs-dir> <repo> [reps]
  23. // e.g. offload-eval-cost.mjs /tmp/cg-offload-eval/runs trezor 3
  24. import { readFileSync, existsSync } from 'fs';
  25. const MAIN_TIER = /sonnet/; // main agent
  26. const SUB_TIER = /haiku/; // Claude Code Explore subagent
  27. const [,, runsDir, repo, repsArg] = process.argv;
  28. if (!runsDir || !repo) { console.error('usage: offload-eval-cost.mjs <runs-dir> <repo> [reps] (env ARMS=nocg,raw,offload)'); process.exit(1); }
  29. const REPS = Number(repsArg || 3);
  30. // Arms to analyze (file stems `<repo>-<arm>-<rep>.jsonl`). Override for the style A/B:
  31. // ARMS=raw,refs,map,src. nocg's Haiku subagent is the only sub-tier; the rest are main-only.
  32. const ARMS = (process.env.ARMS || 'nocg,raw,offload').split(',').map((s) => s.trim()).filter(Boolean);
  33. const toks = (u) => (u.inputTokens||0)+(u.outputTokens||0)+(u.cacheReadInputTokens||0)+(u.cacheCreationInputTokens||0);
  34. function analyzeRun(file) {
  35. let result = null, agentCalls = 0;
  36. const tools = {}, subPids = new Set();
  37. for (const line of readFileSync(file, 'utf8').split('\n')) {
  38. if (!line) continue;
  39. let e; try { e = JSON.parse(line); } catch { continue; }
  40. if (e.parent_tool_use_id && e.message?.usage) subPids.add(e.parent_tool_use_id);
  41. if (e.type === 'assistant' && Array.isArray(e.message?.content))
  42. for (const b of e.message.content)
  43. if (b.type === 'tool_use') { tools[b.name] = (tools[b.name]||0)+1; if (b.name === 'Agent') agentCalls++; }
  44. if (e.type === 'result') result = e;
  45. }
  46. // Authoritative cost + tokens from Claude Code's per-model accounting.
  47. const mu = result?.modelUsage || {};
  48. const main = { cost: 0, tok: 0 }, sub = { cost: 0, tok: 0 };
  49. for (const [model, u] of Object.entries(mu)) {
  50. const bucket = SUB_TIER.test(model) ? sub : main; // sonnet/anything-else => main
  51. bucket.cost += u.costUSD || 0;
  52. bucket.tok += toks(u);
  53. }
  54. return {
  55. main, sub, subagents: subPids.size, agentCalls,
  56. ccTotal: result?.total_cost_usd ?? null,
  57. ok: result?.subtype === 'success',
  58. durationSec: result?.duration_ms ? +(result.duration_ms/1000).toFixed(1) : null,
  59. models: Object.keys(mu), tools,
  60. };
  61. }
  62. const k = (n) => (n/1000).toFixed(0).padStart(5) + 'K';
  63. const d = (n) => '$' + n.toFixed(3);
  64. const cost = (b) => b.cost;
  65. const tot = (b) => b.tok;
  66. const byArm = {};
  67. for (const arm of ARMS) {
  68. const runs = [];
  69. for (let r = 1; r <= REPS; r++) {
  70. const f = `${runsDir}/${repo}-${arm}-${r}.jsonl`;
  71. if (existsSync(f)) runs.push({ rep: r, ...analyzeRun(f) });
  72. }
  73. byArm[arm] = runs;
  74. }
  75. // Per-run detail. Cost is Claude Code's own modelUsage.costUSD (authoritative,
  76. // per-model pricing + correct cache TTL). MAIN=Sonnet, SUB=Haiku Explore subagent.
  77. // cc-check: main$+sub$ must equal result.total_cost_usd (delta should be ~0).
  78. console.log(`\n=== ${repo}: per-run main(Sonnet)/sub(Haiku) split — Claude Code's own cost accounting ===`);
  79. console.log('arm rep | subAg | MAIN(sonnet) tok / $ | SUB(haiku) tok / $ | TOTAL tok / $ | cc_total Δ | dur reads');
  80. for (const arm of ARMS) for (const r of byArm[arm]) {
  81. const mC = cost(r.main), sC = cost(r.sub), mT = tot(r.main), sT = tot(r.sub);
  82. const reads = r.tools['Read'] || 0, grep = (r.tools['Grep']||0)+(r.tools['Bash']||0)+(r.tools['Glob']||0);
  83. const explore = r.tools['mcp__codegraph__codegraph_explore'] || 0;
  84. const delta = (mC + sC) - (r.ccTotal || 0); // should be ~0
  85. console.log(
  86. `${arm.padEnd(8)} #${r.rep} | ${String(r.subagents).padStart(2)} | ${k(mT)} ${d(mC).padStart(7)} | ${k(sT)} ${d(sC).padStart(7)} | ${k(mT+sT)} ${d(mC+sC).padStart(7)} | ${d(r.ccTotal||0).padStart(7)} ${(delta>=0?'+':'')+delta.toFixed(4)} | ${String(r.durationSec).padStart(5)} r=${reads} g=${grep} x=${explore}`
  87. );
  88. }
  89. // Per-arm means
  90. const mean = (arr, f) => arr.length ? arr.reduce((s,x)=>s+f(x),0)/arr.length : 0;
  91. console.log(`\n=== ${repo}: per-arm MEANS (n per arm) ===`);
  92. console.log('arm n | main $ sub $ TOTAL $ | main tok sub tok TOTAL tok | %$ in sub | %tok in sub');
  93. for (const arm of ARMS) {
  94. const runs = byArm[arm]; if (!runs.length) continue;
  95. const mC = mean(runs, r=>cost(r.main)), sC = mean(runs, r=>cost(r.sub));
  96. const mT = mean(runs, r=>tot(r.main)), sT = mean(runs, r=>tot(r.sub));
  97. const pctSubC = (mC+sC) ? (100*sC/(mC+sC)) : 0;
  98. const pctSubT = (mT+sT) ? (100*sT/(mT+sT)) : 0;
  99. console.log(
  100. `${arm.padEnd(8)} ${runs.length} | ${d(mC).padStart(7)} ${d(sC).padStart(7)} ${d(mC+sC).padStart(7)} | ${k(mT)} ${k(sT)} ${k(mT+sT)} | ${pctSubC.toFixed(0).padStart(3)}% | ${pctSubT.toFixed(0).padStart(3)}%`
  101. );
  102. }
  103. // Headline ladders — cost, tokens, duration, all vs a baseline (nocg if present, else first arm).
  104. console.log(`\n=== Ladders (mean, incl. subagent) ===`);
  105. const totals = ARMS.map(a => ({ a, c: mean(byArm[a], r=>cost(r.main)+cost(r.sub)), t: mean(byArm[a], r=>tot(r.main)+tot(r.sub)) })).filter(x=>byArm[x.a].length);
  106. const base = totals.find(x=>x.a==='nocg') ?? totals[0];
  107. const bn = base?.a ?? '?';
  108. console.log(` COST (vs ${bn}):`);
  109. for (const x of totals) {
  110. const vs = base && base.c ? ` (${((x.c/base.c-1)*100>=0?'+':'')}${((x.c/base.c-1)*100).toFixed(0)}%)` : '';
  111. console.log(` ${x.a.padEnd(8)} ${d(x.c)}${vs}`);
  112. }
  113. console.log(` TOKENS (vs ${bn}):`);
  114. for (const x of totals) {
  115. const vs = base && base.t ? ` (${((x.t/base.t-1)*100>=0?'+':'')}${((x.t/base.t-1)*100).toFixed(0)}%)` : '';
  116. console.log(` ${x.a.padEnd(8)} ${k(x.t)}${vs}`);
  117. }
  118. console.log(` DURATION (wall-clock, vs ${bn}):`);
  119. const durs = ARMS.map(a => ({ a, s: mean(byArm[a].filter(r=>r.durationSec!=null), r=>r.durationSec) })).filter(x=>byArm[x.a].length);
  120. const dbase = durs.find(x=>x.a==='nocg') ?? durs[0];
  121. for (const x of durs) {
  122. const vs = dbase && dbase.s ? ` (${((x.s/dbase.s-1)*100>=0?'+':'')}${((x.s/dbase.s-1)*100).toFixed(0)}%)` : '';
  123. console.log(` ${x.a.padEnd(8)} ${x.s.toFixed(0)}s${vs}`);
  124. }