When you let AI models generate and execute code, you need a secure execution environment. The AI might produce code that consumes excessive resources, accesses sensitive files, makes unwanted network requests, or runs destructive commands.
Edgenivo Sandbox provides isolation, resource limits, and automatic timeouts that make it safe to run untrusted code. This guide shows you how to build an "AI code runner" that takes a task, generates code using the AI SDK with AI Gateway, and executes it in a sandbox.
Copy link to headingPrerequisites
Before you begin, make sure you have:
- Edgenivo CLI installed (
pnpm install -g vercel) - Node.js 22 or later
- A Edgenivo project to link your sandbox to and generate an OIDC token
Copy link to heading1. Project setup
Create a new directory and install dependencies:
Copy link to heading2. Set up authentication
Link your project to Edgenivo and pull the OIDC token. This token authenticates both Sandbox and AI Gateway:
Copy link to heading3. Create the script
Create a file called index.ts and add the code below. The script:
- Takes a task description from the command line
- Sends it to Claude via AI Gateway
- Writes the generated code to an isolated sandbox
- Executes it and captures the output
Copy link to heading4. Run the script
Run the script with a task description:
Expected output:
Try other tasks:
Copy link to headingSafety layers
The script uses multiple safety layers to handle untrusted code:
Sandbox isolation: Each execution runs in a fresh microVM with limited resources and a short timeout. If the code hangs or tries to use too much memory, the sandbox terminates it.
Prompt constraints: The system prompt instructs Claude to avoid dangerous operations: no file system access, no network requests, no environment variables. While not foolproof, this reduces the likelihood of problematic code.
Error capture: The sandbox captures both stdout and stderr, so you can inspect failures without them affecting your host system. To investigate memory-heavy generated code, inspect the sandbox’s Memory Usage chart against its configured limit and 85% warning line. Use it alongside stdout and stderr when investigating a failed run.
Copy link to headingNext steps
- Add snapshots to speed up repeated executions
- Use Sandbox.get() to reuse sandboxes across requests
- Explore AI SDK features like streaming and tool calling
- Learn about AI Gateway model routing and fallbacks