← Back to Gists

gen_openrouter_pricing.py - Fetch Live OpenRouter Pricing

📝 Python
retoor
retoor · Level 54802 ·

Fetches complete model catalog from OpenRouter API, parses pricing/context/modalities, outputs sorted Markdown table of all models.

Python
#!/usr/bin/env python3
import json, sys

data = json.load(sys.stdin)
models = data.get('data', [])

lines = ['# OpenRouter AI Model Pricing โ€” Full Reference\n']
lines.append('Compiled from [openrouter.ai](https://openrouter.ai) on September 9, 2026.\n')
lines.append(f'**Total models available: {len(models)}**\n')
lines.append('')
lines.append('| Model Name | Slug | Input/M | Output/M | Context | Modalities | Provider |\n|---|---|---|---|---|---|---|')

for m in sorted(models, key=lambda x: float(x['pricing'].get('prompt', 0)) or 0):
    name = m.get('name', '')
    slug = m.get('id', '')
    pricing = m.get('pricing', {})
    prompt_price = pricing.get('prompt', '0')
    completion_price = pricing.get('completion', '0')
    ctx = m.get('context_length', 0)
    arch = m.get('architecture', {})
    modality = arch.get('modality', 'text->text')
    
    if prompt_price == '-1':
        inp_str = 'Custom'
    elif float(prompt_price) == 0:
        inp_str = 'Free'
    else:
        inp_str = f'${float(prompt_price)*1000000:.4f}'
    
    if float(completion_price) == 0:
        out_str = 'Free'
    else:
        out_str = f'${float(completion_price)*1000000:.4f}'
    
    if ctx >= 1000000:
        ctx_str = f'{ctx/1000000:.1f}M'
    elif ctx >= 1000:
        ctx_str = f'{ctx/1000:.0f}K'
    else:
        ctx_str = str(ctx)
    
    provider = slug.split('/')[0] if '/' in slug else 'N/A'
    
    lines.append(f'| {name} | `{slug}` | {inp_str} | {out_str} | {ctx_str} | {modality} | {provider} |')

print('\n'.join(lines))

Comments

No comments yet. Start the discussion.