跳到主要内容

智能体框架与 SDK

Vercel AI SDK

在隔离的沙箱中运行 AI SDK 工具和智能体运行框架。@e2b/ai-sdk-sandbox 实现了 AI SDK 7 的沙箱接口,命令和文件写入都在 E2B 中进行,而不是在您的服务器上。

维护方
E2B
集成方式
AI SDK 7 沙箱提供方
软件包
@e2b/ai-sdk-sandbox
语言
TypeScript

配置

快速开始

将提示词粘贴到您的编程智能体中,或按以下步骤自行配置。

粘贴到您的编程智能体中

为此 Vercel AI SDK 项目添加 E2B 沙箱。

  1. 1.使用项目的包管理器安装 @e2b/ai-sdk-sandbox。它面向 AI SDK 7。
  2. 2.该提供方需要 E2B_API_KEY 环境变量。如果尚未设置,请向我索要 API 密钥(在 https://console.e2b.dev/?tab=keys 获取)。切勿将其硬编码。
  3. 3.按照 https://docs.e2b.dev/agents/vercel-ai-sdk 上的 Vercel AI SDK 指南操作。用 createE2BSandbox() 创建会话,将 session.restricted() 交给我的工具,让它们可以运行命令但无法销毁沙箱,完成后调用 session.destroy()。
  4. 4.运行一次生成,并展示来自沙箱的输出。

或自行配置

  1. 1. 安装提供方

    npm init -y && npm pkg set type=module
    npm install @e2b/ai-sdk-sandbox ai @ai-sdk/anthropic zod
    npm install -D tsx
  2. 2. 设置 API 密钥

    export E2B_API_KEY="e2b_..."
    export ANTHROPIC_API_KEY="..."
  3. 3. 创建沙箱会话

    import { createE2BSandbox } from '@e2b/ai-sdk-sandbox'
    
    const session = await createE2BSandbox({ template: 'base' }).createSession()
    
    await session.writeTextFile({ path: 'hello.txt', content: 'hi from e2b' })
    
    const { stdout } = await session.run({ command: 'cat hello.txt' })
    console.log(stdout)
    
    await session.destroy()
  4. 4. 将沙箱交给工具

    import { generateText, tool } from 'ai'
    import { anthropic } from '@ai-sdk/anthropic'
    import { createE2BSandbox } from '@e2b/ai-sdk-sandbox'
    import { z } from 'zod'
    
    const session = await createE2BSandbox({ template: 'base' }).createSession()
    const sandbox = session.restricted()
    
    const result = await generateText({
      model: anthropic('claude-sonnet-4-5'),
      prompt: 'Check which Python version is installed.',
      tools: {
        bash: tool({
          description: 'Run a shell command in an isolated E2B sandbox',
          inputSchema: z.object({ command: z.string() }),
          execute: async ({ command }) => {
            const { stdout, stderr, exitCode } = await sandbox.run({ command })
            return { stdout, stderr, exitCode }
          },
        }),
      },
    })
    
    console.log(result.text)
    await session.destroy()

安全

沙箱中运行的内容

工具运行的命令和文件操作都在隔离的沙箱中进行,与您的服务器隔离。

  • 代码来自 AI SDK 工具的 Shell 命令和文件读写,或 Claude Code、Codex 这类完整的智能体运行框架。
  • 访问权限工具获得的是 session.restricted():可进行文件 I/O、run 和 spawn,但没有 stop、destroy 或网络控制。
  • 生命周期默认 30 分钟;可设置 timeoutMs。stop() 会暂停沙箱以便之后恢复,destroy() 会将其移除。
  • 网络用 setNetworkPolicy 收紧出站访问,该方法仅在您的应用保留的完整会话上可用。

已启动 30亿+ 个沙箱

为每个智能体 配一台机器