使用 Google Cloud API Gateway 进行模型路由
Mak Ahmad
Sanjay Pujare
在构建 AI 应用时,开发者需要能够自由地将流量路由到最适合当前任务的模型,而无需硬编码端点或管理开源代理。Google Cloud API Gateway 现已推出公开预览版的模型路由功能来解决这一问题。它提供了一个轻量级、无服务器的入口层,可接受 OpenAI 兼容的请求,并将其动态路由到 Gemini、Claude 或 OpenAI OSS-GPT。这种 AI 网关模式通常被称为 LLM 网关或集中式 LLM 端点。
API Gateway 可以独立使用,用于简单的基于规则的路由、速率限制和 token 追踪,也可以与 Google Cloud 更广泛的 AI 网关体系以及 Gemini Enterprise Agent Platform 无缝配合。
例如,你可以通过 Agent Gateway 路由智能体的出站流量以实现严格的安全治理,然后将请求传递给 API Gateway,由其处理到 Google 托管 LLM 的动态路由。以下是配置路由逻辑的分步指南。
路由你的流量
这为你的所有 LLM 流量提供了一个单一、稳定的端点,因此你可以在中心位置添加或更换后端模型,而无需更改客户端代码。而且由于应用程序是对 Gateway 进行身份验证,而非对模型提供商进行身份验证,客户端认证与后端 LLM 认证保持分离,让你无需改动应用即可轮换或更改后端凭证。
- 配置你的路由规则:你可以使用新的
x-google-api-management扩展块,在 OpenAPI 3.x 规范中直接将虚拟模型名称映射到特定的后端目标。
openapi: 3.0.4
info:
title: OpenAPI 3.x spec using Model Routing
description: Using Model Routing in an OAS 3.x spec
version: 1.0.0
x-google-api-management:
backends:
gemini-35-flashlite:
address: >-
https://aiplatform.googleapis.com/v1/projects/YOUR_PROJECT_ID/locations/global/publishers/google/models/gemini-3.5-flash-lite:generateContent
deadline: 60.0
pathTranslation: CONSTANT_ADDRESS
anthropic-claude-opus-47:
address: >-
https://aiplatform.googleapis.com/v1/projects/YOUR_PROJECT_ID/locations/global/publishers/anthropic/models/claude-opus-4-7:rawPredict
deadline: 60.0
pathTranslation: CONSTANT_ADDRESS
openai-gpt-oss-120b:
address: >-
https://aiplatform.googleapis.com/v1/projects/YOUR_PROJECT_ID/locations/global/endpoints/openapi/chat/completions
deadline: 60.0
pathTranslation: CONSTANT_ADDRESS
ai:
models:
routing:
routers:
# Router 1: route between Gemini (default) and Claude.
gemini-claude-router:
defaultModel:
backend: gemini-35-flashlite
targetModel: google/gemini-3.5-flash-lite
rules:
- model: "claude-opus-4-7"
backend: anthropic-claude-opus-47
targetModel: anthropic/claude-opus-4-7
# Router 2: route between OpenAI GPT (default) and Gemini.
openai-gemini-router:
defaultModel:
backend: openai-gpt-oss-120b
targetModel: openai/gpt-oss-120b-maas
rules:
- model: "gemini-3.5-flash-lite"
backend: gemini-35-flashlite
targetModel: google/gemini-3.5-flash-lite
servers:
- url: "https://my-gateway.example.com"
paths:
/v1/chat/gemini-claude:
post:
summary: "Endpoint:defaults to Gemini & Claude as an option."
operationId: "chatGeminiClaude"
x-google-model-router: gemini-claude-router
responses:
'200':
description: "OK"
/v1/chat/openai-gemini:
post:
summary: "Endpoint:defaults to OpenAI & Gemini as an option."
operationId: "chatOpenAIGemini"
x-google-model-router: openai-gemini-router
responses:
'200':
description: "OK" 注意:单个路由器引用的所有后端必须共享同一主机(例如 aiplatform.googleapis.com)。路由会在该共享的 Agent Platform 主机上选择不同的模型和路径——它不会跨不同主机进行路由。
2. 部署网关:部署你更新后的 API 配置,使网关处于活动状态并准备好处理流量。
3. 发送标准请求:你的应用只需发送标准的 OpenAI POST /v1/chat/gemini-claude 或 POST /v1/chat/openai-gemini 请求。网关会拦截该请求,将负载转码为后端的原生 schema,添加所需的 agent platform 认证 token,并即时进行路由。例如(为 $API_KEY 和 my-gateway.example.com 使用适当的值):
curl -X POST "https://my-gateway.example.com/v1/chat/gemini-claude" \
-H "content-type: application/json" \
-H "x-api-key: $API_KEY" \
-d '{
"model": "claude-opus-4-7",
"messages": [
{"role": "user", "content": "Introduce yourself in 5 words"}
]
}' 模型路由现已在 API Gateway 中开放 Public Preview。要停止管理代理并开始统一你的 AI 流量,请查看我们的文档,今天就部署你的第一个模型路由器。
这些模型路由能力是 Google Cloud 更广泛 AI 网关体系的一部分:通过 Apigee 实现可扩展的 API 与工具管理,并在 Gemini Enterprise Agent Platform 中通过 Agent Gateway 实现端到端的智能体治理,因此你可以从轻量级起步,在需要时再逐步扩展到其余能力。
Model routing with Google Cloud API Gateway
Mak Ahmad
Sanjay Pujare
When building AI applications, developers need the freedom to route traffic to the best model for the job without hardcoding endpoints or managing open-source proxies. Google Cloud API Gateway now offers model routing in Public Preview to solve this. It provides a lightweight, serverless ingress layer that accepts OpenAI-compatible requests and dynamically routes them to Gemini, Claude, or OpenAI OSS-GPT. This AI gateway pattern is commonly referred to as an LLM gateway or centralized LLM endpoint.
API Gateway can be used standalone for simple rules-based routing, rate limiting and token tracking, or paired seamlessly with Google Cloud’s broader AI gateway spectrum and Gemini Enterprise Agent Platform. For example, you can route your agent's egress through Agent Gateway for strict security governance, and then pass the request to API Gateway to handle dynamic routing to Google-hosted LLMs. Here is a step-by-step guide on how to configure your routing logic.
Routing your traffic
This gives you a single, stable endpoint for all your LLM traffic, so you can add or swap backend models centrally without changing client code. And because applications authenticate to the Gateway, not to the model providers, client auth stays separate from backend LLM auth, letting you rotate or change backend credentials without touching your apps.
- Configure your routing rules: You can map virtual model names to specific backend targets directly in your OpenAPI 3.x specification using the new
x-google-api-managementextension block.
openapi: 3.0.4
info:
title: OpenAPI 3.x spec using Model Routing
description: Using Model Routing in an OAS 3.x spec
version: 1.0.0
x-google-api-management:
backends:
gemini-35-flashlite:
address: >-
https://aiplatform.googleapis.com/v1/projects/YOUR_PROJECT_ID/locations/global/publishers/google/models/gemini-3.5-flash-lite:generateContent
deadline: 60.0
pathTranslation: CONSTANT_ADDRESS
anthropic-claude-opus-47:
address: >-
https://aiplatform.googleapis.com/v1/projects/YOUR_PROJECT_ID/locations/global/publishers/anthropic/models/claude-opus-4-7:rawPredict
deadline: 60.0
pathTranslation: CONSTANT_ADDRESS
openai-gpt-oss-120b:
address: >-
https://aiplatform.googleapis.com/v1/projects/YOUR_PROJECT_ID/locations/global/endpoints/openapi/chat/completions
deadline: 60.0
pathTranslation: CONSTANT_ADDRESS
ai:
models:
routing:
routers:
# Router 1: route between Gemini (default) and Claude.
gemini-claude-router:
defaultModel:
backend: gemini-35-flashlite
targetModel: google/gemini-3.5-flash-lite
rules:
- model: "claude-opus-4-7"
backend: anthropic-claude-opus-47
targetModel: anthropic/claude-opus-4-7
# Router 2: route between OpenAI GPT (default) and Gemini.
openai-gemini-router:
defaultModel:
backend: openai-gpt-oss-120b
targetModel: openai/gpt-oss-120b-maas
rules:
- model: "gemini-3.5-flash-lite"
backend: gemini-35-flashlite
targetModel: google/gemini-3.5-flash-lite
servers:
- url: "https://my-gateway.example.com"
paths:
/v1/chat/gemini-claude:
post:
summary: "Endpoint:defaults to Gemini & Claude as an option."
operationId: "chatGeminiClaude"
x-google-model-router: gemini-claude-router
responses:
'200':
description: "OK"
/v1/chat/openai-gemini:
post:
summary: "Endpoint:defaults to OpenAI & Gemini as an option."
operationId: "chatOpenAIGemini"
x-google-model-router: openai-gemini-router
responses:
'200':
description: "OK" Note: All backends referenced by a single router must share the same host (for example, aiplatform.googleapis.com). Routing selects a different model and path on that shared Agent Platform host — it does not route across different hosts.
2. Deploy the Gateway: Deploy your updated API config so the Gateway is active and ready to process traffic.
3. Send standard requests: Your application simply sends a standard OpenAI POST /v1/chat/gemini-claude or POST /v1/chat/openai-gemini request. The Gateway intercepts it, transcodes the payload to the native schema of the backend, adds the required agent platform authentication token, and routes it on the fly. As an example (use appropriate values for $API_KEY and my-gateway.example.com) :
curl -X POST "https://my-gateway.example.com/v1/chat/gemini-claude" \
-H "content-type: application/json" \
-H "x-api-key: $API_KEY" \
-d '{
"model": "claude-opus-4-7",
"messages": [
{"role": "user", "content": "Introduce yourself in 5 words"}
]
}' Model routing is now available in Public Preview for API Gateway. To stop managing proxies and start unifying your AI traffic, check out our documentation to deploy your first model router today.
These model routing capabilities are part of Google Cloud's broader AI gateway spectrum: expansive API and tools management with Apigee, and end-to-end agent governance with Agent Gateway in the Gemini Enterprise Agent Platform, so you can start lightweight and grow into the rest when you need it.