|
| 1 | +# E2B Cloud Sandbox Guide |
| 2 | + |
| 3 | +KODE SDK supports [E2B](https://e2b.dev) as a cloud sandbox backend, providing fully isolated remote Linux environments for AI Agent code execution. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +| Feature | Description | |
| 10 | +|---------|-------------| |
| 11 | +| **Isolation** | Each sandbox runs in an isolated micro-VM | |
| 12 | +| **Startup** | ~150ms to create a full Linux environment | |
| 13 | +| **Pre-installed** | Python, Node.js, common system tools | |
| 14 | +| **Scalable** | Each Agent gets its own sandbox instance | |
| 15 | + |
| 16 | +### When to Use E2B vs Local Sandbox |
| 17 | + |
| 18 | +| Scenario | Recommended | |
| 19 | +|----------|-------------| |
| 20 | +| Development/Testing | Local Sandbox | |
| 21 | +| Production with untrusted code | E2B | |
| 22 | +| Multi-user concurrent agents | E2B | |
| 23 | +| Need specific environment (GPU, packages) | E2B (Custom Template) | |
| 24 | +| Offline / No internet | Local Sandbox | |
| 25 | + |
| 26 | +--- |
| 27 | + |
| 28 | +## Prerequisites |
| 29 | + |
| 30 | +1. Sign up at [e2b.dev](https://e2b.dev) |
| 31 | +2. Get your API Key from the Dashboard |
| 32 | +3. Set environment variable: |
| 33 | + |
| 34 | +```bash |
| 35 | +export E2B_API_KEY=your-api-key |
| 36 | +``` |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +## Quick Start |
| 41 | + |
| 42 | +### Create and Use a Sandbox |
| 43 | + |
| 44 | +```typescript |
| 45 | +import { E2BSandbox } from '@shareai-lab/kode-sdk'; |
| 46 | + |
| 47 | +const sandbox = new E2BSandbox({ |
| 48 | + apiKey: process.env.E2B_API_KEY, |
| 49 | + template: 'base', |
| 50 | + timeoutMs: 300_000, // 5 minutes |
| 51 | +}); |
| 52 | +await sandbox.init(); |
| 53 | + |
| 54 | +// Execute commands |
| 55 | +const result = await sandbox.exec('python3 -c "print(1+1)"'); |
| 56 | +console.log(result.stdout); // "2\n" |
| 57 | + |
| 58 | +// File operations |
| 59 | +await sandbox.fs.write('script.py', 'print("hello")'); |
| 60 | +const content = await sandbox.fs.read('script.py'); |
| 61 | + |
| 62 | +// Cleanup |
| 63 | +await sandbox.dispose(); |
| 64 | +``` |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +## Configuration |
| 69 | + |
| 70 | +### E2BSandboxOptions |
| 71 | + |
| 72 | +```typescript |
| 73 | +interface E2BSandboxOptions { |
| 74 | + apiKey?: string; // E2B API Key (or use E2B_API_KEY env) |
| 75 | + template?: string; // Template ID/alias, default 'base' |
| 76 | + timeoutMs?: number; // Sandbox lifetime, default 300_000 (5min) |
| 77 | + workDir?: string; // Working directory, default '/home/user' |
| 78 | + envs?: Record<string, string>; // Environment variables |
| 79 | + metadata?: Record<string, string>; // Custom metadata |
| 80 | + allowInternetAccess?: boolean; // Allow internet, default true |
| 81 | + execTimeoutMs?: number; // Command timeout, default 120_000 |
| 82 | + sandboxId?: string; // Connect to existing sandbox (resume) |
| 83 | + domain?: string; // E2B API domain |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +### Environment Variables |
| 88 | + |
| 89 | +| Variable | Description | |
| 90 | +|----------|-------------| |
| 91 | +| `E2B_API_KEY` | Your E2B API key (used if `apiKey` not provided) | |
| 92 | + |
| 93 | +--- |
| 94 | + |
| 95 | +## Agent Integration |
| 96 | + |
| 97 | +### Using E2B Sandbox with Agent |
| 98 | + |
| 99 | +```typescript |
| 100 | +import { Agent, E2BSandbox } from '@shareai-lab/kode-sdk'; |
| 101 | +import { createRuntime } from './shared/runtime'; |
| 102 | + |
| 103 | +const sandbox = new E2BSandbox({ |
| 104 | + template: 'base', |
| 105 | + timeoutMs: 600_000, |
| 106 | +}); |
| 107 | +await sandbox.init(); |
| 108 | + |
| 109 | +const deps = createRuntime(({ templates, registerBuiltin }) => { |
| 110 | + registerBuiltin('fs', 'bash', 'todo'); |
| 111 | + templates.register({ |
| 112 | + id: 'coder', |
| 113 | + systemPrompt: 'You are a coding assistant.', |
| 114 | + tools: ['bash_run', 'fs_read', 'fs_write', 'todo_read', 'todo_write'], |
| 115 | + }); |
| 116 | +}); |
| 117 | + |
| 118 | +const agent = await Agent.create({ templateId: 'coder', sandbox }, deps); |
| 119 | +await agent.send('Write and run a Python fibonacci script'); |
| 120 | +``` |
| 121 | + |
| 122 | +### Sandbox Lifecycle Binding |
| 123 | + |
| 124 | +- **Agent Start**: Call `sandbox.init()` before `Agent.create()` |
| 125 | +- **Agent Pause**: Sandbox persists (use `sandboxId` to reconnect) |
| 126 | +- **Agent Resume**: Pass `sandboxId` to reconnect to existing sandbox |
| 127 | +- **Agent Destroy**: Call `sandbox.dispose()` to terminate |
| 128 | + |
| 129 | +### Resume / Fork with Persistent sandboxId |
| 130 | + |
| 131 | +```typescript |
| 132 | +// First run - create |
| 133 | +const sandbox = new E2BSandbox({ template: 'base' }); |
| 134 | +await sandbox.init(); |
| 135 | +const id = sandbox.getSandboxId(); // persist this |
| 136 | + |
| 137 | +// Later - resume |
| 138 | +const restored = new E2BSandbox({ sandboxId: id }); |
| 139 | +await restored.init(); |
| 140 | +// Same sandbox environment is available |
| 141 | +``` |
| 142 | + |
| 143 | +--- |
| 144 | + |
| 145 | +## Custom Templates |
| 146 | + |
| 147 | +### Using E2BTemplateBuilder |
| 148 | + |
| 149 | +```typescript |
| 150 | +import { E2BTemplateBuilder } from '@shareai-lab/kode-sdk'; |
| 151 | + |
| 152 | +await E2BTemplateBuilder.build({ |
| 153 | + alias: 'data-analysis', |
| 154 | + base: 'python', |
| 155 | + baseVersion: '3.11', |
| 156 | + aptPackages: ['graphviz'], |
| 157 | + pipPackages: ['pandas', 'numpy', 'matplotlib'], |
| 158 | + workDir: '/workspace', |
| 159 | + cpuCount: 4, |
| 160 | + memoryMB: 2048, |
| 161 | +}); |
| 162 | +``` |
| 163 | + |
| 164 | +### Available Base Images |
| 165 | + |
| 166 | +| Base | Description | |
| 167 | +|------|-------------| |
| 168 | +| `python` | Python with pip | |
| 169 | +| `node` | Node.js with npm | |
| 170 | +| `debian` | Debian base | |
| 171 | +| `ubuntu` | Ubuntu base | |
| 172 | +| `custom` | Custom Dockerfile | |
| 173 | + |
| 174 | +--- |
| 175 | + |
| 176 | +## Network & Ports |
| 177 | + |
| 178 | +### Exposing Ports |
| 179 | + |
| 180 | +```typescript |
| 181 | +const sandbox = new E2BSandbox({ template: 'node' }); |
| 182 | +await sandbox.init(); |
| 183 | + |
| 184 | +await sandbox.exec('npx serve -l 3000 &'); |
| 185 | +const url = sandbox.getHostUrl(3000); |
| 186 | +console.log(`Preview: ${url}`); |
| 187 | +// https://3000-<sandboxId>.e2b.app |
| 188 | +``` |
| 189 | + |
| 190 | +--- |
| 191 | + |
| 192 | +## Best Practices |
| 193 | + |
| 194 | +1. **Timeout Management**: Set appropriate `timeoutMs` to control costs. Call `dispose()` when done. |
| 195 | +2. **Error Handling**: E2B operations are remote calls; handle network errors gracefully. |
| 196 | +3. **Data Export**: Sandbox data is lost after `dispose()`. Export important results first. |
| 197 | +4. **Concurrency**: E2B has account-level sandbox limits. Plan accordingly with AgentPool. |
| 198 | +5. **Template Reuse**: Build templates once, reuse across sandboxes for faster startup. |
0 commit comments