Skip to content

Commit 5bff81f

Browse files
committed
perf(pricing): flatten embedded models.dev snapshot
Regenerate the committed models.dev pricing snapshot as a flat map keyed by runtime model id instead of preserving upstream provider nesting. The runtime loader now accepts both the live provider-tree shape from models.dev and the flat embedded shape, so network fallback compatibility is unchanged while the checked-in snapshot shrinks from 97,159 bytes to 36,871 bytes. The release binary rebuilt successfully after the change at 2,845,920 bytes. Pricing tests cover the flat embedded shape and the existing offline fallback behaviour.
1 parent 106ab54 commit 5bff81f

3 files changed

Lines changed: 2501 additions & 5254 deletions

File tree

‎nix/models-dev-gen.ts‎

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
* reuse its own `generateCatalog` routine (the same code that backs
66
* https://models.dev/api.json) and then compact the result down to the
77
* Anthropic-relevant models and the few pricing fields ccusage consumes. The
8-
* output is committed to the repository and embedded at build time, so every
9-
* platform ships the identical, pinned data without any build-time network
10-
* access. Run via `just gen-models-dev-pricing` (see `nix/models-dev-pricing.nix`).
8+
* embedded output is a flat map keyed by runtime model id. The output is
9+
* committed to the repository and embedded at build time, so every platform
10+
* ships the identical, pinned data without any build-time network access. Run
11+
* via `just gen-models-dev-pricing` (see `nix/models-dev-pricing.nix`).
1112
*/
1213
import { generateCatalog } from './packages/core/src/generate.ts';
1314

@@ -22,14 +23,17 @@ type Cost = {
2223
};
2324
type Model = { id?: string; cost?: Cost; limit?: { context?: number | null } };
2425
type Provider = { models?: Record<string, Model> };
26+
type EmbeddedModel = {
27+
cost: Cost;
28+
limit?: { context: number };
29+
};
2530

2631
const { providers } = (await generateCatalog('.')) as {
2732
providers: Record<string, Provider>;
2833
};
2934

30-
const out: Record<string, { models: Record<string, unknown> }> = {};
31-
for (const [providerId, provider] of Object.entries(providers)) {
32-
const models: Record<string, unknown> = {};
35+
const out: Record<string, EmbeddedModel> = {};
36+
for (const provider of Object.values(providers)) {
3337
for (const [modelId, model] of Object.entries(provider.models ?? {})) {
3438
// models.dev also exposes the canonical id under `id`; match either so
3539
// provider-prefixed aliases (e.g. us.anthropic.*) are kept too.
@@ -41,23 +45,22 @@ for (const [providerId, provider] of Object.entries(providers)) {
4145
if (cost.input == null || cost.output == null) {
4246
continue;
4347
}
44-
const entry: Record<string, unknown> = {};
45-
if (model.id != null) {
46-
entry.id = model.id;
48+
const pricingKey = model.id ?? modelId;
49+
if (out[pricingKey] != null) {
50+
continue;
4751
}
48-
entry.cost = {
49-
input: cost.input,
50-
output: cost.output,
51-
...(cost.cache_read != null ? { cache_read: cost.cache_read } : {}),
52-
...(cost.cache_write != null ? { cache_write: cost.cache_write } : {}),
52+
const entry: EmbeddedModel = {
53+
cost: {
54+
input: cost.input,
55+
output: cost.output,
56+
...(cost.cache_read != null ? { cache_read: cost.cache_read } : {}),
57+
...(cost.cache_write != null ? { cache_write: cost.cache_write } : {}),
58+
},
5359
};
5460
if (model.limit?.context != null) {
5561
entry.limit = { context: model.limit.context };
5662
}
57-
models[modelId] = entry;
58-
}
59-
if (Object.keys(models).length > 0) {
60-
out[providerId] = { models };
63+
out[pricingKey] = entry;
6164
}
6265
}
6366

0 commit comments

Comments
 (0)