-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathsync-pricing.js
More file actions
217 lines (184 loc) · 6.83 KB
/
sync-pricing.js
File metadata and controls
217 lines (184 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env node
/**
* Sync pricing.json from models.dev/api.json
*
* Usage:
* node sync-pricing.js # dry-run: show diff only
* node sync-pricing.js --write # write changes to pricing.json
*/
const https = require('https');
const fs = require('fs');
const path = require('path');
const MODELS_DEV_URL = 'https://models.dev/api.json';
const PRICING_PATH = path.join(__dirname, 'pricing.json');
// Providers we care about (others are either free or irrelevant)
const PROVIDERS = ['anthropic', 'openai', 'google', 'xai', 'deepseek'];
// ── Fetch models.dev/api.json ──────────────────────────────────
function fetchModels() {
return new Promise((resolve, reject) => {
https.get(MODELS_DEV_URL, (res) => {
if (res.statusCode !== 200) {
reject(new Error(`HTTP ${res.statusCode}`));
res.resume();
return;
}
let data = '';
res.on('data', (chunk) => { data += chunk; });
res.on('end', () => {
try { resolve(JSON.parse(data)); }
catch (e) { reject(e); }
});
}).on('error', reject);
});
}
// ── Convert model id → pricing.json key ─────────────────────────
// "claude-opus-4-5-20251101" → "claude-opus-4-5-20251101"
// "gpt-5.2-pro" → "gpt-5-2-pro"
// "gemini-2.5-flash" → "gemini-2-5-flash"
function toPricingKey(id) {
// Dots → dashes (gpt-5.2 → gpt-5-2)
return id.replace(/\./g, '-').toLowerCase();
}
// ── Main ───────────────────────────────────────────────────────
async function main() {
const writeMode = process.argv.includes('--write');
console.log('Fetching models.dev/api.json...');
const apiData = await fetchModels();
// Load current pricing.json
const current = JSON.parse(fs.readFileSync(PRICING_PATH, 'utf-8'));
const meta = current._meta;
// Build new pricing map from models.dev
// API shape: { providerName: { models: { modelId: { cost: {...}, ... } } } }
const remote = {};
let totalModels = 0;
let skipped = 0;
for (const provider of PROVIDERS) {
const providerData = apiData[provider];
if (!providerData || !providerData.models) continue;
for (const [modelId, m] of Object.entries(providerData.models)) {
totalModels++;
if (!m.cost) { skipped++; continue; }
const input = m.cost.input || 0;
const output = m.cost.output || 0;
if (input === 0 && output === 0) { skipped++; continue; }
const key = toPricingKey(modelId);
const entry = {
input, output,
// Only include cache fields when the API actually provides them
// (many providers don't report cache_write / cache_read)
cacheRead: m.cost.cache_read != null ? m.cost.cache_read : undefined,
cacheWrite: m.cost.cache_write != null ? m.cost.cache_write : undefined,
};
// Skip preview/experimental variants if the base model already exists
if (key.endsWith('-preview') || key.endsWith('-experimental')) {
const base = key.replace(/-(preview|experimental)$/, '');
if (!remote[base]) {
remote[key] = entry;
}
continue;
}
remote[key] = entry;
}
}
console.log(` ${totalModels} models from ${PROVIDERS.join(', ')}`);
console.log(` ${Object.keys(remote).length} priced models (${skipped} skipped)\n`);
// Compare with current
const added = [];
const updated = [];
const removed = [];
const unchanged = [];
// Check remote models against current
for (const [key, pricing] of Object.entries(remote)) {
if (key.startsWith('_')) continue;
const cur = current[key];
const merged = mergePricing(cur, pricing);
if (!cur) {
added.push({ key, pricing: merged });
} else {
const diff = pricingDiff(cur, pricing);
if (diff) {
updated.push({ key, old: cur, new: merged, diff });
} else {
unchanged.push(key);
}
}
}
// Check current models not in remote (potential removals)
for (const key of Object.keys(current)) {
if (key.startsWith('_')) continue;
if (!remote[key]) {
removed.push(key);
}
}
// Report
if (added.length > 0) {
console.log(`\x1b[32m+ ${added.length} new models:\x1b[0m`);
for (const { key, pricing } of added) {
console.log(` ${key} in=$${pricing.input} out=$${pricing.output} cr=$${pricing.cacheRead} cw=$${pricing.cacheWrite}`);
}
console.log('');
}
if (updated.length > 0) {
console.log(`\x1b[33m~ ${updated.length} price changes:\x1b[0m`);
for (const u of updated) {
console.log(` ${u.key} ${u.diff}`);
}
console.log('');
}
if (removed.length > 0) {
console.log(`\x1b[31m- ${removed.length} models in pricing.json but not in models.dev (keeping):\x1b[0m`);
for (const key of removed) {
console.log(` ${key}`);
}
console.log('');
}
console.log(` ${unchanged.length} unchanged`);
if (added.length === 0 && updated.length === 0) {
console.log('\npricing.json is up to date!');
return;
}
if (!writeMode) {
console.log('\nDry run — use --write to apply changes');
return;
}
// Build new pricing.json
const result = { _meta: meta };
// Merge: keep current (including manually-added aliases), apply updates, add new
for (const key of Object.keys(current)) {
if (key.startsWith('_')) continue;
const upd = updated.find(u => u.key === key);
result[key] = upd ? upd.new : current[key];
}
for (const { key, pricing } of added) {
result[key] = pricing;
}
// Update lastVerified date
result._meta = { ...meta, lastVerified: new Date().toISOString().slice(0, 7) };
fs.writeFileSync(PRICING_PATH, JSON.stringify(result, null, 2) + '\n');
console.log(`\n\x1b[32m✓ pricing.json updated (${added.length} added, ${updated.length} updated)\x1b[0m`);
}
function pricingDiff(a, b) {
const fields = ['input', 'output', 'cacheRead', 'cacheWrite'];
const diffs = [];
for (const f of fields) {
// Skip comparison when remote doesn't provide this field
if (b[f] === undefined) continue;
const av = a[f] || 0;
const bv = b[f] || 0;
if (av !== bv) diffs.push(`${f}: $${av} → $${bv}`);
}
return diffs.length > 0 ? diffs.join(', ') : null;
}
// Merge remote pricing into existing, preserving local cache values when remote is undefined
function mergePricing(existing, remote) {
return {
input: remote.input,
output: remote.output,
cacheRead: remote.cacheRead !== undefined ? remote.cacheRead : (existing?.cacheRead || 0),
cacheWrite: remote.cacheWrite !== undefined ? remote.cacheWrite : (existing?.cacheWrite || 0),
};
}
main().catch((err) => {
console.error('Error:', err.message);
process.exit(1);
});