← Back to all guides

Code Sandbox Tool — Safe & Dynamic Code Execution

Langoedge Team2 min read

What is the Code Sandbox Tool?

The Code Sandbox (sandbox) is a powerful tool that allows your AI agents to execute code dynamically within a secure, isolated runtime environment.

While custom Python nodes allow you to build predefined, deterministic logic directly into your graph, the Code Sandbox tool allows the AI agent to write and execute code on the fly during a conversation to solve complex problems, perform data transformations, or generate visual charts.


Capabilities & Key Features

  • Dynamic Code Execution: The agent can generate code dynamically based on user input, run it, and inspect the console results to answer mathematical or algorithmic questions.
  • Isolated Environment: Every execution occurs in a clean, short-lived sandbox, ensuring that code from one run cannot interfere with another session or compromise the parent server.
  • Asset & Image Generation: The sandbox supports compiling plots and charts (e.g., using matplotlib or pandas). If the code yields an image object, the sandbox automatically captures the output, uploads it, and returns a secure image URL.
  • Package Management: Includes support for Pyodide, enabling on-demand loading of select python packages required for complex computations.

Tool Parameters

Parameter Type Required Description
code string Yes The raw Python code to be executed in the sandbox environment.

Example Usage Scenario

  1. User asks: "Can you plot a bar chart showing our sales growth: Jan ($12k), Feb ($15k), Mar ($18k)?"
  2. AI Agent binds the sandbox tool.
  3. AI Agent writes a python script using matplotlib to plot the bar chart and calls sandbox with:
    import matplotlib.pyplot as plt
    months = ['Jan', 'Feb', 'Mar']
    sales = [12000, 15000, 18000]
    plt.bar(months, sales, color='blue')
    plt.title('Sales Growth')
    plt.ylabel('USD ($)')
    plt.show()
    
  4. Sandbox executes the code. Because the output is a plot, it captures the raw data, processes it, and returns:
    {
      "image_url": "https://storage.langoedge.com/generated-files/thread_123/sales_growth.png"
    }
    
  5. AI Agent renders the image directly in the chat to the user.

Safety & Security Restrictions

To maintain high security, the sandbox restricts several operations:

  • Blocked Operations: Direct filesystem writes, network socket binding, execution of raw system commands (os.system), and environment variable modification.
  • Allowed Operations: Data calculations, statistical modeling, plotting, string parsing, and clean package import calls.

Frequently Asked Questions

What python libraries are available in the sandbox?
The sandbox supports standard built-in libraries (such as `math`, `datetime`, `json`) as well as on-demand loading of scientific libraries like `numpy`, `pandas`, and `matplotlib`.
How does the sandbox return images?
If the script generates a visual plot (e.g., calling `plt.show()`), the sandbox intercepts the plot data, uploads the image asset, and returns an `image_url` object key instead of plain text logs.
LT

Langoedge Team

The Langoedge engineering team builds AI agent infrastructure that empowers businesses to deploy reliable, observable AI staff. Follow Langoedge Team on LinkedIn for product updates and architectural deep dives.