Learn
Build an autonomous data analyst using Dynamiq, E2B and Together AI
Build a data analyst agent in Python with the open-source Dynamiq SDK: it reasons with a Together AI model and writes and runs code in an E2B sandbox.

In short
A data analyst agent pairs a model that plans the analysis with a sandbox that runs the code. With the open-source Dynamiq SDK you need three parts: an Agent that reasons with a Together AI model, an E2B code interpreter tool, and your data passed in as a file. The agent loads the data, runs the analysis, fixes its own errors and returns a markdown report.
An autonomous data analyst is an AI agent that answers questions about your data by writing and running code, not by guessing from the numbers in a prompt. You give it a file and a question; it plans the analysis, runs Python in an isolated sandbox, reads the results, fixes its own errors and writes up what it found. This tutorial builds one with the open-source Dynamiq SDK, an E2B sandbox and a model served by Together AI.
The code below targets the current SDK, where every agent is an Agent. If you followed an earlier version of this post that used ReActAgent, the concepts carry over unchanged.
What can a data analyst agent do?
A data analyst agent handles the parts of an analysis that follow a repeatable method:
- Load and inspect data from CSV, Excel, JSON or other files, and check types, gaps and outliers.
- Transform data by cleaning, joining, filtering and aggregating it with pandas.
- Run statistics and models such as correlations, regressions, clustering and simple forecasts.
- Make charts of distributions, trends and relationships.
- Report results in markdown with the method, the numbers and the caveats.
The important design choice is where the code runs. Executing model-written code on your laptop or server is risky, so the agent sends it to a sandbox: a short-lived, isolated machine that E2B starts in the cloud. If the code fails, the agent reads the error, fixes it and tries again.
What do you need?
- Python 3.11 to 3.13.
- The Dynamiq SDK:
pip install dynamiq. The E2B client is included. - A Together AI API key in
TOGETHER_API_KEY. - An E2B API key in
E2B_API_KEY. - A data file. This tutorial uses a CSV of house prices with
bedrooms,bathroomsandpricecolumns.
Step 1: Set up the model and the code interpreter
The TogetherAI node calls any chat model hosted on Together AI, and the connection reads your key from the environment. The E2BInterpreterTool gives the agent a sandbox where it can run Python and shell commands, install packages and read uploaded files.
import io
import os
from dynamiq import Workflow
from dynamiq.callbacks import TracingCallbackHandler
from dynamiq.connections import E2B as E2BConnection
from dynamiq.connections import TogetherAI as TogetherAIConnection
from dynamiq.flows import Flow
from dynamiq.nodes.agents import Agent
from dynamiq.nodes.llms import TogetherAI
from dynamiq.nodes.tools.e2b_sandbox import E2BInterpreterTool
from dynamiq.nodes.types import Behavior, InferenceMode
from dynamiq.runnables import RunnableConfig
llm = TogetherAI(
connection=TogetherAIConnection(), # reads TOGETHER_API_KEY
model="moonshotai/Kimi-K3",
temperature=0.1,
)
code_tool = E2BInterpreterTool(connection=E2BConnection()) # reads E2B_API_KEY
Any model on Together AI that follows instructions and tool formats reliably will work; swap the model string for another from Together's model list. To use a different provider, swap the LLM class, for example OpenAI or Anthropic from dynamiq.nodes.llms. The rest of the code stays the same.
Step 2: Create the agent
The role is the agent's standing instructions. Tell it where files are, how to work and what the answer should look like. Uploaded files land in /home/user/input inside the sandbox.
ANALYST_ROLE = """
You are a senior data analyst who answers questions by writing and running Python.
- Uploaded files are in /home/user/input. Inspect a file before you analyze it.
- Use pandas, numpy, scikit-learn and matplotlib. Install missing packages with the tool.
- Combine loading, cleaning and analysis into as few tool calls as possible.
- Always print results. If code fails, read the error, fix the code and run it again.
- Answer in markdown: a two-sentence summary first, then method, results and caveats.
"""
agent = Agent(
name="data-analyst",
id="data-analyst",
llm=llm,
tools=[code_tool],
role=ANALYST_ROLE,
inference_mode=InferenceMode.XML,
max_loops=12,
behaviour_on_max_loops=Behavior.RETURN,
)
InferenceMode.XML asks the model to format its reasoning and tool calls as XML, which works reliably across providers. max_loops caps the number of reason, act and observe cycles, and Behavior.RETURN returns the best answer so far instead of raising an error if the cap is reached.
Step 3: Pass in a file and run the analysis
Files go in as BytesIO objects. The name becomes the file name in the sandbox, and the optional description tells the agent what the file contains.
def load_file(path: str, description: str) -> io.BytesIO:
with open(path, "rb") as f:
data = io.BytesIO(f.read())
data.name = os.path.basename(path) # saved as /home/user/input/<name>
data.description = description
return data
house_prices = load_file(
"data/house_prices.csv",
"Comma-separated. Columns: bedrooms, bathrooms, price.",
)
tracing = TracingCallbackHandler()
workflow = Workflow(flow=Flow(nodes=[agent]))
result = workflow.run(
input_data={
"input": (
"Fit a linear regression of price on bedrooms and bathrooms. "
"Use a fixed random seed and a held-out test set, report the test MSE "
"and R-squared, and explain which feature matters more."
),
"files": [house_prices],
},
config=RunnableConfig(callbacks=[tracing]),
)
print(result.output[agent.id]["output"]["content"])
Wrapping the agent in a Workflow gives you consistent output handling and callbacks such as tracing. Inside a workflow, the agent's answer is at result.output[agent.id]["output"]["content"].
Step 4: See what the agent did
Every step the agent takes, each model call and each tool call, is recorded by the tracing handler:
for run in tracing.runs.values():
print(run.type, run.name, run.status)
To see the same run tree in the Dynamiq platform, with inputs, outputs, timings and token usage for every node, replace TracingCallbackHandler with DynamiqTracingCallbackHandler and a project-scoped access key. The SDK documentation covers the setup.
What happens during a run?
The agent works in a loop until it can answer:
- Reason. It reads the question and the file description and plans a first step, usually inspecting the file.
- Act. It sends Python code to the E2B sandbox, for example
pd.read_csv("/home/user/input/house_prices.csv").describe(). - Observe. It reads the printed output or the error message.
- Repeat. It fits the model, checks the results and fixes anything that failed.
- Answer. It writes the markdown report with the numbers it computed.
Because the numbers come from code that ran, not from the model's memory, you can check any figure by reading the code in the trace.
What else can you ask it?
Swap the file and the question; the agent stays the same.
- "Which customer attributes are most associated with churn in this file? Show a correlation table and a chart."
- "Compare monthly transaction volumes by region and flag months more than two standard deviations from the mean."
- "Match these two exports on invoice number and list the records that appear in only one of them, with totals."
The last one is a typical back-office reconciliation, where an agent prepares the exceptions and a person reviews them. See how teams run that pattern in production in back-office automation.
How do you use it with sensitive data?
Check where the sandbox runs before you upload regulated data. E2B runs sandboxes in its cloud by default, and the model provider receives whatever the agent puts in its prompts. For experiments, use synthetic or masked data. For production, run the agent where your data policies allow, screen prompts for personal data, and keep a trace of every step for review.
How do you take it to production?
The agent you built in code runs on the same engine as the visual canvas in Agent Builder, so a team can build in Python, review on the canvas and deploy the same workflow as an API with versions and rollback. On the platform you can add guardrails that detect PII and prompt injection, require approval before a step runs, score answers with evaluations on test sets and live traffic, and self-host the whole platform in your own cloud or data center.
FAQ
What is an AI data analyst agent?
It is an AI agent that answers questions about data by planning an analysis, writing code, running it in a sandbox and reporting the results. Unlike a chatbot reading numbers from a prompt, it computes every figure with code you can inspect.
Why run the agent's code in a sandbox?
Model-written code can be wrong or unsafe. A sandbox is an isolated, short-lived machine, so a failed or harmful command cannot touch your own systems, and the agent can install packages and read files freely inside it.
Can I use a different LLM instead of Together AI?
Yes. Replace the TogetherAI node with another LLM class, such as OpenAI, Anthropic or WatsonX. The Dynamiq SDK supports 29 model providers, and the agent and tool code stays the same.
Is the Dynamiq SDK open source?
Yes. The Python SDK is open source under the Apache-2.0 license and available on GitHub. The Dynamiq platform, with its visual builder, deployment, guardrails and observability, is a commercial product.
Can the agent read Excel or PDF files?
Yes. Pass the file the same way, and the agent can install a parsing package such as openpyxl or pdfplumber in the sandbox before reading it. Describe the file's contents in its description to save the agent a step.



