ravensight-playtest 0.7.0 → 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/run/usage.js CHANGED
@@ -32,6 +32,14 @@ export function createUsageLedger() {
32
32
  let modelMs = 0;
33
33
  let serverTotalUsd = null;
34
34
  let serverCalls = null;
35
+ // The run's tokens in input-token units (see weightedUnits), and the last
36
+ // server spend that covered every call this ledger had recorded when it
37
+ // arrived. Together they say what one unit costs on this run, which is how
38
+ // the persona loop keeps enough budget back for the report without ever
39
+ // holding a price: ./budgetReserve.js.
40
+ let units = 0;
41
+ let lastCallUnits = 0;
42
+ let sample = null;
35
43
 
36
44
  return {
37
45
  /**
@@ -50,6 +58,8 @@ export function createUsageLedger() {
50
58
  entry.cache_read_tokens += Number(usage.cache_read_input_tokens || 0);
51
59
  entry.output_tokens += Number(usage.output_tokens || 0);
52
60
  byModel.set(key, entry);
61
+ lastCallUnits = weightedUnits(usage);
62
+ units += lastCallUnits;
53
63
  },
54
64
 
55
65
  /**
@@ -61,6 +71,31 @@ export function createUsageLedger() {
61
71
  if (!runUsage) return;
62
72
  if (Number.isFinite(Number(runUsage.total_usd))) serverTotalUsd = Number(runUsage.total_usd);
63
73
  if (Number.isFinite(Number(runUsage.calls))) serverCalls = Number(runUsage.calls);
74
+ // A figure that has not caught up with every call recorded here would
75
+ // make a unit look cheaper than it is, so only one that covers them all
76
+ // (the server may count more, which only makes a unit look dearer) is
77
+ // kept as the price sample.
78
+ const covers = !Number.isFinite(Number(runUsage.calls)) || Number(runUsage.calls) >= calls;
79
+ if (serverTotalUsd !== null && serverTotalUsd > 0 && units > 0 && covers) sample = { usd: serverTotalUsd, units };
80
+ },
81
+
82
+ /** The weighted units of the last call recorded, 0 before the first. */
83
+ get lastCallUnits() {
84
+ return lastCallUnits;
85
+ },
86
+
87
+ /**
88
+ * What the run has spent and what one weighted unit costs on it, or null
89
+ * before the server has priced a call this ledger recorded. The spend is
90
+ * the server's figure plus the calls made since, priced at the sample's
91
+ * rate, so it never reads lower than the server's own.
92
+ *
93
+ * @returns {{usd: number, usdPerUnit: number}|null}
94
+ */
95
+ spendEstimate() {
96
+ if (!sample) return null;
97
+ const usdPerUnit = sample.usd / sample.units;
98
+ return { usd: Math.max(serverTotalUsd || 0, sample.usd + usdPerUnit * Math.max(0, units - sample.units)), usdPerUnit };
64
99
  },
65
100
 
66
101
  get modelCalls() {
@@ -114,6 +149,25 @@ export function createUsageLedger() {
114
149
  };
115
150
  }
116
151
 
152
+ /** Input-token units per cache write, cache read and output token. */
153
+ export const CACHE_WRITE_WEIGHT = 1.25;
154
+ export const CACHE_READ_WEIGHT = 0.1;
155
+ export const OUTPUT_WEIGHT = 5;
156
+
157
+ /**
158
+ * One call's tokens in input-token units: what the server's price for the call
159
+ * is proportional to on every tier, since each tier prices a cache write at
160
+ * 1.25 input tokens, a cache read at 0.1 and an output token at 5. No price,
161
+ * only the shape of one.
162
+ *
163
+ * @param {Object} usage
164
+ * @returns {number}
165
+ */
166
+ export function weightedUnits(usage = {}) {
167
+ const n = key => Math.max(0, Number(usage && usage[key]) || 0);
168
+ return n('input_tokens') + n('cache_creation_input_tokens') * CACHE_WRITE_WEIGHT + n('cache_read_input_tokens') * CACHE_READ_WEIGHT + n('output_tokens') * OUTPUT_WEIGHT;
169
+ }
170
+
117
171
  /**
118
172
  * @param {string} runDirectory
119
173
  * @param {ReturnType<typeof createUsageLedger>} ledger