@trazum/core 2.7.3 → 2.7.5

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 (95) 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 +15 -2
  6. package/dist/anthropic-usage.d.ts.map +1 -1
  7. package/dist/anthropic-usage.js +41 -5
  8. package/dist/anthropic-usage.js.map +1 -1
  9. package/dist/batch-results.d.ts +77 -0
  10. package/dist/batch-results.d.ts.map +1 -0
  11. package/dist/batch-results.js +145 -0
  12. package/dist/batch-results.js.map +1 -0
  13. package/dist/billing.d.ts +10 -0
  14. package/dist/billing.d.ts.map +1 -1
  15. package/dist/billing.js +15 -0
  16. package/dist/billing.js.map +1 -1
  17. package/dist/claude-code.d.ts +4 -0
  18. package/dist/claude-code.d.ts.map +1 -1
  19. package/dist/claude-code.js +14 -1
  20. package/dist/claude-code.js.map +1 -1
  21. package/dist/connector.d.ts +19 -1
  22. package/dist/connector.d.ts.map +1 -1
  23. package/dist/connector.js +46 -6
  24. package/dist/connector.js.map +1 -1
  25. package/dist/csv.d.ts +1 -1
  26. package/dist/csv.d.ts.map +1 -1
  27. package/dist/csv.js +11 -0
  28. package/dist/csv.js.map +1 -1
  29. package/dist/gateway.d.ts +2 -0
  30. package/dist/gateway.d.ts.map +1 -1
  31. package/dist/gateway.js +16 -0
  32. package/dist/gateway.js.map +1 -1
  33. package/dist/index.d.ts +2 -0
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +1 -0
  36. package/dist/index.js.map +1 -1
  37. package/dist/openai-usage.d.ts +5 -1
  38. package/dist/openai-usage.d.ts.map +1 -1
  39. package/dist/openai-usage.js +20 -4
  40. package/dist/openai-usage.js.map +1 -1
  41. package/dist/pricing-overlay.d.ts +1 -1
  42. package/dist/pricing-overlay.d.ts.map +1 -1
  43. package/dist/pricing-overlay.js +11 -0
  44. package/dist/pricing-overlay.js.map +1 -1
  45. package/dist/pricing.d.ts +8 -0
  46. package/dist/pricing.d.ts.map +1 -1
  47. package/dist/pricing.js +74 -17
  48. package/dist/pricing.js.map +1 -1
  49. package/dist/receipt.d.ts +38 -4
  50. package/dist/receipt.d.ts.map +1 -1
  51. package/dist/receipt.js +19 -1
  52. package/dist/receipt.js.map +1 -1
  53. package/dist/repeats.d.ts.map +1 -1
  54. package/dist/repeats.js +3 -2
  55. package/dist/repeats.js.map +1 -1
  56. package/dist/rollup.d.ts.map +1 -1
  57. package/dist/rollup.js +4 -0
  58. package/dist/rollup.js.map +1 -1
  59. package/dist/session-cost.d.ts.map +1 -1
  60. package/dist/session-cost.js +4 -2
  61. package/dist/session-cost.js.map +1 -1
  62. package/dist/store.d.ts +5 -0
  63. package/dist/store.d.ts.map +1 -1
  64. package/dist/store.js +4 -0
  65. package/dist/store.js.map +1 -1
  66. package/dist/truncation-retry.d.ts.map +1 -1
  67. package/dist/truncation-retry.js +3 -2
  68. package/dist/truncation-retry.js.map +1 -1
  69. package/dist/types.d.ts +13 -0
  70. package/dist/types.d.ts.map +1 -1
  71. package/dist/usage.d.ts +42 -0
  72. package/dist/usage.d.ts.map +1 -1
  73. package/dist/usage.js +42 -3
  74. package/dist/usage.js.map +1 -1
  75. package/package.json +1 -1
  76. package/src/anthropic-cost.ts +46 -12
  77. package/src/anthropic-usage.ts +54 -7
  78. package/src/batch-results.ts +174 -0
  79. package/src/billing.ts +16 -0
  80. package/src/claude-code.ts +16 -1
  81. package/src/connector.ts +68 -8
  82. package/src/csv.ts +11 -0
  83. package/src/gateway.ts +16 -0
  84. package/src/index.ts +2 -0
  85. package/src/openai-usage.ts +24 -5
  86. package/src/pricing-overlay.ts +15 -0
  87. package/src/pricing.ts +83 -17
  88. package/src/receipt.ts +53 -6
  89. package/src/repeats.ts +3 -2
  90. package/src/rollup.ts +4 -0
  91. package/src/session-cost.ts +4 -2
  92. package/src/store.ts +9 -0
  93. package/src/truncation-retry.ts +3 -2
  94. package/src/types.ts +13 -0
  95. package/src/usage.ts +84 -3
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';
@@ -201,6 +201,22 @@ export interface UsageRecord {
201
201
  * not-available are the row's own price.
202
202
  */
203
203
  inferenceGeo?: string;
204
+ /**
205
+ * True when the call ran on the provider's Batch API, which bills it at the
206
+ * row's batch ratio (half the standard rate at Anthropic and OpenAI). Read
207
+ * from Anthropic's `usage.service_tier: "batch"`, which every Messages
208
+ * response carries, or from a record-level `batch: true`, which the usage
209
+ * report converters write for a batch row. Absent is not batch.
210
+ */
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;
204
220
  /**
205
221
  * Set when an OpenAI-shaped line split its cache writes by lifetime and the
206
222
  * parts did not add up to the total. The lifetime is then unknown even on a
@@ -302,6 +318,30 @@ export interface UsageBreakdown {
302
318
  tierCeilingCalls: number;
303
319
  tierCeilingUsd: number;
304
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;
305
345
  }
306
346
 
307
347
  /**
@@ -638,6 +678,8 @@ export interface UsageShares {
638
678
  cacheRead: number;
639
679
  cacheWrite: number;
640
680
  output: number;
681
+ /** Web searches, billed per search. Zero on a bill that ran none. */
682
+ webSearch: number;
641
683
  }
642
684
 
643
685
  const EMPTY = (): UsageBreakdown => ({
@@ -663,6 +705,10 @@ const EMPTY = (): UsageBreakdown => ({
663
705
  tierCeilingCalls: 0,
664
706
  tierCeilingUsd: 0,
665
707
  tierFloorUsd: 0,
708
+ webSearchRequests: 0,
709
+ webSearchUsd: 0,
710
+ webSearchUnpricedRequests: 0,
711
+ batchUsd: 0,
666
712
  });
667
713
 
668
714
  /**
@@ -858,6 +904,19 @@ export function parseUsageLine(line: string): UsageRecord | null {
858
904
  const geminiThoughts = geminiMeta ? readCount(geminiMeta.thoughtsTokenCount) : ({ kind: 'absent' } as Count);
859
905
  const geminiToolPrompt = geminiMeta ? readCount(geminiMeta.toolUsePromptTokenCount) : ({ kind: 'absent' } as Count);
860
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
+
861
920
  const counts: Record<string, Count> = {
862
921
  input: readCount(
863
922
  usage.input_tokens,
@@ -882,6 +941,7 @@ export function parseUsageLine(line: string): UsageRecord | null {
882
941
  geminiToolPrompt,
883
942
  write5m,
884
943
  write1h,
944
+ webSearch,
885
945
  };
886
946
 
887
947
  // Any field present and unreadable rejects the line. See `readCount`.
@@ -999,6 +1059,15 @@ export function parseUsageLine(line: string): UsageRecord | null {
999
1059
  ...(typeof usage.inference_geo === 'string' && usage.inference_geo !== ''
1000
1060
  ? { inferenceGeo: usage.inference_geo }
1001
1061
  : {}),
1062
+ /*
1063
+ * The Batch API, billed at half the standard rate by both providers.
1064
+ * Anthropic names it in `usage.service_tier` ("standard", "priority" or
1065
+ * "batch"); a converted usage report row says `batch: true`. OpenAI's
1066
+ * response-level `service_tier` never says "batch", so the two readings
1067
+ * cannot collide on a flat record.
1068
+ */
1069
+ ...(record.batch === true || usage.service_tier === 'batch' ? { batch: true as const } : {}),
1070
+ ...(valueOf(webSearch) > 0 ? { webSearchRequests: valueOf(webSearch) } : {}),
1002
1071
  ...(openAiSplitGiven && !openAiSplitAgrees ? { writeSplitContradicted: true as const } : {}),
1003
1072
  };
1004
1073
  }
@@ -1091,6 +1160,7 @@ function countInto(into: UsageBreakdown, record: UsageRecord): void {
1091
1160
  into.stopReasonCalls += 1;
1092
1161
  if (record.truncated) into.truncatedCalls += 1;
1093
1162
  }
1163
+ into.webSearchRequests += record.webSearchRequests ?? 0;
1094
1164
  }
1095
1165
 
1096
1166
  function add(into: UsageBreakdown, record: UsageRecord, catalogue: PricingCatalogue, on: Date): boolean {
@@ -1138,6 +1208,7 @@ function add(into: UsageBreakdown, record: UsageRecord, catalogue: PricingCatalo
1138
1208
  per(record.cacheWrite5mTokens, input * rates.cacheWrite5m) +
1139
1209
  per(record.cacheWrite1hTokens, input * rates.cacheWrite1h) +
1140
1210
  per(record.outputTokens, output);
1211
+ if (priced.batch === true) into.batchUsd += recordUsd(inputPerMTok, outputPerMTok);
1141
1212
  const floor = priced.tier?.decided === false ? priced.tier.floor : undefined;
1142
1213
  if (floor !== undefined) {
1143
1214
  into.tierCeilingCalls += 1;
@@ -1181,8 +1252,17 @@ function add(into: UsageBreakdown, record: UsageRecord, catalogue: PricingCatalo
1181
1252
  record.cacheReadTokens + record.cacheWrite5mTokens + record.cacheWrite1hTokens,
1182
1253
  inputPerMTok,
1183
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);
1184
1264
  into.totalUsd =
1185
- into.inputUsd + into.cacheReadUsd + into.cacheWriteUsd + into.outputUsd;
1265
+ into.inputUsd + into.cacheReadUsd + into.cacheWriteUsd + into.outputUsd + into.webSearchUsd;
1186
1266
  return true;
1187
1267
  }
1188
1268
 
@@ -1630,12 +1710,13 @@ export function profileUsage(text: string, options: UsageProfileOptions): UsageP
1630
1710
  */
1631
1711
  export function sharesOf(breakdown: UsageBreakdown): UsageShares {
1632
1712
  const { totalUsd } = breakdown;
1633
- 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 };
1634
1714
  return {
1635
1715
  input: breakdown.inputUsd / totalUsd,
1636
1716
  cacheRead: breakdown.cacheReadUsd / totalUsd,
1637
1717
  cacheWrite: breakdown.cacheWriteUsd / totalUsd,
1638
1718
  output: breakdown.outputUsd / totalUsd,
1719
+ webSearch: breakdown.webSearchUsd / totalUsd,
1639
1720
  };
1640
1721
  }
1641
1722