读见

智能体是会话,而非请求:为 AI 智能体会话配置 GKE 节点池

原题Agents are sessions, not requests: size GKE node pools for AI agent sessions

约 8 分钟约 1,776 字原文英文已翻译

读前速览

4 个要点

本文介绍如何通过将空闲沙箱挂起并存入对象存储,优化 GKE 节点池以支持 AI 智能体会话;与每个会话使用一个 Pod 的方案相比,成本最高可节省 40%。

  1. 智能体会话的资源使用模式与 Web 请求有根本区别

    Web 请求只持续几毫秒,并随流量变化而扩缩容;智能体会话则会在整个工作日保持开启,但只有 22% 的时间在工作(GitHub Copilot 数据显示,两次提示之间有 80% 的时间处于空闲状态)。如果每个会话都占用一台专用机器,就会造成显著的成本浪费。

  2. 将空闲沙箱暂存到对象存储,可提高资源密度并节省成本

    Agent Substrate 的 SuspendActor 会将空闲智能体快照存入对象存储并释放工作节点,从而让多个智能体共享相同的计算资源。对于每小时发送 2 次提示的 100 个编程智能体,该方案可节省 40% 的节点费用,只需 15 个节点,而非 34 个。

  3. 成本效益很大程度上取决于提示频率

    每小时 2 次提示时,暂存可节省 40%;每小时 4 次时,节省幅度降至 4%;达到每小时 4.5 次或更高时,每个会话使用一个 Pod 会更便宜。盈亏平衡分析表明,团队必须衡量实际提示频率,才能选择最优策略。

  4. 集群规模和 CPU 超额分配会影响不同方案的经济性

    约有 37 个编程会话或 13 个聊天助手会话时,暂存方案即可带来收益。CPU 超额分配(声明的容量高于节点实际拥有的容量)可能让每个会话使用一个 Pod 的方案更便宜,因为每个节点可以容纳 12 个 Pod;但在构建和测试期间,智能体争抢 CPU 会导致性能下降。

完整摘要

本文探讨如何优化在 GKE 集群上运行 AI 编程智能体的成本:智能体会话会全天保持开启,但只有 22% 的时间在工作。文章在传统方案之外提出第三种选择:保持会话开启,同时将空闲沙箱暂存到对象存储。文中介绍了开源平台 Agent Substrate 和 AX,说明它们如何通过 SuspendActor 快照和热工作池实现这一方案。成本模型显示,对于每小时发送 2 次提示的 100 个编程智能体,暂存方案可节省 40% 的节点费用;但提示频率提高后,节省幅度会缩小(每小时 4 次提示时仅节省 4%)。盈亏平衡点约为每小时 4.5 次提示,超过后每个会话使用一个 Pod 更便宜。文章还介绍了如何配置热池、实施定时扩缩容,以及衡量提示频率,从而确定最优策略。

目录 · 7 节

查看原文

抱歉,您的浏览器不支持播放此视频。Sorry, your browser doesn't support playback for this video

你的公司希望将编码代理从开发者笔记本迁移到共享的 GKE 集群,以实现持久化、扩展能力和治理。每个代理会话都会全天保持开启,但实际只工作一部分时间;运行方式将决定节点费用中有多少花在了等待上。Your company wants to move its coding agents from developer laptops to a shared GKE cluster, for persistence, scale, and governance. Each of those agent sessions stays open all day but works only part of it, and how you run them decides how much of your node bill pays for waiting.

读完本文后,你将了解为什么 Agent 会话需要不同于 Web 请求的运行时、Agent Substrate 和 AX 如何在 GKE 上运行会话,以及如何在 GKE 节点池中发现成本优化机会。本文假设你了解 Kubernetes Pod 和节点池。如果不了解,请参阅 GKE 概览。By the end of this article, you will know why an agent session needs a different runtime than a web request, how Agent Substrate and AX run sessions on GKE, and how to identify cost optimization opportunities in your GKE node pools. This article assumes you know Kubernetes Pods and node pools. If not, check out the GKE overview.

代理会话在提示之间等待Agent sessions wait between prompts

一次 Web 请求只持续几毫秒。服务器响应请求后不保留任何状态,接着处理下一个请求,因此 Kubernetes 会根据流量扩缩 Web 应用:请求越多,Pod 就越多。A web request lasts milliseconds. The server answers it, keeps nothing, and serves the next one, so Kubernetes scales a web app by its traffic: more requests, more Pods.

Agent 会话的工作方式有所不同。编码代理的工作区可以在开发者整个工作日内保持打开:它会在不同提示之间保留记忆和文件,并且只有在收到提示后才会工作。An agent session works another way. A coding agent's workspace can stay open for the developer's whole workday: it keeps its memory and its files between prompts, and it works only after a prompt arrives.

01-request-vs-session
Figure 1. A request ends in milliseconds and keeps nothing. An agent session stays open all day and works in short turns (drawn to scale for 2 prompts an hour).

智能体当天真正工作的时间有多少?远少于它等待的时间。在一项对 GitHub Copilot 智能体运行轨迹的研究中,Banruo Liu 及其同事发现,在一次包含多个提示的典型会话中,智能体约有 80% 的时间都在等待用户。一轮交互——即一个提示及智能体对此作出的完整回应——平均需要 396.3 秒的智能体工作时间。How much of that day does the agent really work? Much less than it waits. In a study of GitHub Copilot agent traces, Banruo Liu and colleagues found that in a typical session with several prompts, the agent waits for the user about 80% of the time. A turn, one prompt and the agent's whole response to it, takes 396.3 seconds of agent work on average.

把这些数字放到一位开发者的一天中来看。按照我们的假设,每小时发出 2 条提示,在 8 小时工作日里,智能体有 22% 的时间在工作,其余 78% 的时间都在等待。如果每个会话都会占用一台机器,那么这 78% 的空闲时间你也得付费。Put those numbers on one developer's day. With 2 prompts an hour over an 8 hour workday, which is our assumption, the agent works 22% of the day and waits for the other 78%. If each session holds a machine, you pay for that 78%.

每个会话使用一个 Pod,或缩容至零One Pod per session or scale to zero

团队通常会选择两种答案之一,这两种答案都很适用于 Web 应用。Teams usually pick one of two answers, and both work well for web apps.

第一种做法是为每个会话分配一个 Pod。会话会保留自身状态,但其 Pod 全天占用 1 个 vCPU 和 1 GiB 内存。例如,扣除 GKE 预留资源后,一个 c3-standard-4 节点可容纳 3 个这样的 Pod。因此,在我们的成本模型中,100 个编码代理需要 34 个节点,每月费用约为 1,288 美元;这些代理的工作时间占比为 22%。The first is to give each session its own Pod. The session keeps its state, but its Pod holds 1 vCPU and 1 GiB all day. For example, a c3-standard-4 node fits 3 such Pods after GKE's reservations, so in our cost model 100 coding agents need 34 nodes and about USD 1,288 a month, for agents that work 22% of the time.

第二种方法是在提示之间将 Pod 缩容至零。节点资源会被释放,持久卷上的文件会保留,但进程及其内存不会,因此下一个提示会启动一个新的代理进程。The second is to scale the Pod to zero between prompts. The node is free, and files on a persistent volume survive, but the process and its memory do not, so the next prompt starts a new agent process.

基于此,你想要第三种方案:保留会话,在会话等待期间释放机器。Based on that, you want a third option: keep the session, and free the machine while the session waits.

将空闲的代理会话存放在对象存储中Park idle agent sessions in object storage

Agent Substrate 和 AX 正是为此而构建的:开发者的会话保持开启,而为其提供服务的沙箱会在会话等待期间释放所占用的机器。二者共同覆盖了智能体平台中对所有团队都相同的部分:控制平面、运行时和沙箱。Agent Substrate 解决智能体在哪里运行、如何运行的问题;AX 则回答智能体是什么。Agent Substrate and AX are built for that: the developer's session stays open, and the sandbox that serves it gives its machine back while the session waits. Together they cover the parts of an agentic platform that are the same for every team: the control plane, the runtime, and the sandbox. Agent Substrate answers where and how an agent runs. AX answers what the agent is.

继 gVisor、Agent Substrate 和 OpenRL 于 2026 年 11 月捐赠给 CNCF 后,您可以灵活地在任何环境中运行这一开源智能体平台。Agent Substrate 1.0 已在 GKE 上正式推出。Following the CNCF donation of gVisor, Agent Substrate, and OpenRL in November 2026, you have the flexibility to run this open source, agentic platform across any environment. Agent Substrate 1.0 is generally available on GKE.

02-building-blocks
Figure 2. The building blocks of an agent platform. AX covers the control plane, and Agent Substrate covers the runtime and the sandbox.

从这里开始,有一个区别很重要:会话是开发者与智能体之间的多轮交互,包含其对话历史。为会话提供服务的计算环境是一个隔离的沙箱:在 Agent Substrate 中称为 actor,在 AX 中称为 Task。挂起操作作用于沙箱,而会话仍保持打开状态。One distinction helps from here on. The session is the multi turn interaction between the developer and the agent, with its conversation history. The compute that serves it is an isolated sandbox: an actor in Agent Substrate, or a Task in AX. Parking acts on the sandbox, and the session stays open.

Agent Substrate 说明了 agent 在哪里以及如何运行。它将有状态的 agent 与其运行所依赖的计算资源分离。actor 是单个 agent 的沙箱,承载该 agent 正在运行的进程、文件和身份。worker 是从 Kubernetes Pod 热池中取用的可复用沙箱,一个 worker 可承载多个 actor。Agent Substrate answers where and how an agent runs. It separates the stateful agent from the compute it runs on. An actor is the sandbox for one agent, and it holds the agent's running processes, files, and identity. A worker is a reusable sandbox drawn from a warm pool of Kubernetes Pods, and one worker hosts several actors.

代理会运行模型编写的代码,因此其环境中的任何内容都可能通过提示注入或错误命令泄露,包括 API 密钥。网关会控制执行者的入站和出站流量,并根据执行者的身份在出站时添加凭证,因此代理始终不会持有密钥。An agent runs code that a model wrote, so anything in its environment can leak through a prompt injection or a bad command, API keys included. Gateways control the actor's inbound and outbound traffic and add credentials on the way out, based on the actor's identity, so the agent never holds the keys.

SuspendActor 会将空闲 actor 快照保存到对象存储中,并释放其 worker;下一个请求到来时,会在任意一个预热 worker 上恢复该 actor。采用这种方式,我们可以通过在相同算力上复用更多 agent 来提高部署密度。SuspendActor snapshots an idle actor to object storage and frees its worker, and the next request restores the actor on any warm worker. Using this approach, we can achieve higher density by multiplexing more agents to run on the same amount of compute.

AX 回答了代理是什么以及它了解什么。你需要声明一个 Task,用来说明要运行什么(图像、命令和限制);还需要声明一个 Workspace,用来说明代理了解什么(代码库和文件)。AX 将每个 Task 作为一个 actor 运行。ax suspend 会暂停任务并保留其 /workspace。如今,AX 会请求创建一个卷快照,因此恢复时文件会一并返回,而代理的进程会重新启动(参见 runner 指南)。AX answers what the agent is and what it knows. You declare a Task, which says what runs (image, command, and limits), and a Workspace, which says what the agent knows (repositories and files). AX runs each Task as an actor. ax suspend parks the task and keeps its /workspace. Today AX asks for a volume snapshot, so the files come back on resume and the agent's processes start fresh (runner guide).

AX 将暂停、恢复和删除操作交由 Agent Substrate 处理。AX 路线图还将加入预算、策略和网关,用于限制智能体的行为,并记录每项任务执行过的操作。AX passes suspend, resume, and delete to Agent Substrate. The AX roadmap adds budgets, policies, and gateways, which limit what an agent may do, and a record of what each task did.

03-where-and-how
Figure 3. Agent Substrate answers where and how an agent runs, and AX answers what the agent is.

在确定池子的大小之前Before you size the pool

在你确定任何尺寸之前,有两个细节会决定费用。Two details decide the bill before you size anything.

首先,由你决定何时将 sandbox 挂起。Agent Substrate 不会自行挂起空闲 actor(#424),因此你的 agent harness,或其前面的服务,会在空闲超时后调用 suspend API。在此之前,空闲 actor 会一直占用其 worker。当空闲 actor 占满所有 worker 时,另一个 actor 的请求默认最多等待 5 秒,随后会以 HTTP 503 失败(Agent Substrate 文档)。First, you decide when a sandbox parks. Agent Substrate does not suspend an idle actor by itself (#424), so your agent harness, or the service in front of it, calls the suspend API after an idle timeout. Until then, an idle actor keeps its worker. When idle actors hold every worker, a request for another actor waits up to 5 seconds by default, then fails with HTTP 503 (Agent Substrate docs).

其次,你需要决定为每个沙箱承诺多少 CPU。Actor 根据其 CPU 和内存限制来确定规格,每个 worker 都会将这些限制计入你为其所在池声明的容量(API 指南)。我们的成本模型会将该容量设为节点的实际容量,因此每个 actor 都能保留其完整限制。你也可以声明高于节点实际容量的容量,这样就能在池级别而非每个 Pod 中超额分配 CPU。Second, you decide how much CPU each sandbox is promised. An actor is sized by its CPU and memory limits, and each worker books those limits against the capacity you declare for its pool (API guide). Our cost model sets that capacity to what the node really has, so every actor keeps its full limit. You can declare more capacity than the node has, and then you overcommit CPU at the pool instead of in each Pod.

在 GKE 上暂停集群能节省什么What parking saves on GKE

我们的成本模型按照 Google Cloud 在 us-central1 的标价,为同样的 100 个编码代理计算停车费用。Our cost model prices parking for the same 100 coding agents, at Google Cloud list prices in us-central1.

在 2 分钟的空闲超时设置下,每个会话在其存续时间的 29% 内会占用一个工作节点。为应对突发流量而配置的预热池(如果会话彼此独立,则按同时占用工作节点的会话数的第 99 百分位数,再预留 15% 的余量)需要 15 个节点。再增加一个全天候运行的节点,用于承载 Agent Substrate 控制平面及其 PostgreSQL 数据库,并配置一个在非工作时间将池缩减至零的作业:With a 2 minute idle timeout, each session holds a worker 29% of its open time. A warm pool sized for bursts (the 99th percentile of sessions holding a worker at once, if sessions act independently, plus 15% headroom) needs 15 nodes. Add one node that runs around the clock for the Agent Substrate control plane and its PostgreSQL database, and a job that scales the pool to zero after hours:

Untitled document (2)

暂停运行可节省 40% 的节点费用。节点池大约每 3,500 次请求才会满一次,因此提示词很少会遇到 503 错误。如果某个小时内每小时处理 2.5 个提示词而不是 2 个,同样的 15 个节点会在 3% 的请求中处于满载状态,因此应根据最繁忙时段来确定节点池规模。Parking saves 40% of the node bill. The pool is full about once in 3,500 turns, so a prompt rarely meets that 503. In an hour at 2.5 prompts instead of 2, the same 15 nodes are full for 3% of turns, so size the pool for your busiest hour.

请记住,代理等待时间越长,停放带来的收益就越大。每小时收到 2 次提示时,可节省 40%。每小时收到 4 次提示时,代理每小时有 44% 的时间在工作,资源池会扩大到 27 个节点,可节省 4%。每小时提示次数达到约 4.5 次时,每个会话使用一个 Pod 的方案就更便宜了,因此请先测量提示频率。Keep in mind that parking pays most when your agents wait the most. At 2 prompts an hour, the saving is 40%. At 4 prompts an hour, the agent works 44% of the hour, the pool grows to 27 nodes, and the saving is 4%. Around 4.5 prompts an hour, one Pod per session becomes the cheaper option, so measure your prompt rate first.

此外,池的大小由你自行设置。GKE 安装指南中的自动扩缩容选项依据的是 worker Pod,而不是已挂起的 actor,因此可以通过定时任务在非工作时间将池缩容至零:一个月 730 小时都在运行的池,费用约为每个会话一个 Pod 的两倍。Also, the pool size is yours to set. The autoscaling option in the GKE install guide follows worker Pods, not suspended actors, so scale the pool to zero after hours with a scheduled job: a pool that runs all 730 hours of the month costs about twice the one Pod per session bill.

最后,设备集群规模也很重要。Agent Substrate 控制平面无论管理 10 个还是 1,000 个会话,成本都相同,因此大约从 37 个编码会话开始,或从假设每小时工作约一分钟的聊天助手达到 13 个时开始,暂停运行就能节省成本。Finally, the fleet size matters. The Agent Substrate control plane costs the same for 10 sessions or 1,000, so parking pays from about 37 coding sessions, or 13 for an assumed chat assistant that works about a minute an hour.

05-when-parking-pays
Figure 4. Left, the monthly node bill for 100 coding agents against prompts an hour. Right, the cost per session as the fleet grows. Both come from the cost model.

当每个会话使用一个 Pod 的成本更低时When one Pod per session costs less

超额分配会改变比较结果。如果 Pod 请求的 CPU 少于其限制,Kubernetes 会根据它们的请求量来放置 Pod。一个请求 0.25 个 vCPU、限制为 1 个 vCPU 的 Pod,每个节点可容纳 12 个,因此同样的 100 个编码代理只需 9 个节点,比我们闲置的节点池还少。Agent Substrate 也可以超额分配,方法是通过你为工作节点池声明的容量;而我们的成本模型没有对此情况进行定价。Overcommit changes the comparison. If your Pods request less CPU than their limit, Kubernetes places them by their requests. A Pod that requests 0.25 vCPU with a 1 vCPU limit packs 12 to a node, so the same 100 coding agents fit on 9 nodes, fewer than our parked pool. Agent Substrate can overcommit too, through the capacity you declare on the worker pool, and our cost model does not price that case.

04-bills-by-cpu-request
Figure 5. The monthly node bill for 100 coding agents by Pod CPU request, from the cost model. Parked sessions keep their full CPU limit in both groups.

超额承诺在两边的代价相同:代理会共享 CPU,并在构建和测试期间因邻居繁忙而变慢。决定暂停之前,请确保比较对象相同:CPU 保证相同的 Pod 和 actor。Overcommit has the same cost on both sides: agents share CPU and slow down when their neighbors are busy, during builds and tests. Compare like with like before you decide to park: Pods and actors with the same CPU guarantee.

今天就开始吧Get started today

回到共享集群。Agent 是会话,而不是请求:预热池的规模应按占用 worker 的会话数来配置,而不是按已打开的会话数来配置。Back to the shared cluster. Agents are sessions, not requests: size the warm pool for the sessions that hold a worker, not the sessions that are open.

对于每小时大部分时间都在等待的长生命周期会话,我们会将其挂起;当开发者每小时提示超过约 4 次时,则为每个会话保留一个 Pod。在最繁忙时段,根据工作时间加上较短的空闲超时来确定预热池的规模;挂起会话前,先与 CPU 保证相同的当前 Pod 数量进行比较。We would park long lived sessions that wait most of the hour, and keep one Pod per session when developers prompt more than about 4 times an hour. Size the warm pool for working time plus a short idle timeout at your busiest hour, and compare with your current Pods at the same CPU guarantee before you park.

  • 从网关日志中提取一周内每小时的提示词数量,并将最繁忙的一小时与每小时 4.5 条提示词(此时停车就不再划算)进行比较。
    Take a week of prompts an hour from your gateway logs, and compare your busiest hour with the 4.5 prompts an hour where parking stops paying.
  • 按照《在 GKE 上部署 Agent Substrate》指南,在 GKE 集群上安装 Agent Substrate 1.0。
    Install Agent Substrate 1.0 on a GKE cluster with the Agent Substrate on GKE guide.
  • 阅读 Agent Substrate 文档。
  • 关注 AX 仓库。AX 正处于首次稳定版发布前的积极开发阶段,团队欢迎你在 GitHub 上提供反馈并提交问题。
    Follow the AX repository. AX is in active development ahead of its first stable release, and the team welcomes your feedback and issues on GitHub.

感谢阅读!如果你目前在 Kubernetes 上运行智能体会话,用户每小时会发送多少条提示词?提示词速率是我们最不确定的输入参数。欢迎通过 LinkedIn 或 X 联系我,分享你的数据、问题和更正意见。Thanks for reading! If you run agent sessions on Kubernetes today, how many prompts an hour do your users send? The prompt rate is the input we are least sure of. You can reach me on LinkedIn or X with your numbers, questions, and corrections.

值得记下

  1. 智能体会话的运行方式不同。编程智能体的工作区可以在开发者整个工作日保持开启:它会在两次提示之间保留记忆和文件,并且只有收到提示后才会开始工作。
  2. 把这些数字放到一个开发者的一天里来看。按照我们的假设,8 小时工作日里每小时发送 2 次提示,智能体一天中有 22% 的时间在工作,其余 78% 都在等待。如果每个会话都占用一台机器,你就得为这 78% 的空闲时间付费。
  3. 暂存方案可节省 40% 的节点费用。节点池大约每 3,500 轮才会满一次,因此提示请求很少会遇到 503 错误。
  4. 每小时 2 次提示时,节省幅度为 40%。每小时 4 次提示时,智能体每小时有 44% 的时间在工作,节点池会扩大到 27 个节点,节省幅度降至 4%。
  5. 每小时约 4.5 次提示时,每个会话使用一个 Pod 会更便宜,所以请先测量提示频率。