@trazum/core 1.8.0 → 1.10.0

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/usage.ts ADDED
@@ -0,0 +1,479 @@
1
+ import { effectivePricing, multipliersFor } from './pricing.js';
2
+ import type { PricingCatalogue } from './pricing.js';
3
+
4
+ /**
5
+ * Where the money actually went, from calls that actually happened.
6
+ *
7
+ * ## Why this exists
8
+ *
9
+ * Everything else in this package reads a **prompt file** and reasons about what
10
+ * it would cost. That is the smallest line item on most bills, and the gap is not
11
+ * small enough to argue about: measured on an ordinary support prompt, the
12
+ * deterministic rules recover about **1%** of the monthly figure, while output
13
+ * tokens alone were **87%** of it. A tool that reads `prompts/*.txt` cannot see
14
+ * retrieved context, conversation history, tool results or answers, and on a RAG
15
+ * or agent workload those are nearly the whole invoice.
16
+ *
17
+ * So this reads the other direction: **what the provider actually charged**, per
18
+ * call, and says where it went. The sentence it is built to produce is "63% of
19
+ * your bill is retrieved context and nothing is watching it", which is a fact
20
+ * about a system rather than an estimate about a file.
21
+ *
22
+ * ## It reads a file, and that is the design
23
+ *
24
+ * Not a proxy, not an SDK wrapper, not a callback. Trazum's whole security
25
+ * position is that prompts do not leave the machine they are on — asserted by
26
+ * tests, not promised — and a tool that sits in the request path trades that away
27
+ * for convenience. A JSON Lines file is something you already have or can produce
28
+ * in three lines, and it keeps the guarantee intact.
29
+ *
30
+ * ## The format is the one the API already gives you
31
+ *
32
+ * Nothing is invented here. Every Anthropic response carries a `usage` object
33
+ * with exactly these fields, so recording a call is:
34
+ *
35
+ * ```ts
36
+ * appendFileSync('usage.jsonl', JSON.stringify({
37
+ * model: response.model,
38
+ * ...response.usage,
39
+ * }) + '\n');
40
+ * ```
41
+ *
42
+ * OpenAI's `usage` maps onto the same shape with different names, and
43
+ * `parseUsageLine` accepts both. Asking somebody to transform their logs into a
44
+ * bespoke schema before a tool will read them is how a tool goes unused.
45
+ *
46
+ * ## What it refuses to do
47
+ *
48
+ * **It does not read prompt text and there is nowhere to put it.** The record
49
+ * shape has no field for content, so a usage log handed to Trazum cannot contain
50
+ * a prompt even by accident. That is a stronger promise than "we do not look at
51
+ * it", and it is the reason this takes counts rather than calls.
52
+ *
53
+ * **It reports no saving.** Attributing "you could have saved X" to a call that
54
+ * already happened means guessing what the call should have been, and this module
55
+ * exists precisely because guessing is what the rest of the package has to do.
56
+ * It reports what was spent, split by where it went. What to do about it is a
57
+ * different question and belongs to the advisories.
58
+ */
59
+
60
+ /** One recorded call, after parsing. All counts, no content. */
61
+ export interface UsageRecord {
62
+ /** Model id as the provider reported it. */
63
+ model: string;
64
+ /** Uncached input tokens billed at the full rate. */
65
+ inputTokens: number;
66
+ /** Tokens billed at the cache-read rate. Zero when nothing was cached. */
67
+ cacheReadTokens: number;
68
+ /** Cache writes at the 5-minute rate — 1.25x input on Anthropic. */
69
+ cacheWrite5mTokens: number;
70
+ /** Cache writes at the 1-hour rate, which is **2x** input, not 1.25x. */
71
+ cacheWrite1hTokens: number;
72
+ /**
73
+ * Whether the log said which TTL those writes used.
74
+ *
75
+ * `false` when only the flat `cache_creation_input_tokens` was present and it
76
+ * was non-zero: the writes are then priced at the cheaper 5-minute rate because
77
+ * one of the two has to be assumed, and the report says so. Choosing the cheaper
78
+ * rate silently understates a 1-hour workload by 37.5% on its largest line.
79
+ */
80
+ writeTtlKnown: boolean;
81
+ outputTokens: number;
82
+ /**
83
+ * Optional label for grouping — an endpoint, a feature, a prompt name.
84
+ *
85
+ * The whole value of a profile is answering "which part of the product costs
86
+ * this", and without a label every call looks alike. Unlabelled records are
87
+ * grouped under a single bucket rather than dropped, because a profile that
88
+ * refuses to read a log until it is annotated is a profile nobody runs.
89
+ */
90
+ label: string | null;
91
+ }
92
+
93
+ /** What a set of calls cost, split by where the money went. */
94
+ export interface UsageBreakdown {
95
+ calls: number;
96
+ inputTokens: number;
97
+ cacheReadTokens: number;
98
+ cacheWriteTokens: number;
99
+ outputTokens: number;
100
+ /**
101
+ * Calls whose cache-write TTL the log did not state, so the cheaper rate was
102
+ * assumed. Non-zero means this total is a floor on those calls, not a figure.
103
+ */
104
+ assumedWriteTtlCalls: number;
105
+ inputUsd: number;
106
+ cacheReadUsd: number;
107
+ cacheWriteUsd: number;
108
+ outputUsd: number;
109
+ totalUsd: number;
110
+ }
111
+
112
+ export interface UsageProfileReport {
113
+ /** Everything, combined. */
114
+ total: UsageBreakdown;
115
+ /** Per `label`, largest bill first — the order somebody would act in. */
116
+ byLabel: Array<{ label: string; breakdown: UsageBreakdown }>;
117
+ /** Per model, largest bill first. */
118
+ byModel: Array<{ model: string; breakdown: UsageBreakdown }>;
119
+ /**
120
+ * Models in the log that the pricing catalogue does not know.
121
+ *
122
+ * Named rather than silently costed at zero. A profile that quietly omits a
123
+ * model reports a total lower than the real bill, which is the flattering
124
+ * direction and the one this repository refuses.
125
+ */
126
+ unpricedModels: string[];
127
+ /**
128
+ * What those models used, kept entirely out of `total`.
129
+ *
130
+ * The first version added their **tokens** to the totals and their **dollars**
131
+ * to nothing, because pricing failed after the counts had been accumulated. So
132
+ * `total.inputTokens` included them and `total.inputUsd` did not, and anybody
133
+ * dividing one by the other got a cost per token that was wrong by however much
134
+ * of the log was unpriced — silently, and low.
135
+ *
136
+ * They are separated now. `total` is what could be priced, tokens and dollars
137
+ * describing the same calls. This is what could not, so the size of the gap is
138
+ * visible instead of being folded into a number that looks complete.
139
+ */
140
+ unpriced: UsageBreakdown;
141
+ /**
142
+ * Lines that could not be read, with their 1-based position.
143
+ *
144
+ * Reported rather than thrown on. A log with three malformed lines out of forty
145
+ * thousand should still produce a profile, and a parser that dies on the first
146
+ * one makes the tool unusable on real data — but a parser that skips quietly
147
+ * produces a total that is wrong by an unknown amount.
148
+ */
149
+ skippedLines: number[];
150
+ }
151
+
152
+ /** The share of the bill each part accounts for, as fractions of 1. */
153
+ export interface UsageShares {
154
+ input: number;
155
+ cacheRead: number;
156
+ cacheWrite: number;
157
+ output: number;
158
+ }
159
+
160
+ const EMPTY = (): UsageBreakdown => ({
161
+ calls: 0,
162
+ inputTokens: 0,
163
+ cacheReadTokens: 0,
164
+ cacheWriteTokens: 0,
165
+ outputTokens: 0,
166
+ assumedWriteTtlCalls: 0,
167
+ inputUsd: 0,
168
+ cacheReadUsd: 0,
169
+ cacheWriteUsd: 0,
170
+ outputUsd: 0,
171
+ totalUsd: 0,
172
+ });
173
+
174
+ /**
175
+ * A count, and whether the log actually said it.
176
+ *
177
+ * **Absent and corrupt are different, and conflating them cost the whole bill.**
178
+ * The first version used one helper that returned a fallback for both, so a field
179
+ * present as `"200000"` or `null` — a string count out of `jq`, a null out of a
180
+ * Postgres JSON round-trip — became a clean zero indistinguishable from a real
181
+ * one. The record survived, its token class vanished, and it was never added to
182
+ * `skippedLines`, so nothing on screen said a number had been thrown away.
183
+ *
184
+ * Measured on a two-line log with a stringified `input_tokens`: the report came to
185
+ * $0.0150 against a true $2.015, and the headline flipped to "output is 100% of
186
+ * this bill, so shortening prompts has a low ceiling" — the opposite of the truth
187
+ * on a workload that was almost entirely prompt.
188
+ *
189
+ * So: absent is a zero anybody may legitimately mean, and corrupt rejects the
190
+ * line.
191
+ */
192
+ type Count = { kind: 'ok'; value: number } | { kind: 'absent' } | { kind: 'corrupt' };
193
+
194
+ const OK = (value: number): Count => ({ kind: 'ok', value });
195
+
196
+ function readCount(...candidates: unknown[]): Count {
197
+ let sawCorrupt = false;
198
+ for (const value of candidates) {
199
+ if (value === undefined) continue;
200
+ if (typeof value === 'number' && Number.isFinite(value) && value >= 0) return OK(value);
201
+ // Present and unusable: a string, a null, a negative, a NaN.
202
+ sawCorrupt = true;
203
+ }
204
+ return sawCorrupt ? { kind: 'corrupt' } : { kind: 'absent' };
205
+ }
206
+
207
+ /** Zero for an absent count. Callers reject corrupt ones before reaching this. */
208
+ const valueOf = (count: Count): number => (count.kind === 'ok' ? count.value : 0);
209
+
210
+ /**
211
+ * One line of a usage log, or `null` when it is not one.
212
+ *
213
+ * Accepts the Anthropic shape and the OpenAI one, because those are the two
214
+ * things people actually have. The alternative — a Trazum-specific schema — asks
215
+ * for a transformation step before the tool will read anything, and a tool with a
216
+ * setup cost that exceeds its payoff does not get run twice.
217
+ *
218
+ * `null` in three cases, and the third is the one that was wrong:
219
+ *
220
+ * 1. Not JSON, or not an object, or no `model`.
221
+ * 2. **No** token counts at all — counting it would inflate the call count while
222
+ * contributing nothing, which lowers every per-call figure.
223
+ * 3. **Any** count present but unreadable. A field that is there and unusable is
224
+ * corruption, and a corrupt line belongs in `skippedLines` where the report
225
+ * names it, not in the totals as a silent zero.
226
+ */
227
+ export function parseUsageLine(line: string): UsageRecord | null {
228
+ let raw: unknown;
229
+ try {
230
+ raw = JSON.parse(line);
231
+ } catch {
232
+ return null;
233
+ }
234
+ if (typeof raw !== 'object' || raw === null || Array.isArray(raw)) return null;
235
+
236
+ const record = raw as Record<string, unknown>;
237
+ // Anthropic nests usage on a response; a hand-rolled log usually flattens it.
238
+ const usage =
239
+ typeof record.usage === 'object' && record.usage !== null
240
+ ? (record.usage as Record<string, unknown>)
241
+ : record;
242
+
243
+ const model = typeof record.model === 'string' ? record.model : null;
244
+ if (!model) return null;
245
+
246
+ /**
247
+ * OpenAI reports cached tokens inside `prompt_tokens_details` **and counts them
248
+ * in `prompt_tokens`**, while Anthropic reports them separately and does not.
249
+ * Subtracting in one case and not the other is the difference between a correct
250
+ * bill and one that charges the cached half twice.
251
+ */
252
+ const details =
253
+ typeof usage.prompt_tokens_details === 'object' && usage.prompt_tokens_details !== null
254
+ ? (usage.prompt_tokens_details as Record<string, unknown>)
255
+ : null;
256
+ const openAiCached = details ? readCount(details.cached_tokens) : ({ kind: 'absent' } as Count);
257
+
258
+ /**
259
+ * Anthropic splits cache writes by time-to-live, and the two cost different
260
+ * amounts: 1.25x input for the 5-minute entry, **2x** for the 1-hour one.
261
+ *
262
+ * Reading only the flat `cache_creation_input_tokens` threw that distinction
263
+ * away and then priced everything at the cheaper rate — a 1-hour workload
264
+ * reported 37.5% under, silently, on its largest line. The split is in the log
265
+ * whenever the recording recipe in the README is followed, because it is part of
266
+ * the `usage` object the API returns.
267
+ */
268
+ const creation =
269
+ typeof usage.cache_creation === 'object' && usage.cache_creation !== null
270
+ ? (usage.cache_creation as Record<string, unknown>)
271
+ : null;
272
+ const write5m = creation ? readCount(creation.ephemeral_5m_input_tokens) : ({ kind: 'absent' } as Count);
273
+ const write1h = creation ? readCount(creation.ephemeral_1h_input_tokens) : ({ kind: 'absent' } as Count);
274
+
275
+ const counts: Record<string, Count> = {
276
+ input: readCount(usage.input_tokens, usage.inputTokens, usage.prompt_tokens),
277
+ output: readCount(usage.output_tokens, usage.outputTokens, usage.completion_tokens),
278
+ cacheRead: readCount(usage.cache_read_input_tokens, usage.cacheReadTokens),
279
+ cacheWrite: readCount(usage.cache_creation_input_tokens, usage.cacheWriteTokens),
280
+ openAiCached,
281
+ write5m,
282
+ write1h,
283
+ };
284
+
285
+ // Any field present and unreadable rejects the line. See `readCount`.
286
+ if (Object.values(counts).some((c) => c.kind === 'corrupt')) return null;
287
+ // Nothing to count at all.
288
+ if (Object.values(counts).every((c) => c.kind === 'absent')) return null;
289
+
290
+ const cached = valueOf(counts.openAiCached!);
291
+ const flatWrite = valueOf(counts.cacheWrite!);
292
+ const split5m = valueOf(counts.write5m!);
293
+ const split1h = valueOf(counts.write1h!);
294
+ const hasSplit = counts.write5m!.kind === 'ok' || counts.write1h!.kind === 'ok';
295
+
296
+ return {
297
+ model,
298
+ inputTokens: Math.max(0, valueOf(counts.input!) - cached),
299
+ cacheReadTokens: counts.cacheRead!.kind === 'ok' ? counts.cacheRead!.value : cached,
300
+ /**
301
+ * The split when the log carries it, the flat number otherwise — and
302
+ * `writeTtlKnown` says which, so the report can admit that a rate was assumed
303
+ * rather than quietly choosing the cheaper one.
304
+ */
305
+ cacheWrite5mTokens: hasSplit ? split5m : flatWrite,
306
+ cacheWrite1hTokens: hasSplit ? split1h : 0,
307
+ writeTtlKnown: hasSplit || flatWrite === 0,
308
+ outputTokens: valueOf(counts.output!),
309
+ label:
310
+ typeof record.label === 'string' && record.label.trim() !== ''
311
+ ? record.label.trim()
312
+ : null,
313
+ };
314
+ }
315
+
316
+ /** The bucket unlabelled calls land in, named so a report can say so. */
317
+ export const UNLABELLED = 'unlabelled';
318
+
319
+ /** Token counts only. Used for both halves, because both need them. */
320
+ function countInto(into: UsageBreakdown, record: UsageRecord): void {
321
+ into.calls += 1;
322
+ into.inputTokens += record.inputTokens;
323
+ into.cacheReadTokens += record.cacheReadTokens;
324
+ into.cacheWriteTokens += record.cacheWrite5mTokens + record.cacheWrite1hTokens;
325
+ if (!record.writeTtlKnown) into.assumedWriteTtlCalls += 1;
326
+ into.outputTokens += record.outputTokens;
327
+ }
328
+
329
+ function add(into: UsageBreakdown, record: UsageRecord, catalogue: PricingCatalogue, on: Date): boolean {
330
+ /**
331
+ * Looked up directly rather than through `modelFrom`, which **throws** on an id
332
+ * it does not know. A usage log is somebody's production traffic and will
333
+ * contain models this catalogue has never heard of — a fine-tune, a preview, a
334
+ * competitor. Throwing means one unfamiliar id destroys the whole profile;
335
+ * naming it separately means the report is honest about what it could not price
336
+ * and useful about everything else.
337
+ *
338
+ * **Priced first, counted second.** The other order was the bug: counts landed
339
+ * before the lookup could fail, so an unpriced call contributed tokens to a
340
+ * total whose dollars excluded it.
341
+ */
342
+ const model = catalogue.byId.get(record.model);
343
+ if (!model) return false;
344
+
345
+ countInto(into, record);
346
+ const { inputPerMTok, outputPerMTok } = effectivePricing(model, on);
347
+ const rates = multipliersFor(model);
348
+ const per = (tokens: number, rate: number): number => (tokens / 1_000_000) * rate;
349
+
350
+ into.inputUsd += per(record.inputTokens, inputPerMTok);
351
+ into.cacheReadUsd += per(record.cacheReadTokens, inputPerMTok * rates.cacheRead);
352
+ /**
353
+ * Each TTL at its own rate. Anthropic charges 1.25x input for a 5-minute entry
354
+ * and 2x for a 1-hour one, and the first version applied 1.25x to both — 37.5%
355
+ * under on a 1-hour workload, on the largest line, with nothing on screen
356
+ * saying a rate had been chosen.
357
+ */
358
+ into.cacheWriteUsd += per(record.cacheWrite5mTokens, inputPerMTok * rates.cacheWrite5m);
359
+ into.cacheWriteUsd += per(record.cacheWrite1hTokens, inputPerMTok * rates.cacheWrite1h);
360
+ into.outputUsd += per(record.outputTokens, outputPerMTok);
361
+ into.totalUsd =
362
+ into.inputUsd + into.cacheReadUsd + into.cacheWriteUsd + into.outputUsd;
363
+ return true;
364
+ }
365
+
366
+ export interface UsageProfileOptions {
367
+ catalogue: PricingCatalogue;
368
+ /** Date the prices are read at, so a promotional rate resolves the same way. */
369
+ on?: Date;
370
+ }
371
+
372
+ /**
373
+ * Reads a usage log and says where the money went.
374
+ *
375
+ * Takes the whole text rather than a stream: a usage log is measured in megabytes
376
+ * and this package imports no Node builtins, so streaming would mean an interface
377
+ * the browser build cannot satisfy. `@trazum/core/node` is where file reading
378
+ * lives, and it can chunk if it ever needs to.
379
+ */
380
+ export function profileUsage(text: string, options: UsageProfileOptions): UsageProfileReport {
381
+ const { catalogue, on = new Date() } = options;
382
+
383
+ const total = EMPTY();
384
+ const unpriced = EMPTY();
385
+ const byLabel = new Map<string, UsageBreakdown>();
386
+ const byModel = new Map<string, UsageBreakdown>();
387
+ const unpricedModels = new Set<string>();
388
+ const skippedLines: number[] = [];
389
+
390
+ const lines = text.split('\n');
391
+ for (let i = 0; i < lines.length; i += 1) {
392
+ const line = lines[i]!.trim();
393
+ if (line === '') continue;
394
+
395
+ const record = parseUsageLine(line);
396
+ if (!record) {
397
+ skippedLines.push(i + 1);
398
+ continue;
399
+ }
400
+
401
+ if (!add(total, record, catalogue, on)) {
402
+ unpricedModels.add(record.model);
403
+ countInto(unpriced, record);
404
+ // Still grouped by model, so the reader can see which unknown id is costing
405
+ // them attention — but with zero dollars, which the grouping makes obvious.
406
+ if (!byModel.has(record.model)) byModel.set(record.model, EMPTY());
407
+ countInto(byModel.get(record.model)!, record);
408
+ continue;
409
+ }
410
+
411
+ const labelKey = record.label ?? UNLABELLED;
412
+ if (!byLabel.has(labelKey)) byLabel.set(labelKey, EMPTY());
413
+ add(byLabel.get(labelKey)!, record, catalogue, on);
414
+
415
+ if (!byModel.has(record.model)) byModel.set(record.model, EMPTY());
416
+ add(byModel.get(record.model)!, record, catalogue, on);
417
+ }
418
+
419
+ const sorted = <K extends string>(
420
+ map: Map<string, UsageBreakdown>,
421
+ key: K,
422
+ ): Array<Record<K, string> & { breakdown: UsageBreakdown }> =>
423
+ [...map.entries()]
424
+ .sort((a, b) => b[1].totalUsd - a[1].totalUsd || a[0].localeCompare(b[0]))
425
+ .map(([name, breakdown]) => ({ [key]: name, breakdown }) as Record<K, string> & {
426
+ breakdown: UsageBreakdown;
427
+ });
428
+
429
+ return {
430
+ total,
431
+ byLabel: sorted(byLabel, 'label'),
432
+ byModel: sorted(byModel, 'model'),
433
+ unpricedModels: [...unpricedModels].sort(),
434
+ unpriced,
435
+ skippedLines,
436
+ };
437
+ }
438
+
439
+ /**
440
+ * What share of the bill each part is.
441
+ *
442
+ * The point of the whole module in one function: a caller can print "output is
443
+ * 87% of this" without doing arithmetic that would drift from the arithmetic
444
+ * here.
445
+ *
446
+ * All zeroes when nothing was spent, rather than `NaN`. A profile of an empty log
447
+ * is a legitimate result — no calls yet — and a report full of `NaN%` is a bug
448
+ * report from somebody who did nothing wrong.
449
+ */
450
+ export function sharesOf(breakdown: UsageBreakdown): UsageShares {
451
+ const { totalUsd } = breakdown;
452
+ if (totalUsd <= 0) return { input: 0, cacheRead: 0, cacheWrite: 0, output: 0 };
453
+ return {
454
+ input: breakdown.inputUsd / totalUsd,
455
+ cacheRead: breakdown.cacheReadUsd / totalUsd,
456
+ cacheWrite: breakdown.cacheWriteUsd / totalUsd,
457
+ output: breakdown.outputUsd / totalUsd,
458
+ };
459
+ }
460
+
461
+ /**
462
+ * How much of the input that could have been cached was.
463
+ *
464
+ * `null` when nothing was cacheable-looking at all — no reads and no writes —
465
+ * because a hit rate over zero attempts is not zero, it is undefined, and
466
+ * printing "0% cache hit rate" for somebody who never turned caching on is a
467
+ * finding about nothing.
468
+ *
469
+ * Reads against reads-plus-full-price-input, deliberately. Cache *writes* are
470
+ * excluded from the denominator: a write is the cost of establishing an entry,
471
+ * not a missed read, and counting it as a miss makes a healthy cache look broken
472
+ * on the day it warms.
473
+ */
474
+ export function cacheHitRate(breakdown: UsageBreakdown): number | null {
475
+ const attempts = breakdown.cacheReadTokens + breakdown.inputTokens;
476
+ if (breakdown.cacheReadTokens === 0 && breakdown.cacheWriteTokens === 0) return null;
477
+ if (attempts === 0) return null;
478
+ return breakdown.cacheReadTokens / attempts;
479
+ }