From ca170c188fc2001c1f380ddc196ae2351f894472 Mon Sep 17 00:00:00 2001 From: Christiaan Arnoldus Date: Tue, 14 Jul 2026 18:11:49 +0200 Subject: [PATCH 1/2] Disallow fallback to Opus for free models --- .../providers/apply-provider-specific-logic.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/apps/web/src/lib/ai-gateway/providers/apply-provider-specific-logic.ts b/apps/web/src/lib/ai-gateway/providers/apply-provider-specific-logic.ts index 752438bc10..6544b0c09f 100644 --- a/apps/web/src/lib/ai-gateway/providers/apply-provider-specific-logic.ts +++ b/apps/web/src/lib/ai-gateway/providers/apply-provider-specific-logic.ts @@ -40,6 +40,7 @@ import { rewriteChatCompletionsOneOfAsAnyOf, isFriendliChatCompletionsRequest, } from '@/lib/ai-gateway/schema-rewrite'; +import { isFreeModel } from '@/lib/ai-gateway/is-free-model'; export function getPreferredProviderOrder(requestedModel: string): string[] { if (isClaudeModel(requestedModel)) { @@ -97,12 +98,16 @@ export function applyPreferredProvider( } } -export function applyGatewayModelsFallback( +export async function applyGatewayModelsFallback( providerId: ProviderId, requestedModel: string, requestToMutate: GatewayRequest ) { - if (isFableModel(requestedModel) && (providerId === 'openrouter' || providerId === 'vercel')) { + if ( + !(await isFreeModel(requestedModel)) && + isFableModel(requestedModel) && + (providerId === 'openrouter' || providerId === 'vercel') + ) { requestToMutate.body.models = [requestedModel, CLAUDE_OPUS_CURRENT_MODEL_ID]; return; } @@ -122,7 +127,7 @@ export async function applyProviderSpecificLogic( sessionId: string | null, taskId: string | null ) { - applyGatewayModelsFallback(provider.id, requestedModel, requestToMutate); + await applyGatewayModelsFallback(provider.id, requestedModel, requestToMutate); applyTrackingIds(requestToMutate, provider, userId, taskId); sanitizeBinaryToolResults(requestToMutate); From 0a53a422e82500b41d469e59cb30f149eaa2b9ca Mon Sep 17 00:00:00 2001 From: chrarnoldus <12196001+chrarnoldus@users.noreply.github.com> Date: Tue, 14 Jul 2026 16:21:59 +0000 Subject: [PATCH 2/2] Fix async fallback tests Co-authored-by: kiloconnect[bot] <240665456+kiloconnect[bot]@users.noreply.github.com> --- .../providers/apply-provider-specific-logic.test.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/apps/web/src/lib/ai-gateway/providers/apply-provider-specific-logic.test.ts b/apps/web/src/lib/ai-gateway/providers/apply-provider-specific-logic.test.ts index 73c5ac834f..40d3bf97e6 100644 --- a/apps/web/src/lib/ai-gateway/providers/apply-provider-specific-logic.test.ts +++ b/apps/web/src/lib/ai-gateway/providers/apply-provider-specific-logic.test.ts @@ -21,10 +21,10 @@ function makeRequest(model: string, models?: string[]): GatewayRequest { describe('applyGatewayModelsFallback', () => { it.each(['openrouter', 'vercel'])( 'sets Opus as the Fable fallback for the %s provider', - providerId => { + async providerId => { const request = makeRequest('anthropic/claude-fable-5', ['caller/fallback']); - applyGatewayModelsFallback(providerId, 'anthropic/claude-fable-5', request); + await applyGatewayModelsFallback(providerId, 'anthropic/claude-fable-5', request); expect(request.body.models).toEqual([ 'anthropic/claude-fable-5', @@ -33,18 +33,18 @@ describe('applyGatewayModelsFallback', () => { } ); - it('removes caller-provided fallbacks for Fable on other providers', () => { + it('removes caller-provided fallbacks for Fable on other providers', async () => { const request = makeRequest('anthropic/claude-fable-5', ['caller/fallback']); - applyGatewayModelsFallback('martian', 'anthropic/claude-fable-5', request); + await applyGatewayModelsFallback('martian', 'anthropic/claude-fable-5', request); expect(request.body.models).toBeUndefined(); }); - it('removes caller-provided fallbacks for other models', () => { + it('removes caller-provided fallbacks for other models', async () => { const request = makeRequest('openai/gpt-4o', ['caller/fallback']); - applyGatewayModelsFallback('openrouter', 'openai/gpt-4o', request); + await applyGatewayModelsFallback('openrouter', 'openai/gpt-4o', request); expect(request.body.models).toBeUndefined(); });