Skip to content

Backend-aware advice in HTTP errors: key variable, 402, Retry-After - #14

Merged
DevMortimer merged 2 commits into
mainfrom
fix/backend-aware-http-advice
Sep 24, 2026
Merged

DevMortimer merged 2 commits into
mainfrom
fix/backend-aware-http-advice

Conversation

@DevMortimer

Copy link
Copy Markdown
Owner

What and why

HTTP error advice always said Check TYPESAFE_API_KEY., even when the client talked to OpenRouter, where the key comes from OPENROUTER_API_KEY. A 402 fell through to the generic "try again later" text, and a 429 ignored the server's Retry-After header. The advice now matches the backend that was called.

Change

  • safeError(error, backend?) takes an optional backend name or config. The client passes its backend from evaluate() and listModels().
  • 401: Check <keyEnv>. for the backend's key variable (OPENROUTER_API_KEY on OpenRouter). Without a backend, the text stays Check TYPESAFE_API_KEY..
  • 402: Insufficient credits. Add credits at https://openrouter.ai/credits. on OpenRouter, Check your account balance. elsewhere. A 402 does not mark the key unusable.
  • 429: appends Retry after <n> seconds. when the response carries a numeric Retry-After. Dates, blanks, and other values are ignored, so the message quotes no other header content.
  • docs/api.md and CHANGELOG.md updated.

Verification

  • npm ci and npm run check pass: build, typecheck, 102 offline tests (2 new). The base branch passes its 100.
  • The new tests run offline through the injected fetch on both backends: 401, 402, and 429 with and without Retry-After: 7. They assert the exact messages, that usable is false after a 401 and true after a 402, and that no upstream body reaches a message.
  • Documentation and changelog notes updated.

Live testing is not applicable: the transport and response validation are unchanged; only the text of error messages changes.

Compatibility and release notes

  • Additive: the new backend parameter is optional, and one-argument safeError calls return the same messages as before for 401 and 429 without Retry-After.
  • The 402 message changes from the generic retry advice to billing advice.
  • Limitation: OpenRouter is detected by its configured host, so a custom backend on another OpenRouter-compatible host gets the generic 402 text.
  • No version bump.

@DevMortimer
DevMortimer merged commit 7b9730a into main Sep 24, 2026
8 checks passed
@DevMortimer
DevMortimer deleted the fix/backend-aware-http-advice branch September 24, 2026 00:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant