-
Notifications
You must be signed in to change notification settings - Fork 7
fix(llmist): classify OpenRouter provider errors instead of crashing the agent #1417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aaight
wants to merge
1
commit into
dev
Choose a base branch
from
fix/openrouter-credit-exhaustion-handling
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| /** | ||
| * OpenRouter error classification for llmist-backed agent runs. | ||
| * | ||
| * Llmist's OpenRouter provider wraps upstream errors with friendly prefixes | ||
| * such as `OpenRouter: Insufficient credits...` (see `enhanceError` in the | ||
| * llmist SDK). These wrapped messages are the most reliable signal that the | ||
| * underlying failure was a provider-side configuration problem rather than a | ||
| * transient runtime issue — none of them are retryable, none are caused by | ||
| * CASCADE bugs, and none should be treated like ordinary agent execution | ||
| * crashes (Sentry tag `agent_execution`). | ||
| * | ||
| * This module exposes: | ||
| * | ||
| * - `OpenRouterErrorKind` — the actionable categories we care about. | ||
| * - `classifyOpenRouterError(err)` — returns the kind for any wrapped | ||
| * OpenRouter error, or `null` for everything else. | ||
| * - `formatOpenRouterErrorMessage(kind, raw)` — produces the operator-facing | ||
| * summary written to the run row / PM card. | ||
| * - `OPENROUTER_ERROR_SENTRY_TAG` / `openRouterErrorSentryTagValue(kind)` — | ||
| * stable Sentry tag values so credit-exhaustion failures are filterable | ||
| * and don't drown out real agent crashes. | ||
| * | ||
| * The classifier is intentionally string-based: llmist surfaces the error as a | ||
| * regular `Error` with no preserved status code or provider-specific class, | ||
| * and we never want to crash the worker pipeline because llmist changed the | ||
| * error class hierarchy. | ||
| */ | ||
|
|
||
| export type OpenRouterErrorKind = | ||
| | 'insufficient_credits' | ||
| | 'rate_limit' | ||
| | 'unauthorized' | ||
| | 'model_unavailable' | ||
| | 'other'; | ||
|
|
||
| /** Stable Sentry tag name. Used by `captureException({ tags: { source: ... } })`. */ | ||
| export const OPENROUTER_ERROR_SENTRY_TAG = 'openrouter_provider_error'; | ||
|
|
||
| /** | ||
| * Map an `OpenRouterErrorKind` to the Sentry `source` tag value. | ||
| * | ||
| * Insufficient-credit failures get their own tag so operator dashboards can | ||
| * filter them out (they are an account / billing problem, not a CASCADE bug). | ||
| */ | ||
| export function openRouterErrorSentryTagValue(kind: OpenRouterErrorKind): string { | ||
| switch (kind) { | ||
| case 'insufficient_credits': | ||
| return 'openrouter_insufficient_credits'; | ||
| case 'rate_limit': | ||
| return 'openrouter_rate_limit'; | ||
| case 'unauthorized': | ||
| return 'openrouter_unauthorized'; | ||
| case 'model_unavailable': | ||
| return 'openrouter_model_unavailable'; | ||
| default: | ||
| return 'openrouter_provider_error'; | ||
| } | ||
| } | ||
|
|
||
| function extractMessage(err: unknown): string | null { | ||
| if (err === null || err === undefined) return null; | ||
| if (typeof err === 'string') return err; | ||
| if (err instanceof Error) return err.message ?? null; | ||
| if (typeof err === 'object' && 'message' in err) { | ||
| const candidate = (err as { message?: unknown }).message; | ||
| if (typeof candidate === 'string') return candidate; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Classify an error originating from llmist's OpenRouter provider. | ||
| * | ||
| * Returns `null` when the error isn't OpenRouter-flavored. The classifier | ||
| * recognizes both: | ||
| * | ||
| * - llmist's wrapped form: messages starting with `OpenRouter: ...` | ||
| * (produced by `enhanceError` in `node_modules/llmist`). | ||
| * - The unwrapped HTTP signals: explicit `402`, `Insufficient credits`, | ||
| * `429`, `rate limit`, `401`, `Unauthorized`, `503`, etc. Operators may | ||
| * extend the llmist SDK or swap models in ways that change the wrapping, | ||
| * so the classifier covers both shapes. | ||
| */ | ||
| export function classifyOpenRouterError(err: unknown): OpenRouterErrorKind | null { | ||
| const raw = extractMessage(err); | ||
| if (!raw) return null; | ||
| const message = raw.toLowerCase(); | ||
|
|
||
| const hasOpenRouterPrefix = message.includes('openrouter:'); | ||
| const mentionsCredits = | ||
| message.includes('insufficient credits') || | ||
| message.includes('insufficient credit') || | ||
| message.includes('insufficient balance'); | ||
| const mentionsPayment = message.includes('402') || message.includes('payment required'); | ||
|
|
||
| if (mentionsCredits || (hasOpenRouterPrefix && mentionsPayment)) { | ||
| return 'insufficient_credits'; | ||
| } | ||
|
|
||
| // Plain HTTP 402 from any upstream that wasn't wrapped — still a credit / | ||
| // payment failure on the user's side. Treat the same way. | ||
| if (mentionsPayment) { | ||
| return 'insufficient_credits'; | ||
| } | ||
|
|
||
| if (!hasOpenRouterPrefix) { | ||
| return null; | ||
| } | ||
|
|
||
| if (message.includes('rate limit') || message.includes('429')) { | ||
| return 'rate_limit'; | ||
| } | ||
| if ( | ||
| message.includes('authentication failed') || | ||
| message.includes('unauthorized') || | ||
| message.includes('401') | ||
| ) { | ||
| return 'unauthorized'; | ||
| } | ||
| if ( | ||
| message.includes('temporarily unavailable') || | ||
| message.includes('model unavailable') || | ||
| message.includes('503') | ||
| ) { | ||
| return 'model_unavailable'; | ||
| } | ||
|
|
||
| return 'other'; | ||
| } | ||
|
|
||
| /** | ||
| * Build an operator-facing summary for a classified OpenRouter error. | ||
| * | ||
| * The summary is what shows up in `AgentResult.error`, which the PM lifecycle | ||
| * surfaces verbatim as `❌ Agent failed: <message>` on the work item. We keep | ||
| * the original llmist message in parentheses so engineers debugging from logs | ||
| * can still see the exact wording that came back from OpenRouter, but lead | ||
| * with a short, plain-English explanation + the actionable next step. | ||
| * | ||
| * Truncates excessively long raw messages so PM cards (especially Trello, | ||
| * which has a 16k comment cap) never break on multi-paragraph payloads. | ||
| */ | ||
| export function formatOpenRouterErrorMessage( | ||
| kind: OpenRouterErrorKind, | ||
| rawMessage: string | null | undefined, | ||
| ): string { | ||
| const trimmed = rawMessage ? truncate(rawMessage.trim(), 600) : ''; | ||
| const detail = trimmed ? ` (details: ${trimmed})` : ''; | ||
|
|
||
| switch (kind) { | ||
| case 'insufficient_credits': | ||
| return ( | ||
| `OpenRouter rejected the request because the account has insufficient credits. ` + | ||
| `Top up the OpenRouter balance at https://openrouter.ai/credits or switch the project to a ` + | ||
| `different model/engine before retrying.${detail}` | ||
| ); | ||
| case 'rate_limit': | ||
| return ( | ||
| `OpenRouter rate-limited the request. Reduce the project's request rate, upgrade the ` + | ||
| `OpenRouter plan, or switch to a different model before retrying.${detail}` | ||
| ); | ||
| case 'unauthorized': | ||
| return ( | ||
| `OpenRouter rejected the request as unauthorized. Verify the OPENROUTER_API_KEY project ` + | ||
| `credential is current and has access to the configured model.${detail}` | ||
| ); | ||
| case 'model_unavailable': | ||
| return ( | ||
| `OpenRouter reports the requested model is temporarily unavailable. Switch the project ` + | ||
| `to a different model or retry after the provider recovers.${detail}` | ||
| ); | ||
| default: | ||
| return `OpenRouter provider error: the request could not be completed.${detail}`; | ||
| } | ||
| } | ||
|
|
||
| function truncate(value: string, maxLen: number): string { | ||
| if (value.length <= maxLen) return value; | ||
| return `${value.slice(0, maxLen - 1)}…`; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This catch is too late to be the only Sentry event for the retryable OpenRouter kinds. For
OpenRouter: Rate limit exceededandOpenRouter: Model temporarily unavailable, llmist'sisRetryableErrorreturns true, so after the last retry CASCADE'sgetRetryConfig(...).onRetriesExhaustedalready callscaptureException(error, { tags: { source: 'llm_retries_exhausted' } })before the error reaches this block. The run avoids the genericagent_executiontag, but these provider failures still create an unclassified retry-exhaustion event alongside the new warning. The retry-exhaustion path needs to be tagged/suppressed for classified OpenRouter errors, or covered by a test that proves 429/503 only produce the intended OpenRouter-tagged Sentry event.