@trazum/core 2.7.4 → 2.7.6

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.
Files changed (84) hide show
  1. package/dist/anthropic-cost.d.ts +22 -5
  2. package/dist/anthropic-cost.d.ts.map +1 -1
  3. package/dist/anthropic-cost.js +33 -10
  4. package/dist/anthropic-cost.js.map +1 -1
  5. package/dist/anthropic-usage.d.ts +7 -1
  6. package/dist/anthropic-usage.d.ts.map +1 -1
  7. package/dist/anthropic-usage.js +17 -1
  8. package/dist/anthropic-usage.js.map +1 -1
  9. package/dist/billing.d.ts +10 -0
  10. package/dist/billing.d.ts.map +1 -1
  11. package/dist/billing.js +14 -0
  12. package/dist/billing.js.map +1 -1
  13. package/dist/claude-code.d.ts +4 -0
  14. package/dist/claude-code.d.ts.map +1 -1
  15. package/dist/claude-code.js +14 -1
  16. package/dist/claude-code.js.map +1 -1
  17. package/dist/connector.d.ts +19 -1
  18. package/dist/connector.d.ts.map +1 -1
  19. package/dist/connector.js +34 -2
  20. package/dist/connector.js.map +1 -1
  21. package/dist/csv.d.ts +1 -1
  22. package/dist/csv.d.ts.map +1 -1
  23. package/dist/csv.js +11 -0
  24. package/dist/csv.js.map +1 -1
  25. package/dist/gateway.d.ts +2 -0
  26. package/dist/gateway.d.ts.map +1 -1
  27. package/dist/gateway.js +16 -0
  28. package/dist/gateway.js.map +1 -1
  29. package/dist/otel.d.ts +16 -4
  30. package/dist/otel.d.ts.map +1 -1
  31. package/dist/otel.js +23 -5
  32. package/dist/otel.js.map +1 -1
  33. package/dist/pricing-overlay.d.ts +1 -1
  34. package/dist/pricing-overlay.d.ts.map +1 -1
  35. package/dist/pricing-overlay.js +11 -0
  36. package/dist/pricing-overlay.js.map +1 -1
  37. package/dist/pricing.d.ts.map +1 -1
  38. package/dist/pricing.js +38 -0
  39. package/dist/pricing.js.map +1 -1
  40. package/dist/receipt.d.ts +38 -4
  41. package/dist/receipt.d.ts.map +1 -1
  42. package/dist/receipt.js +19 -1
  43. package/dist/receipt.js.map +1 -1
  44. package/dist/repeats.d.ts.map +1 -1
  45. package/dist/repeats.js +3 -2
  46. package/dist/repeats.js.map +1 -1
  47. package/dist/rollup.d.ts.map +1 -1
  48. package/dist/rollup.js +4 -0
  49. package/dist/rollup.js.map +1 -1
  50. package/dist/session-cost.d.ts.map +1 -1
  51. package/dist/session-cost.js +4 -2
  52. package/dist/session-cost.js.map +1 -1
  53. package/dist/store.d.ts +5 -0
  54. package/dist/store.d.ts.map +1 -1
  55. package/dist/store.js +4 -0
  56. package/dist/store.js.map +1 -1
  57. package/dist/truncation-retry.d.ts.map +1 -1
  58. package/dist/truncation-retry.js +3 -2
  59. package/dist/truncation-retry.js.map +1 -1
  60. package/dist/types.d.ts +13 -0
  61. package/dist/types.d.ts.map +1 -1
  62. package/dist/usage.d.ts +34 -0
  63. package/dist/usage.d.ts.map +1 -1
  64. package/dist/usage.js +34 -3
  65. package/dist/usage.js.map +1 -1
  66. package/package.json +1 -1
  67. package/src/anthropic-cost.ts +46 -12
  68. package/src/anthropic-usage.ts +24 -2
  69. package/src/billing.ts +15 -0
  70. package/src/claude-code.ts +16 -1
  71. package/src/connector.ts +56 -3
  72. package/src/csv.ts +11 -0
  73. package/src/gateway.ts +16 -0
  74. package/src/otel.ts +21 -5
  75. package/src/pricing-overlay.ts +15 -0
  76. package/src/pricing.ts +39 -0
  77. package/src/receipt.ts +53 -6
  78. package/src/repeats.ts +3 -2
  79. package/src/rollup.ts +4 -0
  80. package/src/session-cost.ts +4 -2
  81. package/src/store.ts +9 -0
  82. package/src/truncation-retry.ts +3 -2
  83. package/src/types.ts +13 -0
  84. package/src/usage.ts +68 -3
package/src/store.ts CHANGED
@@ -56,6 +56,11 @@ export interface StoreRecord {
56
56
  /** False when the source reported writes without saying which TTL. */
57
57
  ttlKnown: boolean;
58
58
  output: number;
59
+ /**
60
+ * Web searches, billed per search on top of the tokens. Absent is none, so
61
+ * a store written before the field existed reads the same as it did.
62
+ */
63
+ webSearch?: number;
59
64
  /**
60
65
  * The account's own opaque identifiers — workspace, key, tier — as the
61
66
  * provider served them.
@@ -179,6 +184,7 @@ export function recordsFromBuckets(
179
184
  write1h: bucket.cacheWrite1hTokens,
180
185
  ttlKnown: bucket.writeTtlKnown,
181
186
  output: bucket.outputTokens,
187
+ ...((bucket.webSearchRequests ?? 0) > 0 ? { webSearch: bucket.webSearchRequests } : {}),
182
188
  group: bucket.group,
183
189
  pulledAtMs,
184
190
  }));
@@ -197,6 +203,9 @@ export function bucketsFromRecords(records: readonly StoreRecord[]): UsageBucket
197
203
  cacheWrite1hTokens: record.write1h,
198
204
  writeTtlKnown: record.ttlKnown,
199
205
  outputTokens: record.output,
206
+ ...(typeof record.webSearch === 'number' && Number.isFinite(record.webSearch) && record.webSearch > 0
207
+ ? { webSearchRequests: record.webSearch }
208
+ : {}),
200
209
  group: record.group,
201
210
  }));
202
211
  }
@@ -1,5 +1,5 @@
1
1
  import { effectivePricing, multipliersFor } from './pricing.js';
2
- import { billedAt, promptSizeOf } from './billing.js';
2
+ import { billedAt, promptSizeOf, webSearchUsdOf } from './billing.js';
3
3
  import { UNLABELLED } from './usage.js';
4
4
  import type { PricingCatalogue } from './pricing.js';
5
5
  import type { UsageRecord } from './usage.js';
@@ -100,7 +100,8 @@ export function createTruncationRetryTracker(options: TruncationRetryOptions): T
100
100
  per(record.cacheReadTokens, inputPerMTok * rates.cacheRead) +
101
101
  per(record.cacheWrite5mTokens, inputPerMTok * rates.cacheWrite5m) +
102
102
  per(record.cacheWrite1hTokens, inputPerMTok * rates.cacheWrite1h) +
103
- per(record.outputTokens, outputPerMTok)
103
+ per(record.outputTokens, outputPerMTok) +
104
+ webSearchUsdOf(record, model)
104
105
  );
105
106
  };
106
107
 
package/src/types.ts CHANGED
@@ -414,6 +414,19 @@ export interface ModelPricing {
414
414
  * here: a row without the field prices every geography at its own rate.
415
415
  */
416
416
  inferenceGeoMultipliers?: Partial<Record<InferenceGeo, number>>;
417
+ /**
418
+ * What one thousand web searches cost, in US dollars, billed per search on
419
+ * top of the tokens.
420
+ *
421
+ * Anthropic charges $10 per 1,000 searches on every model and counts them in
422
+ * `usage.server_tool_use.web_search_requests`. The fee is flat: the Batch
423
+ * API prices searches "the same as those in regular Messages API requests",
424
+ * and US-only inference and fast mode multiply token rates, which a search
425
+ * is not. Absent on a row whose provider publishes no such fee, and then a
426
+ * search the log counts is named as unpriced rather than billed at a rate
427
+ * borrowed from somewhere else.
428
+ */
429
+ webSearchPerThousandUsd?: number;
417
430
  /**
418
431
  * Other ids that bill at exactly this rate.
419
432
  *
package/src/usage.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { cacheTtlOf, effectivePricing, multipliersFor } from './pricing.js';
2
- import { billedAt, promptSizeOf } from './billing.js';
2
+ import { billedAt, promptSizeOf, webSearchUsdOf } from './billing.js';
3
3
  import { createConversationTracker } from './conversation.js';
4
4
  import { createOutputShapeTracker } from './output-shape.js';
5
5
  import { createInputShapeTracker } from './input-shape.js';
@@ -209,6 +209,14 @@ export interface UsageRecord {
209
209
  * report converters write for a batch row. Absent is not batch.
210
210
  */
211
211
  batch?: true;
212
+ /**
213
+ * Web searches the call ran, when the log counted any: Anthropic's
214
+ * `usage.server_tool_use.web_search_requests`, which every Messages
215
+ * response and every Claude Code log line carries once a search has run.
216
+ * Billed per search at the row's `webSearchPerThousandUsd`, flat, on top of
217
+ * the tokens. Absent is none.
218
+ */
219
+ webSearchRequests?: number;
212
220
  /**
213
221
  * Set when an OpenAI-shaped line split its cache writes by lifetime and the
214
222
  * parts did not add up to the total. The lifetime is then unknown even on a
@@ -310,6 +318,30 @@ export interface UsageBreakdown {
310
318
  tierCeilingCalls: number;
311
319
  tierCeilingUsd: number;
312
320
  tierFloorUsd: number;
321
+ /**
322
+ * Web searches, and what they cost.
323
+ *
324
+ * Billed per search, not per token, so none of the token lines above holds
325
+ * them: `webSearchUsd` is its own part of `totalUsd`, at each model's
326
+ * `webSearchPerThousandUsd`. It is flat, the same on the Batch API, in
327
+ * fast mode and on US-only inference, because the provider says so for
328
+ * the first and states the other two for tokens only.
329
+ *
330
+ * `webSearchUnpricedRequests` are searches counted on a model whose row
331
+ * publishes no per-search fee. They are left out of `totalUsd` and named,
332
+ * the same treatment an unknown model gets, rather than billed at a rate
333
+ * taken from another provider.
334
+ */
335
+ webSearchRequests: number;
336
+ webSearchUsd: number;
337
+ webSearchUnpricedRequests: number;
338
+ /**
339
+ * What the calls priced at the Batch API rate cost in tokens: already inside
340
+ * the four token lines above, and said apart so a provider's own batch
341
+ * charge can be set against it (`reconcile`) rather than against the whole
342
+ * difference. Their searches are in `webSearchUsd`, at the same fee as any.
343
+ */
344
+ batchUsd: number;
313
345
  }
314
346
 
315
347
  /**
@@ -646,6 +678,8 @@ export interface UsageShares {
646
678
  cacheRead: number;
647
679
  cacheWrite: number;
648
680
  output: number;
681
+ /** Web searches, billed per search. Zero on a bill that ran none. */
682
+ webSearch: number;
649
683
  }
650
684
 
651
685
  const EMPTY = (): UsageBreakdown => ({
@@ -671,6 +705,10 @@ const EMPTY = (): UsageBreakdown => ({
671
705
  tierCeilingCalls: 0,
672
706
  tierCeilingUsd: 0,
673
707
  tierFloorUsd: 0,
708
+ webSearchRequests: 0,
709
+ webSearchUsd: 0,
710
+ webSearchUnpricedRequests: 0,
711
+ batchUsd: 0,
674
712
  });
675
713
 
676
714
  /**
@@ -866,6 +904,19 @@ export function parseUsageLine(line: string): UsageRecord | null {
866
904
  const geminiThoughts = geminiMeta ? readCount(geminiMeta.thoughtsTokenCount) : ({ kind: 'absent' } as Count);
867
905
  const geminiToolPrompt = geminiMeta ? readCount(geminiMeta.toolUsePromptTokenCount) : ({ kind: 'absent' } as Count);
868
906
 
907
+ /**
908
+ * Anthropic's server tools, counted per use rather than per token:
909
+ * `usage.server_tool_use.web_search_requests` is the searches this call ran
910
+ * and was billed for, $10 per 1,000 on top of the tokens. Read under the
911
+ * same rule as every count here, so a present and unreadable figure rejects
912
+ * the line instead of becoming a free search.
913
+ */
914
+ const serverTools =
915
+ typeof usage.server_tool_use === 'object' && usage.server_tool_use !== null
916
+ ? (usage.server_tool_use as Record<string, unknown>)
917
+ : null;
918
+ const webSearch = serverTools ? readCount(serverTools.web_search_requests) : ({ kind: 'absent' } as Count);
919
+
869
920
  const counts: Record<string, Count> = {
870
921
  input: readCount(
871
922
  usage.input_tokens,
@@ -890,6 +941,7 @@ export function parseUsageLine(line: string): UsageRecord | null {
890
941
  geminiToolPrompt,
891
942
  write5m,
892
943
  write1h,
944
+ webSearch,
893
945
  };
894
946
 
895
947
  // Any field present and unreadable rejects the line. See `readCount`.
@@ -1015,6 +1067,7 @@ export function parseUsageLine(line: string): UsageRecord | null {
1015
1067
  * cannot collide on a flat record.
1016
1068
  */
1017
1069
  ...(record.batch === true || usage.service_tier === 'batch' ? { batch: true as const } : {}),
1070
+ ...(valueOf(webSearch) > 0 ? { webSearchRequests: valueOf(webSearch) } : {}),
1018
1071
  ...(openAiSplitGiven && !openAiSplitAgrees ? { writeSplitContradicted: true as const } : {}),
1019
1072
  };
1020
1073
  }
@@ -1107,6 +1160,7 @@ function countInto(into: UsageBreakdown, record: UsageRecord): void {
1107
1160
  into.stopReasonCalls += 1;
1108
1161
  if (record.truncated) into.truncatedCalls += 1;
1109
1162
  }
1163
+ into.webSearchRequests += record.webSearchRequests ?? 0;
1110
1164
  }
1111
1165
 
1112
1166
  function add(into: UsageBreakdown, record: UsageRecord, catalogue: PricingCatalogue, on: Date): boolean {
@@ -1154,6 +1208,7 @@ function add(into: UsageBreakdown, record: UsageRecord, catalogue: PricingCatalo
1154
1208
  per(record.cacheWrite5mTokens, input * rates.cacheWrite5m) +
1155
1209
  per(record.cacheWrite1hTokens, input * rates.cacheWrite1h) +
1156
1210
  per(record.outputTokens, output);
1211
+ if (priced.batch === true) into.batchUsd += recordUsd(inputPerMTok, outputPerMTok);
1157
1212
  const floor = priced.tier?.decided === false ? priced.tier.floor : undefined;
1158
1213
  if (floor !== undefined) {
1159
1214
  into.tierCeilingCalls += 1;
@@ -1197,8 +1252,17 @@ function add(into: UsageBreakdown, record: UsageRecord, catalogue: PricingCatalo
1197
1252
  record.cacheReadTokens + record.cacheWrite5mTokens + record.cacheWrite1hTokens,
1198
1253
  inputPerMTok,
1199
1254
  );
1255
+ /**
1256
+ * Web searches, per search and flat. Read off the row rather than through
1257
+ * `effectivePricing`, which is the token rate with every multiplier the call
1258
+ * earned, and not one of those multipliers reaches a search: the Batch API
1259
+ * prices it "the same", and US-only inference and fast mode scale tokens.
1260
+ */
1261
+ const searches = record.webSearchRequests ?? 0;
1262
+ if (searches > 0 && model.webSearchPerThousandUsd === undefined) into.webSearchUnpricedRequests += searches;
1263
+ into.webSearchUsd += webSearchUsdOf(record, model);
1200
1264
  into.totalUsd =
1201
- into.inputUsd + into.cacheReadUsd + into.cacheWriteUsd + into.outputUsd;
1265
+ into.inputUsd + into.cacheReadUsd + into.cacheWriteUsd + into.outputUsd + into.webSearchUsd;
1202
1266
  return true;
1203
1267
  }
1204
1268
 
@@ -1646,12 +1710,13 @@ export function profileUsage(text: string, options: UsageProfileOptions): UsageP
1646
1710
  */
1647
1711
  export function sharesOf(breakdown: UsageBreakdown): UsageShares {
1648
1712
  const { totalUsd } = breakdown;
1649
- if (totalUsd <= 0) return { input: 0, cacheRead: 0, cacheWrite: 0, output: 0 };
1713
+ if (totalUsd <= 0) return { input: 0, cacheRead: 0, cacheWrite: 0, output: 0, webSearch: 0 };
1650
1714
  return {
1651
1715
  input: breakdown.inputUsd / totalUsd,
1652
1716
  cacheRead: breakdown.cacheReadUsd / totalUsd,
1653
1717
  cacheWrite: breakdown.cacheWriteUsd / totalUsd,
1654
1718
  output: breakdown.outputUsd / totalUsd,
1719
+ webSearch: breakdown.webSearchUsd / totalUsd,
1655
1720
  };
1656
1721
  }
1657
1722