CodeAgent(model="gpt-4o") is now accepted (canonical). llm= still works as a deprecated alias — see Model Parameter.Quick Start
- Code
- No Code
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
- Code
- No Code
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
Run in sandbox first
Run in sandbox first
Test generated code in the sandbox environment before enabling execution in production workflows.
Limit interpreter scope
Limit interpreter scope
Register only the tools and packages the agent needs — fewer imports reduce sandbox escape risk.
Use verbose mode when debugging
Use verbose mode when debugging
Enable verbose output to inspect code the model generates and stderr from failed runs.
Prefer safe execution mode
Prefer safe execution mode
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.
Related
Use a specialised agent for numerical and mathematical tasks.
Run the PraisonAI coding assistant from the command line.

