# Credential validation

`validate_credential` in `models/listing/credentials.py` serves save, verify, connectivity, CLI, TUI rows and setup. Validating never runs a model unless a model is named.

| Layer | Question | Cost | Runs |
| --- | --- | --- | --- |
| 0 presence | Credential present, not the mask, OAuth unexpired? | offline | always |
| 1 auth | Did the auth endpoint accept it? | one GET, 0 tokens | the normal check |
| 2 model | Can this named model be reached now? | one inference ping | only with `model=` |

## Probes

| Kind | Providers | Layer-1 probe |
| --- | --- | --- |
| `openai_bearer` | every provider not below | `GET {base}/models`, Bearer |
| `openrouter_key` | openrouter | `GET {base}/key`; `/models` is public there; shows balance |
| `anthropic_native` | anthropic | `GET https://api.anthropic.com/v1/models`, `x-api-key`, `anthropic-version: 2023-06-01` |
| `anthropic_compat` | other `anthropic-messages` providers: minimax, minimax-cn | `GET {base}/v1/models`, same headers, own base URL |
| `google_query` | google | `GET …/v1beta/models?key=…&pageSize=1` |
| `oauth` | openai-codex, gemini-subscription, github-copilot, xai-subscription, opencode, opencode-go | `CredentialProvider.acquire_sync(pid).status`; no network beyond an optional refresh |
| `cloud` | amazon-bedrock, google-vertex, azure-openai-responses | `not_applicable` |

## Results

| Outcome | `status` | Message |
| --- | --- | --- |
| 200, proven usable | `valid` | none |
| 402, or any 4xx whose body says the credit is spent (checked first, so a 401 `CreditsError` lands here) | `valid_no_balance` | Key works — account has no balance. Add funds. |
| 401, 403, otherwise | `invalid_credential` | Key rejected (HTTP 401). and the provider's own sentence ("This API key has expired"), with anything shaped like a key cut out |
| 429, 5xx, no endpoints, data policy (layer 2) | `valid_model_unavailable` | Model X is unavailable right now |
| Layer 1 200, then another 4xx on the layer-2 test message (a parameter the model refuses) | `valid_model_unavailable` (the key works) | Key accepted; the test message failed: HTTP 400. and the provider's sentence |
| Transport error, ambiguous | `unknown` | Couldn't reach the provider. Saved anyway; will validate on first use. |
| The provider has no address here (a custom provider no longer configured) | `unknown`, key check `not_configured`; its rows read Provider not set up | No base URL resolvable. |
| No credential | `missing` | none |
| No key concept | `not_applicable` | none |

`ok` and the green chip mean exactly `valid`. A layer-1 200 proves auth, not balance: only OpenRouter `GET /key` with credit, a layer-2 ping or a live 200 chat may set the credential's status to `ready`. A sign-in that is `needs_login`: `openprogram providers login <pid>`. A non-JSON 200 from a custom endpoint is not valid.

A provider whose requests use the Responses API is pinged on `/responses` with `max_output_tokens`, the way its requests go, not on chat completions.

## Surfaces

| Surface | Use |
| --- | --- |
| `POST /api/config` | Saves first, then layer 1; a slow provider never blocks saving |
| `POST /api/config/verify` | Same call with explicit `api_key`, synchronous |
| `POST /api/providers/{name}/validate`, `/test` | Layer 1; `{model}` adds layer 2 |
| `GET /api/providers/auth-status` | `provider_auth_status`, batch for status rows |
| Status rows | `Configured` (layer 0) and `Validated` (cached layer 1) |

Results are cached 60 s per provider and whether a model was named; `use_cache=False` or `refresh=True` bypasses. The secret is never stored or returned. Not a quota dashboard.

## Adding a provider

Declare its kind in `_kind_for`; `openai_bearer` needs nothing. Anthropic-wire third parties need three things to agree, pinned by `test_model_fetch_routing.py`:

- `_kind_for` returns `anthropic_compat`
- `catalog/metadata.py default_api_for` resolves `anthropic-messages`
- `listing/fetchers/__init__.py` routes to the base-URL-aware Anthropic fetcher

## Probe log

`models/listing/probe_log.py` marks `test_provider` (`connectivity`), `validate_credential` (`credential`) and `fetch_and_normalize` (`model_list`). Requests through `safe_http.observing_requests` become `probe_events` rows: provider, kind, method, origin, path (never the query), status or error type, duration. Answered model lists are skipped; a nested probe takes the outer kind. Never counted as usage. Kept 30 days; `GET /api/providers/{name}/probes?days=7`, newest first. Follows magpie's probelog (yetone/magpie, MIT).

**Signature**

```
def validate_credential(provider_id, *, api_key=None, model=None,
                        timeout=15.0, use_cache=True) -> CredentialResult
# CredentialResult: provider_id status ok kind via http_status latency_ms model detail cached
```

`_validate_api_key` and `test_provider` delegate, keeping their return shapes.

**Peers**

| Project | Design |
| --- | --- |
| OpenClaw | UI calls `models.authStatus`: snapshot cached 60 s with `refresh: true`, validated off usage endpoints; health rollup `ok`, `expiring`, `expired`, `missing`, `static`; secrets redacted |
| opencode | No check at login; the first request shows a bad key; catalog from models.dev; `provider/error.ts` maps errors to remedies |

Taken: the taxonomy, 60 s cache with refresh, redaction, layering, one message mapper. OpenProgram keeps a save-time indicator, so unlike both it probes explicitly.

**Implementation status**

Not in place:

- Fetchers call `validate_credential(pid)` first, not their own presence checks
- `check_providers()` exposes a cached `validated`

Open:

- Bulk save: automatic layer 1 or explicit Verify, with throttling
- Anthropic OAuth token probe headers
- openai-codex has no auth-only list endpoint (403); the default check is structural, an end-to-end probe is a layer-2 `/responses` ping

Test matrix, outcome by kind: `200`, `401`, `402`, OpenRouter public `/models`, missing `anthropic-version`, OAuth `needs_login`, layer-2 `429`, offline, no key.

[Credential status](https://fzkuji.github.io/OpenProgram/docs/reference/design/providers/auth/credential-status-redesign.html)
