Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ export function getMaxTokens(request: GatewayRequest) {
return request.body.max_completion_tokens ?? request.body.max_tokens ?? null;
}

export function getReasoningEffort(request: GatewayRequest) {
if (request.kind === 'messages') {
return request.body.output_config?.effort ?? null;
}
if (request.kind === 'responses') {
return request.body.reasoning?.effort ?? null;
}
return request.body.reasoning?.effort ?? request.body.reasoning_effort ?? null;
}

export function getReasoningEffortTimeoutSuggestion(effort: string | null): string {
return effort === 'xhigh' || effort === 'max'
? ` Try lowering the reasoning effort from "${effort}" to "high" or lower to reduce the chance of timeouts.`
: '';
}

export function hasMiddleOutTransform(request: GatewayRequest) {
return (
(request.kind === 'chat_completions' && request.body.transforms?.includes('middle-out')) ||
Expand Down
2 changes: 2 additions & 0 deletions apps/web/src/lib/ai-gateway/providers/upstream-attempt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { applyProviderSpecificLogic } from '@/lib/ai-gateway/providers/apply-pro
import type { GetProviderProviderResult } from '@/lib/ai-gateway/providers/get-provider';
import { isValidOpenRouterModelId } from '@/lib/ai-gateway/providers/gateway-models-cache';
import type { GatewayRequest } from '@/lib/ai-gateway/providers/openrouter/types';
import { getReasoningEffort } from '@/lib/ai-gateway/providers/openrouter/request-helpers';
import { upstreamRequest } from '@/lib/ai-gateway/providers/upstream-request';
import type { FraudDetectionHeaders } from '@/lib/utils';

Expand Down Expand Up @@ -92,6 +93,7 @@ export async function sendUpstreamAttempt({
provider: providerContext.provider,
signal,
vercelRequestId,
reasoningEffort: getReasoningEffort(request),
});
if (result.type === 'error') return result;

Expand Down
14 changes: 11 additions & 3 deletions apps/web/src/lib/ai-gateway/providers/upstream-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
GatewayMessagesRequest,
} from '@/lib/ai-gateway/providers/openrouter/types';
import { ATTRIBUTION_HEADERS } from '@/lib/ai-gateway/providers/openrouter/attribution-headers';
import { getReasoningEffortTimeoutSuggestion } from '@/lib/ai-gateway/providers/openrouter/request-helpers';
import type { GatewayChatApiKind, Provider } from '@/lib/ai-gateway/providers/types';
import { after, NextResponse } from 'next/server';
import { ProxyErrorType } from '@/lib/proxy-error-types';
Expand Down Expand Up @@ -172,14 +173,15 @@ function clientDisconnectResponse(vercelRequestId: string | null | undefined) {

function upstreamFetchFailureResponse(
failureFamily: UpstreamFetchFailureFamily,
vercelRequestId: string | null | undefined
vercelRequestId: string | null | undefined,
reasoningEffort: string | null
) {
const error = withRequestId(
failureFamily === 'request_timeout' ||
failureFamily === 'headers_timeout' ||
failureFamily === 'connect_timeout' ||
failureFamily === 'read_timeout'
? 'The upstream provider did not send response headers before the gateway timeout.'
? `The upstream provider did not send response headers before the gateway timeout.${getReasoningEffortTimeoutSuggestion(reasoningEffort)}`
: 'The upstream provider closed the connection before sending a response.',
vercelRequestId
);
Expand All @@ -203,6 +205,7 @@ export async function upstreamRequest({
provider,
signal,
vercelRequestId,
reasoningEffort,
}: {
chatApi: GatewayChatApiKind;
search: string;
Expand All @@ -213,6 +216,7 @@ export async function upstreamRequest({
signal?: AbortSignal;
/** Incoming `x-vercel-id`, used to correlate failures with the platform logs. */
vercelRequestId?: string | null;
reasoningEffort: string | null;
}): Promise<{ type: 'success'; response: Response } | { type: 'error'; response: NextResponse }> {
const headers = new Headers();
for (const [key, value] of Object.entries(ATTRIBUTION_HEADERS)) {
Expand Down Expand Up @@ -309,7 +313,11 @@ export async function upstreamRequest({
type: 'error',
response: causedByClientDisconnect
? clientDisconnectResponse(vercelRequestId)
: upstreamFetchFailureResponse(failureFamily ?? 'unknown', vercelRequestId),
: upstreamFetchFailureResponse(
failureFamily ?? 'unknown',
vercelRequestId,
reasoningEffort
),
};
}
}
Expand Down
Loading