在过去十年中的大部分时间里,API 调用看起来像是一次事务。客户端发送请求,等待,然后一次性收到完整的响应。这种模式正是大多数 API 管理工具(包括 API Gateway)所构建的基础,它仍然描述了海量的生产流量。
但这种模式不再适用于所有类型的流量。生成响应的模型在产生响应时会逐字元(token)输出,而不是在最后一次性批量输出。执行多步骤任务的智能体在过程中会报告进度。仪表板、协作编辑器、实时推理端点——这些都需要保持连接打开并双向传输数据。
在此之前,在 API Gateway 中构建这些体验的开发者不得不做出一个艰难的选择:将 API Gateway 置于其 API 前端从而放弃流式传输,或者绕过网关路由流式端点,从而放弃使网关变得至关重要的身份验证、API 密钥强制实施和可观测性。团队最终形成了分裂的基础设施——受治理的 API 走一条路径,不受治理的流式流量走另一条路径。
我们宣布,API Gateway 的流式传输支持现已在公共预览阶段可用。您的流式端点现在可以与其他服务一样,位于同一网关后面,并应用相同的策略。
对于 AI 应用程序,流式传输还加强了 API Gateway 与 Gemini 平台的连接。使用模型路由的网关现在可以将 Gemini 平台 Model Garden 中 Gemini、Claude 和 OpenAI gpt-oss 模型的响应在生成时进行流式传输,而您托管在其他任何地方的流式后端也以相同的方式工作。与 Gemini 平台中的 Apigee 和 Agent Gateway 并列,API Gateway 是 Google Cloud AI 网关功能的轻量级入口点。
您可以流式传输的内容
API Gateway 现在支持现代 API 所依赖的全范围流式模式:
服务器发送事件(SSE)——将模型输出流式传输到浏览器或客户端 SDK 的事实标准,也是大多数 OpenAI 兼容聊天完成 API 背后的传输协议。
分块传输编码和 HTTP 响应流式传输——包括换行符分隔的 JSON(NDJSON)以及任何其他逐步交付的响应体。响应会作为后端产生它们时直接传递,而不是缓冲并在最后交付。
WebSocket——用于聊天、协作、实时仪表板、通知和交互式会话的全双工连接。
gRPC 流式传输——服务端流式传输和双向流式传输,使您可以将网关置于以前无法代理的 gRPC 服务前端。
流式连接最多可保持打开一小时。与任何长生命周期连接一样,客户端应实现重连逻辑,以便会话在流结束后的生存能力——一个行为良好的 WebSocket 或 SSE 客户端无论如何都应该这样做。
您的 API 契约不变
这是我们最满意的部分:无需学习新内容。
流式传输不是不同的产品、不同的资源类型或描述 API 的不同方式。没有需要添加的新扩展,没有特定于流式的注释,也没有用于流式操作的分开的规范。描述您现有 API 的同一文档也描述您的流式 API。
您已经依赖的所有内容继续像以前一样工作:
API 密钥强制实施
JWT 验证,包括 Firebase、Auth0、Okta 和 Google ID 令牌
到后端的 Service Account(服务账号)身份验证
Cloud Logging(云日志记录)和 Cloud Monitoring(云监控)集成
路径模板和请求路由
在公共预览期间,您在创建网关时选择加入:
gcloud api-gateway gateways create GATEWAY_ID \
--api=API_ID \
--api-config=CONFIG_ID \
--location=REGION \
--enable-streaming
纯文本
这个单一标志就是全部差异。在 GA(一般可用性)阶段,计划是流式传输将成为 API Gateway 的默认行为方式,无需选择加入,并且与预览期间一样,无需更改您的 API 配置。
一个可预测的主机名
伴随流式传输功能,网关将获得一个确定性的默认主机名:
GATEWAY_ID-PROJECT_NUMBER.REGION.gateway.dev
(示例:streaming-test-12345.us-central1.gateway.dev)
纯文本
此前,默认的 gateway.dev 主机名包含一个生成的哈希值,这意味着在创建网关之前,你无法得知网关的 URL。这是一个看似微小却引发诸多摩擦的问题:在资源存在之前,你无法将端点写入客户端配置、Terraform 模块、CI 管道、防火墙白名单或合作伙伴集成文档中——而且如果你销毁环境并重新构建,URL 会随之改变。
现在,主机名是你已选定值的纯函数。你可以在网关存在之前生成它、将其模板化、记录文档并提交到源代码控制中,并且在同一项目和中继区域中重建同一网关时,每次都会获得相同的 URL。对于任何以代码方式管理环境或将配置从开发阶段推广到预生产阶段再到生产阶段的人来说,这消除了一整类引导问题。
为何这对 AI 工作负载至关重要
流式传输并非 AI 应用的利基功能——它是预期的交互模式。
用户期望看到模型生成的响应实时出现,而不是在三十秒的暂停之后。智能体需要报告中间步骤。工具调用协议和远程 MCP 服务器依赖于长期存在且增量交付的连接。无法进行流式传输的 API 管理层会严重限制 AI 应用的用户体验。
借助流式传输,API Gateway 成为处理此类智能体流量的可行入口——而入口的价值始终如一,只是更为紧迫。AI 端点成本高昂,消耗配额,需要针对每个消费者的身份验证和速率限制,并且需要具备可观测性。这些正是 AI 网关旨在解决的治理问题。另一种选择——每个团队都在自己的模型端点前搭建自己的代理——这会导致组织无法回答关于谁在调用什么资源等基本问题。
API Gateway 中的模型路由功能允许你暴露一个兼容 OpenAI 的单一端点,并根据请求负载中的 model 字段将请求分发给 Gemini、Claude 或 OpenAI gpt-oss 模型。模型路由加上流式传输意味着客户端可以获得熟悉的、基于标准的流式接口,同时你保留了对哪些模型可由谁在何种策略下访问的集中控制。
流式传输、模型路由和 MCP 支持都是 API Gateway 中的功能,为 Google Cloud 内的 AI 网关能力提供了轻量级的接入途径。这些 AI 网关能力使团队能够监控、保护和控制其生态系统中工具、模型和智能体的智能体流量,并以可组合套件的形式交付,以便你为每个用例选择正确的网关。对于更广泛的 AI 策略和全生命周期管理,请使用 Apigee。要管理和保护智能体连接性,请在 Gemini 平台中使用 Agent Gateway。你也可以组合使用这些网关:API Gateway 或 Apigee 可以位于你的智能体和 API 之前,或位于 Agent Gateway 之后,以便你的智能体能够访问模型、工具和 MCP 服务器。例如,你可以将智能体的出站流量通过 Agent Gateway 路由以实现严格的安全治理,然后将请求传递给 API Gateway 以处理模型路由。
公共预览期间须知
在功能处于预览阶段时,有几件事需要规划:
流式传输在网关创建时启用。现有的网关无法就地转换;请使用标志创建新网关以尝试该功能。
API Gateway 的以下功能在初始预览发布中不可用,但在预览期间将会推出:
通过 Serverless NEGs 和外部 Application Load Balancer 实现的自定义域名
通过 Cloud 控制台启用流式传输
Terraform
延迟指标反映的是流持续时间。对于流式连接,请求延迟衡量的是流的整个生命周期,而非首字节时间(time-to-first-byte),因此请留意长期存活的连接会改变你延迟图表的形态。
入门指南
如果你已经在使用 API Gateway,最快上手的方式是将一个支持流式传输的新网关指向一个现有的、能够发送 SSE 的后端服务,并观察事件如何逐步到达。无需进行任何规范变更。
For most of the last decade, an API call looked like a transaction. A client sent a request, waited, and got a complete response back in one piece. That model is exactly what most API management tooling — including API Gateway — was built to handle, and it still describes a huge amount of production traffic.
But that no longer describes all traffic types. A model generating a response emits tokens as it produces them, not in one batch at the end. An agent working through a multi-step task reports progress as it goes. A dashboard, a collaborative editor, a live inference endpoint — these all need a connection that stays open and carries data in both directions.
Until now, developers building those experiences in API Gateway had to make an uncomfortable choice: put API Gateway in front of their APIs and give up streaming, or route streaming endpoints around the gateway and give up the authentication, API key enforcement, and observability that made the gateway essential in the first place. Teams ended up with a split estate — governed APIs on one path, ungoverned streaming traffic on another.
We're announcing that streaming support in API Gateway is available in Public Preview. Your streaming endpoints can now sit behind the same gateway, with the same policies, as everything else you serve.
For AI applications, streaming also strengthens API Gateway's connection to Gemini platform. A gateway that uses model routing now streams responses from Gemini, Claude and OpenAI gpt-oss models in model garden in Gemini platform as they're generated, and streaming backends you host anywhere else work the same way. Alongside Apigee and Agent Gateway in Gemini platform, API Gateway is the lightweight entry point to Google Cloud's AI gateway capabilities.
What you can stream
API Gateway now supports the full range of streaming patterns modern APIs rely on:
Server-Sent Events (SSE) — the de facto standard for streaming model output to a browser or client SDK, and the transport behind most OpenAI-compatible chat completion APIs.
Chunked transfer encoding and HTTP response streaming — including newline-delimited JSON (NDJSON) and any other progressively-delivered response body. Responses are passed through as your backend produces them rather than buffered and delivered at the end.
WebSockets — full-duplex connections for chat, collaboration, live dashboards, notifications, and interactive sessions.
gRPC streaming — server-side streaming and bidirectional streaming, so you can put a gateway in front of gRPC services that were previously impossible to proxy.
Streams can stay open for up to one hour. As with any long-lived connection, clients should implement reconnection logic so that a session survives the end of a stream — a well-behaved WebSocket or SSE client should be doing this anyway.
Your API contract doesn't change
This is the part we are most pleased with: there is nothing new to learn.
Streaming is not a different product, a different resource type, or a different way of describing your API. There are no new extensions to add, no streaming-specific annotations, no separate spec for streaming operations. The same document that describes your existing API describes your streaming API.
Everything you already rely on continues to work exactly as before:
API key enforcement
JWT validation, including Firebase, Auth0, Okta, and Google ID tokens
Service account authentication to backends
Cloud Logging and Cloud Monitoring integration
Path templating and request routing
During Public Preview, you opt-in when creating the gateway:
gcloud api-gateway gateways create GATEWAY_ID \
--api=API_ID \
--api-config=CONFIG_ID \
--location=REGION \
--enable-streaming
Plain text
That single flag is the entire difference. At GA, it is planned that streaming will simply be how API Gateway behaves, with no opt-in required and, as during Preview, no changes to your API config.
A hostname you can predict
Alongside streaming, gateways get a deterministic default hostname:
GATEWAY_ID-PROJECT_NUMBER.REGION.gateway.dev
(Example: streaming-test-12345.us-central1.gateway.dev)
Plain text
Previously the default gateway.dev hostname included a generated hash, which meant you could not know your gateway's URL until after you had created it. That is a small thing that causes a lot of friction: you can't write the endpoint into a client config, a Terraform module, a CI pipeline, a firewall allowlist, or a partner integration doc until the resource exists — and if you tear the environment down and rebuild it, the URL changes underneath you.
Now the hostname is a pure function of values you already chose. You can generate it, template it, document it, and commit it to source control before the gateway exists, and you get the same URL every time you rebuild the same gateway in the same project and region. For anyone managing environments as code, or promoting a configuration from dev to staging to prod, this removes an entire category of bootstrapping problem.
Why this matters for AI workloads
Streaming isn't a niche feature for AI applications — it's the expected interaction pattern.
Users expect to see a model's response appear as it's generated, not after a thirty-second pause. Agents need to report intermediate steps. Tool-calling protocols and remote MCP servers depend on long-lived, incrementally-delivered connections. An API management layer that can't stream severely limits the user experience of AI applications.
With streaming, API Gateway becomes a viable front door for that agentic traffic — and the value of a front door is exactly the same as it's always been, only more urgent. AI endpoints are expensive, they consume quota, they need per-consumer authentication and rate limiting, and they need to be observable. These are the governance problems that an AI gateway is intended to solve. The alternative — every team standing up its own proxy in front of its own model endpoint — is how organizations end up unable to answer basic questions about who is calling what.
Model routing in API Gateway lets you expose a single OpenAI-compatible endpoint and dispatch requests to Gemini, Claude, or OpenAI gpt-oss models based on the model field in the request payload. Model routing plus streaming means clients get a familiar, standards-based streaming interface, while you retain centralized control over which models are reachable, by whom, and under what policy.
Streaming, model routing and MCP support are all features within API Gateway that provide a lightweight on-ramp to AI gateway capabilities within Google Cloud. These AI gateway capabilities allow teams to monitor, secure and control agentic traffic of tools, models and agents in their ecosystem, and they're delivered as a composable suite so you can choose the right gateway for each use case. For more expansive AI policies and full lifecycle management, use Apigee . To manage and secure agent connectivity, use Agent Gateway in Gemini platform. You can also combine these gateways: API Gateway or Apigee can sit in front of your agents and APIs, or behind Agent Gateway so your agents can reach models, tools, and MCP servers. 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 model routing.
What to know during Public Preview
A few things to plan around while the feature is in Preview:
Streaming is enabled at gateway creation time. Existing gateways can't be converted in place; create a new gateway with the flag to try it.
A few API Gateway features are not available at the initial Preview launch, but will be coming during the Preview time period. Custom domains via Serverless NEGs and an external Application Load Balancer
Streaming enablement via the Cloud console
Terraform
Latency metrics reflect stream duration. Request latency for a streaming connection measures the life of the stream, not time-to-first-byte, so expect long-lived connections to change the shape of your latency graphs.
Get started
If you're already using API Gateway, the fastest way to see this working is to point a new streaming-enabled gateway at an existing backend that emits SSE and watch the events arrive incrementally. No spec changes needed.
首次收录 · 2026-10-23 · 8.4 分