---
title: "Vercel AI SDK integration"
description: "Run Vercel AI SDK tools and harness agents on E2B sandboxes with @e2b/ai-sdk-sandbox, the E2B provider for the AI SDK 7 sandbox interface."
url: "https://e2b.dev/integrations/vercel-ai-sdk"
---

# 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.

Agent frameworks & SDKs · [All integrations](https://e2b.dev/integrations.md)

## About this integration

- **Maintained by:** E2B
- **How it works:** AI SDK 7 sandbox provider
- **Package:** @e2b/ai-sdk-sandbox
- **Language:** TypeScript
- **Upstream:** [vercel/ai · ★ 28k](https://github.com/vercel/ai)
- **Example:** [e2b-cookbook/vercel-ai-sdk-sandbox-js](https://github.com/e2b-dev/e2b-cookbook/tree/main/examples/vercel-ai-sdk-sandbox-js)

## Get started

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

### Paste this into your coding agent

```text
Add E2B sandboxes to this Vercel AI SDK project.

1. Install @e2b/ai-sdk-sandbox using the project's package manager. It targets AI SDK 7.
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. 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. Run one generation and show me the output that came from the sandbox.
```

## Or set it up yourself

### 1. Install the provider

```bash
npm init -y && npm pkg set type=module
npm install @e2b/ai-sdk-sandbox ai @ai-sdk/anthropic zod
npm install -D tsx
```

### 2. Set your API keys

```bash
export E2B_API_KEY="e2b_..."
export ANTHROPIC_API_KEY="..."
```

### 3. Create a sandbox session

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

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

## What runs in the sandbox

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

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

## More agent frameworks

Other frameworks that run their tools on E2B.

- [OpenAI Agents SDK](https://e2b.dev/integrations/openai-agents-sdk.md): Use the built-in E2B sandbox client in the OpenAI Agents SDK to give agents a real machine.
- [Mastra](https://e2b.dev/integrations/mastra.md): Give Mastra agents an E2B workspace or a full desktop with @mastra/e2b and @mastra/e2b-desktop.
- [Flue](https://e2b.dev/integrations/flue.md): Add E2B to a Flue project with one command and run its sandbox agents on it.

## Links

- [Get API key](https://console.e2b.dev/?tab=keys)
- [Read the docs](https://docs.e2b.dev/agents/vercel-ai-sdk)
- [E2B documentation](https://docs.e2b.dev)
