@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
@@ -0,0 +1,174 @@
1
+ /**
2
+ * A provider's Batch API results file, read as a usage log.
3
+ *
4
+ * Both providers return a batch's results as JSON Lines, one per request, and
5
+ * bill every one that ran at half the standard price. Neither shape is a usage
6
+ * line: the model and usage sit one or two objects down, so `parseUsageLine`
7
+ * read a whole results file as nothing at all. This converter takes each line
8
+ * that was billed, keeps its model, usage and end reason, marks it `batch`, and
9
+ * drops everything else, the generated text first among it.
10
+ *
11
+ * ## The two shapes, each from its provider's own page
12
+ *
13
+ * **Anthropic** (platform.claude.com/docs/en/build-with-claude/batch-processing,
14
+ * read 2026-11-05): `{"custom_id", "result": {"type", "message"}}`. `type` is
15
+ * `succeeded`, `errored`, `canceled` or `expired`, and the page says of the
16
+ * last three "You will not be billed for these requests". A succeeded result's
17
+ * `message` is a Messages response, `usage` and all.
18
+ *
19
+ * **OpenAI** (developers.openai.com/api/docs/guides/batch, read 2026-11-05):
20
+ * `{"id", "custom_id", "response": {"status_code", "request_id", "body"},
21
+ * "error"}`. `body` is the endpoint's own response, a Responses or Chat
22
+ * Completions object. An expired request is written with `"response": null`
23
+ * and an `error`; the page says "You will be charged for tokens consumed from
24
+ * any completed requests", so a line is billed only when it carries a 200 and
25
+ * a body with usage.
26
+ *
27
+ * ## What it does not invent
28
+ *
29
+ * **No time.** An Anthropic result carries none; it is counted as undated, the
30
+ * way `bill` already names an undated call, rather than stamped with the day
31
+ * the file was read. An OpenAI body's `created` is Unix seconds and is kept.
32
+ *
33
+ * **No price.** The record says `batch: true` and the model; the batch ratio is
34
+ * the catalogue's, applied where every other call is priced.
35
+ *
36
+ * **No content.** The message's `content`, the body's `output` and `choices`,
37
+ * the `custom_id` (which a caller may have filled with anything) and every id
38
+ * are dropped. A test plants a marker in each and greps the output.
39
+ */
40
+
41
+ /** One converted record, in the shape `parseUsageLine` reads. */
42
+ export interface BatchResultRecord {
43
+ model: string;
44
+ ts?: string;
45
+ label?: string;
46
+ /** Always true: every line here ran on the Batch API. */
47
+ batch: true;
48
+ stop_reason?: string;
49
+ usage: Record<string, unknown>;
50
+ }
51
+
52
+ export interface BatchResultConversion {
53
+ records: BatchResultRecord[];
54
+ /** Which provider's shape the file was, or null when no line was either. */
55
+ provider: 'anthropic' | 'openai' | null;
56
+ /** Non-blank lines read. */
57
+ lines: number;
58
+ /** Results the provider says it did not bill: errored, canceled, expired, or not a 200. */
59
+ notBilled: number;
60
+ /** Billed results with no model or usage to price them by. */
61
+ unpriceable: number;
62
+ /** Lines that were not JSON, or neither shape. */
63
+ unparseable: number;
64
+ }
65
+
66
+ const asObject = (value: unknown): Record<string, unknown> | null =>
67
+ typeof value === 'object' && value !== null && !Array.isArray(value)
68
+ ? (value as Record<string, unknown>)
69
+ : null;
70
+
71
+ /** Usage as the response gave it, minus nothing it bills by and plus nothing. */
72
+ const usageOf = (value: unknown): Record<string, unknown> | null => asObject(value);
73
+
74
+ /**
75
+ * @param label the project these results belong to, chosen by the operator.
76
+ */
77
+ export function batchResultRecords(text: string, options: { label?: string } = {}): BatchResultConversion {
78
+ const records: BatchResultRecord[] = [];
79
+ let provider: BatchResultConversion['provider'] = null;
80
+ let lines = 0;
81
+ let notBilled = 0;
82
+ let unpriceable = 0;
83
+ let unparseable = 0;
84
+
85
+ for (const raw of text.split('\n')) {
86
+ if (raw.trim() === '') continue;
87
+ lines += 1;
88
+ let parsed: unknown;
89
+ try {
90
+ parsed = JSON.parse(raw);
91
+ } catch {
92
+ unparseable += 1;
93
+ continue;
94
+ }
95
+ const line = asObject(parsed);
96
+ if (line === null || typeof line.custom_id !== 'string') {
97
+ unparseable += 1;
98
+ continue;
99
+ }
100
+
101
+ const result = asObject(line.result);
102
+ if (result !== null && typeof result.type === 'string') {
103
+ provider ??= 'anthropic';
104
+ if (result.type !== 'succeeded') {
105
+ notBilled += 1;
106
+ continue;
107
+ }
108
+ const message = asObject(result.message);
109
+ const usage = usageOf(message?.usage);
110
+ if (message === null || typeof message.model !== 'string' || usage === null) {
111
+ unpriceable += 1;
112
+ continue;
113
+ }
114
+ records.push({
115
+ model: message.model,
116
+ ...(options.label === undefined ? {} : { label: options.label }),
117
+ batch: true,
118
+ ...(typeof message.stop_reason === 'string' ? { stop_reason: message.stop_reason } : {}),
119
+ usage,
120
+ });
121
+ continue;
122
+ }
123
+
124
+ if ('response' in line) {
125
+ provider ??= 'openai';
126
+ const response = asObject(line.response);
127
+ if (response === null || response.status_code !== 200) {
128
+ notBilled += 1;
129
+ continue;
130
+ }
131
+ const body = asObject(response.body);
132
+ const usage = usageOf(body?.usage);
133
+ if (body === null || typeof body.model !== 'string' || usage === null) {
134
+ unpriceable += 1;
135
+ continue;
136
+ }
137
+ const created =
138
+ typeof body.created_at === 'number' ? body.created_at : typeof body.created === 'number' ? body.created : null;
139
+ records.push({
140
+ model: body.model,
141
+ ...(created !== null && Number.isFinite(created) ? { ts: new Date(created * 1000).toISOString() } : {}),
142
+ ...(options.label === undefined ? {} : { label: options.label }),
143
+ batch: true,
144
+ usage,
145
+ });
146
+ continue;
147
+ }
148
+
149
+ unparseable += 1;
150
+ }
151
+
152
+ return { records, provider, lines, notBilled, unpriceable, unparseable };
153
+ }
154
+
155
+ /**
156
+ * Whether this text is a Batch API results file.
157
+ *
158
+ * Every line carries a `custom_id`, which no other log here does, beside
159
+ * either Anthropic's `result` with a `type` or OpenAI's `response` with a
160
+ * `status_code`. The first line decides; one that is neither is not this.
161
+ */
162
+ export function looksLikeBatchResults(text: string, prefixBytes = 8192): boolean {
163
+ const first = text.slice(0, prefixBytes).split('\n').find((line) => line.trim() !== '');
164
+ if (first === undefined || !first.includes('"custom_id"')) return false;
165
+ try {
166
+ const line = asObject(JSON.parse(first));
167
+ if (line === null || typeof line.custom_id !== 'string') return false;
168
+ const result = asObject(line.result);
169
+ if (result !== null && typeof result.type === 'string') return true;
170
+ return 'response' in line && ('error' in line || asObject(line.response)?.status_code !== undefined);
171
+ } catch {
172
+ return false;
173
+ }
174
+ }
package/src/billing.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { PromptSize } from './pricing.js';
2
+ import type { ModelPricing } from './types.js';
2
3
  import type { UsageRecord } from './usage.js';
3
4
 
4
5
  /*
@@ -37,6 +38,21 @@ export function promptSizeOf(record: UsageRecord): PromptSize {
37
38
  ...(record.speed === undefined ? {} : { speed: record.speed }),
38
39
  ...(record.serviceTier === undefined ? {} : { serviceTier: record.serviceTier }),
39
40
  ...(record.inferenceGeo === undefined ? {} : { inferenceGeo: record.inferenceGeo }),
41
+ ...(record.batch === true ? { batch: true } : {}),
40
42
  };
41
43
  return record.requests === undefined ? { inputTokens: prompt, ...how } : { inputTokensAtMost: prompt, ...how };
42
44
  }
45
+
46
+ /**
47
+ * What a record's web searches cost: per search, at the row's fee, flat.
48
+ *
49
+ * Here beside `billedAt` and `promptSizeOf` for the same reason they are: the
50
+ * total and every tracker under it price a call, and a finding that costs a
51
+ * call without its searches disagrees with the bill it sits under. Zero for a
52
+ * row with no fee, whose searches the total names as unpriced instead.
53
+ */
54
+ export function webSearchUsdOf(record: UsageRecord, model: ModelPricing): number {
55
+ const searches = record.webSearchRequests ?? 0;
56
+ if (searches <= 0 || model.webSearchPerThousandUsd === undefined) return 0;
57
+ return (searches / 1_000) * model.webSearchPerThousandUsd;
58
+ }
@@ -78,6 +78,8 @@ export interface ClaudeCodeRecord {
78
78
  speed?: string;
79
79
  /** The response's `usage.inference_geo`, kept because US-only inference bills 1.1x on Claude 4.6 and later. */
80
80
  inference_geo?: string;
81
+ /** The searches the call ran, kept because each is billed at $10 per 1,000 on top of the tokens. */
82
+ server_tool_use?: { web_search_requests: number };
81
83
  };
82
84
  }
83
85
 
@@ -291,6 +293,18 @@ export function claudeCodeRecords(
291
293
  ...(typeof usage.inference_geo === 'string' && usage.inference_geo !== ''
292
294
  ? { inference_geo: usage.inference_geo }
293
295
  : {}),
296
+ /*
297
+ * And the searches: Claude Code's web search is the API's server-side
298
+ * tool, billed at $10 per 1,000 searches on top of the tokens it
299
+ * reads, and the response counts them in
300
+ * `usage.server_tool_use.web_search_requests`. Rebuilding the usage
301
+ * without it billed every searching session as if it had searched
302
+ * for nothing. Only the count crosses; what was searched for is in
303
+ * the content, which never does.
304
+ */
305
+ ...((asCount(asObject(usage.server_tool_use)?.web_search_requests) ?? 0) > 0
306
+ ? { server_tool_use: { web_search_requests: asCount(asObject(usage.server_tool_use)?.web_search_requests)! } }
307
+ : {}),
294
308
  },
295
309
  };
296
310
 
@@ -326,7 +340,8 @@ export function claudeCodeRecords(
326
340
  record.usage.input_tokens >= before.input_tokens &&
327
341
  record.usage.output_tokens >= before.output_tokens &&
328
342
  (record.usage.cache_read_input_tokens ?? 0) >= (before.cache_read_input_tokens ?? 0) &&
329
- (record.usage.cache_creation_input_tokens ?? 0) >= (before.cache_creation_input_tokens ?? 0);
343
+ (record.usage.cache_creation_input_tokens ?? 0) >= (before.cache_creation_input_tokens ?? 0) &&
344
+ (record.usage.server_tool_use?.web_search_requests ?? 0) >= (before.server_tool_use?.web_search_requests ?? 0);
330
345
  if (grewOnly) out.streamed += 1;
331
346
  else out.disagreements += 1;
332
347
  seenUsage.set(requestId, record.usage);
package/src/connector.ts CHANGED
@@ -178,6 +178,12 @@ export interface UsageBucket {
178
178
  /** False when the provider reported writes without saying which TTL. */
179
179
  writeTtlKnown: boolean;
180
180
  outputTokens: number;
181
+ /**
182
+ * Web searches the bucket's requests ran, billed per search on top of the
183
+ * tokens: Anthropic's `server_tool_use.web_search_requests`. Absent is none,
184
+ * which is also what a pull stored before 2.7.5 reads as.
185
+ */
186
+ webSearchRequests?: number;
181
187
  /** Whatever the provider grouped by beyond the model — workspace, key, tier. */
182
188
  group: Record<string, string>;
183
189
  }
@@ -198,7 +204,9 @@ export interface PullGap {
198
204
  | 'unreadable-entry'
199
205
  | 'unreadable-field'
200
206
  /** Usage at a tier no catalogue rate prices, left out and named. */
201
- | 'unpriced-tier';
207
+ | 'unpriced-tier'
208
+ /** Web searches on a model whose row publishes no per-search fee, left out and named. */
209
+ | 'unpriced-web-search';
202
210
  detail: string;
203
211
  }
204
212
 
@@ -241,6 +249,9 @@ function collect(buckets: UsageBucket[]): UsageBucket[] {
241
249
  seen.cacheWrite5mTokens += bucket.cacheWrite5mTokens;
242
250
  seen.cacheWrite1hTokens += bucket.cacheWrite1hTokens;
243
251
  seen.outputTokens += bucket.outputTokens;
252
+ if ((bucket.webSearchRequests ?? 0) > 0) {
253
+ seen.webSearchRequests = (seen.webSearchRequests ?? 0) + bucket.webSearchRequests!;
254
+ }
244
255
  seen.writeTtlKnown = seen.writeTtlKnown && bucket.writeTtlKnown;
245
256
  // A count merged with an absent count is still absent: adding a number to
246
257
  // "unknown" produces a number that describes only part of the traffic.
@@ -308,6 +319,8 @@ export function normalizeAnthropicUsage(payload: unknown): ConnectorPull {
308
319
  const write1h = num(creation.ephemeral_1h_input_tokens) ?? 0;
309
320
  const flatWrite = num(r.cache_creation_input_tokens) ?? 0;
310
321
  const ttlKnown = !(flatWrite > 0 && write5m === 0 && write1h === 0);
322
+ /* Billed per search, $10 per 1,000 on Claude: the one count here that is not tokens. */
323
+ const searches = num((r.server_tool_use as Record<string, unknown> | undefined)?.web_search_requests) ?? 0;
311
324
  buckets.push({
312
325
  fromMs,
313
326
  toMs,
@@ -320,6 +333,7 @@ export function normalizeAnthropicUsage(payload: unknown): ConnectorPull {
320
333
  cacheWrite1hTokens: ttlKnown ? write1h : 0,
321
334
  writeTtlKnown: ttlKnown,
322
335
  outputTokens: num(r.output_tokens) ?? 0,
336
+ ...(searches > 0 ? { webSearchRequests: searches } : {}),
323
337
  group: groupOf(r, ['workspace_id', 'api_key_id', 'service_tier', 'context_window', 'speed', 'inference_geo']),
324
338
  });
325
339
  }
@@ -461,6 +475,13 @@ export interface BucketedSlice {
461
475
  tierCeilingBuckets: number;
462
476
  tierCeilingUsd: number;
463
477
  tierFloorUsd: number;
478
+ /**
479
+ * Web searches, billed per search at the model's fee, flat: the Batch API,
480
+ * fast mode and US-only inference move token rates, not this. Part of
481
+ * `totalUsd`.
482
+ */
483
+ webSearchRequests: number;
484
+ webSearchUsd: number;
464
485
  }
465
486
 
466
487
  export interface BucketedReport {
@@ -476,6 +497,9 @@ export interface BucketedReport {
476
497
  cacheReadTokens: number;
477
498
  cacheWriteTokens: number;
478
499
  outputTokens: number;
500
+ /** Web searches billed, and what they came to: already inside `totalUsd`. */
501
+ webSearchRequests: number;
502
+ webSearchUsd: number;
479
503
  };
480
504
  byModel: BucketedSlice[];
481
505
  /** Spend per UTC day, oldest first — the shape a total hides. */
@@ -521,6 +545,8 @@ export function bucketedProfile(
521
545
  + bucket.outputTokens;
522
546
  leftOut.set(key, seen);
523
547
  };
548
+ /* Searches on a model with no per-search fee, per model, so the gap names it. */
549
+ const searchesLeftOut = new Map<string, number>();
524
550
 
525
551
  for (const bucket of pull.buckets) {
526
552
  const model = catalogue.byId.get(bucket.model);
@@ -540,10 +566,14 @@ export function bucketedProfile(
540
566
  * them reads as standard, as it always did.
541
567
  */
542
568
  const group = bucket.group;
543
- const how: { speed?: string; serviceTier?: string; inferenceGeo?: string } = {};
569
+ const how: { speed?: string; serviceTier?: string; inferenceGeo?: string; batch?: boolean } = {};
570
+ /* The Batch API at the row's batch ratio, where the row has one. */
571
+ const batchRate = multipliersFor(model).batch !== null;
544
572
  if (pull.provider === 'anthropic') {
545
573
  const tier = group.service_tier;
546
- if (tier !== undefined && tier !== 'standard') {
574
+ if (tier === 'batch' && batchRate) {
575
+ how.batch = true;
576
+ } else if (tier !== undefined && tier !== 'standard') {
547
577
  leave(bucket, tier);
548
578
  continue;
549
579
  }
@@ -558,11 +588,14 @@ export function bucketedProfile(
558
588
  if (group.inference_geo !== undefined) how.inferenceGeo = group.inference_geo;
559
589
  } else if (pull.provider === 'openai') {
560
590
  if (group.batch === 'true') {
561
- leave(bucket, 'batch');
562
- continue;
591
+ if (!batchRate) {
592
+ leave(bucket, 'batch');
593
+ continue;
594
+ }
595
+ how.batch = true;
563
596
  }
564
597
  const tier = group.service_tier;
565
- if (tier !== undefined && tier !== 'default') {
598
+ if (tier !== undefined && tier !== 'default' && !(how.batch === true && tier === 'batch')) {
566
599
  const named = serviceTierOf(tier);
567
600
  if (named === undefined || model.serviceTiers?.[named] === undefined) {
568
601
  leave(bucket, tier);
@@ -606,6 +639,17 @@ export function bucketedProfile(
606
639
  const ifAssumed1h = bucket.writeTtlKnown
607
640
  ? cacheWriteUsd
608
641
  : per(writeTokens, inputPerMTok * rates.cacheWrite1h);
642
+ /*
643
+ * Searches at the row's fee, flat, read off the model and not out of
644
+ * `effectivePricing`: none of the multipliers `how` carries reaches a
645
+ * search. A model with no fee leaves them out, named below.
646
+ */
647
+ const searches = bucket.webSearchRequests ?? 0;
648
+ const fee = model.webSearchPerThousandUsd;
649
+ const searchUsd = searches > 0 && fee !== undefined ? (searches / 1_000) * fee : 0;
650
+ if (searches > 0 && fee === undefined) {
651
+ searchesLeftOut.set(bucket.model, (searchesLeftOut.get(bucket.model) ?? 0) + searches);
652
+ }
609
653
 
610
654
  const slice = slices.get(bucket.model) ?? {
611
655
  model: bucket.model,
@@ -625,6 +669,8 @@ export function bucketedProfile(
625
669
  tierCeilingBuckets: 0,
626
670
  tierCeilingUsd: 0,
627
671
  tierFloorUsd: 0,
672
+ webSearchRequests: 0,
673
+ webSearchUsd: 0,
628
674
  };
629
675
  slice.calls = slice.calls === null || bucket.calls === null ? null : slice.calls + bucket.calls;
630
676
  slice.inputTokens += bucket.inputTokens;
@@ -635,7 +681,11 @@ export function bucketedProfile(
635
681
  slice.cacheReadUsd += cacheReadUsd;
636
682
  slice.cacheWriteUsd += cacheWriteUsd;
637
683
  slice.outputUsd += outputUsd;
638
- slice.totalUsd += inputUsd + cacheReadUsd + cacheWriteUsd + outputUsd;
684
+ slice.totalUsd += inputUsd + cacheReadUsd + cacheWriteUsd + outputUsd + searchUsd;
685
+ if (fee !== undefined) {
686
+ slice.webSearchRequests += searches;
687
+ slice.webSearchUsd += searchUsd;
688
+ }
639
689
  slice.cachedTokensAtInputRateUsd += atInputRate;
640
690
  slice.cacheWriteUsdIfAssumed1h += ifAssumed1h;
641
691
  slice.writeTtlKnown = slice.writeTtlKnown && bucket.writeTtlKnown;
@@ -648,7 +698,7 @@ export function bucketedProfile(
648
698
 
649
699
  const day = new Date(bucket.fromMs).toISOString().slice(0, 10);
650
700
  const entry = days.get(day) ?? { usd: 0, calls: bucket.calls === null ? null : 0 };
651
- entry.usd += inputUsd + cacheReadUsd + cacheWriteUsd + outputUsd;
701
+ entry.usd += inputUsd + cacheReadUsd + cacheWriteUsd + outputUsd + searchUsd;
652
702
  entry.calls = entry.calls === null || bucket.calls === null ? null : entry.calls + bucket.calls;
653
703
  days.set(day, entry);
654
704
  }
@@ -668,6 +718,8 @@ export function bucketedProfile(
668
718
  cacheReadTokens: byModel.reduce((sum, s) => sum + s.cacheReadTokens, 0),
669
719
  cacheWriteTokens: byModel.reduce((sum, s) => sum + s.cacheWriteTokens, 0),
670
720
  outputTokens: byModel.reduce((sum, s) => sum + s.outputTokens, 0),
721
+ webSearchRequests: byModel.reduce((sum, s) => sum + s.webSearchRequests, 0),
722
+ webSearchUsd: byModel.reduce((sum, s) => sum + s.webSearchUsd, 0),
671
723
  },
672
724
  byModel,
673
725
  byDay: [...days.entries()]
@@ -686,6 +738,14 @@ export function bucketedProfile(
686
738
  + `and ${left.tokens.toLocaleString('en-US')} token(s) were left out rather than priced at the standard rate`,
687
739
  }),
688
740
  ),
741
+ ...[...searchesLeftOut.entries()].map(
742
+ ([model, searches]): PullGap => ({
743
+ kind: 'unpriced-web-search',
744
+ detail:
745
+ `${searches.toLocaleString('en-US')} web search(es) on ${model}, whose row publishes no per-search fee, `
746
+ + 'were left out rather than billed at another provider\'s',
747
+ }),
748
+ ),
689
749
  ],
690
750
  unavailable: pull.unavailable,
691
751
  };
package/src/csv.ts CHANGED
@@ -45,6 +45,13 @@ export const PROFILE_CSV_COLUMNS = [
45
45
  'cache_write_usd',
46
46
  'output_usd',
47
47
  'total_usd',
48
+ /*
49
+ * Appended, never inserted, so a reader that addresses columns by position
50
+ * keeps reading the same twelve. Web searches are billed per search, so
51
+ * `total_usd` is the four token columns plus `web_search_usd`.
52
+ */
53
+ 'web_search_requests',
54
+ 'web_search_usd',
48
55
  ] as const;
49
56
 
50
57
  /**
@@ -164,6 +171,8 @@ export function profileToCsv(report: UsageProfileReport, options: ProfileCsvOpti
164
171
  usd(breakdown.cacheWriteUsd),
165
172
  usd(breakdown.outputUsd),
166
173
  usd(breakdown.totalUsd),
174
+ String(breakdown.webSearchRequests),
175
+ usd(breakdown.webSearchUsd),
167
176
  ].join(','),
168
177
  );
169
178
  }
@@ -193,6 +202,8 @@ export function profileToCsv(report: UsageProfileReport, options: ProfileCsvOpti
193
202
  '',
194
203
  '',
195
204
  '',
205
+ String(row.breakdown.webSearchRequests),
206
+ '',
196
207
  ].join(','),
197
208
  );
198
209
  }
package/src/gateway.ts CHANGED
@@ -523,8 +523,18 @@ export interface GatewayUsage {
523
523
  serviceTier?: string;
524
524
  /** Anthropic's `usage.inference_geo`, the geography that ran: "us" bills 1.1x on Claude 4.6 and later. */
525
525
  inferenceGeo?: string;
526
+ /** Anthropic's `usage.server_tool_use.web_search_requests`: each search bills $10 per 1,000 on top. */
527
+ webSearchRequests?: number;
526
528
  }
527
529
 
530
+ /** The searches a usage object counts, when it counts a readable number of them. */
531
+ const webSearchesIn = (usage: Record<string, unknown>): number | undefined => {
532
+ const tools = usage.server_tool_use;
533
+ if (typeof tools !== 'object' || tools === null) return undefined;
534
+ const searches = (tools as Record<string, unknown>).web_search_requests;
535
+ return typeof searches === 'number' && Number.isFinite(searches) && searches >= 0 ? searches : undefined;
536
+ };
537
+
528
538
  /**
529
539
  * OpenAI's cache writes, inside `prompt_tokens` like its cached reads.
530
540
  *
@@ -693,6 +703,8 @@ export function streamingUsageReader(
693
703
  usage.outputTokens = num(start.output_tokens);
694
704
  if (typeof start.speed === 'string' && start.speed !== '') usage.speed = start.speed;
695
705
  if (typeof start.inference_geo === 'string' && start.inference_geo !== '') usage.inferenceGeo = start.inference_geo;
706
+ const searches = webSearchesIn(start);
707
+ if (searches !== undefined) usage.webSearchRequests = searches;
696
708
  seen = true;
697
709
  }
698
710
  }
@@ -711,6 +723,9 @@ export function streamingUsageReader(
711
723
  if (typeof d.input_tokens === 'number') usage.inputTokens = num(d.input_tokens);
712
724
  if (typeof d.speed === 'string' && d.speed !== '') usage.speed = d.speed;
713
725
  if (typeof d.inference_geo === 'string' && d.inference_geo !== '') usage.inferenceGeo = d.inference_geo;
726
+ // Cumulative like the output count: the last delta's searches are the call's.
727
+ const searches = webSearchesIn(d);
728
+ if (searches !== undefined) usage.webSearchRequests = searches;
714
729
  seen = true;
715
730
  }
716
731
  return;
@@ -814,6 +829,7 @@ export function usageFromResponse(
814
829
  ...cacheWriteBreakdown(u),
815
830
  ...(typeof u.speed === 'string' && u.speed !== '' ? { speed: u.speed } : {}),
816
831
  ...(typeof u.inference_geo === 'string' && u.inference_geo !== '' ? { inferenceGeo: u.inference_geo } : {}),
832
+ ...(webSearchesIn(u) === undefined ? {} : { webSearchRequests: webSearchesIn(u) }),
817
833
  };
818
834
  }
819
835
  if (shape === 'openai') {
package/src/index.ts CHANGED
@@ -242,6 +242,8 @@ export type { AnthropicCostReading, BilledReading, Reconciliation } from './anth
242
242
  export { openaiCostReport, looksLikeOpenaiCost } from './openai-cost.js';
243
243
  export type { OpenaiCostReading } from './openai-cost.js';
244
244
  export { openaiUsageRecords, looksLikeOpenaiUsage } from './openai-usage.js';
245
+ export { batchResultRecords, looksLikeBatchResults } from './batch-results.js';
246
+ export type { BatchResultConversion, BatchResultRecord } from './batch-results.js';
245
247
  export { openrouterActivityRecords, looksLikeOpenrouterActivity } from './openrouter-activity.js';
246
248
  export type {
247
249
  OpenrouterActivityConversion,
@@ -94,7 +94,7 @@
94
94
  * absence and no second reading to derive.
95
95
  */
96
96
 
97
- import { BUNDLED_CATALOGUE, serviceTierOf } from './pricing.js';
97
+ import { BUNDLED_CATALOGUE, multipliersFor, serviceTierOf } from './pricing.js';
98
98
  import type { PricingCatalogue } from './pricing.js';
99
99
 
100
100
  /** One project, and the label the operator gives it. Matched exactly. */
@@ -121,6 +121,8 @@ export interface OpenaiUsageRecord {
121
121
  * the default tier.
122
122
  */
123
123
  service_tier?: string;
124
+ /** True when the row ran on the Batch API: billed at the row's batch ratio. */
125
+ batch?: true;
124
126
  usage: {
125
127
  prompt_tokens: number;
126
128
  completion_tokens: number;
@@ -143,8 +145,10 @@ export interface OpenaiUsageConversion {
143
145
  requests: number;
144
146
  /** Results with `model: null`, which nothing can price. */
145
147
  unnamedModel: number;
146
- /** Results with `batch: true`, billed at a discount no catalogue rate is. */
148
+ /** Results with `batch: true` on a model with no batch rate, left out. */
147
149
  batch: number;
150
+ /** Results with `batch: true` that became records priced at the row's batch ratio. */
151
+ batchRows: number;
148
152
  /** Whether any result said whether it was batch at all. */
149
153
  batchNamed: boolean;
150
154
  /**
@@ -229,6 +233,7 @@ export function openaiUsageRecords(
229
233
  requests: 0,
230
234
  unnamedModel: 0,
231
235
  batch: 0,
236
+ batchRows: 0,
232
237
  batchNamed: false,
233
238
  nonDefaultTier: 0,
234
239
  tiersRefused: [],
@@ -267,6 +272,7 @@ export function openaiUsageRecords(
267
272
  let unnamedModel = 0;
268
273
  let batch = 0;
269
274
  let batchNamed = false;
275
+ let batchRows = 0;
270
276
  let nonDefaultTier = 0;
271
277
  let serviceTierRows = 0;
272
278
  let tierNamed = false;
@@ -295,18 +301,28 @@ export function openaiUsageRecords(
295
301
  if (result === null) continue;
296
302
  requests += count(result.num_model_requests);
297
303
 
304
+ /*
305
+ A batch job is billed at the row's batch ratio, half the standard rate,
306
+ which the pricing page publishes as its own table and `price-drift.mjs`
307
+ checks weekly. A batch row on a model with no batch rate is left out.
308
+ */
309
+ let batchRow = false;
298
310
  if (typeof result.batch === 'boolean') {
299
311
  batchNamed = true;
300
312
  if (result.batch) {
301
- batch += 1;
302
- continue;
313
+ const row = typeof result.model === 'string' ? catalogue.byId.get(result.model) : undefined;
314
+ if (row === undefined || multipliersFor(row).batch === null) {
315
+ batch += 1;
316
+ continue;
317
+ }
318
+ batchRow = true;
303
319
  }
304
320
  }
305
321
 
306
322
  let serviceTier: string | undefined;
307
323
  if (typeof result.service_tier === 'string') {
308
324
  tierNamed = true;
309
- if (result.service_tier !== 'default') {
325
+ if (result.service_tier !== 'default' && !(batchRow && result.service_tier === 'batch')) {
310
326
  /*
311
327
  Priced only from a table that exists for this model at this tier.
312
328
  The response path falls back to the standard rate for a tier with
@@ -390,6 +406,7 @@ export function openaiUsageRecords(
390
406
  ...(label === undefined ? {} : { label }),
391
407
  ...requestsField(result.num_model_requests),
392
408
  ...(serviceTier === undefined ? {} : { service_tier: serviceTier }),
409
+ ...(batchRow ? { batch: true as const } : {}),
393
410
  usage: {
394
411
  prompt_tokens: prompt,
395
412
  completion_tokens: completion,
@@ -404,6 +421,7 @@ export function openaiUsageRecords(
404
421
  },
405
422
  });
406
423
  rows += 1;
424
+ if (batchRow) batchRows += 1;
407
425
  if (serviceTier !== undefined) {
408
426
  serviceTierRows += 1;
409
427
  tiersPriced.add(serviceTier);
@@ -419,6 +437,7 @@ export function openaiUsageRecords(
419
437
  unnamedModel,
420
438
  batch,
421
439
  batchNamed,
440
+ batchRows,
422
441
  nonDefaultTier,
423
442
  tiersRefused: [...tiersRefused],
424
443
  serviceTierRows,
@@ -36,6 +36,7 @@ export const PRICING_MODEL_KEYS = [
36
36
  'promo',
37
37
  'multipliers',
38
38
  'inferenceGeoMultipliers',
39
+ 'webSearchPerThousandUsd',
39
40
  ] as const;
40
41
 
41
42
  /** The geographies a provider prices differently, as `InferenceGeo` names them. */
@@ -281,6 +282,20 @@ function parseModel(
281
282
  model.inferenceGeoMultipliers = {};
282
283
  if (geo.us !== undefined) model.inferenceGeoMultipliers.us = positiveNumber(geo.us, `${label}.us`, source);
283
284
  }
285
+ /*
286
+ * A per-search fee, in dollars per 1,000 searches, as the catalogue carries
287
+ * it on every Claude row. Accepted for the same two reasons: a copied row
288
+ * stays valid, and a contract price for searches can be stated. Zero and
289
+ * negatives are refused like every other rate; a row with no searches to
290
+ * price simply leaves the key out.
291
+ */
292
+ if (raw.webSearchPerThousandUsd !== undefined) {
293
+ model.webSearchPerThousandUsd = positiveNumber(
294
+ raw.webSearchPerThousandUsd,
295
+ `models.${id}.webSearchPerThousandUsd`,
296
+ source,
297
+ );
298
+ }
284
299
 
285
300
  return model;
286
301
  }