Skip to main content

Overview

The Code Interpreter capability allows AI models to write and execute Python code in a secure sandboxed environment. This enables data analysis, mathematical computations, file processing, and more.

How It Works

When enabled, the AI can:
  • Write and execute Python code
  • Process uploaded files (CSV, JSON, images, etc.)
  • Generate visualizations and charts
  • Perform complex calculations
  • Analyze data and return results
Code runs in an isolated sandbox environment with timeout limits for security.

Setup

1

Get API Key

Obtain a code interpreter API key from code.librechat.ai or configure your own service.
2

Configure Environment

Add your API key to the .env file:
# .env
LIBRECHAT_CODE_API_KEY=your-key-here
3

Enable in Agent Configuration

In librechat.yaml, ensure code execution is enabled:
endpoints:
  agents:
    capabilities:
      - execute_code

Using Code Interpreter

In Agents

1

Open Agent Builder

Create a new agent or edit an existing one.
2

Enable Code Execution

Toggle Run Code in the capabilities section.
3

Configure API Key (if user-provided)

If using user-provided authentication, click the key icon to add your API key.

Example Prompts

Analyze this CSV file and show me:
1. Summary statistics
2. Missing values
3. Top 5 correlations

File Upload Support

Code Interpreter can work with uploaded files:
  • Data: CSV, JSON, Excel (.xlsx), TSV
  • Text: TXT, MD, PDF
  • Images: PNG, JPG, GIF (for analysis)
  • Code: Python (.py), Jupyter notebooks (.ipynb)
Files are processed in the execution environment. Maximum file size depends on your configuration.

Configuration Options

Agent-Level Settings

Configure code execution for specific agents:
// Agent form capabilities
{
  execute_code: true,
  tool_options: {
    timeout: 60000  // milliseconds
  }
}

System-Level Settings

Configure globally in librechat.yaml:
endpoints:
  agents:
    capabilities:
      - execute_code
    # Set default recursion limit for code execution loops
    recursionLimit: 25

Authentication Types

Code Interpreter supports different authentication methods:
Admin configures a shared API key:
# .env
LIBRECHAT_CODE_API_KEY=system-wide-key
All users can execute code without individual keys.

Code Execution Flow

Security

Code execution happens in a sandboxed environment with:
  • Isolated processes: Each execution runs separately
  • Timeout limits: Prevents infinite loops
  • Resource constraints: Memory and CPU limits
  • No network access: (Default) Sandbox cannot make external requests

Limitations

  • Timeout: Code execution has time limits (typically 60 seconds)
  • Package restrictions: Only pre-installed Python packages are available
  • No persistence: Environment resets between executions
  • File size limits: Large files may exceed processing limits

API Key Dialog

When using user-provided authentication, a dialog allows key management:
// API Key Dialog features
- Add new API key
- Revoke existing key
- Test connection
- Secure encrypted storage

Troubleshooting

  • Verify LIBRECHAT_CODE_API_KEY is set correctly
  • Check API key validity at code.librechat.ai
  • Ensure the code interpreter service is reachable
  • Simplify complex operations
  • Break large tasks into smaller steps
  • Optimize code for performance
  • Check which packages are pre-installed in the sandbox
  • Use standard library modules when possible
  • Contact admin to add custom packages

Best Practices

  • Test with small data first: Verify code works before processing large files
  • Clear instructions: Tell the AI exactly what you want to compute
  • Error handling: Ask the AI to include error checking in code
  • Visualizations: Request charts and graphs for better insights
  • Iterate: Refine code across multiple messages

Build docs developers (and LLMs) love