Skip to content

Commit ebd74b8

Browse files
fix(proxy): handle 404 with model-not-found guidance (#1748)
* fix(proxy): handle BYOK 404 with model-not-found guidance After user BYOK error handling, return a helpful message when the upstream provider responds with 404, indicating the model does not exist or is no longer available. Recommends switching to kilo-auto/balanced, with explicit /model instructions for kiloclaw/openclaw users. * fix: move 404 model-not-found handler outside BYOK block The 404 handler should apply to all users, not just BYOK users. * fix: recommend kilo-auto/free when user balance is zero When the user has no credits, recommend kilo-auto/free instead of kilo-auto/balanced in the 404 model-not-found message. * fix: add BYOK-specific 404 message without auto model recommendation BYOK users get a plain message pointing them to check the model name and their API provider, while non-BYOK users still get the auto model recommendation. * fix: drop kilocode/ prefix from non-claw 404 recommendation * fix: remove BYOK 404 entry from byokErrorMessages * Format --------- Co-authored-by: kiloconnect[bot] <240665456+kiloconnect[bot]@users.noreply.github.com> Co-authored-by: Christiaan Arnoldus <christiaan.arnoldus@outlook.com>
1 parent e2e2d46 commit ebd74b8

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

src/app/api/openrouter/[...path]/route.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,8 @@ export async function POST(request: NextRequest): Promise<NextResponseType<unkno
591591
request: requestBodyParsed,
592592
response,
593593
isUserByok: !!userByok,
594+
feature,
595+
balance: (await balanceAndSettingsPromise).balance,
594596
});
595597
if (errorResponse) {
596598
return errorResponse;

src/lib/llm-proxy-helpers.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,15 @@ export async function makeErrorReadable({
142142
request,
143143
response,
144144
isUserByok,
145+
feature,
146+
balance,
145147
}: {
146148
requestedModel: string;
147149
request: GatewayRequest;
148150
response: Response;
149151
isUserByok: boolean;
152+
feature: FeatureValue | null;
153+
balance: number;
150154
}) {
151155
if (response.status < 400) {
152156
return undefined;
@@ -163,6 +167,16 @@ export async function makeErrorReadable({
163167
}
164168
}
165169

170+
if (response.status === 404) {
171+
const recommendedModel = balance <= 0 ? KILO_AUTO_FREE_MODEL : KILO_AUTO_BALANCED_MODEL;
172+
const recommendation =
173+
feature === 'kiloclaw' || feature === 'openclaw'
174+
? `The model "${requestedModel}" does not exist or is no longer available. We recommend switching to ${recommendedModel.name}: /model kilocode/${recommendedModel.id}`
175+
: `The model "${requestedModel}" does not exist or is no longer available. We recommend switching to ${recommendedModel.id}.`;
176+
warnExceptInTest(`Responding with 404 ${recommendation}`);
177+
return NextResponse.json({ error: recommendation, message: recommendation }, { status: 404 });
178+
}
179+
166180
// Sometimes we get generic or nonsensical errors when the context length is exceeded
167181
// (such as "Internal Server Error" or "No allowed providers are available for the selected model")
168182
const model = kiloFreeModels.find(m => m.public_id === requestedModel);

0 commit comments

Comments
 (0)