Skip to main content
Code agents run model-generated Python in a sandboxed interpreter so numeric and data tasks execute safely.
The user submits a task; the agent writes Python, runs it in the sandbox, and returns the result.
CodeAgent(model="gpt-4o") is now accepted (canonical). llm= still works as a deprecated alias — see Model Parameter.

Quick Start

1

Install Package

First, install the required packages:
2

Set API Key

Set your OpenAI API key and E2B API key as an environment variable in your terminal:
3

Create a file

Create a new file app.py with the basic setup:
4

Start Agents

Type this in your terminal to run your agents:
Requirements
  • Python 3.10 or higher
  • OpenAI API key. Generate OpenAI API key here. Use Other models using this guide.
  • e2b_code_interpreter package installed

How It Works

The user submits a task; the agent writes Python, runs it in the sandboxed interpreter, and returns the captured result.

Understanding Code Agents

What are Code Agents?

Code agents are specialized AI agents that can:
  • Write Python code based on requirements
  • Execute code safely in a sandboxed environment
  • Handle code execution results and errors
  • Work together in a pipeline (writer → executor)

Features

Code Writer

Writes Python code based on requirements.

Safe Execution

Executes code in a sandboxed environment.

Error Handling

Manages code execution errors and debugging.

Results Processing

Processes and formats execution results.

Multi-Agent Code Development

1

Install Package

First, install the required packages:
2

Set API Key

Set your OpenAI API key as an environment variable in your terminal:
3

Create a file

Create a new file app.py with the basic setup:
4

Start Agents

Type this in your terminal to run your agents:

Configuration Options

Troubleshooting

Code Errors

If code execution fails:
  • Check syntax errors
  • Verify package imports
  • Enable verbose mode for debugging

Sandbox Issues

If sandbox execution fails:
  • Check environment setup
  • Verify permissions
  • Review resource limits

Best Practices

Test generated code in the sandbox environment before enabling execution in production workflows.
Register only the tools and packages the agent needs — fewer imports reduce sandbox escape risk.
Enable verbose output to inspect code the model generates and stderr from failed runs.
Keep code_mode="safe" unless you explicitly need direct host execution and accept the trade-offs.

Next Steps

AutoAgents

Learn about automatically created and managed AI agents

Mini Agents

Explore lightweight, focused AI agents

Code Execution with Tools

Let model-generated code call your registered tools directly — collapse multi-step pipelines into a single turn.
For optimal results, ensure code is properly formatted and tested in the sandbox environment before production use.
Use a specialised agent for numerical and mathematical tasks.
Run the PraisonAI coding assistant from the command line.