WhoBuilt

How to add secure code interpreting in your AI app

Feb 6, 2025· 1 h 48 min· 1.2K views·Watch on YouTube

Speaker

Vasek Mlejnsky, E2B co-founder, leads an AI Engineer workshop on adding secure Python code execution to an AI app using E2B sandboxes and the Vercel AI SDK.

Build a chat app that can run code

Mlejnsky introduces an Artifacts-style app: a chat interface paired with a preview, where Claude can generate code and run it. The workshop uses Python and E2B’s Linux sandboxes, with plans to demonstrate other runtimes and languages.

Connect the app to Claude and add a tool

The starter app uses Next.js and the Vercel AI SDK to stream messages to and from Anthropic’s Sonnet 3.5. Mlejnsky then adds a `runPython` tool, giving the model a code string and descriptive fields to guide its use.

Keep execution isolated and preserve session context

The tool calls E2B’s Code Interpreter SDK to execute generated Python inside a sandboxed VM. The app searches sandbox metadata for a matching user ID and reconnects to that sandbox, preserving notebook state between requests; otherwise, it creates a new one. Mlejnsky says sandboxes start in roughly 800 to 900 milliseconds.

Return output and show a preview

Execution results include standard output, error output, runtime errors with tracebacks, and cell results such as charts, images, PDFs, and HTML. The app streams tool status to the frontend and renders a returned PNG in a side panel. Mlejnsky notes that interactive charts and richer data views would require additional frontend work.

Discuss sandbox security and customization

In the Q&A, Mlejnsky describes Firecracker VMs and the Jailer as the foundation for isolating untrusted code, with a separate VM for each user session. He says sandbox contents are destroyed when a sandbox closes, and custom environments can be built from Ubuntu-based Dockerfiles that E2B converts into VM filesystems.

Takeaways

  • Give generated code its own isolated runtime instead of executing it on your app server.
  • Use a stable user or session ID to reconnect to a sandbox and retain execution context.
  • Return structured logs, errors, and rich cell outputs so the model and frontend can handle them separately.
  • Stream execution status alongside model responses to give users feedback while code runs.
  • Treat sandbox isolation, data retention, and custom runtime contents as explicit design choices.

Mentioned in this talk

Search inside this talk

What was claimed 11

Show all 11 facts

Related talks