3.0 KiB
3.0 KiB
OpenRouter Model Sync Script
Context
User wants a Python script that fetches all available models from the OpenRouter API (GET https://openrouter.ai/api/v1/models) and generates:
~/.litellm/config.yaml— LiteLLM proxy config with all matching models~/.env_keys.example— singleOPENROUTER_API_KEYentry
Filtering is controlled by a YAML config file with provider-level and model-name-pattern include/exclude rules. The script overwrites (not merges) the target files.
Files to Create
1. ~/.litellm/openrouter_sync.yaml — Filter config (with defaults/example)
# Which providers/models to include or exclude from OpenRouter.
# Filters are evaluated in order: include first, then exclude.
# Glob patterns supported (fnmatch-style).
filters:
providers:
include: [] # empty = all providers included
exclude: [] # e.g. ["openrouter", "auto"]
models:
include: [] # empty = all models included (after provider filter)
exclude: [] # e.g. ["*:free", "*-preview", "*/old-*"]
# Output paths (defaults shown)
output:
config_yaml: "~/.litellm/config.yaml"
env_keys: "~/.env_keys.example"
Filter logic:
providers.include: if non-empty, only models from these providers are kept. Provider = first path segment of model id (e.g.googlefromgoogle/gemini-2.0-flash).providers.exclude: remove models from these providers (applied after include).models.include: if non-empty, only model ids matching any of these glob patterns are kept.models.exclude: remove model ids matching any of these glob patterns.
2. ~/.litellm/sync_openrouter.py — Main script
Logic:
- Load filter config from
~/.litellm/openrouter_sync.yaml(create default if missing). GET https://openrouter.ai/api/v1/models— fetch all models.- Apply provider include/exclude filters.
- Apply model name include/exclude filters (fnmatch glob).
- Build LiteLLM
model_listentries:model_list: - model_name: google/gemini-2.0-flash # the OpenRouter model id litellm_params: model: openrouter/google/gemini-2.0-flash api_key: os.environ/OPENROUTER_API_KEY - Write
config.yaml(overwrite). - Write
.env_keys.examplewith single line:export OPENROUTER_API_KEY="sk-or-REPLACE_ME". - Print summary: total models fetched, filtered count, written count.
Dependencies: requests, pyyaml (both common; script will check and error clearly if missing).
No external packages beyond stdlib+requests+pyyaml. Uses fnmatch from stdlib for glob matching.
Verification
- Run
python ~/.litellm/sync_openrouter.pywith default (empty) filters — should populate config.yaml with all OpenRouter models. - Edit
openrouter_sync.yamlto exclude a provider, re-run, confirm those models are gone. - Verify
~/.env_keys.examplecontains the OPENROUTER_API_KEY line. - Validate generated
config.yamlwithpython -c "import yaml; yaml.safe_load(open('config.yaml'))".