@modelprofile.com/flexharness-models 16.1.0 → 16.3.0

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.
@@ -4,5 +4,6 @@ export * from './smartai.cache.js';
4
4
  export * from './smartai.limits.js';
5
5
  export * from './smartai.failures.js';
6
6
  export * from './smartai.textfiles.js';
7
+ export * from './smartai.pricing.js';
7
8
  export { generateText, streamText, tool, jsonSchema } from './plugins.js';
8
9
  export type { ModelMessage, ToolSet, StreamTextResult } from './plugins.js';
@@ -4,5 +4,6 @@ export * from './smartai.cache.js';
4
4
  export * from './smartai.limits.js';
5
5
  export * from './smartai.failures.js';
6
6
  export * from './smartai.textfiles.js';
7
+ export * from './smartai.pricing.js';
7
8
  export { generateText, streamText, tool, jsonSchema } from './plugins.js';
8
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90c19tb2RlbHMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsY0FBYyxpQkFBaUIsQ0FBQztBQUNoQyxjQUFjLDRCQUE0QixDQUFDO0FBQzNDLGNBQWMsb0JBQW9CLENBQUM7QUFDbkMsY0FBYyxxQkFBcUIsQ0FBQztBQUNwQyxjQUFjLHVCQUF1QixDQUFDO0FBQ3RDLGNBQWMsd0JBQXdCLENBQUM7QUFDdkMsT0FBTyxFQUFFLFlBQVksRUFBRSxVQUFVLEVBQUUsSUFBSSxFQUFFLFVBQVUsRUFBRSxNQUFNLGNBQWMsQ0FBQyJ9
9
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90c19tb2RlbHMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsY0FBYyxpQkFBaUIsQ0FBQztBQUNoQyxjQUFjLDRCQUE0QixDQUFDO0FBQzNDLGNBQWMsb0JBQW9CLENBQUM7QUFDbkMsY0FBYyxxQkFBcUIsQ0FBQztBQUNwQyxjQUFjLHVCQUF1QixDQUFDO0FBQ3RDLGNBQWMsd0JBQXdCLENBQUM7QUFDdkMsY0FBYyxzQkFBc0IsQ0FBQztBQUNyQyxPQUFPLEVBQUUsWUFBWSxFQUFFLFVBQVUsRUFBRSxJQUFJLEVBQUUsVUFBVSxFQUFFLE1BQU0sY0FBYyxDQUFDIn0=
@@ -0,0 +1,111 @@
1
+ /**
2
+ * Provider-neutral model prices and the arithmetic that turns one model call's usage into its cost.
3
+ *
4
+ * Every amount is an integer, so a sum never drifts: a token rate is micro-USD per million tokens (USD per
5
+ * million tokens × 1e6; $4/MTok is `4_000_000`, $0.005/MTok is `5_000`), a tool-call fee is micro-USD per call
6
+ * and a cost is micro-USD. A cost is rounded up once, at the end. A call whose usage the price cannot cover is
7
+ * never priced at zero: `priceModelCall` names why it cannot price it.
8
+ */
9
+ /** Where a price or a model's metadata was read, and on which day (`YYYY-MM-DD`). */
10
+ export interface IModelSource {
11
+ url: string;
12
+ checkedAt: string;
13
+ }
14
+ /** Token rates in micro-USD per million tokens. */
15
+ export interface IModelTokenRates {
16
+ /** Input that was neither read from nor written to the prompt cache. */
17
+ input: number;
18
+ /** Input read from the prompt cache; absent when the provider publishes no cache-read price. */
19
+ cacheRead?: number;
20
+ /** Input written to the prompt cache, by the cache lifetime the provider bills it under (`5m`, `1h`, `30m`, `12h`). */
21
+ cacheWrite: Readonly<Record<string, number>>;
22
+ output: number;
23
+ }
24
+ /** The rates for prompts up to a size. */
25
+ export interface IModelPriceBand {
26
+ /**
27
+ * The largest prompt, in tokens, these rates apply to; absent on the last band, which has no limit. The prompt
28
+ * counts every input token of the request: uncached, cache reads and cache writes.
29
+ */
30
+ maxPromptTokens?: number;
31
+ rates: IModelTokenRates;
32
+ }
33
+ /** The prices of one service tier, by prompt size: bands in ascending `maxPromptTokens`, the unlimited one last. */
34
+ export interface IModelServicePrice {
35
+ bands: readonly IModelPriceBand[];
36
+ }
37
+ export interface IModelPrice {
38
+ providerId: string;
39
+ /** The model id the price is published for. */
40
+ modelId: string;
41
+ /** Other ids the provider's pages name for the same model, such as dated snapshots. Matched exactly. */
42
+ aliases?: readonly string[];
43
+ /**
44
+ * Prices by the service tier the provider's response reports (OpenAI `default`, `flex`, `priority`; Anthropic
45
+ * `standard`, `fast`). A tier with no published price is absent, so a call served in it is refused.
46
+ */
47
+ services: Readonly<Record<string, IModelServicePrice>>;
48
+ /** Integer micro-USD per server-side tool call, by tool name, e.g. `{ web_search: 10_000 }`. */
49
+ toolCallFees?: Readonly<Record<string, number>>;
50
+ /**
51
+ * Multipliers on every token category, in per-mille (`1100` is 1.1×), by the surcharge a reader names, e.g.
52
+ * `{ 'inference_geo:us': 1100 }`. Tool-call fees are not multiplied.
53
+ */
54
+ surcharges?: Readonly<Record<string, number>>;
55
+ source: IModelSource;
56
+ }
57
+ /** One model call's usage as a provider's usage reader states it. */
58
+ export interface IModelPricedUsage {
59
+ /** The service tier the response reports. */
60
+ service: string;
61
+ /** Input neither read from nor written to the prompt cache. */
62
+ uncachedInputTokens: number;
63
+ cacheReadTokens: number;
64
+ /** Cache writes by the lifetime the provider reported for them. */
65
+ cacheWriteTokens: Readonly<Record<string, number>>;
66
+ /**
67
+ * Cache writes the provider reported without saying which lifetime they were written for. They are charged at
68
+ * the highest write rate of the band, and the cost then is an upper bound.
69
+ */
70
+ unknownLifetimeCacheWriteTokens?: number;
71
+ /** Output, reasoning included. */
72
+ outputTokens: number;
73
+ /** Server-side tool calls by tool name. */
74
+ toolCalls?: Readonly<Record<string, number>>;
75
+ /** Surcharges the call incurred, by the names `IModelPrice.surcharges` uses. */
76
+ surcharges?: readonly string[];
77
+ }
78
+ /**
79
+ * Why a call cannot be priced:
80
+ * - `invalidPrice`: the price is not a well-formed price (negative or fractional amounts, bands out of order).
81
+ * - `invalidUsage`: the usage is not a well-formed usage, or its cost exceeds a safe integer.
82
+ * - `unknownService`: the price has no service tier of that name.
83
+ * - `noBand`: no band of the tier covers a prompt of that size.
84
+ * - `unpricedCategory`: tokens in a category the band has no rate for (cache reads, a write lifetime, or
85
+ * writes of unknown lifetime where the band prices no writes).
86
+ * - `unpricedToolCall`: calls to a tool the price has no fee for.
87
+ * - `unknownSurcharge`: a surcharge the price does not name.
88
+ */
89
+ export type TModelCallCostRefusal = 'invalidPrice' | 'invalidUsage' | 'unknownService' | 'noBand' | 'unpricedCategory' | 'unpricedToolCall' | 'unknownSurcharge';
90
+ export type TModelCallCost = {
91
+ priced: true;
92
+ /** The cost in micro-USD, rounded up. */
93
+ microUsd: number;
94
+ /** Whether cache writes of unknown lifetime were charged at the highest write rate, making the cost an upper bound. */
95
+ upperBound: boolean;
96
+ } | {
97
+ priced: false;
98
+ reason: TModelCallCostRefusal;
99
+ };
100
+ /** A usage reader's refusal of a provider usage object it cannot read completely. */
101
+ export declare class ModelUsageReadError extends Error {
102
+ constructor(messageArg: string);
103
+ }
104
+ /**
105
+ * The cost of one model call, in micro-USD, rounded up.
106
+ *
107
+ * The band is the first of the reported tier whose `maxPromptTokens` covers the whole prompt (uncached input, cache
108
+ * reads and every cache write). Each token category is charged at the band's rate; a category with tokens and no
109
+ * rate refuses the call. Surcharges multiply the token cost; tool-call fees are added after them.
110
+ */
111
+ export declare function priceModelCall(priceArg: IModelPrice, usageArg: IModelPricedUsage): TModelCallCost;
@@ -0,0 +1,119 @@
1
+ /**
2
+ * Provider-neutral model prices and the arithmetic that turns one model call's usage into its cost.
3
+ *
4
+ * Every amount is an integer, so a sum never drifts: a token rate is micro-USD per million tokens (USD per
5
+ * million tokens × 1e6; $4/MTok is `4_000_000`, $0.005/MTok is `5_000`), a tool-call fee is micro-USD per call
6
+ * and a cost is micro-USD. A cost is rounded up once, at the end. A call whose usage the price cannot cover is
7
+ * never priced at zero: `priceModelCall` names why it cannot price it.
8
+ */
9
+ /** A usage reader's refusal of a provider usage object it cannot read completely. */
10
+ export class ModelUsageReadError extends Error {
11
+ constructor(messageArg) {
12
+ super(messageArg);
13
+ this.name = 'ModelUsageReadError';
14
+ }
15
+ }
16
+ const tokensPerMillion = 1000000n;
17
+ const perMille = 1000n;
18
+ const isAmount = (valueArg) => Number.isSafeInteger(valueArg) && valueArg >= 0;
19
+ const refuse = (reasonArg) => ({ priced: false, reason: reasonArg });
20
+ const isRecordOfAmounts = (valueArg) => typeof valueArg === 'object' && valueArg !== null && !Array.isArray(valueArg)
21
+ && Object.values(valueArg).every(isAmount);
22
+ const isRates = (ratesArg) => !!ratesArg && isAmount(ratesArg.input)
23
+ && isAmount(ratesArg.output) && (ratesArg.cacheRead === undefined || isAmount(ratesArg.cacheRead))
24
+ && isRecordOfAmounts(ratesArg.cacheWrite);
25
+ const isServicePrice = (serviceArg) => {
26
+ if (!serviceArg || !Array.isArray(serviceArg.bands) || serviceArg.bands.length === 0)
27
+ return false;
28
+ let previous = -1;
29
+ return serviceArg.bands.every((band, index) => {
30
+ if (!band || !isRates(band.rates))
31
+ return false;
32
+ if (band.maxPromptTokens === undefined)
33
+ return index === serviceArg.bands.length - 1;
34
+ if (!isAmount(band.maxPromptTokens) || band.maxPromptTokens <= previous)
35
+ return false;
36
+ previous = band.maxPromptTokens;
37
+ return true;
38
+ });
39
+ };
40
+ /** The record's entry for a name the record itself holds, never one inherited from `Object.prototype`. */
41
+ const own = (recordArg, keyArg) => recordArg !== undefined && Object.prototype.hasOwnProperty.call(recordArg, keyArg) ? recordArg[keyArg] : undefined;
42
+ /**
43
+ * The cost of one model call, in micro-USD, rounded up.
44
+ *
45
+ * The band is the first of the reported tier whose `maxPromptTokens` covers the whole prompt (uncached input, cache
46
+ * reads and every cache write). Each token category is charged at the band's rate; a category with tokens and no
47
+ * rate refuses the call. Surcharges multiply the token cost; tool-call fees are added after them.
48
+ */
49
+ export function priceModelCall(priceArg, usageArg) {
50
+ if (!priceArg || typeof priceArg.services !== 'object' || priceArg.services === null
51
+ || Object.values(priceArg.services).some((service) => !isServicePrice(service))
52
+ || (priceArg.toolCallFees !== undefined && !isRecordOfAmounts(priceArg.toolCallFees))
53
+ || (priceArg.surcharges !== undefined && (!isRecordOfAmounts(priceArg.surcharges)
54
+ || Object.values(priceArg.surcharges).some((multiplier) => multiplier === 0)))) {
55
+ return refuse('invalidPrice');
56
+ }
57
+ const unknownLifetimeWrites = usageArg?.unknownLifetimeCacheWriteTokens ?? 0;
58
+ if (!usageArg || typeof usageArg.service !== 'string' || !isAmount(usageArg.uncachedInputTokens)
59
+ || !isAmount(usageArg.cacheReadTokens) || !isRecordOfAmounts(usageArg.cacheWriteTokens)
60
+ || !isAmount(unknownLifetimeWrites) || !isAmount(usageArg.outputTokens)
61
+ || (usageArg.toolCalls !== undefined && !isRecordOfAmounts(usageArg.toolCalls))
62
+ || (usageArg.surcharges !== undefined && (!Array.isArray(usageArg.surcharges)
63
+ || usageArg.surcharges.some((name) => typeof name !== 'string')
64
+ || new Set(usageArg.surcharges).size !== usageArg.surcharges.length))) {
65
+ return refuse('invalidUsage');
66
+ }
67
+ const service = own(priceArg.services, usageArg.service);
68
+ if (!service)
69
+ return refuse('unknownService');
70
+ const promptTokens = BigInt(usageArg.uncachedInputTokens) + BigInt(usageArg.cacheReadTokens)
71
+ + Object.values(usageArg.cacheWriteTokens).reduce((sum, tokens) => sum + BigInt(tokens), 0n)
72
+ + BigInt(unknownLifetimeWrites);
73
+ const band = service.bands.find((entry) => entry.maxPromptTokens === undefined || promptTokens <= BigInt(entry.maxPromptTokens));
74
+ if (!band)
75
+ return refuse('noBand');
76
+ const rates = band.rates;
77
+ // Exact integer sums: micro-USD per million tokens × tokens.
78
+ let tokenCost = BigInt(usageArg.uncachedInputTokens) * BigInt(rates.input) + BigInt(usageArg.outputTokens) * BigInt(rates.output);
79
+ if (usageArg.cacheReadTokens > 0) {
80
+ if (rates.cacheRead === undefined)
81
+ return refuse('unpricedCategory');
82
+ tokenCost += BigInt(usageArg.cacheReadTokens) * BigInt(rates.cacheRead);
83
+ }
84
+ for (const [lifetime, tokens] of Object.entries(usageArg.cacheWriteTokens)) {
85
+ if (tokens === 0)
86
+ continue;
87
+ const rate = own(rates.cacheWrite, lifetime);
88
+ if (rate === undefined)
89
+ return refuse('unpricedCategory');
90
+ tokenCost += BigInt(tokens) * BigInt(rate);
91
+ }
92
+ if (unknownLifetimeWrites > 0) {
93
+ const writeRates = Object.values(rates.cacheWrite);
94
+ if (writeRates.length === 0)
95
+ return refuse('unpricedCategory');
96
+ tokenCost += BigInt(unknownLifetimeWrites) * BigInt(Math.max(...writeRates));
97
+ }
98
+ let denominator = tokensPerMillion;
99
+ for (const name of usageArg.surcharges ?? []) {
100
+ const multiplier = own(priceArg.surcharges, name);
101
+ if (multiplier === undefined)
102
+ return refuse('unknownSurcharge');
103
+ tokenCost *= BigInt(multiplier);
104
+ denominator *= perMille;
105
+ }
106
+ let microUsd = (tokenCost + denominator - 1n) / denominator;
107
+ for (const [tool, calls] of Object.entries(usageArg.toolCalls ?? {})) {
108
+ if (calls === 0)
109
+ continue;
110
+ const fee = own(priceArg.toolCallFees, tool);
111
+ if (fee === undefined)
112
+ return refuse('unpricedToolCall');
113
+ microUsd += BigInt(calls) * BigInt(fee);
114
+ }
115
+ if (microUsd > BigInt(Number.MAX_SAFE_INTEGER))
116
+ return refuse('invalidUsage');
117
+ return { priced: true, microUsd: Number(microUsd), upperBound: unknownLifetimeWrites > 0 };
118
+ }
119
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRhaS5wcmljaW5nLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHNfbW9kZWxzL3NtYXJ0YWkucHJpY2luZy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7Ozs7OztHQU9HO0FBMkdILHFGQUFxRjtBQUNyRixNQUFNLE9BQU8sbUJBQW9CLFNBQVEsS0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BQyxjQUFjLENBQUMsQ0FBQztJQUM5RSxPQUFPLEVBQUUsTUFBTSxFQUFFLElBQUksRUFBRSxRQUFRLEVBQUUsTUFBTSxDQUFDLFFBQVEsQ0FBQyxFQUFFLFVBQVUsRUFBRSxxQkFBcUIsR0FBRyxDQUFDLEVBQUUsQ0FBQztBQUM3RixDQUFDIn0=
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "author": "Task Venture Capital GmbH",
3
3
  "license": "MIT",
4
4
  "name": "@modelprofile.com/flexharness-models",
5
- "version": "16.1.0",
5
+ "version": "16.3.0",
6
6
  "type": "module",
7
7
  "description": "Provider-independent model contracts, prompt caching and explicit model registration.",
8
8
  "main": "./dist_ts_models/index.js",
package/readme.md CHANGED
@@ -48,6 +48,25 @@ media type and the content decoded in the stated charset (UTF-8 by default), in
48
48
  than any backtick run in the content. Images, PDFs, other binary files, URLs and file references
49
49
  pass unchanged; a file that names text but does not decode fails before the request is sent.
50
50
 
51
+ ### Model call prices
52
+
53
+ `priceModelCall(price, usage)` turns one call's usage into its cost. An `IModelPrice` holds integer
54
+ rates in micro-USD per million tokens (`$4/MTok` is `4_000_000`) per service tier the response
55
+ reports, each tier as prompt-size bands (`maxPromptTokens`, ascending, the unlimited band last; the
56
+ prompt counts uncached input, cache reads and every cache write), cache-write rates by lifetime
57
+ (`5m`, `1h`, `30m`, `12h`), tool-call fees in micro-USD per call, per-mille surcharges on the token
58
+ cost (`1100` is 1.1×) and its `source` (`url`, `checkedAt`). An `IModelPricedUsage`, as a provider's
59
+ usage reader states it, names the tier, the uncached, cached-read and written tokens, output,
60
+ tool calls and surcharges; writes the provider did not split by lifetime go to
61
+ `unknownLifetimeCacheWriteTokens`.
62
+
63
+ The result is `{ priced: true, microUsd, upperBound }`, summed exactly and rounded up once;
64
+ `upperBound` is set when writes of unknown lifetime were charged at the band's highest write rate.
65
+ A call the price cannot cover is never priced at zero: `{ priced: false, reason }` names
66
+ `unknownService`, `noBand`, `unpricedCategory` (tokens in a category without a rate),
67
+ `unpricedToolCall`, `unknownSurcharge`, `invalidPrice` or `invalidUsage`. Readers refuse an
68
+ incomplete provider usage with `ModelUsageReadError`.
69
+
51
70
  ### Prompt caching
52
71
 
53
72
  `TSmartAiCacheSetting` is `'auto'` (the default of a FlexHarness session), `false`, or an
@@ -4,5 +4,6 @@ export * from './smartai.cache.js';
4
4
  export * from './smartai.limits.js';
5
5
  export * from './smartai.failures.js';
6
6
  export * from './smartai.textfiles.js';
7
+ export * from './smartai.pricing.js';
7
8
  export { generateText, streamText, tool, jsonSchema } from './plugins.js';
8
9
  export type { ModelMessage, ToolSet, StreamTextResult } from './plugins.js';
@@ -48,6 +48,25 @@ media type and the content decoded in the stated charset (UTF-8 by default), in
48
48
  than any backtick run in the content. Images, PDFs, other binary files, URLs and file references
49
49
  pass unchanged; a file that names text but does not decode fails before the request is sent.
50
50
 
51
+ ### Model call prices
52
+
53
+ `priceModelCall(price, usage)` turns one call's usage into its cost. An `IModelPrice` holds integer
54
+ rates in micro-USD per million tokens (`$4/MTok` is `4_000_000`) per service tier the response
55
+ reports, each tier as prompt-size bands (`maxPromptTokens`, ascending, the unlimited band last; the
56
+ prompt counts uncached input, cache reads and every cache write), cache-write rates by lifetime
57
+ (`5m`, `1h`, `30m`, `12h`), tool-call fees in micro-USD per call, per-mille surcharges on the token
58
+ cost (`1100` is 1.1×) and its `source` (`url`, `checkedAt`). An `IModelPricedUsage`, as a provider's
59
+ usage reader states it, names the tier, the uncached, cached-read and written tokens, output,
60
+ tool calls and surcharges; writes the provider did not split by lifetime go to
61
+ `unknownLifetimeCacheWriteTokens`.
62
+
63
+ The result is `{ priced: true, microUsd, upperBound }`, summed exactly and rounded up once;
64
+ `upperBound` is set when writes of unknown lifetime were charged at the band's highest write rate.
65
+ A call the price cannot cover is never priced at zero: `{ priced: false, reason }` names
66
+ `unknownService`, `noBand`, `unpricedCategory` (tokens in a category without a rate),
67
+ `unpricedToolCall`, `unknownSurcharge`, `invalidPrice` or `invalidUsage`. Readers refuse an
68
+ incomplete provider usage with `ModelUsageReadError`.
69
+
51
70
  ### Prompt caching
52
71
 
53
72
  `TSmartAiCacheSetting` is `'auto'` (the default of a FlexHarness session), `false`, or an
@@ -0,0 +1,224 @@
1
+ /**
2
+ * Provider-neutral model prices and the arithmetic that turns one model call's usage into its cost.
3
+ *
4
+ * Every amount is an integer, so a sum never drifts: a token rate is micro-USD per million tokens (USD per
5
+ * million tokens × 1e6; $4/MTok is `4_000_000`, $0.005/MTok is `5_000`), a tool-call fee is micro-USD per call
6
+ * and a cost is micro-USD. A cost is rounded up once, at the end. A call whose usage the price cannot cover is
7
+ * never priced at zero: `priceModelCall` names why it cannot price it.
8
+ */
9
+
10
+ /** Where a price or a model's metadata was read, and on which day (`YYYY-MM-DD`). */
11
+ export interface IModelSource {
12
+ url: string;
13
+ checkedAt: string;
14
+ }
15
+
16
+ /** Token rates in micro-USD per million tokens. */
17
+ export interface IModelTokenRates {
18
+ /** Input that was neither read from nor written to the prompt cache. */
19
+ input: number;
20
+ /** Input read from the prompt cache; absent when the provider publishes no cache-read price. */
21
+ cacheRead?: number;
22
+ /** Input written to the prompt cache, by the cache lifetime the provider bills it under (`5m`, `1h`, `30m`, `12h`). */
23
+ cacheWrite: Readonly<Record<string, number>>;
24
+ output: number;
25
+ }
26
+
27
+ /** The rates for prompts up to a size. */
28
+ export interface IModelPriceBand {
29
+ /**
30
+ * The largest prompt, in tokens, these rates apply to; absent on the last band, which has no limit. The prompt
31
+ * counts every input token of the request: uncached, cache reads and cache writes.
32
+ */
33
+ maxPromptTokens?: number;
34
+ rates: IModelTokenRates;
35
+ }
36
+
37
+ /** The prices of one service tier, by prompt size: bands in ascending `maxPromptTokens`, the unlimited one last. */
38
+ export interface IModelServicePrice {
39
+ bands: readonly IModelPriceBand[];
40
+ }
41
+
42
+ export interface IModelPrice {
43
+ providerId: string;
44
+ /** The model id the price is published for. */
45
+ modelId: string;
46
+ /** Other ids the provider's pages name for the same model, such as dated snapshots. Matched exactly. */
47
+ aliases?: readonly string[];
48
+ /**
49
+ * Prices by the service tier the provider's response reports (OpenAI `default`, `flex`, `priority`; Anthropic
50
+ * `standard`, `fast`). A tier with no published price is absent, so a call served in it is refused.
51
+ */
52
+ services: Readonly<Record<string, IModelServicePrice>>;
53
+ /** Integer micro-USD per server-side tool call, by tool name, e.g. `{ web_search: 10_000 }`. */
54
+ toolCallFees?: Readonly<Record<string, number>>;
55
+ /**
56
+ * Multipliers on every token category, in per-mille (`1100` is 1.1×), by the surcharge a reader names, e.g.
57
+ * `{ 'inference_geo:us': 1100 }`. Tool-call fees are not multiplied.
58
+ */
59
+ surcharges?: Readonly<Record<string, number>>;
60
+ source: IModelSource;
61
+ }
62
+
63
+ /** One model call's usage as a provider's usage reader states it. */
64
+ export interface IModelPricedUsage {
65
+ /** The service tier the response reports. */
66
+ service: string;
67
+ /** Input neither read from nor written to the prompt cache. */
68
+ uncachedInputTokens: number;
69
+ cacheReadTokens: number;
70
+ /** Cache writes by the lifetime the provider reported for them. */
71
+ cacheWriteTokens: Readonly<Record<string, number>>;
72
+ /**
73
+ * Cache writes the provider reported without saying which lifetime they were written for. They are charged at
74
+ * the highest write rate of the band, and the cost then is an upper bound.
75
+ */
76
+ unknownLifetimeCacheWriteTokens?: number;
77
+ /** Output, reasoning included. */
78
+ outputTokens: number;
79
+ /** Server-side tool calls by tool name. */
80
+ toolCalls?: Readonly<Record<string, number>>;
81
+ /** Surcharges the call incurred, by the names `IModelPrice.surcharges` uses. */
82
+ surcharges?: readonly string[];
83
+ }
84
+
85
+ /**
86
+ * Why a call cannot be priced:
87
+ * - `invalidPrice`: the price is not a well-formed price (negative or fractional amounts, bands out of order).
88
+ * - `invalidUsage`: the usage is not a well-formed usage, or its cost exceeds a safe integer.
89
+ * - `unknownService`: the price has no service tier of that name.
90
+ * - `noBand`: no band of the tier covers a prompt of that size.
91
+ * - `unpricedCategory`: tokens in a category the band has no rate for (cache reads, a write lifetime, or
92
+ * writes of unknown lifetime where the band prices no writes).
93
+ * - `unpricedToolCall`: calls to a tool the price has no fee for.
94
+ * - `unknownSurcharge`: a surcharge the price does not name.
95
+ */
96
+ export type TModelCallCostRefusal =
97
+ | 'invalidPrice'
98
+ | 'invalidUsage'
99
+ | 'unknownService'
100
+ | 'noBand'
101
+ | 'unpricedCategory'
102
+ | 'unpricedToolCall'
103
+ | 'unknownSurcharge';
104
+
105
+ export type TModelCallCost =
106
+ | {
107
+ priced: true;
108
+ /** The cost in micro-USD, rounded up. */
109
+ microUsd: number;
110
+ /** Whether cache writes of unknown lifetime were charged at the highest write rate, making the cost an upper bound. */
111
+ upperBound: boolean;
112
+ }
113
+ | { priced: false; reason: TModelCallCostRefusal };
114
+
115
+ /** A usage reader's refusal of a provider usage object it cannot read completely. */
116
+ export class ModelUsageReadError extends Error {
117
+ constructor(messageArg: string) {
118
+ super(messageArg);
119
+ this.name = 'ModelUsageReadError';
120
+ }
121
+ }
122
+
123
+ const tokensPerMillion = 1_000_000n;
124
+ const perMille = 1_000n;
125
+
126
+ const isAmount = (valueArg: unknown): valueArg is number => Number.isSafeInteger(valueArg) && (valueArg as number) >= 0;
127
+
128
+ const refuse = (reasonArg: TModelCallCostRefusal): TModelCallCost => ({ priced: false, reason: reasonArg });
129
+
130
+ const isRecordOfAmounts = (valueArg: unknown): valueArg is Readonly<Record<string, number>> =>
131
+ typeof valueArg === 'object' && valueArg !== null && !Array.isArray(valueArg)
132
+ && Object.values(valueArg).every(isAmount);
133
+
134
+ const isRates = (ratesArg: IModelTokenRates | undefined): boolean => !!ratesArg && isAmount(ratesArg.input)
135
+ && isAmount(ratesArg.output) && (ratesArg.cacheRead === undefined || isAmount(ratesArg.cacheRead))
136
+ && isRecordOfAmounts(ratesArg.cacheWrite);
137
+
138
+ const isServicePrice = (serviceArg: IModelServicePrice | undefined): boolean => {
139
+ if (!serviceArg || !Array.isArray(serviceArg.bands) || serviceArg.bands.length === 0) return false;
140
+ let previous = -1;
141
+ return serviceArg.bands.every((band, index) => {
142
+ if (!band || !isRates(band.rates)) return false;
143
+ if (band.maxPromptTokens === undefined) return index === serviceArg.bands.length - 1;
144
+ if (!isAmount(band.maxPromptTokens) || band.maxPromptTokens <= previous) return false;
145
+ previous = band.maxPromptTokens;
146
+ return true;
147
+ });
148
+ };
149
+
150
+ /** The record's entry for a name the record itself holds, never one inherited from `Object.prototype`. */
151
+ const own = <T>(recordArg: Readonly<Record<string, T>> | undefined, keyArg: string): T | undefined =>
152
+ recordArg !== undefined && Object.prototype.hasOwnProperty.call(recordArg, keyArg) ? recordArg[keyArg] : undefined;
153
+
154
+ /**
155
+ * The cost of one model call, in micro-USD, rounded up.
156
+ *
157
+ * The band is the first of the reported tier whose `maxPromptTokens` covers the whole prompt (uncached input, cache
158
+ * reads and every cache write). Each token category is charged at the band's rate; a category with tokens and no
159
+ * rate refuses the call. Surcharges multiply the token cost; tool-call fees are added after them.
160
+ */
161
+ export function priceModelCall(priceArg: IModelPrice, usageArg: IModelPricedUsage): TModelCallCost {
162
+ if (!priceArg || typeof priceArg.services !== 'object' || priceArg.services === null
163
+ || Object.values(priceArg.services).some((service) => !isServicePrice(service))
164
+ || (priceArg.toolCallFees !== undefined && !isRecordOfAmounts(priceArg.toolCallFees))
165
+ || (priceArg.surcharges !== undefined && (!isRecordOfAmounts(priceArg.surcharges)
166
+ || Object.values(priceArg.surcharges).some((multiplier) => multiplier === 0)))) {
167
+ return refuse('invalidPrice');
168
+ }
169
+ const unknownLifetimeWrites = usageArg?.unknownLifetimeCacheWriteTokens ?? 0;
170
+ if (!usageArg || typeof usageArg.service !== 'string' || !isAmount(usageArg.uncachedInputTokens)
171
+ || !isAmount(usageArg.cacheReadTokens) || !isRecordOfAmounts(usageArg.cacheWriteTokens)
172
+ || !isAmount(unknownLifetimeWrites) || !isAmount(usageArg.outputTokens)
173
+ || (usageArg.toolCalls !== undefined && !isRecordOfAmounts(usageArg.toolCalls))
174
+ || (usageArg.surcharges !== undefined && (!Array.isArray(usageArg.surcharges)
175
+ || usageArg.surcharges.some((name) => typeof name !== 'string')
176
+ || new Set(usageArg.surcharges).size !== usageArg.surcharges.length))) {
177
+ return refuse('invalidUsage');
178
+ }
179
+
180
+ const service = own(priceArg.services, usageArg.service);
181
+ if (!service) return refuse('unknownService');
182
+ const promptTokens = BigInt(usageArg.uncachedInputTokens) + BigInt(usageArg.cacheReadTokens)
183
+ + Object.values(usageArg.cacheWriteTokens).reduce((sum, tokens) => sum + BigInt(tokens), 0n)
184
+ + BigInt(unknownLifetimeWrites);
185
+ const band = service.bands.find((entry) => entry.maxPromptTokens === undefined || promptTokens <= BigInt(entry.maxPromptTokens));
186
+ if (!band) return refuse('noBand');
187
+ const rates = band.rates;
188
+
189
+ // Exact integer sums: micro-USD per million tokens × tokens.
190
+ let tokenCost = BigInt(usageArg.uncachedInputTokens) * BigInt(rates.input) + BigInt(usageArg.outputTokens) * BigInt(rates.output);
191
+ if (usageArg.cacheReadTokens > 0) {
192
+ if (rates.cacheRead === undefined) return refuse('unpricedCategory');
193
+ tokenCost += BigInt(usageArg.cacheReadTokens) * BigInt(rates.cacheRead);
194
+ }
195
+ for (const [lifetime, tokens] of Object.entries(usageArg.cacheWriteTokens)) {
196
+ if (tokens === 0) continue;
197
+ const rate = own(rates.cacheWrite, lifetime);
198
+ if (rate === undefined) return refuse('unpricedCategory');
199
+ tokenCost += BigInt(tokens) * BigInt(rate);
200
+ }
201
+ if (unknownLifetimeWrites > 0) {
202
+ const writeRates = Object.values(rates.cacheWrite);
203
+ if (writeRates.length === 0) return refuse('unpricedCategory');
204
+ tokenCost += BigInt(unknownLifetimeWrites) * BigInt(Math.max(...writeRates));
205
+ }
206
+
207
+ let denominator = tokensPerMillion;
208
+ for (const name of usageArg.surcharges ?? []) {
209
+ const multiplier = own(priceArg.surcharges, name);
210
+ if (multiplier === undefined) return refuse('unknownSurcharge');
211
+ tokenCost *= BigInt(multiplier);
212
+ denominator *= perMille;
213
+ }
214
+ let microUsd = (tokenCost + denominator - 1n) / denominator;
215
+
216
+ for (const [tool, calls] of Object.entries(usageArg.toolCalls ?? {})) {
217
+ if (calls === 0) continue;
218
+ const fee = own(priceArg.toolCallFees, tool);
219
+ if (fee === undefined) return refuse('unpricedToolCall');
220
+ microUsd += BigInt(calls) * BigInt(fee);
221
+ }
222
+ if (microUsd > BigInt(Number.MAX_SAFE_INTEGER)) return refuse('invalidUsage');
223
+ return { priced: true, microUsd: Number(microUsd), upperBound: unknownLifetimeWrites > 0 };
224
+ }