(file)
| 36 | const toks = (u) => (u.inputTokens||0)+(u.outputTokens||0)+(u.cacheReadInputTokens||0)+(u.cacheCreationInputTokens||0); |
| 37 | |
| 38 | function analyzeRun(file) { |
| 39 | let result = null, agentCalls = 0; |
| 40 | const tools = {}, subPids = new Set(); |
| 41 | for (const line of readFileSync(file, 'utf8').split('\n')) { |
| 42 | if (!line) continue; |
| 43 | let e; try { e = JSON.parse(line); } catch { continue; } |
| 44 | if (e.parent_tool_use_id && e.message?.usage) subPids.add(e.parent_tool_use_id); |
| 45 | if (e.type === 'assistant' && Array.isArray(e.message?.content)) |
| 46 | for (const b of e.message.content) |
| 47 | if (b.type === 'tool_use') { tools[b.name] = (tools[b.name]||0)+1; if (b.name === 'Agent') agentCalls++; } |
| 48 | if (e.type === 'result') result = e; |
| 49 | } |
| 50 | // Authoritative cost + tokens from Claude Code's per-model accounting. |
| 51 | const mu = result?.modelUsage || {}; |
| 52 | const main = { cost: 0, tok: 0 }, sub = { cost: 0, tok: 0 }; |
| 53 | for (const [model, u] of Object.entries(mu)) { |
| 54 | const bucket = SUB_TIER.test(model) ? sub : main; // sonnet/anything-else => main |
| 55 | bucket.cost += u.costUSD || 0; |
| 56 | bucket.tok += toks(u); |
| 57 | } |
| 58 | return { |
| 59 | main, sub, subagents: subPids.size, agentCalls, |
| 60 | ccTotal: result?.total_cost_usd ?? null, |
| 61 | ok: result?.subtype === 'success', |
| 62 | durationSec: result?.duration_ms ? +(result.duration_ms/1000).toFixed(1) : null, |
| 63 | models: Object.keys(mu), tools, |
| 64 | }; |
| 65 | } |
| 66 | |
| 67 | const k = (n) => (n/1000).toFixed(0).padStart(5) + 'K'; |
| 68 | const d = (n) => '$' + n.toFixed(3); |
no test coverage detected