File size: 5,238 Bytes
d150943
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'use strict';

// Presentation values are derived; original episode usage remains unchanged.
window.UsageMetrics = (() => {
  let priceData = {episodes: {}, sources: []};
  const finite = v => typeof v === 'number' && Number.isFinite(v);
  const signature = ep => {
    const u = ep.usage || {};
    return [ep.id, ep.links?.usage, u.input_tokens, u.output_tokens,
      u.cached_input_tokens, u.cache_write_input_tokens, u.estimated_cost_usd ?? null];
  };
  function price(ep) {
    const row = priceData.episodes?.[ep?.id];
    return row && JSON.stringify(row.signature) === JSON.stringify(signature(ep)) ? row : null;
  }
  function usage(ep) {
    const u = ep?.usage || {}, p = price(ep), inclusive = finite(u.input_tokens) ? u.input_tokens : null;
    const cached = finite(u.cached_input_tokens) ? u.cached_input_tokens : null;
    const cacheWrite = finite(u.cache_write_input_tokens) && u.cache_write_input_tokens >= 0 ? u.cache_write_input_tokens : null;
    const fresh = inclusive != null && cached != null && cacheWrite != null && inclusive >= cached + cacheWrite
      ? inclusive - cached - cacheWrite : null;
    const writeBasis = u.cache_write_basis || (u.usage_source === 'provider-raw' ? 'provider-reported'
      : ep?.harness === 'codex' ? 'codex-native-counter'
      : u.usage_source === 'claude-native' ? 'claude-native-counter'
      : u.usage_source === 'pi-session' ? 'pi-session-counter' : 'recorded-counter');
    const cacheLow = cached ?? (p ? p.cache_lower : null);
    const cacheHigh = cached ?? (p ? p.cache_upper : null);
    return {
      inclusive, fresh, cached, cacheWrite, writeBasis, cacheLow, cacheHigh,
      input: inclusive != null && cached != null ? inclusive - cached : null,
      inputLow: inclusive != null && cacheHigh != null ? inclusive - cacheHigh : null,
      inputHigh: inclusive != null && cacheLow != null ? inclusive - cacheLow : null,
      output: finite(u.output_tokens) ? u.output_tokens : null,
      reasoning: finite(u.reasoning_output_tokens) ? u.reasoning_output_tokens : null,
      cacheRate: inclusive > 0 && cached != null ? cached / inclusive : null,
      cacheRateLow: inclusive > 0 && cacheLow != null ? cacheLow / inclusive : null,
      cacheRateHigh: inclusive > 0 && cacheHigh != null ? cacheHigh / inclusive : null,
    };
  }
  function stats(es) {
    const valid = es.filter(e => e.valid !== false), n = valid.length;
    const us = valid.map(usage), ps = valid.map(price);
    const sum = (rows, key) => rows.length && rows.every(r => finite(r?.[key])) ? rows.reduce((s,r) => s+r[key],0) : null;
    const inclusive = sum(us, 'inclusive'), cacheLow = sum(us, 'cacheLow'), cacheHigh = sum(us, 'cacheHigh');
    const costCount = ps.filter(Boolean).length, hasCost = n > 0 && costCount === n;
    const buckets = new Map();
    for (const p of ps.filter(Boolean)) for (const b of p.buckets) {
      const key = [b.category, b.tier, b.rate, b.rate_upper].join('|');
      const a = buckets.get(key) || {...b, tokens:0, lower:0, upper:0};
      a.tokens += b.tokens; a.lower += b.lower; a.upper += b.upper; buckets.set(key,a);
    }
    return {
      n, passes:valid.filter(e => e.success === true).length,
      rate:n ? valid.filter(e => e.success === true).length/n : null,
      inclusive, fresh:sum(us,'fresh'), cacheWrite:sum(us,'cacheWrite'),
      freshCount:us.filter(u => finite(u.fresh)).length, writeCount:us.filter(u => finite(u.cacheWrite)).length,
      providerZeroWrites:us.filter(u => u.cacheWrite === 0 && u.writeBasis === 'provider-reported').length,
      nativeZeroWrites:us.filter(u => u.cacheWrite === 0 && u.writeBasis !== 'provider-reported').length,
      input:sum(us,'input'), cached:sum(us,'cached'), output:sum(us,'output'),
      inputLow:sum(us,'inputLow'), inputHigh:sum(us,'inputHigh'), cacheLow, cacheHigh,
      cacheRateLow:inclusive > 0 && cacheLow != null ? cacheLow/inclusive : null,
      cacheRateHigh:inclusive > 0 && cacheHigh != null ? cacheHigh/inclusive : null,
      cacheRate:inclusive > 0 && sum(us,'cached') != null ? sum(us,'cached')/inclusive : null,
      reasoning:sum(us,'reasoning'), responses:sum(valid.map(e => e.usage),'response_count'), usageMessages:sum(valid.map(e => e.usage),'usage_message_count'),
      wall:sum(valid,'wall_time_s'), costCount,
      costLow:hasCost ? sum(ps,'lower') : null, costHigh:hasCost ? sum(ps,'upper') : null,
      buckets:[...buckets.values()], conditional:ps.some(p => p?.cache_write_assumed),
      bounded:ps.some(p => p && p.lower !== p.upper),
      knownCost:ps.filter(Boolean).reduce((s,p) => s+p.lower,0),
      longContext:ps.filter(Boolean).reduce((s,p) => s+p.long_context_responses,0),
    };
  }
  function bounds(low, high, format) {
    if (!finite(low) || !finite(high)) return '—';
    const a = format(low), b = format(high);
    return Math.abs(high-low) < 1e-9 ? a : a === b ? '≈'+a : a+'–'+b;
  }
  const money = v => finite(v) ? new Intl.NumberFormat('en-US', {style:'currency',currency:'USD',maximumFractionDigits:2}).format(v) : '—';
  return {init: value => {priceData=value;}, price, usage, stats, bounds, money,
    get sources() {return priceData.sources;}, get pricingDate() {return priceData.pricing_date;}};
})();