@trazum/core 2.7.1 → 2.7.3

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 (58) hide show
  1. package/dist/anthropic-usage.d.ts +58 -0
  2. package/dist/anthropic-usage.d.ts.map +1 -1
  3. package/dist/anthropic-usage.js +91 -0
  4. package/dist/anthropic-usage.js.map +1 -1
  5. package/dist/billing.d.ts.map +1 -1
  6. package/dist/billing.js +6 -2
  7. package/dist/billing.js.map +1 -1
  8. package/dist/claude-code.d.ts +2 -0
  9. package/dist/claude-code.d.ts.map +1 -1
  10. package/dist/claude-code.js +9 -0
  11. package/dist/claude-code.js.map +1 -1
  12. package/dist/connector.d.ts +3 -1
  13. package/dist/connector.d.ts.map +1 -1
  14. package/dist/connector.js +64 -5
  15. package/dist/connector.js.map +1 -1
  16. package/dist/gateway.d.ts +18 -0
  17. package/dist/gateway.d.ts.map +1 -1
  18. package/dist/gateway.js +20 -1
  19. package/dist/gateway.js.map +1 -1
  20. package/dist/index.d.ts +1 -1
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +1 -1
  23. package/dist/index.js.map +1 -1
  24. package/dist/openai-usage.d.ts +38 -6
  25. package/dist/openai-usage.d.ts.map +1 -1
  26. package/dist/openai-usage.js +44 -8
  27. package/dist/openai-usage.js.map +1 -1
  28. package/dist/pricing-overlay.d.ts +1 -1
  29. package/dist/pricing-overlay.d.ts.map +1 -1
  30. package/dist/pricing-overlay.js +21 -0
  31. package/dist/pricing-overlay.js.map +1 -1
  32. package/dist/pricing.d.ts +37 -1
  33. package/dist/pricing.d.ts.map +1 -1
  34. package/dist/pricing.js +528 -9
  35. package/dist/pricing.js.map +1 -1
  36. package/dist/store.d.ts +4 -0
  37. package/dist/store.d.ts.map +1 -1
  38. package/dist/store.js +22 -1
  39. package/dist/store.js.map +1 -1
  40. package/dist/types.d.ts +54 -0
  41. package/dist/types.d.ts.map +1 -1
  42. package/dist/usage.d.ts +16 -0
  43. package/dist/usage.d.ts.map +1 -1
  44. package/dist/usage.js +17 -0
  45. package/dist/usage.js.map +1 -1
  46. package/package.json +1 -1
  47. package/src/anthropic-usage.ts +134 -1
  48. package/src/billing.ts +6 -2
  49. package/src/claude-code.ts +11 -0
  50. package/src/connector.ts +69 -6
  51. package/src/gateway.ts +35 -2
  52. package/src/index.ts +2 -0
  53. package/src/openai-usage.ts +72 -10
  54. package/src/pricing-overlay.ts +21 -0
  55. package/src/pricing.ts +552 -10
  56. package/src/store.ts +24 -3
  57. package/src/types.ts +57 -0
  58. package/src/usage.ts +33 -0
package/src/index.ts CHANGED
@@ -443,6 +443,8 @@ export {
443
443
  cheapestOfTierIn,
444
444
  multipliersFor,
445
445
  isOffered,
446
+ serviceTierOf,
447
+ inferenceGeoOf,
446
448
  } from './pricing.js';
447
449
  export type { PricingCatalogue, PromptSize } from './pricing.js';
448
450
  export type { CachingMode, Capability, CostMultipliers } from './types.js';
@@ -51,11 +51,21 @@
51
51
  * conversion says the question was not asked, because the alternative is a
52
52
  * reader assuming it was answered.
53
53
  *
54
- * **A service tier that is not `default`.** The schema does not enumerate
55
- * the values, so this does not either: any string but `default` is left out,
56
- * counted, and *named* in the conversion, so the operator sees `flex` or
57
- * `priority` or whatever the report said rather than a number. Refusing
58
- * by name is the honest version of not knowing the list.
54
+ * **A service tier with no table of its own.** OpenAI sells Flex, Fast
55
+ * (Priority until 2026-07-30) and Ultrafast from tables of their own, and the
56
+ * catalogue writes each model's row of each one out in `serviceTiers`. A row
57
+ * at one of those tiers, on a model whose catalogue row has that tier's table,
58
+ * is carried into the record as `service_tier` and priced from that table,
59
+ * exactly as a logged response naming the same tier is: the published rate
60
+ * for the tier that ran, not a multiple of another.
61
+ *
62
+ * Anything else is still left out, counted, and *named*: a tier the
63
+ * catalogue has no table for on that model (GPT-4o has no Flex), and any
64
+ * value `serviceTierOf` does not know, because the schema does not enumerate
65
+ * the tiers and so this does not either. Pricing such a row at the standard
66
+ * rate would put a Flex bill at twice its cost and look right doing it; the
67
+ * operator sees `flex` or `scale` or whatever the report said, never a
68
+ * number standing in for it.
59
69
  *
60
70
  * **Audio and image tokens.** `input_tokens` folds text, audio and image
61
71
  * tokens together, and a text rate is not the rate for the other two. A row
@@ -84,6 +94,9 @@
84
94
  * absence and no second reading to derive.
85
95
  */
86
96
 
97
+ import { BUNDLED_CATALOGUE, serviceTierOf } from './pricing.js';
98
+ import type { PricingCatalogue } from './pricing.js';
99
+
87
100
  /** One project, and the label the operator gives it. Matched exactly. */
88
101
  export interface ProjectLabel {
89
102
  project: string;
@@ -101,6 +114,13 @@ export interface OpenaiUsageRecord {
101
114
  * request, which is then a call like any other. See `UsageRecord.requests`.
102
115
  */
103
116
  requests?: number | null;
117
+ /**
118
+ * The report's own tier, as it spelled it, when it is one the catalogue
119
+ * prices this model from: `parseUsageLine` reads it as a response's
120
+ * `service_tier` and the call is priced from that tier's table. Absent for
121
+ * the default tier.
122
+ */
123
+ service_tier?: string;
104
124
  usage: {
105
125
  prompt_tokens: number;
106
126
  completion_tokens: number;
@@ -127,10 +147,18 @@ export interface OpenaiUsageConversion {
127
147
  batch: number;
128
148
  /** Whether any result said whether it was batch at all. */
129
149
  batchNamed: boolean;
130
- /** Results at a service tier other than `default`, left out. */
150
+ /**
151
+ * Results at a service tier other than `default` that no table prices, left
152
+ * out: a tier the model's catalogue row has no table for, or a value
153
+ * `serviceTierOf` does not know.
154
+ */
131
155
  nonDefaultTier: number;
132
156
  /** The tiers those results named, so the refusal has a name. */
133
157
  tiersRefused: string[];
158
+ /** Results at Flex, Fast or Ultrafast that became records priced from that tier's own table. */
159
+ serviceTierRows: number;
160
+ /** The tiers those results named, as the report spelled them. */
161
+ tiersPriced: string[];
134
162
  /** Whether any result named a tier at all. */
135
163
  tierNamed: boolean;
136
164
  /** Results that carried audio or image tokens and were reduced to text. */
@@ -183,7 +211,16 @@ const NON_TEXT = [
183
211
  */
184
212
  export function openaiUsageRecords(
185
213
  text: string,
186
- options: { label?: string; labelByProject?: readonly ProjectLabel[] } = {},
214
+ options: {
215
+ label?: string;
216
+ labelByProject?: readonly ProjectLabel[];
217
+ /**
218
+ * Where a tier's table is looked for. The bundled catalogue unless a
219
+ * caller prices with another; a row whose model this catalogue has no
220
+ * table for at that tier is refused, never priced at the standard rate.
221
+ */
222
+ catalogue?: PricingCatalogue;
223
+ } = {},
187
224
  ): OpenaiUsageConversion {
188
225
  const empty: OpenaiUsageConversion = {
189
226
  records: [],
@@ -195,6 +232,8 @@ export function openaiUsageRecords(
195
232
  batchNamed: false,
196
233
  nonDefaultTier: 0,
197
234
  tiersRefused: [],
235
+ serviceTierRows: 0,
236
+ tiersPriced: [],
198
237
  tierNamed: false,
199
238
  mixedRows: 0,
200
239
  nonTextTokens: 0,
@@ -219,7 +258,9 @@ export function openaiUsageRecords(
219
258
 
220
259
  const rules = options.labelByProject;
221
260
  const records: OpenaiUsageRecord[] = [];
261
+ const catalogue = options.catalogue ?? BUNDLED_CATALOGUE;
222
262
  const tiersRefused = new Set<string>();
263
+ const tiersPriced = new Set<string>();
223
264
  let buckets = 0;
224
265
  let rows = 0;
225
266
  let requests = 0;
@@ -227,6 +268,7 @@ export function openaiUsageRecords(
227
268
  let batch = 0;
228
269
  let batchNamed = false;
229
270
  let nonDefaultTier = 0;
271
+ let serviceTierRows = 0;
230
272
  let tierNamed = false;
231
273
  let mixedRows = 0;
232
274
  let nonTextTokens = 0;
@@ -261,12 +303,25 @@ export function openaiUsageRecords(
261
303
  }
262
304
  }
263
305
 
306
+ let serviceTier: string | undefined;
264
307
  if (typeof result.service_tier === 'string') {
265
308
  tierNamed = true;
266
309
  if (result.service_tier !== 'default') {
267
- nonDefaultTier += 1;
268
- tiersRefused.add(result.service_tier);
269
- continue;
310
+ /*
311
+ Priced only from a table that exists for this model at this tier.
312
+ The response path falls back to the standard rate for a tier with
313
+ no table, because a response is one call the provider really ran;
314
+ a report row is the sum of many, and a Flex row priced at the
315
+ standard rate is twice its cost.
316
+ */
317
+ const named = serviceTierOf(result.service_tier);
318
+ const row = typeof result.model === 'string' ? catalogue.byId.get(result.model) : undefined;
319
+ if (named === undefined || row?.serviceTiers?.[named] === undefined) {
320
+ nonDefaultTier += 1;
321
+ tiersRefused.add(result.service_tier);
322
+ continue;
323
+ }
324
+ serviceTier = result.service_tier;
270
325
  }
271
326
  }
272
327
 
@@ -334,6 +389,7 @@ export function openaiUsageRecords(
334
389
  ts,
335
390
  ...(label === undefined ? {} : { label }),
336
391
  ...requestsField(result.num_model_requests),
392
+ ...(serviceTier === undefined ? {} : { service_tier: serviceTier }),
337
393
  usage: {
338
394
  prompt_tokens: prompt,
339
395
  completion_tokens: completion,
@@ -348,6 +404,10 @@ export function openaiUsageRecords(
348
404
  },
349
405
  });
350
406
  rows += 1;
407
+ if (serviceTier !== undefined) {
408
+ serviceTierRows += 1;
409
+ tiersPriced.add(serviceTier);
410
+ }
351
411
  }
352
412
  }
353
413
 
@@ -361,6 +421,8 @@ export function openaiUsageRecords(
361
421
  batchNamed,
362
422
  nonDefaultTier,
363
423
  tiersRefused: [...tiersRefused],
424
+ serviceTierRows,
425
+ tiersPriced: [...tiersPriced],
364
426
  tierNamed,
365
427
  mixedRows,
366
428
  nonTextTokens,
@@ -35,8 +35,12 @@ export const PRICING_MODEL_KEYS = [
35
35
  'notes',
36
36
  'promo',
37
37
  'multipliers',
38
+ 'inferenceGeoMultipliers',
38
39
  ] as const;
39
40
 
41
+ /** The geographies a provider prices differently, as `InferenceGeo` names them. */
42
+ const INFERENCE_GEO_KEYS = ['us'] as const;
43
+
40
44
  const PROMO_KEYS = ['inputPerMTok', 'outputPerMTok', 'until'] as const;
41
45
 
42
46
  /**
@@ -260,6 +264,23 @@ function parseModel(
260
264
  if (raw.multipliers !== undefined) {
261
265
  model.multipliers = parseMultipliers(raw.multipliers, `models.${id}.multipliers`, source);
262
266
  }
267
+ /*
268
+ * The multiplier a provider publishes for one geography, as the catalogue
269
+ * carries it on Claude 4.6 and later. Accepted so a catalogue row copied into
270
+ * an overlay is still a valid overlay row, and so a model added there can say
271
+ * what US-only inference costs on it. Zero and negatives are refused, as for
272
+ * every other rate here.
273
+ */
274
+ if (raw.inferenceGeoMultipliers !== undefined) {
275
+ const label = `models.${id}.inferenceGeoMultipliers`;
276
+ const geo = raw.inferenceGeoMultipliers;
277
+ if (!isPlainObject(geo)) {
278
+ throw new PricingOverlayError(`"${label}" must be an object such as {"us": 1.1}`, source);
279
+ }
280
+ rejectUnknownKeys(geo, INFERENCE_GEO_KEYS, source, `${label}.`);
281
+ model.inferenceGeoMultipliers = {};
282
+ if (geo.us !== undefined) model.inferenceGeoMultipliers.us = positiveNumber(geo.us, `${label}.us`, source);
283
+ }
263
284
 
264
285
  return model;
265
286
  }