官方更新 Google Developers Blog 正文 ok 474 words

面向 AI 模型路由的统一 API

英文标题:A unified API for AI model routing

Google Cloud API Gateway 现已在 Public Preview 中推出模型路由功能,可接收兼容 OpenAI 的请求,并动态转发至 Gemini、Claude 或 OpenAI OSS-GPT 等模型。开发者无需在应用中硬编码模型端点,也不必自行维护开源代理,即可通过 OpenAPI 3.x 配置虚拟模型名称、后端目标及路由规则。该功能还能与 Gemini Enterprise Agent Platform 配合使用,在安全治理与模型动态分流之间建立统一入口。对开发者而言,这意味着可更灵活地按任务、成本或能力选择模型,并简化多模型 AI 应用的流量管理。
打开原文相关 6 · 机会 5
media11.7 KB media54.1 KB media83.9 KB media64.7 KB media114.8 KB media351.9 KB media57.3 KB

中文

构建 AI 应用时,开发者需要能够自由地将流量路由至最适合任务的模型,而不必硬编码端点或管理开源代理。Google Cloud API Gateway 现已在 Public Preview 中提供模型路由功能,以解决这一问题。它提供了一个轻量级、无服务器的入口层,可接收兼容 OpenAI 的请求,并将其动态路由至 Gemini、Claude 或 OpenAI OSS-GPT。

API Gateway 可以单独用于实现简单的速率限制和 token 跟踪,也可以与 Gemini Enterprise Agent Platform 无缝配合。例如,你可以通过 Agent Gateway 路由 Agent 的出站流量,以实施严格的安全治理,然后将请求传递给 API Gateway,由后者负责将流量动态路由至 Google 托管的 LLM。以下是配置路由逻辑的分步指南。

路由你的流量

只需几个步骤即可设置模型路由逻辑:

  1. **配置路由规则:**你可以使用新的 x-google-api-management 扩展块,直接在 OpenAPI 3.x 规范中将虚拟模型名称映射至特定后端目标。

yaml 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:

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)。路由会在这个共享的 Vertex 主机上选择不同的模型和路径,而不会跨不同主机进行路由。

  1. **部署 Gateway:**部署更新后的 API 配置,使 Gateway 启用并准备处理流量。

  2. **发送标准请求:**你的应用只需发送标准的 OpenAI POST /v1/chat/gemini-claude 或 POST /v1/chat/openai-gemini 请求。Gateway 会拦截请求,将负载转码为后端的原生 schema,并即时完成路由。示例如下(请为 $API_KEY 和 my-gateway-url.com 使用相应的值):

shell curl -X POST "https://my-gateway-url.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 流量,请查阅我们的文档,立即部署你的首个模型路由器。

English / 原文

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.

API Gateway can be used standalone for simple rate limiting and token tracking, or paired seamlessly with the 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

Setting up your model routing logic takes just a few steps:

  1. 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-management extension 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-url.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"

YAML

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 Vertex 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, and routes it on the fly. As an example (use appropriate values for $API_KEY and my-gateway-url.com) :

curl -X POST "https://my-gateway-url.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"}
        ]
      }'

Shell

Get started

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.