Agent 需要的是一台电脑,而非容器:Cloudflare 推出 @cloudflare/computer
英文标题:🔶 Agent needs a computer, not a container – introducing Cloudflare/computer
Cloudflare 发布开源早期预览版 @cloudflare/computer,旨在为 AI Agent 提供统一的虚拟文件系统与执行运行时,而不要求每个 Agent 都独占一个容器。该方案将隔离环境(isolate)用于文件操作、数据处理和 Git 管理,将容器留给 Linux、npm 或原生二进制等较重任务,并让两类环境共享同步文件系统。Cloudflare 的目标是让容器承担不到 10% 的 Agent 工作,从而以更低成本扩展至数亿乃至数十亿并发 Agent。对开发者而言,该软件包提供 Durable Object、SQLite、Git、文件工具、Shell 工具及 Cloudflare Containers 的组合接口,也支持自定义执行后端。
1.6 KB
4.7 KB
974 B
2.3 KB
50.5 KB
50.5 KB
10.2 KB
23.3 KB
18.6 KB
44.1 KB
18.5 KB
41.0 KB
18.0 KB
47.9 KB
20.3 KB
44.6 KB
23.1 KB
50.6 KB
1.1 KB
120.8 KB
中文
能力最强的 Agent 有一个简单的共同点:它们都拥有一台可供自己工作的电脑。
编程 Agent 就是这样工作的。你为它们提供文件系统、Shell、工具、软件包以及运行代码的能力。它们会检查环境、进行修改、测试工作成果,并持续推进。电脑为模型提供了一种熟悉的方式来作用于现实世界。在 Cloudflare,我们正努力提供构建最强大 Agent 所需的正确基础原语。
今天,我们发布 @cloudflare/computer 的早期预览版。@cloudflare/computer 软件包提供了一种 Agent 运行时,其中,代码是在 isolate、容器沙箱还是 Web 浏览器中运行的细节和机制均由平台处理。每个 Agent 都会获得一台电脑,而运行时则针对效率与可扩展性进行优化。
我们相信,为了满足 Agent 系统对计算资源日益增长的需求,必须寻求传统容器化之外的解决方案。
改变 Agent 的构建方式
过去六个月里,我们看到这一领域发生了一种微妙演进。年初时,启动一个容器并在其中运行 Agent 还是常态。近几个月,我们看到 Agent harness 快速转向通过工具提供沙箱化代码执行。这将手(实际执行工作的沙箱)与大脑(Agent 循环)分离开来。
无论 harness 在哪里运行,为每个 Agent 配备一个容器都会带来挑战——纵观所有云平台和超大规模云服务商,全球的计算资源都远远不足以让每家公司为每位用户的 Agent 都提供各自的容器化计算环境。这种模式无法扩展到数亿、继而数十亿个并发 Agent。这正是行业对 CPU 算力而不只是 GPU 算力产生急切乃至恐慌性需求的原因。
Cloudflare 长期以来一直在解决这一问题,打造一种更高效的计算原语:isolate。近 10 年前,我们在 推出 Cloudflare Workers时做出了这一反共识的押注。近 6 年前,我们在 推出 Durable Objects时再次做出这一押注。我们之所以这样做,是因为 isolate 可以无限水平扩展。它们启动和销毁的速度极快。Agent 空闲时,它们可以 休眠,可以 存储 Agent 自身状态,甚至可以 启动自己的 isolate来运行不受信任的代码。isolate 是实现水平扩展的最佳方式,而水平扩展正是 Agent 所需要的。
去年,我们 让 isolate 具备启动自身容器沙箱的能力。从第一天起,Cloudflare 的架构就被设计为:在 isolate 内部(即 Durable Object 中)运行 Agent harness,并按需将附加容器作为工具调用。这样一来,你仅在必要时使用更重的计算原语,从而优化性能与成本。Durable Objects 可以无限水平扩展,所附加的容器则使其能够垂直扩展以执行任何任务。这正是我们自己构建 Agent 的方式,我们也看到客户以这种方式打造出令人惊叹的成果。
但当我们看到,构建 Agent 需要多种底层计算原语(isolate 和容器),而我们的客户和开发者又需要自行在用户空间中将它们组合起来时,我们认为可以做得更好。我们认为可以提供一种更简单的抽象。
因此,我们以开源库的形式发布 @cloudflare/computer,作为这一实验的起点,并与那些不断突破大规模运行 Agent 边界的客户共同学习。
跨 isolate 与容器的共享文件系统
@cloudflare/computer 软件包始于一个简单前提:如果我们为 Agent 提供一个预先准备好的、以声明式方式定义的文件系统,其中包含完成当前任务所需的一切,并提供多种可操作这些文件的执行环境——每种环境在速度、能力和成本方面各有优劣——会怎样?
事实证明,如今的 Agent 已经非常擅长为当前任务选择正确的环境。只需处理文件、处理数据或管理 Git 仓库的工作可以在 isolate 内运行。需要 Linux、npm 或原生二进制文件的命令可以在容器内运行。两者都操作同一批文件,这些文件会与源文件系统保持同步。
@cloudflare/computer 软件包提供持久化文件系统,可搭配 Git 仓库、存储桶或你选择的任何文件使用。它还提供工具,使你能够使用 Code Mode或 bash 命令读取、写入和编辑文件。所有操作均经过权限控制、审计和观测,使你能够精细控制 Agent 被允许执行的修改,并获得清晰的审计记录,了解 Agent 完成了哪些操作。
如何使用
可以在任意 Durable Object 上实例化一个 @cloudflare/computer workspace 实例,以提供虚拟文件系统和执行运行时。
通过 npm 安装:
npm install @cloudflare/computer
其主要用途是向 Agent 提供该文件系统和工具。例如,以下展示了如何在由 @cloudflare/think 驱动、用于分诊 Bug 报告的 Agent 上实例化 workspace。
import { Think } from "@cloudflare/think";
import { Workspace, type DurableObjectStorageLike } from "@cloudflare/computer";
import { createWorkersAI } from "workers-ai-provider";
export class Agent extends Think {
override workspaceBash = false;
override workspace = new Workspace({
storage: this.ctx.storage,
useThink: true, // soon will not be needed
});
override getModel() {
return createWorkersAI({ binding: this.env.AI })("@cf/zai-org/glm-5.2");
}
override getSystemPrompt() {
return `
You are a bug triage agent.
Use the project in /workspace/repo to reproduce the bug, inspect the
code, make a focused fix when it is safe, and run verification. In your
final answer, include what you changed, which commands you ran, and
whether verification passed.`;
}
}
@cloudflare/computer 软件包内置了多个执行后端,你也可以自行编写后端。以下示例接入了一个 Cloudflare Container。
import { Think } from "@cloudflare/think";
import { Workspace, WorkspaceProxy } from "@cloudflare/computer";
import {
CloudflareContainerBackend,
withWorkspaceContainer,
} from "@cloudflare/computer/backends/container";
export { WorkspaceProxy };
export class Agent extends withWorkspaceContainer(Think) {
override workspaceBash = false;
override workspace = new Workspace({
storage: this.ctx.storage,
useThink: true, // soon will not be needed
backends: [
new CloudflareContainerBackend({
container: () => this,
workspace: {
binding: "Agent",
id: this.ctx.id.toString(),
},
}),
],
});
/* Example code truncated for readability... */
}
将文件、Git 和 Shell 工具与产品专属工具一同暴露出来,以便回复已报告的问题。
import { createAITools } from "@cloudflare/computer/tools";
import type { ToolSet } from "ai";
import { replyToIssue } from "./tools/github";
export class Agent extends withWorkspaceContainer(Think) {
override workspaceBash = false;
/* Example code truncated for readability... */
override getTools(): ToolSet {
return {
...createAITools({
workspace: this.workspace,
shell: {
defaultBackend: "container",
backends: {
container: {
description:
"Cloudflare Container with a full Linux userland: " +
"npm, node, package managers, test runners, and real " +
"binaries on $PATH. Use it when a task needs more than " +
"file manipulation.",
},
},
},
}),
replyToIssue,
};
}
}
模型可以在 Agent 循环期间使用工具,但你也可以直接使用 workspace API,例如在向 Agent 发送提示前准备环境。
export class Agent extends withWorkspaceContainer(Think) {
override workspaceBash = false;
/* Example code truncated for readability... */
async startTriage(report: { title: string; body: string; repoUrl: string }) {
await this.workspace.fs.mkdir("/workspace", { recursive: true });
await this.workspace.fs.writeFile(
"/workspace/BUG_REPORT.md",
`# ${report.title}\n\n${report.body}\n`,
);
await this.workspace.git.clone({
url: report.repoUrl,
dir: "/workspace/repo",
});
return this.submitMessages([
{
id: crypto.randomUUID(),
role: "user",
parts: [
{
type: "text",
text: [
`Triage this bug: ${report.title}`,
"The bug report is in /workspace/BUG_REPORT.md.",
"The repository is checked out at /workspace/repo.",
].join("\n"),
},
],
},
]);
}
}
请查看 workspace 仓库,了解使用不同后端和工具的更多示例,其中包括一份 分步教程,带你从零开始构建一个 Agent。
工作原理
@cloudflare/computer 的核心是 workspace:一个由 SQLite 支持的虚拟文件系统,可以从云存储和源代码控制等多种来源填充内容。
workspace 支持可选执行运行时,允许针对文件系统运行代码。所有运行时均支持相同的 exec(string, options) 接口,目前开箱即用地提供了两种运行时(你也可以自行编写):
- 基于 isolate 的运行时环境:使用 just-bash 将 Shell 代码转换为 JavaScript,并在 动态 Worker中运行。在这里,文件系统可通过 Worker bindings 直接访问。
- 容器运行时:使用 Cloudflare Containers 提供完整的 Linux 环境。在这里,文件系统通过用户空间文件系统(FUSE)挂载提供,确保文件可供容器访问,并将更改同步回去。
Workspace 类提供了直接操作文件系统的 API 接口,以及一个与 node:fs 兼容的封装器,因此可以轻松与第三方 JavaScript 库配合使用。
对于 Agent 使用场景,我们提供了一个兼容 AI SDK 的工具包,其中包含最常用的工具:读取、写入、编辑、ls 和 exec。exec 工具稍有特殊,因为它可以跨运行时工作,并接受 backend 参数。工具描述会引导 Agent 为当前任务选择正确的运行时:快速、低成本的 Worker 后端,或功能完整的容器。在我们的测试中,前沿模型非常善于做出正确决策,仅在必要时才回退到使用容器。
下一步
在 Cloudflare,我们已经看到 Agent 完全使用 isolate 来构建、测试和部署采用现代工具链的 JavaScript 应用,为每位客户生成定制化文档,并使用 Web 浏览器执行复杂任务。
我们对 @cloudflare/computer 的目标是:为 Agent 提供一种运行时,使容器仅在其不到 10% 的工作中成为必需;而编程任务、音频/视频处理和文档创建均可由 isolate 完成。
立即试用 早期预览版——我们迫不及待想听到你的反馈。
English / 原文
The most capable agents have something simple in common: they are given their own computer to work with.
Coding agents work this way. You give them a filesystem, a shell, tools, packages, and the ability to run code. They inspect the environment, make changes, test their work, and keep going. The computer gives the model a familiar way to act on the world. At Cloudflare, weâre working hard to provide the right primitives on which to build the most capable agents.
Today weâre introducing an early preview of @cloudflare/computer. The @cloudflare/computer package provides an agent runtime where the details and mechanics of what code runs in an isolate, a container sandbox, or a web browser are handled by the platform. Each agent gets a computer, the runtime optimizes for efficiency, and scalability.
We believe that in order to meet the growing demand for compute required by agentic systems we need to look to solutions beyond traditional containerization.Â
Changing how agents are built
Weâve seen a subtle evolution of this story over the past six months. At the start of the year, spinning up a container and running an agent inside of it was the norm. In recent months, weâve seen a rapid move for agent harnesses to provide sandboxed code execution via tools. This separates the hands (the sandbox where work is done) from the brain (the agent loop).
No matter where the harness runs, giving every agent a container presents a challenge â across all the clouds, all the hyperscalers, thereâs nowhere near enough compute in the world for every company to give each of their usersâ agents their own containerized compute environment. This will not scale to hundreds of millions, then billions, of concurrent agents. This is why there is desperate, panicked industry demand for CPU compute, not just GPU compute.
Weâve been working on this problem for a long time at Cloudflare, creating a more efficient compute primitive: isolates. We made that out-of-consensus bet almost 10 years ago when we introduced Cloudflare Workers. We made it again when we introduced Durable Objects almost six years ago. We made this bet because isolates are infinitely horizontally scalable. They spin up and tear down incredibly quickly. They can hibernate when the agent is idle, store the agentâs own state, and even spin up their own isolates to run untrusted code. Isolates are the best way to scale horizontally, and horizontal scale is what agents demand.
Last year, we gave isolates the ability to spin up their own container sandboxes. From day one, Cloudflareâs architecture has been designed to run the agent harness in the isolate (in a Durable Object) and call an attached container on-demand as a tool. This allows you to utilize heavier compute primitives only when required, optimizing performance and cost. Durable Objects scale infinitely horizontally, and the attached container lets it scale vertically to perform any task. This is how we build agents ourselves, and weâre seeing customers build incredible things this way too.
But when we look at this need to have multiple underlying compute primitives to build agents (isolates and containers) and the need for our customers and developers to combine them themselves in userspace, we think we can do better. We think that we can provide a simpler abstraction.
Thatâs why weâre starting this experiment by shipping @cloudflare/computer as an open-source library, to learn with our customers who are pushing the bounds of running agents at scale.
A shared filesystem across isolates and containers
The @cloudflare/computer package starts with a simple premise: what if we give an agent a primed filesystem, declaratively defined, containing everything required for the task at hand and a selection of execution environments to operate on those files, each with their own pros and cons regarding speed, capability and cost?
It turns out that agents today are surprisingly capable of selecting the right environment for the task at hand. A job that only needs to manipulate files, process data, or manage a git repository can run inside an isolate. A command that needs Linux, npm, or a native binary can run inside a container. Both work against the same files that are kept in sync with the source filesystem.
The @cloudflare/computer package provides a durable filesystem that you can use with git repositories, storage buckets or any files you choose. It provides tools that let you read, write and edit files using Code Mode or bash commands. All operations are gated, audited and observed, giving you fine-grained control over changes the agent is allowed to perform as well as a clear paper trail showing what the agent did.
How you use it
An instance of a @cloudflare/computer workspace can be instantiated on any Durable Object to provide a virtual filesystem and execution runtime.
It is installed via npm:
npm install @cloudflare/computer
The primary use case is to provide that filesystem and tooling to an agent. For example, hereâs how to instantiate the workspace on an agent powered by @cloudflare/think intended to triage bug reports.
import { Think } from "@cloudflare/think";
import { Workspace, type DurableObjectStorageLike } from "@cloudflare/computer";
import { createWorkersAI } from "workers-ai-provider";
export class Agent extends Think {
override workspaceBash = false;
override workspace = new Workspace({
storage: this.ctx.storage,
useThink: true, // soon will not be needed
});
override getModel() {
return createWorkersAI({ binding: this.env.AI })("@cf/zai-org/glm-5.2");
}
override getSystemPrompt() {
return `
You are a bug triage agent.
Use the project in /workspace/repo to reproduce the bug, inspect the
code, make a focused fix when it is safe, and run verification. In your
final answer, include what you changed, which commands you ran, and
whether verification passed.`;
}
}
Several execution backends are provided as part of the @cloudflare/computer package, or you can write your own. Here we wire up a Cloudflare Container.
import { Think } from "@cloudflare/think";
import { Workspace, WorkspaceProxy } from "@cloudflare/computer";
import {
CloudflareContainerBackend,
withWorkspaceContainer,
} from "@cloudflare/computer/backends/container";
export { WorkspaceProxy };
export class Agent extends withWorkspaceContainer(Think) {
override workspaceBash = false;
override workspace = new Workspace({
storage: this.ctx.storage,
useThink: true, // soon will not be needed
backends: [
new CloudflareContainerBackend({
container: () => this,
workspace: {
binding: "Agent",
id: this.ctx.id.toString(),
},
}),
],
});
/* Example code truncated for readability... */
}
Expose the file, git, and shell tools alongside product specific tools to reply to reported issues.
import { createAITools } from "@cloudflare/computer/tools";
import type { ToolSet } from "ai";
import { replyToIssue } from "./tools/github";
export class Agent extends withWorkspaceContainer(Think) {
override workspaceBash = false;
/* Example code truncated for readability... */
override getTools(): ToolSet {
return {
...createAITools({
workspace: this.workspace,
shell: {
defaultBackend: "container",
backends: {
container: {
description:
"Cloudflare Container with a full Linux userland: " +
"npm, node, package managers, test runners, and real " +
"binaries on $PATH. Use it when a task needs more than " +
"file manipulation.",
},
},
},
}),
replyToIssue,
};
}
}
The model can use tools during the agent loop, but you can also use the workspace API directly, for example, to prepare the environment before prompting the agent.
export class Agent extends withWorkspaceContainer(Think) {
override workspaceBash = false;
/* Example code truncated for readability... */
async startTriage(report: { title: string; body: string; repoUrl: string }) {
await this.workspace.fs.mkdir("/workspace", { recursive: true });
await this.workspace.fs.writeFile(
"/workspace/BUG_REPORT.md",
`# ${report.title}\n\n${report.body}\n`,
);
await this.workspace.git.clone({
url: report.repoUrl,
dir: "/workspace/repo",
});
return this.submitMessages([
{
id: crypto.randomUUID(),
role: "user",
parts: [
{
type: "text",
text: [
`Triage this bug: ${report.title}`,
"The bug report is in /workspace/BUG_REPORT.md.",
"The repository is checked out at /workspace/repo.",
].join("\n"),
},
],
},
]);
}
}
Check out the workspace repository for more examples of how to use the different backends and tools including a step-by-step tutorialwalking through building an agent from scratch.
How it works
The central piece of @cloudflare/computer is the workspace. A virtual filesystem backed by SQLite that can be populated from various sources including cloud storage and source control.
The workspace supports optional execution runtimes that allow code to be run against the file system. All runtimes support the same interface exec(string, options) and currently two are provided out of the box (but you can write your own):
- An isolate-based runtime environment that uses just-bash to translate shell code into JavaScript runs in a dynamic worker. Here, the filesystem is available directly via worker bindings.
- A container runtime that uses Cloudflare Containers to provide a full Linux environment. Here, the filesystem is provided via a Filesystem in Userspace (FUSE) mount, which ensures files are available to the container and changes are synced back.
The Workspace class provides an API interface for manipulating the filesystem directly as well as a node:fs compatible wrapper so that it can be used easily with third-party JavaScript libraries.
For use with agents, we provide an AI SDK compatible toolkit that provides the most common tools: read, write, edit, ls and exec. The exec tool is a little special as it works across the runtimes taking a backend argument. The tool description guides the agent into choosing the correct runtime for the task at hand: either a fast, cheap worker backend or the fully featured container. In our testing, the frontier models are very good at making the correct decision and falling back to using containers only when needed.
Whatâs next
Here at Cloudflare weâre already seeing agents exclusively using isolates to build, test, and deploy JavaScript applications with modern tooling, generate tailored documentation for each of our customers, and use web browsers to perform complex tasks.
Our goal with @cloudflare/computer is to provide an agent with a runtime where a container is required for less than 10% of its work, and coding tasks, audio/video manipulation, and document creation can all be handled by isolates.
Try out the early preview today - we canât wait to hear your thoughts.