Best practices for data-analysis agents: run model-generated analysis logic in an isolated sandbox, use a fixed script entry point, structured output, and dependencies baked into the template, returning explainable, downloadable, and auditable results.
Code Interpreter Sandbox is designed for agents that need to run Python/Node scripts, process files, or generate structured results and report artifacts. The core need of a data-analysis agent is to run model-generated analysis logic in an isolated environment over your data and return explainable, downloadable, and auditable results. Running external data and model-generated code directly in a business service turns dependency conflicts, leftover files, long-running computation, noisy logs, and data permissions into engineering problems.
Sandbox can be used as a Code Interpreter Sandbox: the business system uploads data, generates or selects analysis scripts, and validates inputs and outputs; the sandbox runs scripts, handles temporary files, and returns structured results. This fits intelligent Q&A, operations analysis, report generation, and customer-service data diagnosis.
First create a template based on the Code Interpreter image (
For a Notebook-style
The following example analyzes revenue by segment in the sandbox and returns the total revenue, the top segment, and a summary:
Use Cases
- Upload a CSV, automatically compute metrics, and produce a summary.
- Trigger ad-hoc analysis in natural language without landing data in the main service's working directory.
- Run intermediate report computation in the sandbox and hand result files back to the business system.
- Use an independent sandbox per analysis in multi-tenant scenarios, reducing the risk of data cross-contamination.
Recommended Flow
First create a template based on the Code Interpreter image (code-interpreter-v1) and obtain its templateCode—see Template Management. Then split the analysis task into four steps:
- Write the input data and analysis script.
- Execute the script through a fixed entry point, for example
python3 analyze.py. - Require the script to print a JSON summary and, if needed, produce file artifacts.
- Read the results and destroy the sandbox.
Interactive Code Execution
For a Notebook-style run_code experience, use the e2b-code-interpreter SDK to create an instance and call run_code directly:
run_code better fits interactive tasks where the model iteratively generates and corrects code; a fixed script entry point better fits production pipelines, because script versions, input directories, timeouts, and output formats are easier to pin down.
Example Code
The following example analyzes revenue by segment in the sandbox and returns the total revenue, the top segment, and a summary:
Pin the Node.js SDK with
npm install e2b@2.31.0. Newer SDK versions return 405 when creating a sandbox.Production Advice
- Limit the size, type, and path of input files so a single task cannot exhaust memory or disk.
- Prefer JSON summaries for output; write charts, tables, and report files to
/tmpand download them afterwards. - Wrap
stdout,stderr, the exit code, and result files uniformly, so upper-layer agents never parse unstructured logs directly. - Version the analysis code. Do not keep only the model's natural-language explanation in a production system.
- Bake common dependencies such as
pandas,openpyxl, plotting libraries, and business SDKs into the template. - Keep end-user-visible results separate from troubleshooting logs, and never return full stack traces or sensitive data directly.
Next Steps
- Manage and Use Instances: Run commands, execute code, and read/write files
- Template Management: Create a template from the Code Interpreter image
- Using Browser Use Sandbox: Best practices for web automation tasks