Code Executor
Execute Python code snippets using either secure sandboxed execution or high-performance local execution.Executor Types
Built-In Code Executor (Sandboxed)
Secure execution in isolated E2B containers with full feature support.Unsafe Local Code Executor
Direct execution on local system with identical feature set.Common Features
Both executors support the same feature set:| Feature | Built-In (Sandboxed) | Unsafe Local |
|---|---|---|
| Security | ✅ Isolated | ⚠️ Full access |
| Performance | Network overhead | ✅ Maximum speed |
| Context Persistence | ✅ Yes | ✅ Yes |
| Real-time Output | ✅ on_stdout/on_stderr | ✅ on_stdout/on_stderr |
| Environment Variables | ✅ envs | ✅ envs |
| Timeout Control | ✅ timeout_ms | ✅ timeout_ms |
| Multi-language | ✅ Python/JS/TS/Java/R/Bash | ✅ Python/JS/TS/Bash |
Basic Usage
Advanced Features
Persistent Context
Variables persist between executions in both executors:Real-time Output Streaming
Environment Variables
Timeout Configuration
Setup Instructions
Built-In Executor Setup
Environment Setup: Create a.envfile in your project root:Get your API key from E2B Dashboard.
Local Executor Setup
Ensure Python is installed:API Reference
Common Interface
Constructor Signatures
Execution Result
Language Support
| Language | Built-In | Local | Notes |
|---|---|---|---|
| Python | ✅ | ✅ | Full support |
| JavaScript | ✅ | ✅ | Full support |
| TypeScript | ✅ | ✅ | Full support |
| Bash | ✅ | ✅ | Full support |
| Java | ✅ | ❌ | Sandbox only |
| R | ✅ | ❌ | Sandbox only |
Examples
Basic Computation
Data Processing
Function Definition and Usage
Environment-Based Selection
Error Handling
Security Considerations
Built-In Executor
- ✅ Safe for untrusted code
- ✅ Network isolation
- ✅ Filesystem restrictions
- ✅ Resource limits
Local Executor
- ⚠️ Full system access
- ⚠️ Can modify files
- ⚠️ Can make network requests
- ⚠️ Can execute system commands
- Use Built-In for untrusted code
- Validate input before local execution
- Run in restricted environments when possible
- Monitor resource usage
Performance Tips
Built-In Executor
- Reuse sandbox instances
- Batch multiple executions
- Use connection pooling
Local Executor
- Leverage persistent context
- Use real-time callbacks for long operations
- Optimize interpreter startup time
Cleanup
Troubleshooting
Built-In Executor
- Sandbox creation failed: Check AG_KIT_SANDBOX_API_KEY
- Network timeout: Increase timeout or check connectivity
Local Executor
- Python not found: Install Python 3.8+
- Permission denied: Check file/directory permissions
- Context lost: Session may have terminated, restart executor
Best Practices
- Set reasonable timeouts: Prevent infinite loops
- Validate input: Sanitize code before execution
- Use sandboxed execution: For untrusted code
- Monitor resources: Track memory and CPU usage
- Handle errors gracefully: Catch and log execution errors
- Clean up resources: Call cleanup() when done
Examples Repository
Find complete working examples at:- code_executor.py - Comprehensive code executor examples
- langchain_adapter.py - LangChain integration
- llamaindex_adapter.py - LlamaIndex integration