# Named keys and remembered health

Status: adopted 2026-11-22 (option B of the keys proposal); landings 2 to 4 built: failure classes,
remembered health, named keys, the swap prompt, and free checks through a probe on the provider
adapter.

Tier: core records (`cred.c`, `http.c`, `provider_http.c`, the journal's `credential` column);
bundled decides (`keyhealth.c`) and checks (`keyprobe.c`, and each adapter's probe). Clock: vendor
(error wording, rate-limit headers, balance endpoints). Record: `picu_credential_health` per
credential name (state, reason, status, until, checked, remaining and its unit, probed),
`pic_generation.credential` per request. Default: the bundled reading of refusals, registered by
the composition root; checks only on `pic auth status --check`, `/key` and terminal start.

## The decisions

1. Stored beats the variable, and pic says so (landed before this note).
2. A refusal is read into a class and remembered per key: refused (401, 403), out of credit (402,
   Anthropic's `billing_error` and "credit balance is too low" as 400 or 402, OpenAI's
   `insufficient_quota`, OpenRouter's 402), limited until a time (a 429 whose `retry-after`, or
   Anthropic's or OpenAI's reset headers, say a minute or more), transient (5xx, 529, a short 429:
   retried as before), other. The first three are never retried blindly.
3. Extra keys are rows `provider:label` in `picu_credential`, sealed under their own names; the
   provider's existing row stays its default with no reseal. The active label is the setting
   `activeKey:PROVIDER`; `--key` overrides it for one process.
4. Ask, never fail over. With `--hold-key-failures` (the terminal interface's children) a refusal of
   the first three classes fails the attempt and leaves the request unfinished; the interface asks.
   The choice is a setting written before the next request, and the next request asks the turn again
   as attempt N+1, so it fits "a generation started, no reply: failed, the turn is asked again"
   ([durability](../dev/durability.md)). Each attempt still counts toward the turn's three.

## The seam

Core reaches the vendor wording only through `pic_cred_failure_ops` (cred.h: classify and message),
registered by `main.c` beside the sign-in flows; with nothing registered core classifies by status
alone. The rows are data in `keyhealth.c`, so wording that drifts is a table edit. The probe for
free checks is not on that interface after all: what to ask is the wire API's, so it is the
adapter's `probe` (`pic_provider_probe`, provider_http.h) and `keyprobe.c` runs it.

## Free checks (landing 4)

5. Free checks only on demand, and never a request that spends tokens. OpenRouter's `GET /key`
   gives what the key may still spend, DeepSeek's `GET /user/balance` the balance; Anthropic, OpenAI,
   Google and the rest get their models list, which says valid or refused (and carries rate-limit
   headers, recorded in the row's reason). A check sends the key only to the base URL the provider's
   requests use, never a conversation, and reads a refusal as a request's is read. It runs on
   `pic auth status --check`, on opening `/key`, and once at terminal start for the active key
   (background child, two seconds, `--refresh`); never in print, JSON or RPC mode; never more than
   once per key in ten minutes unless `--check`. A models list does not undo a recorded
   out-of-credit or limit, since it cannot see credit. A locked store's keys are skipped by a
   background check: it never asks for the passphrase.

## Not verified

Whether rate-limit headers come on error replies of every provider; whether two keys of one
Anthropic workspace share a prompt cache (the prompt always shows the uncached price).
