Agrawal argues that running AI-generated code is effectively running code from an untrusted source, often with the application's privileges. He describes three risks: hallucinated code that can crash or consume resources, code that exposes secrets while trying to help, and prompt injection that can lead to data exfiltration.
Why, and how you need to sandbox AI-Generated Code?
Speaker
Harshil Agrawal, senior developer advocate at Cloudflare, explains how to sandbox AI-generated code at an AI Engineer event.
Agrawal recommends capability-based security: deny access by default, then grant only the specific capabilities the code needs. V8 isolates suit short-lived functions and tool calls that need speed but no file system or processes; containers suit tasks that require Linux, package installs, files, or development servers.
For an app that lets an AI generate small functions, Agrawal used V8 isolates. His example passes only a restricted database interface and logger, while disabling outbound network access with `globalOutbound: null`. Isolates are fast and lightweight, but have no file system or process model and support only a limited set of runtimes.
For a video generator that builds apps and serves previews, Agrawal used a separate container for each user. Containers provide a file system, processes, package installation, and networking, but take longer to start and cost more to run. He advises keeping secrets outside the container and proxying authenticated requests through the application.
Agrawal's checklist includes blocking network access by default, granting minimal capabilities, isolating users, setting resource limits, keeping secrets outside the sandbox, cleaning up, logging execution, and validating code before it runs. He recommends choosing an isolate or container based on what the current task requires, and using both when an agent's workflow includes different kinds of work.
Takeaways
- Treat AI-generated code as untrusted, even when it appears correct.
- Default-deny access and grant only the capabilities the code needs.
- Use isolates for quick, constrained execution; use containers when code needs files, processes, or package installs.
- Give each user a separate sandbox, and keep secrets outside it.
- Set resource limits, log execution, validate inputs, and clean up sandboxes when work ends.
Mentioned in this talk
Search inside this talk
What was claimed 5
- works atHarshil Agrawal works at
Cloudflare
Sr. Developer Educator / Developer Advocate“I'm a senior developer advocate at Cloudflare.”
YouTube Verified2 sources· Talk, Web· archived
- featureV8 isolates: Lightweight sandboxed execution for JavaScript, TypeScript, Python, and WebAssembly; no filesystem or process model.
“They start in a quarter millisecond, and they can run JavaScript, Python, TypeScript, and even WebAssembly.”
YouTube Unverified1 source· Talk
- featuresandbox: Provide a Linux environment with a filesystem, processes, and networking.
“They have a full Linux environment, a real file system, real processes, and real networking.”
YouTube Unverified1 source· Talk
- usesFor small, repetitive AI-generated functions that need fast, lightweight execution.
“For this, I used V8 isolates.”
YouTube Unverified1 source· Talk
- featurePrompt Motion: Generates video from user descriptions by running AI-written code in a container and providing a preview URL.
“Once the code is ready, it starts the development server and shows the user a preview.”
YouTube Unverified1 source· Talk











