codeCode Interpreter Agent

Execution Flow & Architecture

Process Flow Diagram

Workflow Explanation

  1. Input Detection: Detects if input is natural language (generates code) or raw code (executes as-is). Identifies language (Python default, or JS/TS/R/Java)

  2. Code Preparation: Adds imports, installs packages dynamically, includes error handling and save commands for artifacts

  3. Secure Execution: Runs in sandbox. On error: analyzes -> modifies -> re-executes (max 5 attempts). Blocks dangerous operations

  4. Result Formatting: Returns final code only (hides iterations), formatted results, and embedded artifacts


Detailed Mechanisms

Code Execution Mechanism

How Code is Executed:

  1. Agent calls the E2B Sandbox Tool with parameters:

    • code: The Python/JS/TS/R/Java code to execute

    • language: Target programming language (default: Python)

    • timeout: Maximum execution time in seconds (default: 30s, max: 300s)

    • additional_packages: Optional packages to install before execution

  2. Sandbox Initialization: E2B Cloud Sandbox creates isolated environment with specified language runtime

  3. Package Installation: Installs required packages dynamically within the sandbox

  4. Code Execution: Runs code in isolated environment with resource monitoring

  5. Result Collection & Response: Captures execution details and returns structured response:

Language Support

  1. Supported Languages: Python (default), JavaScript, TypeScript, R, Java

  2. Extensible: Additional languages can be installed dynamically based on user requirements or query needs

Security Mechanisms

Layer
Protection

Execution Boundaries

Isolated sandbox; no system commands; max 5 iterations; reject eval/exec

Resource Protection

Memory monitoring; computation limits; output truncation

Code Validation

Block harmful operations; sanitize errors; prevent system info exposure

Iterative Error Recovery

  1. Error Detection: Captures exceptions, syntax errors, runtime failures

  2. Error Analysis: Parses messages, identifies root causes

  3. Code Modification: Adjusts imports, fixes syntax, installs packages

  4. Re-execution: Runs modified code (max 5 attempts)

  5. Clean Presentation: Only final successful code shown to users

Example: Missing matplotlib -> agent installs it automatically -> re-executes -> user sees only working result.

Artifacts

  1. Visualization Generation: Charts, graphs, plots, heatmaps saved as PNG/JPG/SVG and embedded in markdown

  2. File Generation: CSV exports, JSON data, reports, processed files


Sample Code

Using Code Interpreter Agent via SDK

Output:

Executed Code:

Output: Example execution returns a generated chessboard image artifact plus summary text.

Last updated

Was this helpful?