# Credentials

API keys come from pic's credential store or from the environment (`ANTHROPIC_API_KEY`,
`OPENAI_API_KEY`, `OPENROUTER_API_KEY`, ...), as in Pi.

```sh
pic login anthropic          # the key, typed with echo off (or piped on stdin), sealed into ~/.pic/pic.sqlite
pic keys                     # stored keys: names and dates, never values
pic logout anthropic         # remove it
```

`/login [PROVIDER]` and `/logout [PROVIDER]` do the same in the terminal interface. A provider with
a subscription sign-in can use that instead of a key: see [subscription sign-in](sign-in.md).

## The lookup order

As in Pi, a stored credential owns its provider. A request uses the first of these that exists:

1. the key chosen for the provider, with `--key` for one run or `pic auth use` (or `/key`) from now
   on; see [several keys](named-keys.md);
2. the credential stored with `pic login` or `pic auth login` (an API key or a sign-in), else the
   provider's oldest key stored with `--as`;
3. the `apiKey` written in `models.json`;
4. the provider's environment variable.

If none exists the request fails with an error naming `pic login`. A stored credential that cannot
be used (a damaged row, a sign-in whose refresh failed) is an error; pic does not quietly fall
back to the variable. So an exported variable that is out of credit or stale never hides a key you
stored; `pic logout PROVIDER` returns the provider to its variable.

`pic auth status` lists every key, marks the one in use, and says of a variable a stored key hides
`not used: a stored key wins`; the provider list in `/login` says the same.

## The store

Each stored key is sealed with ChaCha20-Poly1305 (the provider's name bound as associated data), so
no plaintext key reaches the database, its WAL, a backup or a log. The sealing key comes from a
passphrase. pic asks for the passphrase on the terminal the first time a stored key is needed, and
keeps the key only for the life of the process; the terminal interface asks once and hands the key
to its request processes through an inherited pipe, never argv, the environment's values or a file.
The store protects against a stolen disk, backup or database copy; not against a weak passphrase or
malware running as you.

## With no terminal

Where nothing can ask for the passphrase (cron, CI, a git hook, `ssh -T`), a request whose key is
sealed fails at once with one line naming the ways out, and nothing is saved for it:

```sh
export ANTHROPIC_API_KEY=...         # in the shell that has the key
pic auth use anthropic env           # from now on, requests to anthropic use the variable
```

Once chosen, the variable is used wherever it is set, with no passphrase; where it is not set,
requests fail and say so. `pic auth use anthropic default` goes back to the stored key. RPC mode
never asks on a terminal either: see [RPC mode](rpc.md).

## A forgotten passphrase

The passphrase cannot be recovered, and nothing sealed under it can be opened without it.
`pic auth reset` forgets it with everything sealed under it: the stored keys and sign-ins and the
phones paired for remote control. Settings, sessions and trusted folders stay. It asks first; with
no terminal to ask on it needs `--yes`. The next `pic login` sets a new passphrase, and phones pair
again with `pic remote pair`.

How the key is derived and where it is kept is in [the architecture
notes](../dev/architecture.md#the-credential-store).
