@@ -809,6 +809,7 @@ function custom(dep: CustomDep): Record<string, CustomLoader> {
809809 const { createUnified } = yield * Effect . promise ( ( ) => import ( "ai-gateway-provider/providers/unified" ) )
810810 const { createOpenAI } = yield * Effect . promise ( ( ) => import ( "ai-gateway-provider/providers/openai" ) )
811811 const { createAnthropic } = yield * Effect . promise ( ( ) => import ( "ai-gateway-provider/providers/anthropic" ) )
812+ const { createOpenAICompatible } = yield * Effect . promise ( ( ) => import ( "@ai-sdk/openai-compatible" ) )
812813
813814 const metadata = iife ( ( ) => {
814815 if ( input . options ?. metadata ) return input . options . metadata
@@ -855,9 +856,23 @@ function custom(dep: CustomDep): Record<string, CustomLoader> {
855856 // The Unified API addresses Workers AI both with the explicit "workers-ai/" prefix and as
856857 // bare "@cf/..." ids. Third-party providers must not receive the token; they rely on the
857858 // gateway's stored/BYOK keys instead.
859+ // Workers AI is Cloudflare's own upstream, so it rides the unified compat route with the
860+ // Cloudflare token as its upstream Authorization header.
858861 const isWorkersAi = modelID . startsWith ( "workers-ai/" ) || modelID . startsWith ( "@cf/" )
859- const unified = createUnified ( isWorkersAi ? { apiKey : apiToken } : { } )
860- return aigateway ( unified ( modelID ) )
862+ if ( isWorkersAi ) return aigateway ( createUnified ( { apiKey : apiToken } ) ( modelID ) )
863+
864+ // Every other third-party provider (google, xai, alibaba, deepseek, moonshotai, …) is only
865+ // served by Cloudflare's catalog-aware REST API. The universal/compat gateway route rejects
866+ // them with "Invalid provider" (the gateway's compat endpoint doesn't front those upstreams),
867+ // so point an OpenAI-compatible client at the REST endpoint and bind it to the gateway with
868+ // cf-aig-gateway-id — that keeps requests gateway-routed (analytics/caching/BYOK), not a
869+ // bypass. models.dev ids (provider/model, dotted) pass through unchanged.
870+ return createOpenAICompatible ( {
871+ name : "cloudflare-ai-gateway" ,
872+ baseURL : `https://api.cloudflare.com/client/v4/accounts/${ accountId } /ai/v1` ,
873+ apiKey : apiToken ,
874+ headers : { "cf-aig-gateway-id" : gateway } ,
875+ } ) ( modelID )
861876 } ,
862877 options : { } ,
863878 }
0 commit comments