# API key resolution

| Provider | Credential source |
| --- | --- |
| LLM provider with a key or sign-in | The provider's list in the AuthStore under `~/.openprogram`: its sign-ins and its company's keys, written by Settings or `openprogram providers keys <provider> add` |
| A key variable in the Worker's environment | A read-only row of that same list (`env:<VAR>`), read from the environment at each use; removed by unsetting it |
| Amazon Bedrock | AWS credential chain; sentinel `"<authenticated>"`, no bearer key |
| Google Vertex | GCP ADC plus project and location; same sentinel |
| `config.json` | Never read for a key. What `api_keys` held moves into the store once, at start ([Companies, ways and credentials](https://fzkuji.github.io/OpenProgram/docs/reference/design/providers/auth/credential-model.html)). |

One list means the runtime, catalog and status surfaces cannot disagree, and a request chooses among stored keys and environment keys the same way.

## Calls

| Function | Answers |
| --- | --- |
| `selection.with_credential(model, opts, attempt)` | Every model request: chooses, prepares and switches credentials ([Provider keys and failover](https://fzkuji.github.io/OpenProgram/docs/reference/design/providers/auth/unified-account-management.html)) |
| `resolve_provider_key(provider)` | A key-shaped credential (a key, an environment variable, a helper command) or the sentinel, else `None`. Sign-in tokens are left out, since these callers send `x-api-key` or Bearer and an access token would 401. |
| `is_configured(provider_id)` | A key, or a satisfied cloud chain |
| `resolve_api_key_with_auth_store(provider_id)` | Model listing and probes: the bearer of any kind a request would use now, through `resolve_api_key_sync` |
| `env_vars_for(provider_id)` | The provider's key variables, first preferred, from its connection entry; falls back to models.dev |
| `provider_id_for_env_var(env_var)` | Reverse, the first listed owner; `None` for non-LLM keys |

The names live in one table, `auth/login/login_method_registry.py`, beside each provider's ways to connect. `openprogram/models/providers/settings/env_api_keys.py` answers from it, so runtime and server import it without a cycle.

**Order inside `resolve_api_key_sync`**

1. The credential a request would choose now (`selection.choose`, without counting a use), then the other usable ones in order
2. Each made ready by `CredentialProvider.prepare` (a sign-in near expiry is refreshed) and read by `resolve_connection`
3. `resolve_provider_key`: the cloud sentinels

`None` lets the caller show a sign-in prompt or go keyless for local endpoints. `AuthCredentialProcessError` always propagates: a broken helper is shown by name, not replaced by another key.

## Verification

`tests/unit/providers/auth/test_api_key_resolution.py` pins that an environment key resolves as a row of the list, the cloud sentinels, the reverse map and key labels.

## Implementation status

All of the above is in place. Not built: a `Provider` metadata dataclass (id, env_vars, kind, base_url, default_api) folding this table together with [credential validation](https://fzkuji.github.io/OpenProgram/docs/reference/design/providers/auth/credential-validation-unification.html)'s KIND table and `_PROVIDER_DEFAULT_API`.

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