Code Execution

Execute Python code in a sandboxed environment using Vercel Sandbox

Diagram

Input

Agent

Tool

Output

Code Execution Tool

Execute Python code safely in a sandboxed environment using Vercel Sandbox. Run calculations, data processing, and computational tasks in isolation.

Installation

bash
npm install @vercel/sandbox

Environment Variables

bash
# Vercel Sandbox is included with Vercel deployments
# For local development, you may need additional setup

Quick Start

  1. Simple Usage
    typescript
    import { Agent } from 'praisonai';
    import { codeExecution } from 'praisonai/tools';
    
    const agent = new Agent({
      name: 'CodeRunner',
      instructions: 'You can execute Python code to solve problems.',
      tools: [codeExecution()],
    });
    
    const result = await agent.run('Calculate the factorial of 10');
    console.log(result.text);
  2. With Configuration

    Adjust agent instructions, tool options, and provider settings for production — see the Configuration section below.

Configuration Options

typescript
import { codeExecution } from 'praisonai/tools';

const codeTool = codeExecution({
  // Timeout in milliseconds
  timeout: 30000,

  // Memory limit in MB
  memoryLimit: 128,

  // Allowed imports
  allowedImports: ['math', 'json', 'datetime'],
});

Supported Languages

Currently supports Python 3.13 with common libraries:

  • math – Mathematical functions
  • json – JSON parsing
  • datetime – Date/time operations
  • re – Regular expressions
  • collections – Data structures

Advanced Example

typescript
import { Agent } from 'praisonai';
import { codeExecution } from 'praisonai/tools';

const agent = new Agent({
  name: 'DataAnalyst',
  instructions: `You are a data analyst.
    Use Python code to perform calculations and data analysis.
    Always show your work and explain the results.`,
  tools: [codeExecution({ timeout: 60000 })],
});

const result = await agent.run(`
  Calculate the following:
  1. Sum of first 100 prime numbers
  2. Standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  3. Fibonacci sequence up to 1000
`);
console.log(result.text);

Response Format

typescript
interface CodeExecutionResult {
  output: string;
  error?: string;
  executionTime: number;
  memoryUsed?: number;
}

Security

  • Sandboxed – Code runs in isolated environment
  • No network – No external network access
  • No filesystem – No persistent storage
  • Time limited – Execution timeout enforced
  • Memory limited – Memory usage capped

Best Practices

Guidelines
  1. Set timeouts – Prevent infinite loops
  2. Limit memory – Avoid memory exhaustion
  3. Validate output – Check for errors
  4. Use for computation – Not for I/O operations

Related Tools