Update LLM Pricing #9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Update LLM Pricing | |
| on: | |
| schedule: | |
| - cron: '0 6 * * 1' # Every Monday at 6am UTC | |
| workflow_dispatch: # Manual trigger | |
| jobs: | |
| update: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| - name: Fetch latest pricing from OpenRouter | |
| run: | | |
| node -e " | |
| const https = require('https'); | |
| function fetch(url) { | |
| return new Promise((resolve, reject) => { | |
| https.get(url, (res) => { | |
| let data = ''; | |
| res.on('data', chunk => data += chunk); | |
| res.on('end', () => resolve(JSON.parse(data))); | |
| }).on('error', reject); | |
| }); | |
| } | |
| (async () => { | |
| const response = await fetch('https://openrouter.ai/api/v1/models'); | |
| const models = response.data; | |
| const providerMap = {}; | |
| const processed = models | |
| .map(m => { | |
| const parts = m.id.split('/'); | |
| const provider = parts[0]; | |
| const providerName = provider.charAt(0).toUpperCase() + provider.slice(1).replace(/-/g, ' '); | |
| const input = m.pricing?.prompt ? parseFloat(m.pricing.prompt) * 1e6 : 0; | |
| const output = m.pricing?.completion ? parseFloat(m.pricing.completion) * 1e6 : 0; | |
| const entry = { | |
| id: m.id, | |
| name: m.name, | |
| provider: providerName, | |
| input_per_million: Math.round(input * 100) / 100, | |
| output_per_million: Math.round(output * 100) / 100, | |
| context_window: m.context_length || null, | |
| max_output: m.top_provider?.max_completion_tokens || null, | |
| is_free: input === 0 && output === 0, | |
| modalities: m.architecture?.modality?.split('+>')[0]?.split('+') || ['text'], | |
| type: m.architecture?.tokenizer?.includes('embedding') ? 'embedding' : 'chat' | |
| }; | |
| if (!providerMap[entry.provider]) providerMap[entry.provider] = []; | |
| providerMap[entry.provider].push(entry); | |
| return entry; | |
| }) | |
| .sort((a, b) => a.provider.localeCompare(b.provider) || a.name.localeCompare(b.name)); | |
| const today = new Date().toISOString().split('T')[0]; | |
| const pricing = { | |
| last_updated: today, | |
| source: 'OpenRouter API via BenchGecko', | |
| models_count: processed.length, | |
| url: 'https://benchgecko.ai', | |
| models: processed | |
| }; | |
| // Sort provider map keys | |
| const sortedProviders = {}; | |
| Object.keys(providerMap).sort().forEach(k => { | |
| sortedProviders[k] = providerMap[k].sort((a, b) => a.name.localeCompare(b.name)); | |
| }); | |
| require('fs').writeFileSync('pricing.json', JSON.stringify(pricing, null, 2)); | |
| require('fs').writeFileSync('pricing-by-provider.json', JSON.stringify(sortedProviders, null, 2)); | |
| console.log('Updated: ' + processed.length + ' models from ' + Object.keys(sortedProviders).length + ' providers'); | |
| })(); | |
| " | |
| - name: Update badge in README | |
| run: | | |
| MODELS=$(node -e "console.log(require('./pricing.json').models_count)") | |
| DATE=$(node -e "console.log(require('./pricing.json').last_updated)") | |
| sed -i "s/last_updated-[0-9\-]*/last_updated-${DATE}/" README.md | |
| sed -i "s/models-[0-9]*/models-${MODELS}/" README.md | |
| - name: Commit and push if changed | |
| run: | | |
| git config user.name "BenchGecko Bot" | |
| git config user.email "hello@benchgecko.ai" | |
| git add -A | |
| if git diff --staged --quiet; then | |
| echo "No changes" | |
| else | |
| git commit -m "chore: update pricing data ($(date +%Y-%m-%d))" | |
| git push | |
| fi |