diff --git a/src/lib/ai-proxy.ts b/src/lib/ai-proxy.ts index b812d0d..8653580 100644 --- a/src/lib/ai-proxy.ts +++ b/src/lib/ai-proxy.ts @@ -327,12 +327,22 @@ export async function handleChatCompletion( try { response = await fetch(url, init); - if (response.ok && proxyPool && proxyPool.size > 0 && init.proxy && attempt > 0) { - proxyPool.markSuccess(); + if (response.ok) { + // Success — reset proxy failure if we used one + if (proxyPool && proxyPool.size > 0 && init.proxy && attempt > 0) { + proxyPool.markSuccess(); + } + break; + } + // Non-2xx — mark proxy as failed so next attempt rotates + if (proxyPool && proxyPool.size > 0 && init.proxy) { + proxyPool.markFailed(); } - if (response.ok || response.status < 500) break; // non-5xx = no retry } catch { - // fall through to next attempt + // Network error — mark proxy as failed, retry + if (proxyPool && proxyPool.size > 0 && init.proxy) { + proxyPool.markFailed(); + } } } diff --git a/src/lib/anthropic-proxy.ts b/src/lib/anthropic-proxy.ts index bb58681..ebdf19d 100644 --- a/src/lib/anthropic-proxy.ts +++ b/src/lib/anthropic-proxy.ts @@ -417,18 +417,22 @@ export async function handleAnthropicMessages( try { response = await fetch(url, init); - if ( - response.ok && - proxyPool && - proxyPool.size > 0 && - init.proxy && - attempt > 0 - ) { - proxyPool.markSuccess(); + if (response.ok) { + // Success — reset proxy failure if we used one + if (proxyPool && proxyPool.size > 0 && init.proxy && attempt > 0) { + proxyPool.markSuccess(); + } + break; + } + // Non-2xx — mark proxy as failed so next attempt rotates + if (proxyPool && proxyPool.size > 0 && init.proxy) { + proxyPool.markFailed(); } - if (response.ok || response.status < 500) break; } catch { - // fall through to next attempt + // Network error — mark proxy as failed, retry + if (proxyPool && proxyPool.size > 0 && init.proxy) { + proxyPool.markFailed(); + } } }