AI models are increasingly used to generate code. Often, applications return this code to the user as plain text. But some apps could run the generated code to produce UI or other results.
This creates powerful possibilities but introduces risk. Generated code is untrusted. It may delete files, leak sensitive data, or consume excessive resources. The danger increases when users can influence prompts and craft malicious input. So running AI generated code on your machine or in your production application is unsafe.
Vercel Sandbox addresses this by running untrusted code in a remote, isolated environment with strong safeguards and full control.
In this guide, you'll learn:
- What Vercel Sandbox is and how it works.
- How to create a sandbox, run commands, and capture results.
- Example: Use an AI SDK Agent to generate and safely execute code inside a sandbox.
To understand how Vercel Sandbox works, let's build a minimal AI app that responds to natural language queries that require computation or network access, such as: "Get the top story from Hacker News” or "What is 44 × 44?"
To keep the example simple and avoid boilerplate code, we'll use the following tools:
- A Next.js route handler that accepts user input and returns the result.
- The AI Gateway to query OpenAI without managing API keys.
- The AI SDK to create an agent that orchestrates tool calls and sandbox execution.
- Vercel Sandbox to run the generated code.
Here's how it works:


Since the generated code and packages are unpredictable and potentially unsafe, we will run and install them inside Vercel Sandbox.
Create a minimal Next.js project:
Install required packages:
- Prerequisite: Install the Vercel CLI.
Vercel Sandbox and AI Gateway use Vercel OIDC tokens to authenticate whenever available. This is the most straightforward and recommended way to authenticate. You can also authenticate using access tokens.
Link local directory to Vercel project:
Pull OIDC token for local development:
In development, the token expires after 12 hours (run vercel env pull again to refresh). In production, Vercel manages token expiration for you.
Finally, we'll set up a Next.js route handler that accepts a request, generates and runs code and returns a response to the user.
We can now test our application using a prompt. Start the Next.js development server by running pnpm run dev, then in a new terminal window, send a user query using cURL:
You can switch to the development server terminal to observe the program running. Finally, once done, you can switch back to the other terminal to see the result. You should see a message that includes the title and url of a hackernews post.
You can also see the benefit of running the generated code in a sandbox by intentionally sending destructive and malicious queries to your application.
Attempt to delete files:
Expectation: The folder (if it exists) is removed inside the sandbox, but your main application files are unchanged.
Attempt to read secrets:
Expectation: You only see environment variables of the sandbox and not the host environment.
This example showed how to combine Next.js, the AI SDK, and Vercel Sandbox to safely run generated code. Learn more in the Vercel Sandbox docs and SDK reference and try it out in your own project today.