@trazum/core 2.7.9 → 2.7.11

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 CHANGED
@@ -222,6 +222,20 @@ export interface UsageRecord {
222
222
  * report converters write for a batch row. Absent is not batch.
223
223
  */
224
224
  batch?: true;
225
+ /**
226
+ * True when something waited on the answer: a person in a conversation, or
227
+ * an agent whose next step needed it. The Batch API answers within a day,
228
+ * so a call like this cannot move to it, and the batch lever leaves it out
229
+ * (`batchExcludedUsd` on the breakdown) instead of asking the reader whether
230
+ * a turn of their coding agent "can wait for a batch window".
231
+ *
232
+ * Read from a record-level `interactive: true`, which is what the agent
233
+ * session converters write on every line: `from-claude-code`, `from-codex`
234
+ * and `from-gemini-cli`. Absent says nothing either way. A log that marks
235
+ * none of its calls is read as it always was, because most logs never
236
+ * recorded who was waiting.
237
+ */
238
+ interactive?: true;
225
239
  /**
226
240
  * Web searches the call ran, when the log counted any: Anthropic's
227
241
  * `usage.server_tool_use.web_search_requests`, which every Messages
@@ -355,6 +369,22 @@ export interface UsageBreakdown {
355
369
  * difference. Their searches are in `webSearchUsd`, at the same fee as any.
356
370
  */
357
371
  batchUsd: number;
372
+ /**
373
+ * The input and output the Batch API lever leaves out, in tokens and in
374
+ * dollars at each call's own rate: calls already billed at the batch rate,
375
+ * which cannot be halved twice, and calls something waited on
376
+ * (`interactive` on the record), which cannot wait a day.
377
+ *
378
+ * Already inside `inputTokens`, `outputTokens`, `inputUsd` and `outputUsd`.
379
+ * Input and output only, because those are the two lines the lever prices.
380
+ * Tokens as well as dollars, because a batch saving on another model's rates
381
+ * is priced from the tokens. Written as what is left out, never as what is
382
+ * left in, so a document from before the fields existed reads as zero and
383
+ * prices the lever as it always did.
384
+ */
385
+ batchExcludedInputTokens: number;
386
+ batchExcludedOutputTokens: number;
387
+ batchExcludedUsd: number;
358
388
  }
359
389
 
360
390
  /**
@@ -379,6 +409,8 @@ export interface FieldCoverage {
379
409
  cacheTtl: number;
380
410
  /** Records that wrote to the cache at all — the denominator for `cacheTtl`. */
381
411
  cacheWrites: number;
412
+ /** Records marked `interactive`: something waited on the answer. */
413
+ interactive: number;
382
414
  /** Every record that parsed, priced or not — the denominator for the rest. */
383
415
  parsed: number;
384
416
  }
@@ -722,6 +754,9 @@ const EMPTY = (): UsageBreakdown => ({
722
754
  webSearchUsd: 0,
723
755
  webSearchUnpricedRequests: 0,
724
756
  batchUsd: 0,
757
+ batchExcludedInputTokens: 0,
758
+ batchExcludedOutputTokens: 0,
759
+ batchExcludedUsd: 0,
725
760
  });
726
761
 
727
762
  /**
@@ -1081,6 +1116,8 @@ export function parseUsageLine(line: string): UsageRecord | null {
1081
1116
  * cannot collide on a flat record.
1082
1117
  */
1083
1118
  ...(record.batch === true || usage.service_tier === 'batch' ? { batch: true as const } : {}),
1119
+ /* Said by the record alone: nothing a provider returns names who was waiting. */
1120
+ ...(record.interactive === true ? { interactive: true as const } : {}),
1084
1121
  ...(valueOf(webSearch) > 0 ? { webSearchRequests: valueOf(webSearch) } : {}),
1085
1122
  ...(openAiSplitGiven && !openAiSplitAgrees ? { writeSplitContradicted: true as const } : {}),
1086
1123
  };
@@ -1223,6 +1260,16 @@ function add(into: UsageBreakdown, record: UsageRecord, catalogue: PricingCatalo
1223
1260
  per(record.cacheWrite1hTokens, input * rates.cacheWrite1h) +
1224
1261
  per(record.outputTokens, output);
1225
1262
  if (priced.batch === true) into.batchUsd += recordUsd(inputPerMTok, outputPerMTok);
1263
+ /*
1264
+ * What the batch lever must not count: a call the log says ran on the Batch
1265
+ * API, and a call something waited on. By the record, not by the price: a
1266
+ * batch call on a model with no batch rate is still a call that was batched.
1267
+ */
1268
+ if (record.batch === true || record.interactive === true) {
1269
+ into.batchExcludedInputTokens += record.inputTokens;
1270
+ into.batchExcludedOutputTokens += record.outputTokens;
1271
+ into.batchExcludedUsd += per(record.inputTokens, inputPerMTok) + per(record.outputTokens, outputPerMTok);
1272
+ }
1226
1273
  const floor = priced.tier?.decided === false ? priced.tier.floor : undefined;
1227
1274
  if (floor !== undefined) {
1228
1275
  into.tierCeilingCalls += 1;
@@ -1355,7 +1402,7 @@ export function profileUsage(text: string, options: UsageProfileOptions): UsageP
1355
1402
  const outcomesByModel = new Map<string, Map<string, { calls: number; usd: number }>>();
1356
1403
  const unrecordedByLabel = new Map<string, number>();
1357
1404
  const unrecordedByModel = new Map<string, number>();
1358
- const coverage = { label: 0, session: 0, outcome: 0, ts: 0, stopReason: 0, cacheTtl: 0, cacheWrites: 0, parsed: 0 };
1405
+ const coverage = { label: 0, session: 0, outcome: 0, ts: 0, stopReason: 0, cacheTtl: 0, cacheWrites: 0, interactive: 0, parsed: 0 };
1359
1406
  /**
1360
1407
  * Raw lines already seen, for the duplicate check. Bounded by the number of
1361
1408
  * *timestamped* lines — the price of catching a doubled bill, paid only on
@@ -1443,6 +1490,7 @@ export function profileUsage(text: string, options: UsageProfileOptions): UsageP
1443
1490
  */
1444
1491
  coverage.parsed += 1;
1445
1492
  if (record.label !== null) coverage.label += 1;
1493
+ if (record.interactive === true) coverage.interactive += 1;
1446
1494
  if (record.session !== null) coverage.session += 1;
1447
1495
  if (record.outcome !== null) coverage.outcome += 1;
1448
1496
  if (record.ts !== null) coverage.ts += 1;