@trazum/core 2.7.2 → 2.7.4

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 (63) hide show
  1. package/dist/anthropic-usage.d.ts +66 -1
  2. package/dist/anthropic-usage.d.ts.map +1 -1
  3. package/dist/anthropic-usage.js +114 -3
  4. package/dist/anthropic-usage.js.map +1 -1
  5. package/dist/batch-results.d.ts +77 -0
  6. package/dist/batch-results.d.ts.map +1 -0
  7. package/dist/batch-results.js +145 -0
  8. package/dist/batch-results.js.map +1 -0
  9. package/dist/billing.d.ts.map +1 -1
  10. package/dist/billing.js +2 -0
  11. package/dist/billing.js.map +1 -1
  12. package/dist/claude-code.d.ts +2 -0
  13. package/dist/claude-code.d.ts.map +1 -1
  14. package/dist/claude-code.js +9 -0
  15. package/dist/claude-code.js.map +1 -1
  16. package/dist/connector.d.ts +3 -1
  17. package/dist/connector.d.ts.map +1 -1
  18. package/dist/connector.js +72 -5
  19. package/dist/connector.js.map +1 -1
  20. package/dist/gateway.d.ts +10 -0
  21. package/dist/gateway.d.ts.map +1 -1
  22. package/dist/gateway.js +7 -1
  23. package/dist/gateway.js.map +1 -1
  24. package/dist/index.d.ts +3 -1
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +2 -1
  27. package/dist/index.js.map +1 -1
  28. package/dist/openai-usage.d.ts +43 -7
  29. package/dist/openai-usage.d.ts.map +1 -1
  30. package/dist/openai-usage.js +63 -11
  31. package/dist/openai-usage.js.map +1 -1
  32. package/dist/pricing-overlay.d.ts +1 -1
  33. package/dist/pricing-overlay.d.ts.map +1 -1
  34. package/dist/pricing-overlay.js +21 -0
  35. package/dist/pricing-overlay.js.map +1 -1
  36. package/dist/pricing.d.ts +24 -1
  37. package/dist/pricing.d.ts.map +1 -1
  38. package/dist/pricing.js +87 -0
  39. package/dist/pricing.js.map +1 -1
  40. package/dist/store.d.ts +4 -0
  41. package/dist/store.d.ts.map +1 -1
  42. package/dist/store.js +22 -1
  43. package/dist/store.js.map +1 -1
  44. package/dist/types.d.ts +17 -0
  45. package/dist/types.d.ts.map +1 -1
  46. package/dist/usage.d.ts +16 -0
  47. package/dist/usage.d.ts.map +1 -1
  48. package/dist/usage.js +16 -0
  49. package/dist/usage.js.map +1 -1
  50. package/package.json +1 -1
  51. package/src/anthropic-usage.ts +163 -5
  52. package/src/batch-results.ts +174 -0
  53. package/src/billing.ts +2 -0
  54. package/src/claude-code.ts +11 -0
  55. package/src/connector.ts +76 -6
  56. package/src/gateway.ts +16 -2
  57. package/src/index.ts +3 -0
  58. package/src/openai-usage.ts +95 -14
  59. package/src/pricing-overlay.ts +21 -0
  60. package/src/pricing.ts +105 -1
  61. package/src/store.ts +24 -3
  62. package/src/types.ts +18 -0
  63. package/src/usage.ts +32 -0
package/src/pricing.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { ModelPricing, PricingTier, ServiceTier, TierCondition } from './types.js';
1
+ import type { InferenceGeo, ModelPricing, PricingTier, ServiceTier, TierCondition } from './types.js';
2
2
 
3
3
  /**
4
4
  * Model and pricing catalogue (USD per million tokens).
@@ -310,6 +310,19 @@ const MISTRAL_CACHE_NOTE =
310
310
  'Cached input costs 10% of the input price (mistral.ai/cloud/inference, read 2026-10-11). How caching starts and'
311
311
  + ' its minimum are not published anywhere Trazum can read, so no caching advice is given for it.';
312
312
 
313
+ /**
314
+ * US-only inference on Claude 4.6 and later.
315
+ *
316
+ * platform.claude.com/docs/en/about-claude/pricing, "Data residency pricing",
317
+ * read 2026-11-05: "For Claude 4.6 and later models, specifying US-only
318
+ * inference through the `inference_geo` parameter incurs a 1.1x multiplier on
319
+ * all token pricing categories, including input tokens, output tokens, cache
320
+ * writes, and cache reads." It stacks with fast mode and the Batch API
321
+ * discount, the same page says. Earlier models reject the parameter, so
322
+ * they carry no multiplier. `price-drift.mjs` reads the sentence weekly.
323
+ */
324
+ const US_ONLY_INFERENCE = Object.freeze({ us: 1.1 });
325
+
313
326
  export const MODELS: ModelPricing[] = [
314
327
  {
315
328
  /*
@@ -321,6 +334,7 @@ export const MODELS: ModelPricing[] = [
321
334
  */
322
335
  id: 'claude-fable-5-1',
323
336
  provider: 'anthropic',
337
+ inferenceGeoMultipliers: US_ONLY_INFERENCE,
324
338
  displayName: 'Claude Fable 5.1',
325
339
  inputPerMTok: 10,
326
340
  outputPerMTok: 50,
@@ -339,6 +353,7 @@ export const MODELS: ModelPricing[] = [
339
353
  */
340
354
  id: 'claude-mythos-5-1',
341
355
  provider: 'anthropic',
356
+ inferenceGeoMultipliers: US_ONLY_INFERENCE,
342
357
  displayName: 'Claude Mythos 5.1',
343
358
  recommendable: false,
344
359
  inputPerMTok: 10,
@@ -355,6 +370,7 @@ export const MODELS: ModelPricing[] = [
355
370
  {
356
371
  id: 'claude-fable-5',
357
372
  provider: 'anthropic',
373
+ inferenceGeoMultipliers: US_ONLY_INFERENCE,
358
374
  displayName: 'Claude Fable 5',
359
375
  inputPerMTok: 10,
360
376
  outputPerMTok: 50,
@@ -367,6 +383,7 @@ export const MODELS: ModelPricing[] = [
367
383
  {
368
384
  id: 'claude-mythos-5',
369
385
  provider: 'anthropic',
386
+ inferenceGeoMultipliers: US_ONLY_INFERENCE,
370
387
  displayName: 'Claude Mythos 5',
371
388
  recommendable: false,
372
389
  inputPerMTok: 10,
@@ -388,6 +405,7 @@ export const MODELS: ModelPricing[] = [
388
405
  */
389
406
  id: 'claude-opus-5-5',
390
407
  provider: 'anthropic',
408
+ inferenceGeoMultipliers: US_ONLY_INFERENCE,
391
409
  displayName: 'Claude Opus 5.5',
392
410
  inputPerMTok: 4,
393
411
  outputPerMTok: 20,
@@ -410,6 +428,7 @@ export const MODELS: ModelPricing[] = [
410
428
  {
411
429
  id: 'claude-opus-5',
412
430
  provider: 'anthropic',
431
+ inferenceGeoMultipliers: US_ONLY_INFERENCE,
413
432
  displayName: 'Claude Opus 5',
414
433
  inputPerMTok: 5,
415
434
  outputPerMTok: 25,
@@ -424,6 +443,7 @@ export const MODELS: ModelPricing[] = [
424
443
  {
425
444
  id: 'claude-opus-4-8',
426
445
  provider: 'anthropic',
446
+ inferenceGeoMultipliers: US_ONLY_INFERENCE,
427
447
  displayName: 'Claude Opus 4.8',
428
448
  inputPerMTok: 5,
429
449
  outputPerMTok: 25,
@@ -442,6 +462,7 @@ export const MODELS: ModelPricing[] = [
442
462
  {
443
463
  id: 'claude-opus-4-7',
444
464
  provider: 'anthropic',
465
+ inferenceGeoMultipliers: US_ONLY_INFERENCE,
445
466
  displayName: 'Claude Opus 4.7',
446
467
  inputPerMTok: 5,
447
468
  outputPerMTok: 25,
@@ -453,6 +474,7 @@ export const MODELS: ModelPricing[] = [
453
474
  {
454
475
  id: 'claude-opus-4-6',
455
476
  provider: 'anthropic',
477
+ inferenceGeoMultipliers: US_ONLY_INFERENCE,
456
478
  displayName: 'Claude Opus 4.6',
457
479
  inputPerMTok: 5,
458
480
  outputPerMTok: 25,
@@ -537,6 +559,7 @@ export const MODELS: ModelPricing[] = [
537
559
  */
538
560
  id: 'claude-sonnet-5-5',
539
561
  provider: 'anthropic',
562
+ inferenceGeoMultipliers: US_ONLY_INFERENCE,
540
563
  displayName: 'Claude Sonnet 5.5',
541
564
  inputPerMTok: 2,
542
565
  outputPerMTok: 10,
@@ -550,6 +573,7 @@ export const MODELS: ModelPricing[] = [
550
573
  {
551
574
  id: 'claude-sonnet-5',
552
575
  provider: 'anthropic',
576
+ inferenceGeoMultipliers: US_ONLY_INFERENCE,
553
577
  displayName: 'Claude Sonnet 5',
554
578
  inputPerMTok: 2,
555
579
  outputPerMTok: 10,
@@ -564,6 +588,7 @@ export const MODELS: ModelPricing[] = [
564
588
  {
565
589
  id: 'claude-sonnet-4-6',
566
590
  provider: 'anthropic',
591
+ inferenceGeoMultipliers: US_ONLY_INFERENCE,
567
592
  displayName: 'Claude Sonnet 4.6',
568
593
  inputPerMTok: 3,
569
594
  outputPerMTok: 15,
@@ -638,6 +663,7 @@ export const MODELS: ModelPricing[] = [
638
663
  */
639
664
  id: 'claude-haiku-5-5',
640
665
  provider: 'anthropic',
666
+ inferenceGeoMultipliers: US_ONLY_INFERENCE,
641
667
  displayName: 'Claude Haiku 5.5',
642
668
  inputPerMTok: 0.1,
643
669
  outputPerMTok: 0.5,
@@ -2439,6 +2465,10 @@ interface Rates {
2439
2465
  } | null;
2440
2466
  /** The service tier whose table priced the call, when it was not the standard one. */
2441
2467
  serviceTier?: ServiceTier;
2468
+ /** The geography whose published multiplier priced the call, when it was not global. */
2469
+ inferenceGeo?: InferenceGeo;
2470
+ /** True when the call was billed at the row's batch ratio. */
2471
+ batch?: true;
2442
2472
  }
2443
2473
 
2444
2474
  /**
@@ -2467,6 +2497,52 @@ export interface PromptSize {
2467
2497
  * `serviceTierOf`; anything else prices at the standard rate.
2468
2498
  */
2469
2499
  serviceTier?: string;
2500
+ /**
2501
+ * The response's `usage.inference_geo`, when the log kept it: Anthropic's
2502
+ * `"us"`, `"global"` or `"not_available"`. Normalised by `inferenceGeoOf`;
2503
+ * only a geography the row publishes a multiplier for changes the price.
2504
+ */
2505
+ inferenceGeo?: string;
2506
+ /**
2507
+ * The call ran on the provider's Batch API: Anthropic's `usage.service_tier`
2508
+ * `"batch"`, a batch result file's line, or a usage report's batch row.
2509
+ * Billed at the row's batch ratio.
2510
+ */
2511
+ batch?: boolean;
2512
+ }
2513
+
2514
+ /**
2515
+ * A response's `usage.inference_geo` as the geography that prices it, or
2516
+ * `undefined` for the global rate. `"global"` is the default and the row's
2517
+ * own price; `"not_available"` is what a model that cannot pin a geography
2518
+ * reports, and is priced the same.
2519
+ */
2520
+ export function inferenceGeoOf(value: unknown): InferenceGeo | undefined {
2521
+ if (typeof value !== 'string') return undefined;
2522
+ return value.trim().toLowerCase() === 'us' ? 'us' : undefined;
2523
+ }
2524
+
2525
+ /** Rounded to a billionth of a dollar per million tokens, so 1.1x of $5 is $5.5 and not $5.500000000000001. */
2526
+ const scaled = (rate: number, factor: number): number => Math.round(rate * factor * 1e9) / 1e9;
2527
+
2528
+ /** Every rate a call is billed at, multiplied by one published factor: the floor of an undecided band too. */
2529
+ function scaleRates(rates: Rates, factor: number): Rates {
2530
+ return {
2531
+ ...rates,
2532
+ inputPerMTok: scaled(rates.inputPerMTok, factor),
2533
+ outputPerMTok: scaled(rates.outputPerMTok, factor),
2534
+ ...(rates.tier?.floor !== undefined
2535
+ ? {
2536
+ tier: {
2537
+ ...rates.tier,
2538
+ floor: {
2539
+ inputPerMTok: scaled(rates.tier.floor.inputPerMTok, factor),
2540
+ outputPerMTok: scaled(rates.tier.floor.outputPerMTok, factor),
2541
+ },
2542
+ },
2543
+ }
2544
+ : {}),
2545
+ };
2470
2546
  }
2471
2547
 
2472
2548
  /**
@@ -2629,6 +2705,34 @@ export function effectivePricing(
2629
2705
  on: Date = new Date(),
2630
2706
  context: PromptSize = {},
2631
2707
  ): Rates {
2708
+ /*
2709
+ * A geography with a published multiplier scales whatever the rest of the
2710
+ * call decides, fast mode and the long band included, because the page says
2711
+ * it stacks with all of them. Applied once, outside everything else.
2712
+ */
2713
+ if (context.inferenceGeo !== undefined) {
2714
+ const { inferenceGeo, ...rest } = context;
2715
+ const rates = effectivePricing(model, on, rest);
2716
+ const geo = inferenceGeoOf(inferenceGeo);
2717
+ const factor = geo === undefined ? undefined : model.inferenceGeoMultipliers?.[geo];
2718
+ if (geo === undefined || factor === undefined) return rates;
2719
+ return { ...scaleRates(rates, factor), inferenceGeo: geo };
2720
+ }
2721
+ /*
2722
+ * A call the provider ran on its Batch API is billed at the row's batch
2723
+ * ratio, on input and output alike, and the cache multipliers apply on top
2724
+ * of the batch input rate, as both providers' pages say. The ratio is the
2725
+ * one the batch advisory quotes and `price-drift.mjs` checks against each
2726
+ * batch table every week. A row with no batch API (`batch: null`) cannot
2727
+ * have run one, so such a call keeps the standard price: a ceiling.
2728
+ */
2729
+ if (context.batch === true) {
2730
+ const { batch: _batch, ...rest } = context;
2731
+ const rates = effectivePricing(model, on, rest);
2732
+ const ratio = multipliersFor(model, on).batch;
2733
+ if (ratio === null) return rates;
2734
+ return { ...scaleRates(rates, ratio), batch: true };
2735
+ }
2632
2736
  /* A call before a published price change is priced as it was billed then. */
2633
2737
  if (model.history !== undefined) {
2634
2738
  const period = pricedAt(model, on);
package/src/store.ts CHANGED
@@ -17,6 +17,10 @@
17
17
  * the later pull wins, because a window pulled again is at worst as complete
18
18
  * as it was. That makes overlapping pulls idempotent, which is what lets a
19
19
  * scheduled job run every hour over a rolling day without inventing money.
20
+ * And because one pull carries every grouping of its window at once, the
21
+ * newest pull of a window and model replaces all older records of it, even
22
+ * ones grouped differently: a finer grouping restates a day, it does not add
23
+ * one beside it.
20
24
  *
21
25
  * **Deduplication that cannot lie.** Two records the store cannot tell apart —
22
26
  * a source that served no window, or no model — are kept as *two* and reported
@@ -130,10 +134,27 @@ export function resolveStore(records: readonly StoreRecord[]): ResolvedStore {
130
134
  }
131
135
  }
132
136
 
137
+ /*
138
+ * One pull is the whole statement of its window and model: every grouping
139
+ * the provider served for that window arrives in it together. So the newest
140
+ * pull of a window replaces every older record of that window and model,
141
+ * whatever grouping the older one carried. Without this, a store written
142
+ * before `connect` asked for the tier, speed and geography groupings (2.7.3)
143
+ * would keep its old summed row beside the new split ones, and count the
144
+ * same day twice.
145
+ */
146
+ const slotOf = (record: StoreRecord): string =>
147
+ [record.provider, record.fromMs, record.toMs, record.model].join('\n');
148
+ const newest = new Map<string, number>();
149
+ for (const record of byIdentity.values()) {
150
+ const slot = slotOf(record);
151
+ newest.set(slot, Math.max(newest.get(slot) ?? Number.NEGATIVE_INFINITY, record.pulledAtMs));
152
+ }
153
+
133
154
  return {
134
- records: [...byIdentity.values()].sort(
135
- (a, b) => a.fromMs - b.fromMs || a.model.localeCompare(b.model),
136
- ),
155
+ records: [...byIdentity.values()]
156
+ .filter((record) => record.pulledAtMs === newest.get(slotOf(record)))
157
+ .sort((a, b) => a.fromMs - b.fromMs || a.model.localeCompare(b.model)),
137
158
  possiblyDouble,
138
159
  unknownVersion,
139
160
  };
package/src/types.ts CHANGED
@@ -230,6 +230,9 @@ export interface PricingTier {
230
230
  */
231
231
  export type ServiceTier = 'flex' | 'fast' | 'ultrafast';
232
232
 
233
+ /** A geography a provider prices inference in differently. Anthropic's `inference_geo: "us"`. */
234
+ export type InferenceGeo = 'us';
235
+
233
236
  /** The rates one service tier charges, with its own conditional bands. */
234
237
  export interface ServiceTierRates {
235
238
  inputPerMTok: number;
@@ -396,6 +399,21 @@ export interface ModelPricing {
396
399
  * standard rate: the page publishes nothing else for it.
397
400
  */
398
401
  serviceTiers?: Partial<Record<ServiceTier, ServiceTierRates>>;
402
+ /**
403
+ * What running the call in one geography multiplies every rate by.
404
+ *
405
+ * Anthropic bills US-only inference (`inference_geo: "us"`) at 1.1x "on
406
+ * all token pricing categories, including input tokens, output tokens,
407
+ * cache writes, and cache reads" for Claude 4.6 and later, and says the
408
+ * multiplier stacks with fast mode, the Batch API discount and caching.
409
+ * Global routing, the default, is the row's own price, and earlier models
410
+ * reject the parameter. The response names the geography that ran in
411
+ * `usage.inference_geo`, so a call is decided by its own log line.
412
+ *
413
+ * A published multiplier, applied as the page states it, never one derived
414
+ * here: a row without the field prices every geography at its own rate.
415
+ */
416
+ inferenceGeoMultipliers?: Partial<Record<InferenceGeo, number>>;
399
417
  /**
400
418
  * Other ids that bill at exactly this rate.
401
419
  *
package/src/usage.ts CHANGED
@@ -193,6 +193,22 @@ export interface UsageRecord {
193
193
  * catalogue). Absent, or `"default"`, is the standard rate.
194
194
  */
195
195
  serviceTier?: string;
196
+ /**
197
+ * The geography the provider says the call ran in, when the log kept it:
198
+ * Anthropic's `usage.inference_geo`, `"us"`, `"global"` or
199
+ * `"not_available"`. US-only inference bills 1.1x on Claude 4.6 and later
200
+ * (`inferenceGeoMultipliers` in the catalogue). Absent, global and
201
+ * not-available are the row's own price.
202
+ */
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;
196
212
  /**
197
213
  * Set when an OpenAI-shaped line split its cache writes by lifetime and the
198
214
  * parts did not add up to the total. The lifetime is then unknown even on a
@@ -983,6 +999,22 @@ export function parseUsageLine(line: string): UsageRecord | null {
983
999
  ...(typeof record.service_tier === 'string' && record.service_tier !== ''
984
1000
  ? { serviceTier: record.service_tier }
985
1001
  : {}),
1002
+ /*
1003
+ * Anthropic's `usage.inference_geo`, which every Messages response and
1004
+ * every Claude Code log line carries: "us" bills 1.1x on Claude 4.6 and
1005
+ * later, "global" and "not_available" do not.
1006
+ */
1007
+ ...(typeof usage.inference_geo === 'string' && usage.inference_geo !== ''
1008
+ ? { inferenceGeo: usage.inference_geo }
1009
+ : {}),
1010
+ /*
1011
+ * The Batch API, billed at half the standard rate by both providers.
1012
+ * Anthropic names it in `usage.service_tier` ("standard", "priority" or
1013
+ * "batch"); a converted usage report row says `batch: true`. OpenAI's
1014
+ * response-level `service_tier` never says "batch", so the two readings
1015
+ * cannot collide on a flat record.
1016
+ */
1017
+ ...(record.batch === true || usage.service_tier === 'batch' ? { batch: true as const } : {}),
986
1018
  ...(openAiSplitGiven && !openAiSplitAgrees ? { writeSplitContradicted: true as const } : {}),
987
1019
  };
988
1020
  }