# How to build an agent that analyzes an uploaded CSV

Give an agent a sales CSV, run its Python in a sandbox, and check the chart against a second question about the same file.

Canonical URL: https://getfloo.com/blog/build-a-csv-analysis-agent

## TL;DR

Revenue fell $400 in October. North accounted for a $500 drop. How can one customer explain more than the whole decline? Build an agent that can show its arithmetic.

- Upload six rows once and ask two questions about the same file.
- Give the model the column names and execute its Python in a sandbox.
- Open the chart and reconcile its bars to the monthly totals.

## Start with a question you can check

A sales lead asks which customers account for the October drop. A ranking of October's biggest customers won't answer that. You need the change for each customer, including customers whose revenue grew. Give the agent a small file you can check by eye before trusting it with a quarter of sales.

| Customer | September | October | Change |
| --- | ---: | ---: | ---: |
| North | 1,200 | 700 | −500 |
| West | 800 | 950 | +150 |
| East | 500 | 450 | −50 |

The downloadable script contains those six observations as CSV. The headers are `month,customer,revenue`, with one row per month and customer. Amounts are whole dollars in one currency. [CSV agent source](https://getfloo.com/examples/sandbox-agents/csv_agent.py) · [Host helper](https://getfloo.com/examples/sandbox-agents/agent_tools.py)

## Run your first report

Use Python 3.10 or later, a signed-in Claude Code CLI, and a floo account with sandbox credits. Sign in with the floo CLI or set FLOO_API_KEY in your shell. Run these commands in an empty directory.

```bash
python3 -m venv .venv
. .venv/bin/activate
python -m pip install floo-sandbox==0.2.0
export FLOO_API_KEY="${FLOO_API_KEY:-$(floo auth token)}"
base=https://getfloo.com/examples/sandbox-agents
curl -fsSLO "$base/agent_tools.py"
curl -fsSLO "$base/csv_agent.py"
python csv_agent.py
```

Open `revenue.html` in a browser. Bar width shows the magnitude of each change. The sign and dollar label give its direction. In our run North fell $500, East fell $50, and West gained $150. Together those movements explain the $400 decline.

## Give the model a schema and Python a file

The host reads the headers and writes the upload to `/work/sales.csv`. Claude receives the headers, the question, and an output contract: return a Python script that prints one JSON object mapping customer names to integer dollar changes. The program runs that script in a sandbox with outbound networking blocked. The ordinary file write happens before Python needs a machine.

```python
box.filesystem.write_text(data, "/work/sales.csv")
changes = analyze(box, "For each customer, calculate October minus September. "
                  "Sum all revenue rows in each month before subtracting.", headers)
write_chart(changes)
```

The host checks the result shape and draws the bars itself. This catches a paragraph where the chart needs numbers. It takes a separate arithmetic check to catch a plausible but incorrect number.

## Ask a second question without uploading the file again

The next call asks for September and October totals. It writes a fresh script into the same workspace and reads the same CSV from disk. Our second answer was $2,500 and $2,100. Subtract those totals and compare the difference with the sum of the bars. If they differ, inspect the generated aggregation before sharing the chart.

Try adding a second October sale of $100 for North. Run `python csv_agent.py sales.csv` with your edited file. North's decline should shrink to $400 and the net decline to $300. This catches a common mistake: overwriting an earlier transaction for the same customer instead of adding the two. For files with dates spanning several years, specify a year and month in the prompt and fixture before using the result.

The script terminates the sandbox after both questions. Your HTML chart remains in the directory where you launched it. For a service that needs a different mix of file access, Python execution, and notebook state, see [Python sandboxes for data-analysis agents](https://getfloo.com/blog/best-python-sandboxes-for-data-analysis-agents).
