Code Execution
Execute Python code in a sandboxed environment using Vercel Sandbox
Code Execution Tool
Execute Python code safely in a sandboxed environment using Vercel Sandbox. Run calculations, data processing, and computational tasks in isolation.
Installation
npm install @vercel/sandboxEnvironment Variables
# Vercel Sandbox is included with Vercel deployments
# For local development, you may need additional setupQuick Start
- 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); - With Configuration
Adjust agent instructions, tool options, and provider settings for production — see the Configuration section below.
Configuration Options
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 functionsjson– JSON parsingdatetime– Date/time operationsre– Regular expressionscollections– Data structures
Advanced Example
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
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
- Set timeouts – Prevent infinite loops
- Limit memory – Avoid memory exhaustion
- Validate output – Check for errors
- Use for computation – Not for I/O operations
Related Tools
- Code Mode – Transform tools into code