Enterprise AI Security Guide

Agent Security & Code Sandboxing

Safely isolate and execute LLM-generated code in ephemeral microVMs, WebAssembly sandboxes, and secure container runtimes.

01

Prompt Injection

Adversarial inputs that trick LLM agents into running arbitrary shell commands (e.g. rm -rf /, exfiltrating credentials).

02

Resource Exhaustion

Infinite loops or memory-bomb code generated by models that freeze host server CPUs and exhaust memory buffers.

03

Network Exfiltration

Agents attempting outbound HTTP requests to malicious endpoints to leak environment variables or API secrets.

Sandboxing Architecture Comparison

Technology Isolation Level Startup Time Ideal Use Case
E2B Code Interpreter Firecracker MicroVM < 150ms Python/JS code execution for AI agents in cloud
Pyodide (WASM) Browser/V8 Sandbox Instant (0ms) Client-side browser Python execution with zero server load
Modal / GCP Cloud Run gVisor / Container VM ~ 500ms Heavy data science & ML model code execution

E2B MicroVM Sandboxed Code Execution

The Python snippet below creates an ephemeral, isolated MicroVM sandbox using E2B to execute LLM-generated code safely without risking the host server.

from e2b_code_interpreter import Sandbox

def execute_agent_code_safely(generated_code: str) -> str:
    # 1. Spawn isolated Firecracker MicroVM
    with Sandbox(timeout=30) as sandbox:
        print(f"Sandbox ID: {sandbox.sandbox_id} initialized.")
        
        # 2. Execute untrusted LLM python code
        execution = sandbox.run_code(generated_code)
        
        # 3. Handle errors or return stdout
        if execution.error:
            return f"Execution Failed: {execution.error.name} - {execution.error.value}"
        
        output = "".join([log.line for log in execution.logs.stdout])
        return output

# Example Usage
llm_code = """
import math
data = [10, 20, 30, 40, 50]
print(f"Mean: {sum(data)/len(data)}, StdDev: {math.sqrt(sum((x-30)**2 for x in data)/len(data))}")
"""

result = execute_agent_code_safely(llm_code)
print("Sandboxed Result:", result)