# Model catalog

1. Browse: official list and models.dev, memory only
2. enable
3. config.json: full spec rows with provider.json fields
4. ENABLED_MODELS: get_model, chat picker, agents, failover

Selectable in chat equals enabled equals resolvable: there is one list. No big files, nothing stale, and the package directory stays read-only.

## Who writes what

| Author | Location | Content |
| --- | --- | --- |
| Humans, in git | `providers/adapters/<p>/provider.json`, plus `<p>.py` for dedicated protocols | endpoints, thinking, cache, overrides |
| The program | `config.json` → `providers.<p>.models` | enabled specs, keys, user state |
| Network | models.dev, official `/v1/models` | browsing only |

Providers with no directory work through models.dev. `catalog/enabled_models.py` is the registry, `settings/storage.py` persists config, `settings/thinking_spec.py` holds thinking, `models/listing/` is what interfaces read. Nothing else is named a catalog.

## `provider.json`

Every field optional. If a machine can obtain a field, a human does not write it, so there is no model list.

| Field | Purpose | Default |
| --- | --- | --- |
| `endpoints` | named api and base_url groups | models.dev base_url, OpenAI-compatible |
| `thinking` | wire_format, effort maps, per-model levels: [Thinking effort](https://fzkuji.github.io/OpenProgram/docs/reference/design/providers/models/thinking-effort.html) | low, medium, high |
| `cache` | prompt caching | none |
| `model_overrides` | per-model headers, compat, `endpoint`, `key_prefix`; folded into the spec at enable time | none |

Directories use underscores (`amazon_bedrock/`); `id` keeps hyphens. One service with two protocols is two endpoints of one provider.

## Browse

Provider list: `provider.json` providers plus the models.dev index, with names and status only. Model list: queried for one provider, by `_browse_models_with_error`: the official source when credentialed, models.dev filling gaps or standing in, else empty.

A model the official list names and models.dev does not takes what it accepts (images, files, tools) from the listed model its id extends, `grok-4.7-build-fast` from `grok-4.7`: an official list seldom says what a model accepts, and a row with no word on it is text only. Price, context and name are never carried over.

| Source shape | Fetcher |
| --- | --- |
| Standard `/v1/models` | `listing/fetchers/openai_compat.py` |
| Anthropic, per-model capabilities | `adapters/anthropic/list_models.py` |
| Account-level private | `adapters/openai_codex/list_models.py`, `xai_subscription/list_models.py` |
| Vendor-specific | `google` (query key), `amazon_bedrock` (SigV4), `github_copilot` (session bearer), `deepseek` (ids only, enriched) |

A provider that departs from standard ships `list_models.py` exposing `fetch(provider_id, timeout)`; `_load_fetcher` imports it by directory, no central edit. Success: `list[dict]` with id and name. Failure: `{"error": …}`. `fetch_and_normalize` is the one normalisation point.

Ordinary results use a short memory cache. Subscription catalogues also keep an atomic last-known-good copy: a failed request can show stale rows, but only a non-empty official catalogue can retire models.

The models.dev catalogue is kept in memory and on disk, fresh for an hour. The disk copy's age is its file's: every command is a new process, and one that took the file for stale whatever its age downloaded the catalogue again and waited about three seconds for it on its way out.

## Enable, disable, refresh

| Action | Effect |
| --- | --- |
| Enable (`toggle_model`) | Browse row, overrides, endpoint api and base_url, derived thinking levels go in `providers.<p>.models`; the registry reloads |
| Disable | Deletes the row; a manual row stays with `enabled: false`. Subscription providers record the id in `disabled_models`. |
| Refresh | Re-browses enabled models and overwrites their specs |
| Manual model | Same list, `source: "manual"` |
| Subscription catalogue | After login, worker start when stale, every 6 h, manual Refresh: new ids enabled, changes refreshed, removed retired, disables kept. A change broadcasts `provider_models_changed`; web and desktop re-read, reconnecting clients too. |

## Use

| Reader | Source |
| --- | --- |
| Backend | `ENABLED_MODELS`, key `<prefix>/<id>`, through `get_model`, `get_providers`, `get_models`; aliases on a miss |
| `GET /api/providers/list` | Provider list |
| `GET /api/providers/{name}/models` | That provider's cached browse and enabled flags; `POST …/fetch-models` queries now |
| `GET /api/models/enabled` | Chat picker: `ENABLED_MODELS` as is |
| `interfaces/server/thinking_effort.py` | Levels from enabled rows |

A failover or agent reference to a non-enabled model is a configuration error pointing at Settings. Old sessions keep their history but cannot continue on a deleted model.

An unknown id is refused with the nearest ones (`catalog/suggestions.py`): "Unknown model 'openai-codex':'gpt6 sol'; did you mean openai-codex:gpt-6.1-sol, …?". Names compare by words: display-name word 4, id word 3; a version or provider id counts only beside those. Follows magpie (yetone/magpie, MIT).

## Invariants

1. The only persisted model data is enabled specs in config, plus the subscription last-known-good copy.
2. Ordinary browsing is never written to a file.
3. Human data in git, program data in user config.
4. `provider.json` has only what machines cannot obtain.
5. `openprogram.models.providers` never imports `openprogram.interfaces.server`; the listing imports providers, never the reverse.
6. `<prefix>/<id>`, alias fallback and `key_prefix` are preserved; the registry stays one mutable dict.
7. One each: `fetch_and_normalize` (row shape), `_upsert_spec_row` with `_normalize_spec_row` (into config), `_build_model_from_row` (into `Model`).

**What a change must preserve**

- A config row resolves through `get_model` to the same `Model` for every provider.
- `google-gemini-cli/*` and `gemini-subscription/*` are distinct keys; rows carry their own.
- `cost`, multimodal `input`, `headers` (copilot) and `compat` travel in the spec.
- Multi-wire providers need one exec per `(api, base_url, headers, compat)`.
- `test_provider_wire_invariants.py` and `test_model_fetch_routing.py` stay green.

[Fast tier](https://fzkuji.github.io/OpenProgram/docs/reference/design/providers/models/fast-tier.html)
