> ## Documentation Index
> Fetch the complete documentation index at: https://praison.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Code Execution

> Execute Python code in a sandboxed environment using Vercel Sandbox

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
graph LR
    U[Input] --> A[Agent]
    A --> T[Tool]
    T --> O[Output]

    classDef agent fill:#8B0000,color:#fff
    classDef tool fill:#189AB4,color:#fff

    class A agent
    class U,O tool
    class T tool
```

# 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 theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
npm install @vercel/sandbox
```

## Environment Variables

```bash theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
# Vercel Sandbox is included with Vercel deployments
# For local development, you may need additional setup
```

## Quick Start

<Steps>
  <Step title="Simple Usage">
    ```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    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);
    ```
  </Step>

  <Step title="With Configuration">
    Adjust agent instructions, tool options, and provider settings for production — see the Configuration section below.
  </Step>
</Steps>

## Configuration Options

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
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 theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
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 theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
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

<AccordionGroup>
  <Accordion title="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
  </Accordion>
</AccordionGroup>

## Related Tools

* [Code Mode](/docs/js/tools/code-mode) - Transform tools into code
