Skip to content
pi-c Artifact [3dd50c7a10]
Login

Artifact [3dd50c7a10]

Artifact 3dd50c7a10ea71b397dc57dea0206df67b2bb0dd8dbf967dd6afd195157d5c7d:


# Several keys and their health

A provider can have more than one key: a second Anthropic key on another account, a key kept for
a team. Each has a name; one is the provider's active key, and pic remembers what the last reply
said of each.

```sh
pic auth login anthropic --api-key --as second   # another key, named second
pic auth use anthropic second                    # requests use it from now on
pic -p 'hello' --key default                     # the provider's own key, for this run only
pic auth import ANTHROPIC_API_KEY_2 --as spare   # seal a variable's value once; it can go
pic auth logout anthropic:spare                  # remove one key
pic auth status                                  # every key, the active one marked *
```

`--key` names a key of the provider the model belongs to (a bare `claude-` model id is
Anthropic's); a key that provider does not have is an error, never a quiet fall back to another.
A conversation keeps the key its replies went with: going on with it later uses that key,
whichever is active then ([a conversation keeps its own](models.md#a-conversation-keeps-its-own)).

The key `pic login` has always stored is the provider's own, named `default`. `env` is the
provider's environment variable and `file` the key written in `models.json`, so either can be
chosen by name too. With nothing chosen, a request uses the provider's own key, else its oldest
named one, else the models file's, else the variable: [the lookup order](credentials.md#the-lookup-order).

## What pic remembers

Every reply tells pic something about the key that sent it. pic keeps the last word per key, never
the key itself:

```
$ pic auth status
anthropic    default    API key                out of credit since 14:02 (HTTP 402)
anthropic    second*    API key                ok, checked 2 min ago
anthropic    env        ANTHROPIC_API_KEY      not used: a stored key wins
```

- **refused**: the provider turned the key down (401, 403). It may be revoked or mistyped.
- **out of credit**: the account has no credit or quota left (Anthropic's billing error, OpenAI's
  quota error or spent prepaid credit, OpenRouter's 402). Waiting does not help.
- **limited until a time**: a rate limit that says to wait a minute or more, or Anthropic's usage
  limit for the account, until the day it names.
- A short rate limit or an overloaded server is retried, as before, and changes nothing.

None of these is retried blindly, and pic never moves to another key on its own: a swap costs money
and starts the prompt cache again, so you decide. Anthropic and OpenAI give an API key no balance
to ask for, so pic learns that a key is out of credit from its first failure, then remembers it.

A variable's state is kept under its name and a salted hash of its value: a new value starts with
no state.

## Free checks

pic can ask a provider about a key without spending tokens: `pic auth status --check` does it for
every key, all at once, each with a deadline of a few seconds, then prints the table:

```
$ pic auth status --check
anthropic    default*   API key                valid, checked just now
openrouter   default*   API key                $4.12 left
deepseek     env*       DEEPSEEK_API_KEY       ¥110.00 left
```

- OpenRouter says what the key may still spend; DeepSeek says the balance. A key at its limit, or
  with nothing left, shows as out of credit.
- Anthropic, OpenAI, Google and the other providers have no balance for an API key to ask for, so
  the check says only that the key is valid or refused (or limited, for a rate limit that lasts).
  A valid answer never clears what a failed request taught: a key that ran out of credit stays so
  until a request succeeds, since the check cannot say credit came back. The row then says both,
  as `valid · last request failed 03:20 (usage limit until Dec 1)`.
- A check sends the key only to the address your requests to that provider use, and nothing of any
  conversation. A provider that does not answer in time, or answers with a status that says nothing
  about the key (a 404, a server error), changes nothing: the row shows `check failed`.
- A subscription sign-in is never checked this way.

pic never checks on its own except in the terminal interface: at start it checks the key in use
(waiting at most two seconds, in the background), and opening `/auth` checks the rest, the rows
filling in as answers arrive. A key checked in the last ten minutes, or that answered a request
then, is not checked again unless you run `--check`; `pic auth status --refresh` is what the
interface runs. The store's passphrase is never asked for by a background check, so a stored key is
checked there only once a request has opened the store. Print, JSON and RPC modes never check on
their own; `check_credentials` over RPC and `pic auth status --check` do when asked. With the
setting `offline` on nothing checks, `--check` and `check_credentials` included.

## In the terminal interface

`/auth` (or `/key`) lists every provider's keys with their state; Enter makes one its provider's
active key, from the next request, or replaces a refused one ([/auth](auth.md)). The footer names the key (`· key second`) when the provider has more
than one or uses its variable, and warns in red when one is known to be bad (`! out of credit`,
`! limited until 15:45`).

When a reply is refused for its key, the request stops where it is and asks:

```
The key "default" is out of credit. Nothing was lost.
  s  retry with "second"  (re-sends 84k tokens uncached, about $0.31)
  m  another model
  esc  stop
```

- `s` makes the other key active and asks again; another key may not share the prompt cache, so
  the price shown is the conversation sent again uncached. The key offered is the one that last
  answered a request, else one a check said is valid, else one never checked; never one whose last
  request failed, so with none left `s` is not offered.
- `m` stops the request and opens the model picker; send a message to go on.
- `w`, for a limit, waits until it lifts and goes on.
- `l`, for a refused key, opens the login for it; then Enter retries.
- Esc stops the request; its message stays in the conversation. Enter tries the same key again.

A choice applies from the next reply request, never to one already sent. Print mode and RPC mode
never ask: the request fails with the same plain sentence, such as
`The key "default" for anthropic is out of credit (HTTP 402: ...)`.

A choice in `/auth` or at the prompt also replaces what `--key` chose for that provider when the
terminal interface was started.

## Expiry

A key made with an end date (most consoles offer one) says so before it stops working. OpenRouter's
free check reports its key's date; for the rest, record it once:

```sh
pic auth login anthropic --api-key --as second --expires 2027-01-31
pic auth expires anthropic:second 30d        # a date, a number of days, or never
pic auth status --expiring 7 || notify-me     # exit 1 when a key in use expires within 7 days
```

Each key shows when it expires and where the date came from, `recorded` or `provider`. Within
`keyExpiryWarnDays` (7) days of it, `-p` and `ask` say so once on stderr, the footer warns in red,
and `/auth` marks the row; past it, the row is red. The request still goes, since a recorded date can
be wrong. A new value for a key forgets the old one's date; the same key stored again keeps it.

## Keys from a command

A key kept in a secret manager is best rotated there. Store the command that prints it instead:

```sh
pic auth login openai --as work --command 'pass show api/openai-work'
```

pic stores the command, sealed, and no key. It runs when a request needs the key, its first line of
output is the key, and that is held in memory for five minutes, never written. When the key is
refused (401 or 403) the command runs again: a new value goes once more, and the row says when it
rotated; the same value is a real refusal. A new value starts with no health and no expiry. A
command that fails (a non-zero exit, no output, ten seconds without an answer) stops the request
with its first line of error, and pic does not fall back to another key. A phone or RPC client
cannot add one.

## A key for a family of models

A rule sends a family of models to one of their provider's keys: the o-series to a work key,
Opus to a key with a higher limit. A pattern is a model id with `*` for any part:

```sh
pic auth use anthropic second --for 'anthropic/claude-opus-*'
pic auth use anthropic --for 'anthropic/claude-opus-*' --clear     # remove it
```

In `/auth`, `b` on a key asks for its pattern (Enter on a pattern the key already has removes it).
Which key a request uses: `--key` (or the conversation's own, or RPC's `set_credential`), then the
rule for its model, an exact pattern before the longest that matches, then the provider's key. A
rule names a key of the model's own provider and never sends a model elsewhere; a rule as long as
one it could share models with is refused, naming it. The model picker's key column and `pic models
--json` say which key each model uses, and when a rule chose it.

## For scripts and agents

Everything `/login` and `/auth` do has a command with JSON out that needs no terminal. Each can be
run again safely: a second run changes nothing and says `"changed":false`. No answer carries a key.

| To | Shell | RPC |
|---|---|---|
| store an API key | `pic auth login P --api-key [--as L] --json`, key on stdin | `add_credential` |
| list keys and what is left | `pic auth status --json` | `get_credentials` |
| run the free checks | `pic auth status --check --json` | `check_credentials` |
| make a key the provider's | `pic auth use P L --json` | `set_credential` with `persist` |
| switch model, and key | `pic models use M [--key L] --json` | `set_model` with `credential` |
| give a family of models a key | `pic auth use P L --for PATTERN --json` | `set_model_credential` |
| remove a key | `pic auth logout P[:L] --json` | `logout` with `label` |

Without `persist`, `set_credential` and `set_model`'s `credential` hold for that RPC process only.
A store behind a passphrase is opened over RPC with `unlock` first; until then its keys are not
checked. The arguments are in [the CLI reference](../reference/cli.md) and [RPC
commands](../reference/rpc-commands.md).

A provider's error that quotes a key, whole or in part (its first eight or last four characters), is
shown and kept with that part replaced by `[key]`. Provider names may not contain `:`, which
separates a provider from a key's name.