# Authentication

OpenProgram requires safe, lawful and compliant use of Anthropic services: the official API, legitimately obtained API keys, API-account billing, and calls that comply with Anthropic's terms of service and usage policy.

## Storage

`~/.openprogram/auth/<provider>/<account>.json`, mode 0600. With `--profile <name>` the root is `~/.openprogram-<name>/`.

Runtime reads the store only. A key in an environment variable must be imported first, and later changes to the variable do not affect it. Exceptions, detected at runtime: Amazon Bedrock (`AWS_PROFILE`, access keys, bearer token and so on) and Google Vertex (ADC).

## Sources

| Source | How |
| --- | --- |
| API key | `openprogram providers login deepseek`, or `printf %s "$KEY" \| openprogram providers login deepseek --api-key-stdin`. `--api-key` also works but lands in shell history. |
| Anthropic | `openprogram providers login anthropic`, then your Anthropic API key |
| `openai-codex` | ChatGPT subscription, browser PKCE; or import an existing `codex` login |
| `gemini-subscription` | Imports `~/.gemini/oauth_creds.json`; log in with the Gemini CLI first |
| `google-gemini-cli` | Reads `~/.gemini/oauth_creds.json` directly, without discover |
| `github-copilot` | GitHub device code in the browser, or import `COPILOT_GITHUB_TOKEN`, `GH_TOKEN` or `GITHUB_TOKEN`. The short-lived Copilot token is exchanged on demand and never written to disk. |

`login` picks the method; `--method` forces one.

### Import from this machine

```
openprogram providers discover        # lists only, writes nothing
openprogram providers adopt codex_cli # one entry; --all imports everything
```

| Source | Location | Imported into |
| --- | --- | --- |
| Codex CLI | `~/.codex/auth.json` | `openai-codex` |
| Qwen CLI | `~/.qwen/oauth_creds.json` | `qwen` |
| gh CLI | `~/.config/gh/hosts.yml` | `github` |
| Environment | `OPENAI_API_KEY`, `ANTHROPIC_API_KEY` and others | The matching provider |

While the external CLI is present a pointer is stored and its file re-read on every call. Otherwise the token is copied and OpenProgram refreshes it.

### Helper command

For tokens issued by a CLI such as `aws`, `sso-helper` or `token-fetcher`: in Settings → Model services add type `credential_process`.

| Field | Meaning | Default |
| --- | --- | --- |
| `command` | Argv list, run without a shell | required |
| `parses` | `json`, or `text` for stripped stdout | `json` |
| `json_key_path` | Keys to walk, such as `["creds", "token"]`. Empty: the document must be a string | `[]` |
| `cache_seconds` | Token reuse. `0` runs on every call | `300` |
| `timeout_seconds` | Limit per run | `60` |

The login smoke test runs it once. A failure (non-zero exit, timeout, bad output, missing key path) fails the request with the command and its stderr, and never falls back to another credential. Fix the helper, or remove it to use another source.

Enterprise SSO (`sso`) is not implemented; the API and resolver reject it.

## Manage

Commands: [Configure models](https://fzkuji.github.io/OpenProgram/docs/models/README.html).

A provider's keys and sign-ins form one ordered list. A conversation keeps the key it started on; one that is rate limited, out of quota or refused gives way to the next usable one, and the reply shows a one-line notice.

Strategies: `in-order` (default, the first usable one), `evenly`, `least-used`. A conversation can be pinned to one key: [Use several keys](https://fzkuji.github.io/OpenProgram/docs/models/several-keys.html).

```
openprogram providers keys openai list
openprogram providers keys openai add --name work
openprogram providers keys openai move work --to 1
openprogram providers keys openai strategy evenly
```

**Codex sign-in**

- Program calls use the provider's list like a conversation does: the first usable sign-in, in the order `openprogram providers keys openai-codex list` shows. Signing in again with the same ChatGPT account renews its row.
- Terminal authentication failures and errors marked non-retryable stop after one request, keeping their category through the Program runtime. Other callback failures keep the Program's bounded recovery.
- The callback listener starts before the authorization page. Keep the provider panel open until it reports success.

| Problem | Do |
| --- | --- |
| Page does not open | **Open sign-in page** |
| Callback cannot reach the app | Paste the callback URL into the login panel; it must belong to the current attempt |
| Code expired or already used | Start a fresh sign-in |
| Token exchange returns HTTP 403 | OpenAI refused; account access or the network may be the cause. The panel names recognized authorization errors and shows connection retries. |

Cancelling or leaving the panel ends the attempt.
