language-models 2.1.1 → 2.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.
Files changed (53) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +36 -0
  3. package/README.md +106 -43
  4. package/dist/index.d.ts +3 -1
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +13 -1
  7. package/dist/index.js.map +1 -1
  8. package/dist/models.d.ts +1 -1
  9. package/dist/models.d.ts.map +1 -1
  10. package/dist/models.js +8 -10
  11. package/dist/models.js.map +1 -1
  12. package/dist/policy.d.ts +127 -0
  13. package/dist/policy.d.ts.map +1 -0
  14. package/dist/policy.js +246 -0
  15. package/dist/policy.js.map +1 -0
  16. package/dist/pricing/index.d.ts +19 -0
  17. package/dist/pricing/index.d.ts.map +1 -0
  18. package/dist/pricing/index.js +18 -0
  19. package/dist/pricing/index.js.map +1 -0
  20. package/dist/pricing/lookup.d.ts +46 -0
  21. package/dist/pricing/lookup.d.ts.map +1 -0
  22. package/dist/pricing/lookup.js +94 -0
  23. package/dist/pricing/lookup.js.map +1 -0
  24. package/dist/pricing/table.d.ts +46 -0
  25. package/dist/pricing/table.d.ts.map +1 -0
  26. package/dist/pricing/table.js +214 -0
  27. package/dist/pricing/table.js.map +1 -0
  28. package/dist/pricing/types.d.ts +84 -0
  29. package/dist/pricing/types.d.ts.map +1 -0
  30. package/dist/pricing/types.js +32 -0
  31. package/dist/pricing/types.js.map +1 -0
  32. package/package.json +6 -2
  33. package/src/index.ts +42 -1
  34. package/src/models.ts +8 -12
  35. package/src/policy.ts +343 -0
  36. package/src/pricing/index.ts +29 -0
  37. package/src/pricing/lookup.ts +124 -0
  38. package/src/pricing/table.ts +235 -0
  39. package/src/pricing/types.ts +90 -0
  40. package/{src → test}/aliases.test.ts +20 -22
  41. package/{src → test}/index.test.ts +9 -9
  42. package/{src → test}/models.test.ts +8 -6
  43. package/test/policy.test.ts +203 -0
  44. package/test/pricing.test.ts +279 -0
  45. package/vitest.config.ts +21 -1
  46. package/.turbo/turbo-test.log +0 -7
  47. package/src/aliases.js +0 -40
  48. package/src/aliases.test.js +0 -264
  49. package/src/index.js +0 -9
  50. package/src/index.test.js +0 -320
  51. package/src/models.js +0 -108
  52. package/src/models.test.js +0 -335
  53. package/vitest.config.js +0 -10
package/dist/policy.js ADDED
@@ -0,0 +1,246 @@
1
+ /**
2
+ * ModelPolicy - per-model resilience and tier policy data
3
+ *
4
+ * `language-models` owns model identity (alias resolution, capability lookup).
5
+ * Resilience policy (retry, circuit breaker, fallback chain, batch tier) is
6
+ * a per-model concern that belongs alongside the catalog data — but the
7
+ * runtime *machinery* that applies the policy lives in `ai-functions`.
8
+ *
9
+ * This module provides:
10
+ * - `ModelPolicy` MDXLD type (`$type: 'ModelPolicy'`)
11
+ * - `policyFor(alias)` - resolve an alias and return its derived policy
12
+ * - `derivePolicy(model, alias?)` - inference layer that turns OpenRouter raw
13
+ * data into a policy by applying heuristics (newest frontier model is best,
14
+ * price within a family is inversely correlated with capability, etc.)
15
+ *
16
+ * Source of truth: OpenRouter raw catalog (data/models.json) + heuristics.
17
+ * Strategy: runtime derivation, cached. A static snapshot generator could be
18
+ * added later (see `derivePolicy` — it's pure, so you can pre-compute it).
19
+ *
20
+ * @packageDocumentation
21
+ */
22
+ import { resolve, get, list } from './models.js';
23
+ import { ALIASES } from './aliases.js';
24
+ // ============================================================================
25
+ // Defaults & heuristics
26
+ // ============================================================================
27
+ /** Frontier labs — newer releases tend to be more capable. */
28
+ const FRONTIER_PROVIDERS = new Set(['anthropic', 'openai', 'google']);
29
+ /** Providers with batch APIs supported by ai-functions. */
30
+ const BATCH_PROVIDERS = new Set(['anthropic', 'openai', 'google', 'amazon-bedrock', 'cloudflare']);
31
+ /** Providers with flex (faster-than-batch) APIs. */
32
+ const FLEX_PROVIDERS = new Set(['openai', 'amazon-bedrock']);
33
+ /** Curated fallback seeds — picked one per frontier family. */
34
+ const FRONTIER_FALLBACK = [
35
+ 'anthropic/claude-sonnet-4.5',
36
+ 'anthropic/claude-opus-4.5',
37
+ 'openai/gpt-4o',
38
+ 'google/gemini-2.5-pro',
39
+ ];
40
+ /** Default retry policy — matches `ai-functions` `RetryPolicy` defaults. */
41
+ export const DEFAULT_RETRY = {
42
+ maxRetries: 3,
43
+ baseDelay: 1000,
44
+ maxDelay: 30000,
45
+ multiplier: 2,
46
+ jitter: 0,
47
+ retryableCategories: ['network', 'rate_limit', 'server', 'unknown'],
48
+ };
49
+ /** Default circuit breaker — matches `ai-functions` defaults. */
50
+ export const DEFAULT_CIRCUIT_BREAKER = {
51
+ failureThreshold: 5,
52
+ resetTimeout: 30000,
53
+ successThreshold: 1,
54
+ };
55
+ /**
56
+ * Default policy for an unknown model. Used when no catalog entry is found.
57
+ */
58
+ export function defaultPolicy(modelId) {
59
+ const provider = modelId.includes('/') ? modelId.split('/')[0] : 'unknown';
60
+ return {
61
+ $type: 'ModelPolicy',
62
+ $id: modelId,
63
+ provider,
64
+ retry: { ...DEFAULT_RETRY },
65
+ circuitBreaker: { ...DEFAULT_CIRCUIT_BREAKER },
66
+ fallbackChain: [],
67
+ batchTier: ['immediate'],
68
+ errorMapping: {},
69
+ };
70
+ }
71
+ // ============================================================================
72
+ // Derivation layer
73
+ // ============================================================================
74
+ /**
75
+ * Parse a price-per-token string to a number. Returns 0 on parse failure.
76
+ * Pricing comes through OpenRouter as a string (e.g. "0.000003").
77
+ */
78
+ function parsePrice(p) {
79
+ if (!p)
80
+ return 0;
81
+ const n = Number(p);
82
+ return Number.isFinite(n) ? n : 0;
83
+ }
84
+ /**
85
+ * Extract a "family" key from a model id for fallback grouping.
86
+ * 'anthropic/claude-opus-4.5' → 'anthropic/claude'
87
+ * 'openai/gpt-4o-mini' → 'openai/gpt'
88
+ */
89
+ function familyKey(id) {
90
+ const slash = id.indexOf('/');
91
+ if (slash < 0)
92
+ return id;
93
+ const provider = id.substring(0, slash);
94
+ const rest = id.substring(slash + 1).toLowerCase();
95
+ // Strip trailing version tags / size qualifiers
96
+ const family = rest
97
+ .replace(/[-_]?\d.*$/, '') // drop -4.5, -2.0, etc
98
+ .replace(/(opus|sonnet|haiku|mini|pro|flash|lite|maverick|instruct).*$/, '$1')
99
+ .replace(/-?(opus|sonnet|haiku|mini|pro|flash|lite|maverick|instruct)$/, '');
100
+ return `${provider}/${family || rest.split('-')[0]}`;
101
+ }
102
+ /**
103
+ * Derive the fallback chain for a model.
104
+ *
105
+ * Heuristics:
106
+ * 1. Prefer same-family siblings (e.g. sonnet → opus → haiku within Claude)
107
+ * ordered by `created` (newer first), then by price descending (more
108
+ * expensive within a family is usually more capable).
109
+ * 2. Then fall back to frontier-lab seeds, skipping the model itself and
110
+ * anything from the same family already included.
111
+ * 3. Cap at 4 entries to keep latency bounded.
112
+ */
113
+ function deriveFallbackChain(model, allModels) {
114
+ const chain = [];
115
+ const seen = new Set([model.id]);
116
+ const fam = familyKey(model.id);
117
+ // Step 1: same-family siblings, sorted newest-first then by price desc.
118
+ const siblings = allModels
119
+ .filter((m) => m.id !== model.id && familyKey(m.id) === fam)
120
+ .map((m) => ({
121
+ m,
122
+ created: m.created ?? 0,
123
+ price: parsePrice(m.pricing?.completion),
124
+ }))
125
+ .sort((a, b) => {
126
+ if (b.created !== a.created)
127
+ return b.created - a.created;
128
+ return b.price - a.price;
129
+ });
130
+ for (const { m } of siblings.slice(0, 2)) {
131
+ if (!seen.has(m.id)) {
132
+ chain.push(m.id);
133
+ seen.add(m.id);
134
+ }
135
+ }
136
+ // Step 2: frontier seeds.
137
+ for (const seed of FRONTIER_FALLBACK) {
138
+ if (chain.length >= 4)
139
+ break;
140
+ if (seen.has(seed))
141
+ continue;
142
+ if (familyKey(seed) === fam)
143
+ continue;
144
+ chain.push(seed);
145
+ seen.add(seed);
146
+ }
147
+ return chain;
148
+ }
149
+ /**
150
+ * Derive batch-tier eligibility from provider capability.
151
+ */
152
+ function deriveBatchTiers(provider) {
153
+ const tiers = ['immediate'];
154
+ if (FLEX_PROVIDERS.has(provider))
155
+ tiers.push('flex');
156
+ if (BATCH_PROVIDERS.has(provider))
157
+ tiers.push('batch');
158
+ return tiers;
159
+ }
160
+ /**
161
+ * Derive the retry policy. Frontier providers get one extra attempt because
162
+ * their rate limits are typically more transient than long-tail providers.
163
+ */
164
+ function deriveRetry(provider) {
165
+ if (FRONTIER_PROVIDERS.has(provider)) {
166
+ return { ...DEFAULT_RETRY, maxRetries: 4, jitter: 0.2 };
167
+ }
168
+ return { ...DEFAULT_RETRY };
169
+ }
170
+ /**
171
+ * Derive the circuit-breaker policy. Frontier providers get a higher
172
+ * failure threshold (more capacity) and a shorter reset timeout.
173
+ */
174
+ function deriveCircuitBreaker(provider) {
175
+ if (FRONTIER_PROVIDERS.has(provider)) {
176
+ return { failureThreshold: 8, resetTimeout: 20000, successThreshold: 1 };
177
+ }
178
+ return { ...DEFAULT_CIRCUIT_BREAKER };
179
+ }
180
+ /**
181
+ * Derivation layer — turn a `ModelInfo` (from OpenRouter raw data) into a
182
+ * `ModelPolicy` by applying heuristics.
183
+ *
184
+ * Pure function; safe to call at build time to pre-compute a static snapshot.
185
+ *
186
+ * @param model - The catalog entry for the model.
187
+ * @param allModels - The full catalog, used for sibling lookup. Optional;
188
+ * defaults to `list()`.
189
+ */
190
+ export function derivePolicy(model, allModels) {
191
+ const all = allModels ?? list();
192
+ const slash = model.id.indexOf('/');
193
+ const provider = slash > 0 ? model.id.substring(0, slash) : model.provider ?? 'unknown';
194
+ return {
195
+ $type: 'ModelPolicy',
196
+ $id: model.id,
197
+ provider,
198
+ retry: deriveRetry(provider),
199
+ circuitBreaker: deriveCircuitBreaker(provider),
200
+ fallbackChain: deriveFallbackChain(model, all),
201
+ batchTier: deriveBatchTiers(provider),
202
+ errorMapping: {},
203
+ };
204
+ }
205
+ // ============================================================================
206
+ // Public API
207
+ // ============================================================================
208
+ /** Per-process cache of derived policies, keyed by resolved model id. */
209
+ const policyCache = new Map();
210
+ /**
211
+ * Resolve an alias (or full model id) and return its policy.
212
+ *
213
+ * Falls back to `defaultPolicy(id)` if the model is not in the catalog —
214
+ * callers always get a usable policy.
215
+ *
216
+ * @example
217
+ * ```ts
218
+ * const p = policyFor('sonnet')
219
+ * // p.fallbackChain → ['anthropic/claude-opus-4.5', 'openai/gpt-4o', ...]
220
+ * // p.batchTier → ['immediate', 'batch']
221
+ * ```
222
+ */
223
+ export function policyFor(input) {
224
+ const id = resolve(input);
225
+ const cached = policyCache.get(id);
226
+ if (cached)
227
+ return cached;
228
+ const model = get(id);
229
+ const policy = model ? derivePolicy(model) : defaultPolicy(id);
230
+ policyCache.set(id, policy);
231
+ return policy;
232
+ }
233
+ /**
234
+ * Reset the policy cache. Useful for tests, or after the catalog is reloaded.
235
+ */
236
+ export function resetPolicyCache() {
237
+ policyCache.clear();
238
+ }
239
+ /**
240
+ * List all known aliases. Convenience for tooling that wants to enumerate
241
+ * derived policies (e.g. a static snapshot generator).
242
+ */
243
+ export function listAliases() {
244
+ return Object.keys(ALIASES);
245
+ }
246
+ //# sourceMappingURL=policy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"policy.js","sourceRoot":"","sources":["../src/policy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,IAAI,EAAkB,MAAM,aAAa,CAAA;AAChE,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAiFtC,+EAA+E;AAC/E,wBAAwB;AACxB,+EAA+E;AAE/E,8DAA8D;AAC9D,MAAM,kBAAkB,GAAG,IAAI,GAAG,CAAC,CAAC,WAAW,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAA;AAErE,2DAA2D;AAC3D,MAAM,eAAe,GAAG,IAAI,GAAG,CAAC,CAAC,WAAW,EAAE,QAAQ,EAAE,QAAQ,EAAE,gBAAgB,EAAE,YAAY,CAAC,CAAC,CAAA;AAElG,oDAAoD;AACpD,MAAM,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AE,aAAa;QACpB,GAAG,EAAE,KAAK,CAAC,EAAE;QACb,QAAQ;QACR,KAAK,EAAE,WAAW,CAAC,QAAQ,CAAC;QAC5B,cAAc,EAAE,oBAAoB,CAAC,QAAQ,CAAC;QAC9C,aAAa,EAAE,mBAAmB,CAAC,KAAK,EAAE,GAAG,CAAC;QAC9C,SAAS,EAAE,gBAAgB,CAAC,QAAQ,CAAC;QACrC,YAAY,EAAE,EAAE;KACjB,CAAA;AACH,CAAC;AAED,+EAA+E;AAC/E,aAAa;AACb,+EAA+E;AAE/E,yEAAyE;AACzE,MAAM,WAAW,GAAG,IAAI,GAAG,EAAuB,CAAA;AAElD;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,SAAS,CAAC,KAAa;IACrC,MAAM,EAAE,GAAG,OAAO,CAAC,KAAK,CAAC,CAAA;IACzB,MAAM,MAAM,GAAG,WAAW,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;IAClC,IAAI,MAAM;QAAE,OAAO,MAAM,CAAA;IAEzB,MAAM,KAAK,GAAG,GAAG,CAAC,EAAE,CAAC,CAAA;IACrB,MAAM,MAAM,GAAG,KAAK,CAAC,CAAC,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,EAAE,CAAC,CAAA;IAC9D,WAAW,CAAC,GAAG,CAAC,EAAE,EAAE,MAAM,CAAC,CAAA;IAC3B,OAAO,MAAM,CAAA;AACf,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,gBAAgB;IAC9B,WAAW,CAAC,KAAK,EAAE,CAAA;AACrB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,WAAW;IACzB,OAAO,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;AAC7B,CAAC"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * language-models / pricing — canonical LLM model pricing table.
3
+ *
4
+ * Consume via the subpath export:
5
+ *
6
+ * import { PRICING_TABLE, priceFor, type ModelPricing } from 'language-models/pricing'
7
+ *
8
+ * (Equivalent symbols are also re-exported from the package root —
9
+ * `import { priceFor } from 'language-models'` works too.)
10
+ *
11
+ * Rates are sourced from public Vertex / Bedrock / AI Studio list prices.
12
+ * `priceFor()` throws on unknown slug/tier rather than returning a silent
13
+ * zero (per BYOK_GATEWAY_LIES discipline: loud failure beats silent
14
+ * downgrade).
15
+ */
16
+ export type { HasPricingArgs, ModelPricing, PriceForArgs, PriceForResult, PricingTier, Provider, RateBlock, } from './types.js';
17
+ export { PRICING_TABLE } from './table.js';
18
+ export { priceFor, listSlugs, hasPricing, rowsForSlug } from './lookup.js';
19
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/pricing/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,YAAY,EACV,cAAc,EACd,YAAY,EACZ,YAAY,EACZ,cAAc,EACd,WAAW,EACX,QAAQ,EACR,SAAS,GACV,MAAM,YAAY,CAAA;AAEnB,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAE1C,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * language-models / pricing — canonical LLM model pricing table.
3
+ *
4
+ * Consume via the subpath export:
5
+ *
6
+ * import { PRICING_TABLE, priceFor, type ModelPricing } from 'language-models/pricing'
7
+ *
8
+ * (Equivalent symbols are also re-exported from the package root —
9
+ * `import { priceFor } from 'language-models'` works too.)
10
+ *
11
+ * Rates are sourced from public Vertex / Bedrock / AI Studio list prices.
12
+ * `priceFor()` throws on unknown slug/tier rather than returning a silent
13
+ * zero (per BYOK_GATEWAY_LIES discipline: loud failure beats silent
14
+ * downgrade).
15
+ */
16
+ export { PRICING_TABLE } from './table.js';
17
+ export { priceFor, listSlugs, hasPricing, rowsForSlug } from './lookup.js';
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/pricing/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAYH,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAE1C,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA"}
@@ -0,0 +1,46 @@
1
+ /**
2
+ * language-models / pricing — lookup helpers.
3
+ *
4
+ * The single source of truth for cost computation across the Phase-2
5
+ * three-repo cascade. Throws on unknown slug/tier (per BYOK_GATEWAY_LIES
6
+ * memory: silent zero is the lying-gateway pattern; loud failure beats
7
+ * silent downgrade).
8
+ */
9
+ import type { HasPricingArgs, ModelPricing, PriceForArgs, PriceForResult } from './types.js';
10
+ /**
11
+ * Compute USD cost for a known generation. Throws on:
12
+ * - unknown slug (no rows for the slug at all)
13
+ * - unknown/unmodeled tier for the slug (no row for the (slug, tier) pair)
14
+ * - negative token counts (programming error — fail loud)
15
+ *
16
+ * Honors `contextTierBreakpoint` when present: if `inputTokens >=
17
+ * breakpoint`, the entire request is billed at `contextTierAbove` rates
18
+ * (matches Google's published billing model — the rate switches once the
19
+ * input crosses 200K, applied to the full request).
20
+ *
21
+ * `cachedInputTokens` is billed at `cachedInputPer1M` when defined,
22
+ * falling back to the regular input rate otherwise. Cached tokens are
23
+ * SUBTRACTED from `inputTokens` to compute the non-cached portion — i.e.
24
+ * `inputTokens` is the TOTAL input including any cached tokens.
25
+ */
26
+ export declare function priceFor(args: PriceForArgs): PriceForResult;
27
+ /**
28
+ * Returns the unique set of slugs in the table. Useful for adapters that
29
+ * want to validate caller-supplied model ids before dispatching.
30
+ */
31
+ export declare function listSlugs(): readonly string[];
32
+ /**
33
+ * Returns true if pricing exists for the given (slug, tier). Use this
34
+ * when you need a non-throwing existence check before calling
35
+ * `priceFor()` (e.g. a metering middleware that wants to fall back to
36
+ * "unknown cost" telemetry rather than throwing on a not-yet-registered
37
+ * model).
38
+ */
39
+ export declare function hasPricing(args: HasPricingArgs): boolean;
40
+ /**
41
+ * Returns all pricing rows for a slug, across all tiers. Useful for
42
+ * tooling that wants to display "this model has standard + batch tiers"
43
+ * in a UI.
44
+ */
45
+ export declare function rowsForSlug(slug: string): readonly ModelPricing[];
46
+ //# sourceMappingURL=lookup.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lookup.d.ts","sourceRoot":"","sources":["../../src/pricing/lookup.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,KAAK,EACV,cAAc,EACd,YAAY,EACZ,YAAY,EACZ,cAAc,EAEf,MAAM,YAAY,CAAA;AAEnB;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,YAAY,GAAG,cAAc,CAoC3D;AAED;;;GAGG;AACH,wBAAgB,SAAS,IAAI,SAAS,MAAM,EAAE,CAI7C;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,cAAc,GAAG,OAAO,CAExD;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,YAAY,EAAE,CAEjE"}
@@ -0,0 +1,94 @@
1
+ /**
2
+ * language-models / pricing — lookup helpers.
3
+ *
4
+ * The single source of truth for cost computation across the Phase-2
5
+ * three-repo cascade. Throws on unknown slug/tier (per BYOK_GATEWAY_LIES
6
+ * memory: silent zero is the lying-gateway pattern; loud failure beats
7
+ * silent downgrade).
8
+ */
9
+ import { PRICING_TABLE } from './table.js';
10
+ /**
11
+ * Compute USD cost for a known generation. Throws on:
12
+ * - unknown slug (no rows for the slug at all)
13
+ * - unknown/unmodeled tier for the slug (no row for the (slug, tier) pair)
14
+ * - negative token counts (programming error — fail loud)
15
+ *
16
+ * Honors `contextTierBreakpoint` when present: if `inputTokens >=
17
+ * breakpoint`, the entire request is billed at `contextTierAbove` rates
18
+ * (matches Google's published billing model — the rate switches once the
19
+ * input crosses 200K, applied to the full request).
20
+ *
21
+ * `cachedInputTokens` is billed at `cachedInputPer1M` when defined,
22
+ * falling back to the regular input rate otherwise. Cached tokens are
23
+ * SUBTRACTED from `inputTokens` to compute the non-cached portion — i.e.
24
+ * `inputTokens` is the TOTAL input including any cached tokens.
25
+ */
26
+ export function priceFor(args) {
27
+ const { slug, tier, inputTokens, outputTokens, cachedInputTokens } = args;
28
+ if (inputTokens < 0 || outputTokens < 0 || (cachedInputTokens ?? 0) < 0) {
29
+ throw new RangeError(`priceFor() requires non-negative token counts; got input=${inputTokens}, output=${outputTokens}, cachedInput=${cachedInputTokens ?? 0}`);
30
+ }
31
+ const row = findRow(slug, tier);
32
+ // Pick the right rate block based on context-tier breakpoint.
33
+ const useAbove = typeof row.contextTierBreakpoint === 'number' &&
34
+ row.contextTierAbove !== undefined &&
35
+ inputTokens >= row.contextTierBreakpoint;
36
+ const block = useAbove
37
+ ? row.contextTierAbove
38
+ : {
39
+ inputPer1M: row.inputPer1M,
40
+ outputPer1M: row.outputPer1M,
41
+ cachedInputPer1M: row.cachedInputPer1M,
42
+ };
43
+ const cachedTok = Math.min(cachedInputTokens ?? 0, inputTokens);
44
+ const nonCachedInputTok = inputTokens - cachedTok;
45
+ const cachedRate = block.cachedInputPer1M ?? block.inputPer1M;
46
+ const inputUsd = (nonCachedInputTok / 1_000_000) * block.inputPer1M + (cachedTok / 1_000_000) * cachedRate;
47
+ const outputUsd = (outputTokens / 1_000_000) * block.outputPer1M;
48
+ const totalUsd = inputUsd + outputUsd;
49
+ return { inputUsd, outputUsd, totalUsd };
50
+ }
51
+ /**
52
+ * Returns the unique set of slugs in the table. Useful for adapters that
53
+ * want to validate caller-supplied model ids before dispatching.
54
+ */
55
+ export function listSlugs() {
56
+ const set = new Set();
57
+ for (const row of PRICING_TABLE)
58
+ set.add(row.slug);
59
+ return Array.from(set);
60
+ }
61
+ /**
62
+ * Returns true if pricing exists for the given (slug, tier). Use this
63
+ * when you need a non-throwing existence check before calling
64
+ * `priceFor()` (e.g. a metering middleware that wants to fall back to
65
+ * "unknown cost" telemetry rather than throwing on a not-yet-registered
66
+ * model).
67
+ */
68
+ export function hasPricing(args) {
69
+ return PRICING_TABLE.some((row) => row.slug === args.slug && row.tier === args.tier);
70
+ }
71
+ /**
72
+ * Returns all pricing rows for a slug, across all tiers. Useful for
73
+ * tooling that wants to display "this model has standard + batch tiers"
74
+ * in a UI.
75
+ */
76
+ export function rowsForSlug(slug) {
77
+ return PRICING_TABLE.filter((row) => row.slug === slug);
78
+ }
79
+ // ---------------------------------------------------------------------------
80
+ // Internal
81
+ // ---------------------------------------------------------------------------
82
+ function findRow(slug, tier) {
83
+ const slugRows = PRICING_TABLE.filter((row) => row.slug === slug);
84
+ if (slugRows.length === 0) {
85
+ throw new Error(`Unknown model slug: '${slug}'. Known slugs: ${listSlugs().slice(0, 8).join(', ')}${listSlugs().length > 8 ? `, ...(${listSlugs().length - 8} more)` : ''}`);
86
+ }
87
+ const tierRow = slugRows.find((row) => row.tier === tier);
88
+ if (tierRow === undefined) {
89
+ const availableTiers = slugRows.map((row) => row.tier);
90
+ throw new Error(`No '${tier}' pricing for slug '${slug}'. Available tiers: ${availableTiers.join(', ')}`);
91
+ }
92
+ return tierRow;
93
+ }
94
+ //# sourceMappingURL=lookup.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lookup.js","sourceRoot":"","sources":["../../src/pricing/lookup.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAS1C;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,QAAQ,CAAC,IAAkB;IACzC,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,WAAW,EAAE,YAAY,EAAE,iBAAiB,EAAE,GAAG,IAAI,CAAA;IAEzE,IAAI,WAAW,GAAG,CAAC,IAAI,YAAY,GAAG,CAAC,IAAI,CAAC,iBAAiB,IAAI,CAAC,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AI,KAAK,IAAI,CAAC,CAAA;IACzD,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QAC1B,MAAM,cAAc,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;QACtD,MAAM,IAAI,KAAK,CACb,OAAO,IAAI,uBAAuB,IAAI,uBAAuB,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CACzF,CAAA;IACH,CAAC;IACD,OAAO,OAAO,CAAA;AAChB,CAAC"}
@@ -0,0 +1,46 @@
1
+ /**
2
+ * language-models / pricing — canonical model pricing table.
3
+ *
4
+ * All rates are USD per 1,000,000 tokens (per1M form, NOT per1k). Sourced
5
+ * from public list prices on 2026-05-07. Where prior repo tables disagree,
6
+ * the most-recently-updated source wins:
7
+ *
8
+ * - **Vertex Gemini 3.x**: startup-builder/packages/llm-vertex-batch
9
+ * (sb-srnl 2026-05-07 verified) is canonical for the flex/batch tier.
10
+ * startup-builder/packages/llm-vertex (same date) is canonical for
11
+ * standard interactive pricing.
12
+ * - **Vertex Gemini 2.5**: startup-builder/llm-vertex-batch is canonical
13
+ * (icps's `gemini-2.5-flash` rates were ~4× off — likely transposed
14
+ * from a different SKU).
15
+ * - **Bedrock Anthropic**: startup-builder/packages/llm-bedrock is the
16
+ * most-recently-curated table (matches AWS Bedrock public pricing on
17
+ * 2026-05-07).
18
+ * - **Embeddings**: icps/packages/llm/pricing.ts is the source for
19
+ * `gemini-embedding-2`. Bedrock has no first-party Gemini embedding
20
+ * SKU; Anthropic has no embedding SKU. Embedding lives under
21
+ * `aistudio/` since the cheapest path is the AI Studio API key (icps's
22
+ * 2026-05-07 fallback chain — the path startup-builder also uses).
23
+ *
24
+ * 200K-context tier breakpoint:
25
+ *
26
+ * Gemini 3.1 Pro Preview list pricing has a 2× rate above 200K input
27
+ * tokens. We model this with `contextTierBreakpoint: 200_000` +
28
+ * `contextTierAbove`. Anthropic Claude has flat pricing (no breakpoint).
29
+ * Gemini 2.5 + 3.x flash-lite + embedding SKUs are flat too.
30
+ *
31
+ * Adding a new SKU:
32
+ *
33
+ * 1. Append a new row keyed on `<provider>/<short-slug>` + `tier`. Both
34
+ * fields together form the lookup key; duplicates are caught by the
35
+ * table-integrity test.
36
+ * 2. If the SKU has a context-tier breakpoint, add
37
+ * `contextTierBreakpoint` + `contextTierAbove`. Otherwise omit them.
38
+ * 3. If both standard + batch tiers exist, add BOTH rows; downstream
39
+ * consumers select via `priceFor({ tier: ... })`. Vertex's batch tier
40
+ * is ~50% of standard; that's not enforced — declare the actual
41
+ * published rates.
42
+ * 4. Bump the package version (semver minor for additions).
43
+ */
44
+ import type { ModelPricing } from './types.js';
45
+ export declare const PRICING_TABLE: readonly ModelPricing[];
46
+ //# sourceMappingURL=table.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"table.d.ts","sourceRoot":"","sources":["../../src/pricing/table.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAE9C,eAAO,MAAM,aAAa,EAAE,SAAS,YAAY,EA4LhD,CAAA"}