@trazum/core 2.7.2 → 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.
- package/dist/anthropic-usage.d.ts +58 -0
- package/dist/anthropic-usage.d.ts.map +1 -1
- package/dist/anthropic-usage.js +91 -0
- package/dist/anthropic-usage.js.map +1 -1
- package/dist/billing.d.ts.map +1 -1
- package/dist/billing.js +1 -0
- package/dist/billing.js.map +1 -1
- package/dist/claude-code.d.ts +2 -0
- package/dist/claude-code.d.ts.map +1 -1
- package/dist/claude-code.js +9 -0
- package/dist/claude-code.js.map +1 -1
- package/dist/connector.d.ts +3 -1
- package/dist/connector.d.ts.map +1 -1
- package/dist/connector.js +64 -5
- package/dist/connector.js.map +1 -1
- package/dist/gateway.d.ts +10 -0
- package/dist/gateway.d.ts.map +1 -1
- package/dist/gateway.js +7 -1
- package/dist/gateway.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/openai-usage.d.ts +38 -6
- package/dist/openai-usage.d.ts.map +1 -1
- package/dist/openai-usage.js +44 -8
- package/dist/openai-usage.js.map +1 -1
- package/dist/pricing-overlay.d.ts +1 -1
- package/dist/pricing-overlay.d.ts.map +1 -1
- package/dist/pricing-overlay.js +21 -0
- package/dist/pricing-overlay.js.map +1 -1
- package/dist/pricing.d.ts +16 -1
- package/dist/pricing.d.ts.map +1 -1
- package/dist/pricing.js +68 -0
- package/dist/pricing.js.map +1 -1
- package/dist/store.d.ts +4 -0
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +22 -1
- package/dist/store.js.map +1 -1
- package/dist/types.d.ts +17 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/usage.d.ts +8 -0
- package/dist/usage.d.ts.map +1 -1
- package/dist/usage.js +8 -0
- package/dist/usage.js.map +1 -1
- package/package.json +1 -1
- package/src/anthropic-usage.ts +134 -1
- package/src/billing.ts +1 -0
- package/src/claude-code.ts +11 -0
- package/src/connector.ts +69 -6
- package/src/gateway.ts +16 -2
- package/src/index.ts +1 -0
- package/src/openai-usage.ts +72 -10
- package/src/pricing-overlay.ts +21 -0
- package/src/pricing.ts +78 -1
- package/src/store.ts +24 -3
- package/src/types.ts +18 -0
- package/src/usage.ts +16 -0
package/src/connector.ts
CHANGED
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
* recorded is not not-happened, at the level of the type system.
|
|
29
29
|
*/
|
|
30
30
|
|
|
31
|
-
import { effectivePricing, multipliersFor } from './pricing.js';
|
|
31
|
+
import { effectivePricing, multipliersFor, serviceTierOf } from './pricing.js';
|
|
32
32
|
import type { PricingCatalogue } from './pricing.js';
|
|
33
33
|
|
|
34
34
|
// --------------------------------------------------------------------------
|
|
@@ -196,7 +196,9 @@ export interface PullGap {
|
|
|
196
196
|
| 'cursor-expired'
|
|
197
197
|
| 'page-limit'
|
|
198
198
|
| 'unreadable-entry'
|
|
199
|
-
| 'unreadable-field'
|
|
199
|
+
| 'unreadable-field'
|
|
200
|
+
/** Usage at a tier no catalogue rate prices, left out and named. */
|
|
201
|
+
| 'unpriced-tier';
|
|
200
202
|
detail: string;
|
|
201
203
|
}
|
|
202
204
|
|
|
@@ -318,7 +320,7 @@ export function normalizeAnthropicUsage(payload: unknown): ConnectorPull {
|
|
|
318
320
|
cacheWrite1hTokens: ttlKnown ? write1h : 0,
|
|
319
321
|
writeTtlKnown: ttlKnown,
|
|
320
322
|
outputTokens: num(r.output_tokens) ?? 0,
|
|
321
|
-
group: groupOf(r, ['workspace_id', 'api_key_id', 'service_tier', 'context_window']),
|
|
323
|
+
group: groupOf(r, ['workspace_id', 'api_key_id', 'service_tier', 'context_window', 'speed', 'inference_geo']),
|
|
322
324
|
});
|
|
323
325
|
}
|
|
324
326
|
}
|
|
@@ -398,7 +400,7 @@ export function normalizeOpenAIUsage(payload: unknown): ConnectorPull {
|
|
|
398
400
|
// states (`cacheTtl`), and an absent field is not an assumed TTL.
|
|
399
401
|
writeTtlKnown: true,
|
|
400
402
|
outputTokens: num(r.output_tokens) ?? 0,
|
|
401
|
-
group: groupOf(r, ['project_id', 'api_key_id', 'batch']),
|
|
403
|
+
group: groupOf(r, ['project_id', 'api_key_id', 'batch', 'service_tier']),
|
|
402
404
|
});
|
|
403
405
|
}
|
|
404
406
|
}
|
|
@@ -508,6 +510,17 @@ export function bucketedProfile(
|
|
|
508
510
|
const slices = new Map<string, BucketedSlice>();
|
|
509
511
|
const days = new Map<string, { usd: number; calls: number | null }>();
|
|
510
512
|
const unpriced = new Map<string, { inputTokens: number; outputTokens: number }>();
|
|
513
|
+
/* Usage at a tier no rate here prices, summed per model and tier so the gap names both. */
|
|
514
|
+
const leftOut = new Map<string, { model: string; tier: string; buckets: number; tokens: number }>();
|
|
515
|
+
const leave = (bucket: UsageBucket, tier: string): void => {
|
|
516
|
+
const key = `${bucket.model}\n${tier}`;
|
|
517
|
+
const seen = leftOut.get(key) ?? { model: bucket.model, tier, buckets: 0, tokens: 0 };
|
|
518
|
+
seen.buckets += 1;
|
|
519
|
+
seen.tokens +=
|
|
520
|
+
bucket.inputTokens + bucket.cacheReadTokens + bucket.cacheWrite5mTokens + bucket.cacheWrite1hTokens
|
|
521
|
+
+ bucket.outputTokens;
|
|
522
|
+
leftOut.set(key, seen);
|
|
523
|
+
};
|
|
511
524
|
|
|
512
525
|
for (const bucket of pull.buckets) {
|
|
513
526
|
const model = catalogue.byId.get(bucket.model);
|
|
@@ -519,6 +532,46 @@ export function bucketedProfile(
|
|
|
519
532
|
continue;
|
|
520
533
|
}
|
|
521
534
|
|
|
535
|
+
/*
|
|
536
|
+
* What the row says about how its tokens ran, where that changes the
|
|
537
|
+
* price. A tier a catalogue rate is the rate for is priced from it; any
|
|
538
|
+
* other is left out and named, never priced at the standard rate. The
|
|
539
|
+
* groupings are the ones `trazum connect` asks for; a payload without
|
|
540
|
+
* them reads as standard, as it always did.
|
|
541
|
+
*/
|
|
542
|
+
const group = bucket.group;
|
|
543
|
+
const how: { speed?: string; serviceTier?: string; inferenceGeo?: string } = {};
|
|
544
|
+
if (pull.provider === 'anthropic') {
|
|
545
|
+
const tier = group.service_tier;
|
|
546
|
+
if (tier !== undefined && tier !== 'standard') {
|
|
547
|
+
leave(bucket, tier);
|
|
548
|
+
continue;
|
|
549
|
+
}
|
|
550
|
+
if (group.speed === 'fast') {
|
|
551
|
+
const fastRate = model.tiers?.some((t) => t.when.kind === 'speed' && t.when.speed === 'fast') ?? false;
|
|
552
|
+
if (!fastRate) {
|
|
553
|
+
leave(bucket, 'fast mode');
|
|
554
|
+
continue;
|
|
555
|
+
}
|
|
556
|
+
how.speed = 'fast';
|
|
557
|
+
}
|
|
558
|
+
if (group.inference_geo !== undefined) how.inferenceGeo = group.inference_geo;
|
|
559
|
+
} else if (pull.provider === 'openai') {
|
|
560
|
+
if (group.batch === 'true') {
|
|
561
|
+
leave(bucket, 'batch');
|
|
562
|
+
continue;
|
|
563
|
+
}
|
|
564
|
+
const tier = group.service_tier;
|
|
565
|
+
if (tier !== undefined && tier !== 'default') {
|
|
566
|
+
const named = serviceTierOf(tier);
|
|
567
|
+
if (named === undefined || model.serviceTiers?.[named] === undefined) {
|
|
568
|
+
leave(bucket, tier);
|
|
569
|
+
continue;
|
|
570
|
+
}
|
|
571
|
+
how.serviceTier = tier;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
522
575
|
const at = new Date(bucket.fromMs);
|
|
523
576
|
/*
|
|
524
577
|
* A bucket of exactly one request says that request's prompt; any other
|
|
@@ -529,7 +582,7 @@ export function bucketedProfile(
|
|
|
529
582
|
const priced = effectivePricing(
|
|
530
583
|
model,
|
|
531
584
|
at,
|
|
532
|
-
bucket.calls === 1 ? { inputTokens: prompt } : { inputTokensAtMost: prompt },
|
|
585
|
+
bucket.calls === 1 ? { inputTokens: prompt, ...how } : { inputTokensAtMost: prompt, ...how },
|
|
533
586
|
);
|
|
534
587
|
const { inputPerMTok, outputPerMTok } = priced;
|
|
535
588
|
const rates = multipliersFor(model, at);
|
|
@@ -623,7 +676,17 @@ export function bucketedProfile(
|
|
|
623
676
|
unpricedModels: [...unpriced.entries()]
|
|
624
677
|
.map(([model, tokens]) => ({ model, ...tokens }))
|
|
625
678
|
.sort((a, b) => b.inputTokens + b.outputTokens - (a.inputTokens + a.outputTokens)),
|
|
626
|
-
gaps:
|
|
679
|
+
gaps: [
|
|
680
|
+
...pull.gaps,
|
|
681
|
+
...[...leftOut.values()].map(
|
|
682
|
+
(left): PullGap => ({
|
|
683
|
+
kind: 'unpriced-tier',
|
|
684
|
+
detail:
|
|
685
|
+
`${left.buckets} row(s) of ${left.model} ran at ${left.tier}, which no catalogue rate here is the rate for, `
|
|
686
|
+
+ `and ${left.tokens.toLocaleString('en-US')} token(s) were left out rather than priced at the standard rate`,
|
|
687
|
+
}),
|
|
688
|
+
),
|
|
689
|
+
],
|
|
627
690
|
unavailable: pull.unavailable,
|
|
628
691
|
};
|
|
629
692
|
}
|
package/src/gateway.ts
CHANGED
|
@@ -101,6 +101,14 @@ export interface GatewayCall {
|
|
|
101
101
|
* `"fast"` at up to twice, `"ultrafast"`. A setting, not content.
|
|
102
102
|
*/
|
|
103
103
|
serviceTier?: string | null;
|
|
104
|
+
/**
|
|
105
|
+
* The geography the request pinned, when it named one: Anthropic's
|
|
106
|
+
* `inference_geo: "us"`, billed at 1.1x on Claude 4.6 and later. A setting,
|
|
107
|
+
* not content. A request that names none may still run in the workspace's
|
|
108
|
+
* default geography; the response says which ran, and the spend is
|
|
109
|
+
* recorded from that.
|
|
110
|
+
*/
|
|
111
|
+
inferenceGeo?: string | null;
|
|
104
112
|
}
|
|
105
113
|
|
|
106
114
|
/** Where the budget stands, as the gateway was told at the last refresh. */
|
|
@@ -222,10 +230,11 @@ export interface GatewayOptions {
|
|
|
222
230
|
waivers?: readonly WaiveEntry[];
|
|
223
231
|
}
|
|
224
232
|
|
|
225
|
-
/** How the call asked to be run, where that changes its price: speed
|
|
226
|
-
const speedOf = (call: GatewayCall): { speed?: string; serviceTier?: string } => ({
|
|
233
|
+
/** How the call asked to be run, where that changes its price: speed, service tier and geography. */
|
|
234
|
+
const speedOf = (call: GatewayCall): { speed?: string; serviceTier?: string; inferenceGeo?: string } => ({
|
|
227
235
|
...(typeof call.speed === 'string' && call.speed !== '' ? { speed: call.speed } : {}),
|
|
228
236
|
...(typeof call.serviceTier === 'string' && call.serviceTier !== '' ? { serviceTier: call.serviceTier } : {}),
|
|
237
|
+
...(typeof call.inferenceGeo === 'string' && call.inferenceGeo !== '' ? { inferenceGeo: call.inferenceGeo } : {}),
|
|
229
238
|
});
|
|
230
239
|
|
|
231
240
|
/** What this call costs at a model's rates, or null when it cannot be priced. */
|
|
@@ -512,6 +521,8 @@ export interface GatewayUsage {
|
|
|
512
521
|
speed?: string;
|
|
513
522
|
/** OpenAI's `service_tier`, the tier that actually answered: each has its own table. */
|
|
514
523
|
serviceTier?: string;
|
|
524
|
+
/** Anthropic's `usage.inference_geo`, the geography that ran: "us" bills 1.1x on Claude 4.6 and later. */
|
|
525
|
+
inferenceGeo?: string;
|
|
515
526
|
}
|
|
516
527
|
|
|
517
528
|
/**
|
|
@@ -681,6 +692,7 @@ export function streamingUsageReader(
|
|
|
681
692
|
// that ends before any delta still reports what it reported.
|
|
682
693
|
usage.outputTokens = num(start.output_tokens);
|
|
683
694
|
if (typeof start.speed === 'string' && start.speed !== '') usage.speed = start.speed;
|
|
695
|
+
if (typeof start.inference_geo === 'string' && start.inference_geo !== '') usage.inferenceGeo = start.inference_geo;
|
|
684
696
|
seen = true;
|
|
685
697
|
}
|
|
686
698
|
}
|
|
@@ -698,6 +710,7 @@ export function streamingUsageReader(
|
|
|
698
710
|
if (typeof d.output_tokens === 'number') usage.outputTokens = num(d.output_tokens);
|
|
699
711
|
if (typeof d.input_tokens === 'number') usage.inputTokens = num(d.input_tokens);
|
|
700
712
|
if (typeof d.speed === 'string' && d.speed !== '') usage.speed = d.speed;
|
|
713
|
+
if (typeof d.inference_geo === 'string' && d.inference_geo !== '') usage.inferenceGeo = d.inference_geo;
|
|
701
714
|
seen = true;
|
|
702
715
|
}
|
|
703
716
|
return;
|
|
@@ -800,6 +813,7 @@ export function usageFromResponse(
|
|
|
800
813
|
cacheWriteTokens: num(u.cache_creation_input_tokens),
|
|
801
814
|
...cacheWriteBreakdown(u),
|
|
802
815
|
...(typeof u.speed === 'string' && u.speed !== '' ? { speed: u.speed } : {}),
|
|
816
|
+
...(typeof u.inference_geo === 'string' && u.inference_geo !== '' ? { inferenceGeo: u.inference_geo } : {}),
|
|
803
817
|
};
|
|
804
818
|
}
|
|
805
819
|
if (shape === 'openai') {
|
package/src/index.ts
CHANGED
|
@@ -444,6 +444,7 @@ export {
|
|
|
444
444
|
multipliersFor,
|
|
445
445
|
isOffered,
|
|
446
446
|
serviceTierOf,
|
|
447
|
+
inferenceGeoOf,
|
|
447
448
|
} from './pricing.js';
|
|
448
449
|
export type { PricingCatalogue, PromptSize } from './pricing.js';
|
|
449
450
|
export type { CachingMode, Capability, CostMultipliers } from './types.js';
|
package/src/openai-usage.ts
CHANGED
|
@@ -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
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
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
|
-
/**
|
|
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: {
|
|
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
|
-
|
|
268
|
-
|
|
269
|
-
|
|
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,
|
package/src/pricing-overlay.ts
CHANGED
|
@@ -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
|
}
|
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,8 @@ 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;
|
|
2442
2470
|
}
|
|
2443
2471
|
|
|
2444
2472
|
/**
|
|
@@ -2467,8 +2495,28 @@ export interface PromptSize {
|
|
|
2467
2495
|
* `serviceTierOf`; anything else prices at the standard rate.
|
|
2468
2496
|
*/
|
|
2469
2497
|
serviceTier?: string;
|
|
2498
|
+
/**
|
|
2499
|
+
* The response's `usage.inference_geo`, when the log kept it: Anthropic's
|
|
2500
|
+
* `"us"`, `"global"` or `"not_available"`. Normalised by `inferenceGeoOf`;
|
|
2501
|
+
* only a geography the row publishes a multiplier for changes the price.
|
|
2502
|
+
*/
|
|
2503
|
+
inferenceGeo?: string;
|
|
2470
2504
|
}
|
|
2471
2505
|
|
|
2506
|
+
/**
|
|
2507
|
+
* A response's `usage.inference_geo` as the geography that prices it, or
|
|
2508
|
+
* `undefined` for the global rate. `"global"` is the default and the row's
|
|
2509
|
+
* own price; `"not_available"` is what a model that cannot pin a geography
|
|
2510
|
+
* reports, and is priced the same.
|
|
2511
|
+
*/
|
|
2512
|
+
export function inferenceGeoOf(value: unknown): InferenceGeo | undefined {
|
|
2513
|
+
if (typeof value !== 'string') return undefined;
|
|
2514
|
+
return value.trim().toLowerCase() === 'us' ? 'us' : undefined;
|
|
2515
|
+
}
|
|
2516
|
+
|
|
2517
|
+
/** Rounded to a billionth of a dollar per million tokens, so 1.1x of $5 is $5.5 and not $5.500000000000001. */
|
|
2518
|
+
const scaled = (rate: number, factor: number): number => Math.round(rate * factor * 1e9) / 1e9;
|
|
2519
|
+
|
|
2472
2520
|
/**
|
|
2473
2521
|
* A response's `service_tier` as the tier whose table prices it, or
|
|
2474
2522
|
* `undefined` for the standard one.
|
|
@@ -2629,6 +2677,35 @@ export function effectivePricing(
|
|
|
2629
2677
|
on: Date = new Date(),
|
|
2630
2678
|
context: PromptSize = {},
|
|
2631
2679
|
): Rates {
|
|
2680
|
+
/*
|
|
2681
|
+
* A geography with a published multiplier scales whatever the rest of the
|
|
2682
|
+
* call decides, fast mode and the long band included, because the page says
|
|
2683
|
+
* it stacks with all of them. Applied once, outside everything else.
|
|
2684
|
+
*/
|
|
2685
|
+
if (context.inferenceGeo !== undefined) {
|
|
2686
|
+
const { inferenceGeo, ...rest } = context;
|
|
2687
|
+
const rates = effectivePricing(model, on, rest);
|
|
2688
|
+
const geo = inferenceGeoOf(inferenceGeo);
|
|
2689
|
+
const factor = geo === undefined ? undefined : model.inferenceGeoMultipliers?.[geo];
|
|
2690
|
+
if (geo === undefined || factor === undefined) return rates;
|
|
2691
|
+
return {
|
|
2692
|
+
...rates,
|
|
2693
|
+
inputPerMTok: scaled(rates.inputPerMTok, factor),
|
|
2694
|
+
outputPerMTok: scaled(rates.outputPerMTok, factor),
|
|
2695
|
+
...(rates.tier?.floor !== undefined
|
|
2696
|
+
? {
|
|
2697
|
+
tier: {
|
|
2698
|
+
...rates.tier,
|
|
2699
|
+
floor: {
|
|
2700
|
+
inputPerMTok: scaled(rates.tier.floor.inputPerMTok, factor),
|
|
2701
|
+
outputPerMTok: scaled(rates.tier.floor.outputPerMTok, factor),
|
|
2702
|
+
},
|
|
2703
|
+
},
|
|
2704
|
+
}
|
|
2705
|
+
: {}),
|
|
2706
|
+
inferenceGeo: geo,
|
|
2707
|
+
};
|
|
2708
|
+
}
|
|
2632
2709
|
/* A call before a published price change is priced as it was billed then. */
|
|
2633
2710
|
if (model.history !== undefined) {
|
|
2634
2711
|
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()]
|
|
135
|
-
(
|
|
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
|
*
|