# 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:
1. `~/.litellm/config.yaml` — LiteLLM proxy config with all matching models
2. `~/.env_keys.example` — single `OPENROUTER_API_KEY` entry

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)

```yaml
# 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. `google` from `google/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:**
1. Load filter config from `~/.litellm/openrouter_sync.yaml` (create default if missing).
2. `GET https://openrouter.ai/api/v1/models` — fetch all models.
3. Apply provider include/exclude filters.
4. Apply model name include/exclude filters (fnmatch glob).
5. Build LiteLLM `model_list` entries:
   ```yaml
   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
   ```
6. Write `config.yaml` (overwrite).
7. Write `.env_keys.example` with single line: `export OPENROUTER_API_KEY="sk-or-REPLACE_ME"`.
8. 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
1. Run `python ~/.litellm/sync_openrouter.py` with default (empty) filters — should populate config.yaml with all OpenRouter models.
2. Edit `openrouter_sync.yaml` to exclude a provider, re-run, confirm those models are gone.
3. Verify `~/.env_keys.example` contains the OPENROUTER_API_KEY line.
4. Validate generated `config.yaml` with `python -c "import yaml; yaml.safe_load(open('config.yaml'))"`.
