Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ const TERMINAL_ERROR_NAMES = new Set(['contextoverflowerror']);
const POLICY_ERROR_NAMES = new Set(['contentfiltererror']);

// Client errors are terminal because replaying the same request cannot
// succeed, except timeouts (408) and rate limits (429) which are transient.
const RETRYABLE_CLIENT_STATUS_CODES = new Set([408, 429]);
// succeed, except timeouts (408), rate limits (429), and provider-defined 402s
// without structured evidence of a permanent account limit.
const RETRYABLE_CLIENT_STATUS_CODES = new Set([402, 408, 429]);

export function collectProviderErrorValues(error: unknown): unknown[] {
const pending: Array<{ value: unknown; depth: number }> = [
Expand All @@ -46,7 +47,7 @@ export function collectProviderErrorValues(error: unknown): unknown[] {
while (pending.length > 0) {
const current = pending.shift();

if (!current || current.depth > 4) {
if (!current || current.depth > 6) {
continue;
}

Expand Down Expand Up @@ -130,6 +131,15 @@ function hasErrorName(values: unknown[], names: Set<string>): boolean {
});
}

function isOpenRouterCreditsLimitError(values: unknown[]): boolean {
return (
extractProviderErrorHttpStatus(values) === 402 &&
values.some(
(value) => asRecord(value)?.limit_source === 'openrouter_credits',
)
);
}

export function isOpenCodeContextOverflowError(error: unknown): boolean {
return hasErrorName(collectProviderErrorValues(error), TERMINAL_ERROR_NAMES);
}
Expand All @@ -143,6 +153,10 @@ function isExplicitlyTerminal(values: unknown[]): boolean {
return true;
}

if (isOpenRouterCreditsLimitError(values)) {
return true;
}

const status = extractProviderErrorHttpStatus(values);

return (
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,10 @@ class FastAgentInferenceError extends Error {
}
}

export function isNonRetryableFastAgentInferenceError(error: unknown): boolean {
return error instanceof FastAgentInferenceError && !error.failure.retryable;
}

function resolveFastAgentInferenceMaxRetries(
failure: FastAgentInferenceFailure,
): number {
Expand Down
76 changes: 60 additions & 16 deletions packages/cloud-agents/src/server/non-task-provider-usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1919,6 +1919,43 @@ function isInferenceErrorExplicitlyNonRetryable(error: unknown): boolean {
return false;
}

function isOpenRouterCreditsLimitError(
error: unknown,
statusCode: number | undefined,
): boolean {
if (statusCode !== 402) return false;

const pending: Array<{ value: unknown; depth: number }> = [
{ value: error, depth: 0 },
];
const seen = new Set<object>();

while (pending.length > 0) {
const current = pending.shift();
if (!current || current.depth > 6) continue;

const { value, depth } = current;
if (typeof value === 'string') {
try {
pending.push({ value: JSON.parse(value), depth: depth + 1 });
} catch {
// Only structured OpenRouter metadata identifies this account limit.
}
continue;
}
if (!value || typeof value !== 'object' || seen.has(value)) continue;

seen.add(value);
const record = value as Record<string, unknown>;
if (record.limit_source === 'openrouter_credits') return true;
for (const nested of Object.values(record)) {
pending.push({ value: nested, depth: depth + 1 });
}
}

return false;
}

function isContentFilterInferenceError(error: unknown): boolean {
const pending: Array<{ value: unknown; depth: number }> = [
{ value: error, depth: 0 },
Expand Down Expand Up @@ -1985,6 +2022,10 @@ export function classifyNonTaskInferenceError(
? (record.data as Record<string, unknown>)
: undefined;
const statusCode = findInferenceErrorStatusCode(inferenceError);
const openRouterCreditsLimited = isOpenRouterCreditsLimitError(
inferenceError,
statusCode,
);
const responseBody =
typeof data?.responseBody === 'string' ? data.responseBody : '';
const detail =
Expand All @@ -2004,6 +2045,15 @@ export function classifyNonTaskInferenceError(
};
}

if (openRouterCreditsLimited) {
return {
message:
'The inference provider account does not have enough credits or quota.',
reason: 'insufficient_credits',
retryable: false,
};
}

if (isInferenceErrorExplicitlyNonRetryable(inferenceError)) {
return {
message: 'The inference provider rejected the request.',
Expand Down Expand Up @@ -2084,15 +2134,6 @@ export function classifyNonTaskInferenceError(
};
}

if (statusCode === 402) {
return {
message:
'The inference provider account does not have enough credits or quota.',
reason: 'insufficient_credits',
retryable: false,
};
}

if (statusCode === 404) {
return {
message: 'The selected model is unavailable with these credentials.',
Expand Down Expand Up @@ -2123,12 +2164,13 @@ export function classifyNonTaskInferenceError(
}

if (
detail.includes('insufficient_quota') ||
detail.includes('insufficient quota') ||
detail.includes('insufficient credit') ||
detail.includes('payment required') ||
detail.includes('billing') ||
/\b402\b/u.test(detail)
statusCode !== 402 &&
!/\b402\b/u.test(detail) &&
(detail.includes('insufficient_quota') ||
detail.includes('insufficient quota') ||
detail.includes('insufficient credit') ||
detail.includes('payment required') ||
detail.includes('billing'))
) {
return {
message:
Expand Down Expand Up @@ -2199,11 +2241,13 @@ export function classifyNonTaskInferenceError(

// Remaining structured 4xx responses (400, 413, 422, …) are client errors:
// resending the same request cannot recover them. 408 stays retryable as a
// timeout; 401/402/403/404/429 were classified above.
// timeout; provider-defined 402 responses stay retryable unless structured
// metadata or an explicit isRetryable=false signal classified them above.
if (
statusCode !== undefined &&
statusCode >= 400 &&
statusCode < 500 &&
statusCode !== 402 &&
statusCode !== 408
) {
return {
Expand Down
Loading
Loading