Skip to main content

Agent frameworks & SDKs

Vercel AI SDK

Run AI SDK tools and harness agents in an isolated sandbox. @e2b/ai-sdk-sandbox implements the AI SDK 7 sandbox interface, so commands and file writes happen in E2B, not on your server.

Maintained by
E2B
How it works
AI SDK 7 sandbox provider
Package
@e2b/ai-sdk-sandbox
Language
TypeScript

Set it up

Get started

Paste the prompt into your coding agent, or follow the steps yourself.

Paste this into your coding agent

Add E2B sandboxes to this Vercel AI SDK project.

  1. 1.Install @e2b/ai-sdk-sandbox using the project's package manager. It targets AI SDK 7.
  2. 2.The provider needs the E2B_API_KEY environment variable. If it isn't set, ask me for my API key from https://console.e2b.dev/?tab=keys. Never hardcode it.
  3. 3.Follow the Vercel AI SDK guide at https://docs.e2b.dev/agents/vercel-ai-sdk. Create a session with createE2BSandbox(), give my tools session.restricted() so they can run commands but not destroy the sandbox, and call session.destroy() when done.
  4. 4.Run one generation and show me the output that came from the sandbox.

Or set it up yourself

  1. 1. Install the provider

    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. Set your API keys

    export E2B_API_KEY="e2b_..."
    export ANTHROPIC_API_KEY="..."
  3. 3. Create a sandbox session

    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. Hand the sandbox to a tool

    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()

Security

What runs in the sandbox

Commands and file operations your tools run happen in an isolated sandbox, away from your server.

  • CodeShell commands and file reads and writes from your AI SDK tools, or a whole harness agent like Claude Code or Codex.
  • AccessTools get session.restricted(): file I/O, run and spawn, without stop, destroy or network controls.
  • Lifetime30 minutes by default; set timeoutMs. stop() pauses the sandbox so it can resume, destroy() removes it.
  • NetworkTighten outbound access with setNetworkPolicy, available only on the full session your app keeps.

3B+ sandboxes started

Give every agent a machine