Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ Read more about E2B on the [E2B website](https://e2b.dev/?utm_source=github&utm_
<td>-</td>
<td><a href="./examples/agentkit-coding-agent">TypeScript</a></td>
</tr>
<tr>
<td>AgentsKit</td>
<td>AgentsKit sandbox lifecycle, timeouts, and output handling</td>
<td>-</td>
<td><a href="./examples/agentskit-sandbox-js">TypeScript</a></td>
</tr>
<tr>
<td><a href="https://sandboxagent.dev/docs/sdk-overview">Sandbox Agent SDK</a></td>
<td>Run Sandbox Agent inside E2B and connect with the SDK</td>
Expand Down
1 change: 1 addition & 0 deletions examples/agentskit-sandbox-js/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
E2B_API_KEY=
28 changes: 28 additions & 0 deletions examples/agentskit-sandbox-js/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# AgentsKit Sandbox with E2B

This example runs JavaScript and Python through the AgentsKit sandbox abstraction backed by E2B. It demonstrates explicit timeouts, structured output handling, and reliable sandbox cleanup without requiring a model provider.

## Tech stack

- [E2B Code Interpreter](https://e2b.dev/docs/code-interpreting)
- [`@agentskit/sandbox`](https://www.agentskit.io/docs/reference/packages/sandbox)
- TypeScript

## Setup

1. Copy `.env.template` to `.env`.
2. Add an [E2B API key](https://e2b.dev/docs/getting-started/api-key) to `E2B_API_KEY`.
3. Install dependencies and run the example:

```sh
npm install
npm run start
```

The process executes one JavaScript calculation and one Python calculation in the same E2B sandbox. Each result includes standard output, standard error, exit code, and duration. The `finally` block always disposes of the sandbox.

## Validate

```sh
npm run check
```
44 changes: 44 additions & 0 deletions examples/agentskit-sandbox-js/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'dotenv/config'

import { createSandbox } from '@agentskit/sandbox'

const apiKey = process.env.E2B_API_KEY

if (!apiKey) {
throw new Error('Set E2B_API_KEY before running this example.')
}

const sandbox = createSandbox({ apiKey, timeout: 30_000 })

const printResult = (
label: string,
result: { stdout: string; stderr: string; exitCode: number; durationMs: number },
) => {
console.log(`\n${label}`)
console.log(`exit code: ${result.exitCode}`)
console.log(`duration: ${result.durationMs} ms`)
if (result.stdout) console.log(`stdout: ${result.stdout}`)
if (result.stderr) console.error(`stderr: ${result.stderr}`)

if (result.exitCode !== 0) {
throw new Error(`${label} failed with exit code ${result.exitCode}`)
}
}

try {
const javascript = await sandbox.execute(
`const values = [3, 5, 8, 13]
console.log(JSON.stringify({ sum: values.reduce((total, value) => total + value, 0) }))`,
{ language: 'javascript', timeout: 10_000 },
)
printResult('JavaScript result', javascript)

const python = await sandbox.execute(
`values = [3, 5, 8, 13]
print({"mean": sum(values) / len(values)})`,
{ language: 'python', timeout: 10_000 },
)
printResult('Python result', python)
} finally {
await sandbox.dispose()
}
Loading