# Companies, ways and credentials

1. The company, MiniMax: the grouping table we maintain; a new way is placed by name and corrected in `config.json`
2. its four ways come from models.dev: address, protocol, billing, key variable
3. A pasted key, a key from a tool's settings, a key moved out of `config.json`: stored once for the company
4. A sign-in, another CLI's login, a helper command: one way's only; a key in a variable is never stored
5. the grid: refused, works or unknown for one key on each way, filled by use or by Check
6. A model request goes through the way picked and follows its key to a way it works on when every key is refused
7. Search, image and speech ask the store for the company's key and no longer read the environment

## Three things

| Thing | What it is | Who maintains it |
| --- | --- | --- |
| Company | Who issued the key: MiniMax, Moonshot AI, OpenCode. Groups its ways on one card. | Our table (`COMPANIES` in `auth/login/login_method_registry.py`); an entry the table does not list is placed by `_place` |
| Way | One address to send requests to: its protocol, its models, whether it bills per use or by plan, the variable that holds its key | [models.dev](https://models.dev), refreshed with the catalogue |
| Credential | A sign-in is one way's. An API key is the company's, stored once. | The person |

## Where a key works

Nobody knows this when the key is saved, so nothing decides it then.

| When | What happens |
| --- | --- |
| Key saved | Stored for the company; a row on every key way, state unknown. Checked first only when the company has a single key way. |
| Request through a way | The key goes to that way's address. A refusal marks that way's row and nothing else; another way's row stays as it was. |
| Check on a row | Asks that way about that key, then and there |
| Rename or replace | Changes the key itself, so every way shows it |
| Remove | Removes the key and its row on every way |

Nothing is asked in the background. A key with its own address or headers is no company key: it stays the one way's it was added on.

## One request

| Case | What happens |
| --- | --- |
| Every key refused on the way picked | The request is sent again, same model, through another way of the company that lists one of those keys and has not refused it; a way the key has answered on first. It uses only those keys there, and the model need not be enabled on that way. The reply says so once. |
| Limit, spent quota, server error | Stays on the way it is on, picked or moved to. Moving it could spend another plan's money. A plan's spent quota sent as a rate limit (MiniMax Token Plan) is read as a spent quota: no wait, no retry. |
| Pinned credential, or the caller's own key | Never moved |

## Sources

| Source | Lands in | Seen by |
| --- | --- | --- |
| Pasted on a way (Settings, `openprogram providers keys <way> add`, setup) | Company key file | Every key way of the company |
| Saved for a tool (search, image, speech settings) | Company key file | The tool; the company's ways too when it serves models |
| Environment variable | Nowhere. A read-only row `env:<VAR>`; hidden when it holds a key already stored. | Every way and tool that names the variable |
| `config.json` `api_keys` | Moved to the company its variable names, once, at start; a value already stored is dropped; the name leaves `config.json` | As a pasted key |
| A key in a way's own list, from before | Moved to the company file; the way keeps its row and state | Every key way |
| Sign-in (OAuth, device code) | That way's list | That way |
| Another CLI's login (Codex, Gemini, GitHub, Qwen) | That way's list, as a pointer to the other CLI's file | That way |
| Helper command | That way's list, as the command | That way |

## Tools

A tool asks for its service's key (`auth/services.py`, `service_key`): the company's stored key, else the variable. It never reads `os.environ` itself.

| Service | Reads the keys of |
| --- | --- |
| Tavily, Brave, Serper, Exa, You.com, Jina, Kagi, Firecrawl, Google PSE, fal, ElevenLabs, PlayHT | Itself: a company with no way |
| Perplexity, MiniMax, Kimi and Ollama search | The model company. One key serves chat and search. |

MiniMax and Kimi search have an international and a China address. The search tries one, then the other when the key is refused, and remembers which answered.

Google PSE's engine id is no secret: it is the setting `search.google_pse_cx`, else `GOOGLE_PSE_CX`.

Several keys: a tool reads the first, in stored order, that some way has not refused. A key saved in a tool's settings replaces a tool-only company's key; for a company that also serves models it is added and placed first.

## Added, removed, kept

|  | What |
| --- | --- |
| Added | Company key files. Placement of unlisted catalogue entries. The service table. The one-time move. A lock that gives up after its timeout. A refused request following its key to another way. The person's own placement. |
| Removed | Copying `config.json` keys into the process environment. Tools reading `os.environ`. Picking one way for a shared variable. Trying a key against every way in the background. The same key stored once per way. |
| Kept | One list per way for sign-ins, order and state. Read-only environment rows. Per-way health, cooldown and blocked models. Failover among a way's credentials. |

**Placing an entry the table does not list**

1. Its id extends a listed company's id, or one of its ways', at a hyphen: it joins that company (`minimax-cn-max-plan` joins MiniMax)
2. Several unlisted entries read the same key variable and one id extends another: they form a company of their own
3. Otherwise it is a company of one way

A shared prefix alone joins nothing: two entries that read different variables are different companies.

The person's own word comes first: `config.json` `provider_companies` maps an unlisted entry's id to a company's, or to its own id to stand alone.

## Decisions

| Decision | Follows | Why |
| --- | --- | --- |
| One store for model and tool keys | OpenCode, Oh My Pi | One place to add, list and remove; search keys are not a second system |
| Environment variables are read, never written | OpenCode, Codex, Claude Code | Hermes writes its key file into the process environment, which is what `config.json` did here: every subprocess inherits every key |
| Ways and their key variables come from models.dev | OpenCode, Oh My Pi | It has the widest coverage and is refreshed for us |
| A key is the company's, not one way's | No peer: each stores a key under one catalogue entry | The person has one key and cannot say which entries take it. Stored per entry, the same key is pasted four times or sits on the wrong one. |
| The company table is ours | No catalogue has one. Cherry Studio's `presetProviderId` comes closest and covers three cases. | Placement keeps a new entry from needing a release |
| A refused request follows its key to another way | No peer. LiteLLM's router falls back across deployments on any error. | Only a refusal moves, and only along the same key, so the same account pays and a limit never spills onto another plan |
| A moved request uses only the key it follows | No peer | The other way may hold a sign-in or another account's key. Using those would bill an account the person did not pick. |
| Secrets stay in owner-only files, not the system keychain | OpenCode, Pi, Hermes, Oh My Pi. Codex and Claude Code use the keychain. | The worker runs headless and over remote access, where a keychain prompt has nobody to answer it; the stable and dev instances each keep their own copy; one format on macOS, Windows and Linux. |
| MCP server and channel secrets stay with their server or account | OpenCode: `mcp-auth.json` beside `auth.json` | Each belongs to one server or one bot account. There is no company and no second way to share it with. |
| Two lookups: a key, or whatever the way signs with | Pi: `getApiKey` returns a key or a sign-in's token | A tool or key-only request needs a key; a model request takes a sign-in's token too. One function with a flag would say less than two names. |
| A service's non-secret value is a setting | OpenCode: provider `options` in its config | An engine id is shown, edited and backed up like any setting; it needs no key file |
| Saving a tool's key again replaces it | OpenCode: one credential per provider | A tool has one key field; a company that also serves models keeps its other keys |
| The person can correct a placement | No peer has a company table | Placement by name can be wrong, and a fix should not wait for a release |
| No check of a key against every way | All peers: none validates on save | A check spends quota or a request on ways the person never uses. LiteLLM's sends a real chat. |

## Verification

`tests/unit/providers/auth/test_company_keys.py`: one key on every way, the secret in one file, per-way state, rename and removal, environment rows, both moves, tool services, placement, a request moved along its key. With a real MiniMax China key picked on either international way: 401 there, then the China way accepts the key.

## Implementation status

All of the above is in place. The company card draws a key as one row with the ways it has answered on ([One key, several ways](https://fzkuji.github.io/OpenProgram/docs/reference/design/providers/provider-management.html#shared-environment-keys)); the model picker already lists each way with its state.

[API key resolution](https://fzkuji.github.io/OpenProgram/docs/reference/design/providers/auth/api-key-resolution-unification.html)
