Repository navigation
Description
Summary
With the composer on Auto and two direct provider credentials active, one of which has run out of credit (an OpenRouter key whose usage limit is spent), a message can fail with the provider's 402 even though the other provider could have answered. Auto picks a model without any knowledge of whether the credential behind it can still pay, and the turn does not retry on another candidate when the provider refuses.
What was reported
Chat, model set to Auto, one message sent. The turn ended with:
The model provider answered 402: {"error":{"message":"This request requires more credits, or fewer max_tokens. You requested up to 64000 tokens, but can only afford 272. …","code":402,"metadata":{"limit_source":"openrouter_key_limit","remedy_hint":"Raise or remove this API key's usage limit …"}}}
The organization has two providers active; one is out of credit, the other is not. The pick that Auto made ([chat-model] resolved … log line) was not captured, so which of the two it chose is inferred from the 402 body (openrouter_key_limit), not observed.
What the code does today (main at 039e421)
services/platform/backend/core/chat/turn_action.ts:1308-1329resolves Auto first, into one concrete (provider, model) pair, and refuses the turn if nothing resolves ("never a silent fallback").services/platform/backend/core/lib/providers/resolve_chat_model.ts:81-91reads the active direct credentials (api-key/env) and walks their catalogs. The only facts it has about a credential are that it is active and what its catalog lists. There is no input for remaining credit, a recent 402/429, or a key limit.resolve_chat_model.ts:165then callschooseChatModel(services/platform/lib/chat/model-choice.ts): the curated per-band preferred list first, lowest output price otherwise. A credential that cannot pay is indistinguishable from one that can.turn_action.ts:~804turns a non-OK provider response into an error carryingstatus; the turn ends there. TheproviderStrict/ "falls back to another connector" wording atturn_action.ts:844-846concerns which connector serves a named model id at resolution time, not a retry after the provider refuses.- The user sees the provider's raw 402 text. OpenRouter's body names the key limit and a dashboard URL, so it reaches the chat as written.
Observed vs. not examined
- Observed: the 402 text above, from the reporter's chat.
- Not examined: which provider and model Auto chose; whether the same message succeeds when the other provider's model is picked by hand (expected, not tried); whether the 402 also appears for a non-Auto send to the same model (expected); how other credentials fail when out of credit (other vendors answer with a different status or body).
- The message says "up to 64000 tokens, can only afford 272": the key had a few hundred tokens of headroom left rather than none, so a request with a small
max_tokensmay still have succeeded. Whether the chat lane could sizemax_tokensto what a key can afford was not examined.
Constraints
- Auto must stay a per-message pick that governance applies to in full (
resolve_chat_model.tsheader), and Auto and the picker must not disagree about what the user may run. - A refusal must stay explicit; the file's own rule is no silent fallback. A retry on another candidate would need to be visible (which model answered is already shown on the message).
- A turn that failed after the provider accepted the request (tokens already streamed or billed) is not the same as a 402 before any byte; the pipeline already tells these apart (
onAccepted). - Org-owned credential state is per organization (tenant isolation).
Options (no preference stated)
- Retry on refusal. When the provider refuses an Auto turn before accepting the prompt (402, perhaps 401/429), re-resolve with that (provider, model) excluded and run once more, bounded to a few candidates.
- Remember it. Record a credential's payment refusal for a short window and have Auto skip that credential's models until it expires or the credential is edited.
- Ask the vendor. Where a vendor exposes balance or key limit (OpenRouter's key endpoint does), Auto reads it before picking. Per-vendor code, and an extra round trip on every send.
- Explain it. Keep the single pick but replace the raw vendor text with a Tale message naming the provider and suggesting a manual model choice or a top-up.
- Smaller request. Lower
max_tokensto what the key can afford when the 402 body says so (OpenRouter only).
These combine (1 + 4 is the smallest user-visible change).
Questions
- Should Auto ever answer on a different provider than the first pick after a refusal, or is a visible error the intended behaviour?
- Is a spent key a state worth showing in Settings → providers, independent of Auto?
Related
- Bug: chat offers OpenRouter models that take no tools, and every message to one fails #5063 (chat offers OpenRouter models that take no tools; every message to one fails): same family, a pick that fails at the provider with no second candidate tried.
Activity
fix(platform): let Auto answer on another provider after a refusal
fix(platform): let Auto answer on another provider after a refusal