You can use E2B Sandbox to run AI-generated code to analyze data. Here’s how the AI data analysis workflow usually looks like:Documentation Index
Fetch the complete documentation index at: https://e2b.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
- Your user has a dataset in CSV format or other formats.
- You prompt the LLM to generate code (usually Python) based on the user’s data.
- The sandbox runs the AI-generated code and returns the results.
- You display the results to the user.
Example: Analyze CSV file with E2B and Claude 3.5 Sonnet
This short example will show you how to use E2B Sandbox to run AI-generated code to analyze CSV data.Table of Contents
- Install dependencies
- Set your API keys
- Download example CSV file
- Initialize the sandbox and upload the dataset to the sandbox
- Prepare the method for running AI-generated code
- Prepare the prompt and initialize Anthropic client
- Connect the sandbox to the LLM with tool calling
- Parse the LLM response and run the AI-generated code in the sandbox
- Save the generated chart
- Run the code
- Full final code
1. Install dependencies
Install the E2B SDK and Claude SDK to your project by running the following command in your terminal.2. Set your API keys
- Get your E2B API key from E2B Dashboard.
- Get your Claude API key from Claude API Dashboard.
- Paste the keys into your
.envfile.
3. Download example CSV file
We’ll be using the publicly available dataset of about 10,000 movies.- Click the “Download” button at the top of the page.
- Select “Download as zip (2 MB)”.
- Unzip the file and you should see
dataset.csvfile. Move it to the root of your project.
4. Initialize the sandbox and upload the dataset to the sandbox
We’ll upload the dataset from the third step to the sandbox and save it asdataset.csv file.
5. Prepare the method for running AI-generated code
Add the following code to the file. Here we’re adding the method for code execution.6. Prepare the prompt and initialize Anthropic client
The prompt we’ll be using describes the dataset and the analysis we want to perform like this:- Describe the columns in the CSV dataset.
- Ask the LLM what we want to analyze - here we want to analyze the vote average over time. We’re asking for a chart as the output.
- Instruct the LLM to generate Python code for the data analysis.
7. Connect the sandbox to the LLM with tool calling
We’ll use Claude’s ability to use tools (function calling) to run the code in the sandbox. The way we’ll do it is by connecting the method for running AI-generated code we created in the previous step to the Claude model. Update the initialization of the Anthropic client to include the tool use like this:8. Parse the LLM response and run the AI-generated code in the sandbox
Now we’ll parse themsg object to get the code from the LLM’s response based on the tool we created in the previous step.
Once we have the code, we’ll pass it to the runAIGeneratedCode method in JavaScript or run_ai_generated_code method in Python we created in the previous step to run the code in the sandbox.
9. Save the generated chart
When running code in the sandbox for data analysis, you can get different types of results. Including stdout, stderr, charts, tables, text, runtime errors, and more. In this example we’re specifically asking for a chart so we’ll be looking for the chart in the results. Let’s update therunAIGeneratedCode method in JavaScript and run_ai_generated_code method in Python to check for the chart in the results and save it to the file.
10. Run the code
Now you can run the whole code to see the results.