A Practical Guide to LLM Agents for Data Analysis

    This article explains why LLM agents should not answer data analysis questions directly, and how a plan-execute-observe loop separates probabilistic generation from deterministic calculation. It covers the agent workflow for a quantitative daily report, why script-driven charts and document assembly outperform pixel-generated images, an illustrative comparison of quantile results across implementation paths, and six engineering rules for reliable, reproducible output.

    1. The Problem: Why Data Analysis Cannot Rely on the Model Answering Directly

    The most common mistake when introducing large language models into data analysis is treating them as a question-answering machine that happens to do arithmetic. In reality, an LLM is fundamentally a next-token probability predictor. It does not enforce mathematical rules, and it does not guarantee decimal precision. When a task involves structured numbers such as quantiles, basis levels, or inventory changes, letting the model “compute in its head” or “infer from context” usually yields approximations. Once the error is amplified a few decimal places down, it is enough to completely reverse the conclusion of a research report.

    The more reliable engineering pattern is therefore: let the model understand intent, break down the task, and write code; let the interpreter do the exact calculation. This division of labor confines probabilistic generation to natural language and code structure, and hands deterministic computation to the runtime. Architecturally, it removes hallucination from numerical conclusions.

    2. The Core Mechanism: Why an Agent Beats Single-Shot Generation

    The key difference between an agent and a single-turn conversation is the closed loop of “plan, execute, observe, re-plan.” Take generating a quantitative daily report. A mature agent typically creates a task list automatically, then works through it item by item:

    1. Read the local spreadsheet or data source, identify fields, and clean the data;
    2. Retrieve relevant background information online or from a local news library;
    3. Write and run a script to compute the metrics;
    4. Call a plotting library to generate the charts;
    5. Merge the text and charts into a document.

    This flow closely resembles how mainstream AI coding tools work: a todo list comes first, items are executed one by one, and failures are self-corrected based on the error output. The value lies in an observable process and auditable intermediate artifacts, rather than a single block of untraceable conclusions.

    A sample task description can be written as:

    1. Complete the daily report in the following steps:
    2. 1. Read data/inventory.csv and compute the current inventory quantile and the current basis quantile;
    3. 2. Search for and summarize the key points of the day's related news;
    4. 3. Automatically call a plotting library to generate the charts;
    5. 4. Merge the body text and the charts into an illustrated document.

    3. The Critical Difference in Chart Generation: Pixels vs. Data

    The charting step is the dividing line between a professional tool and a toy. One path is to have a multimodal image generation model “draw” a picture that looks like a chart. The other is to have the agent write a script and let a plotting library compute coordinates from the data.

    The problem with the first path is clear: every point on the image is a probability-generated pixel, not a value derived from the data. Axis ticks, bar height ratios, and turning points on a line may have nothing to do with the real numbers, and a professional reader can spot this at a glance. The second path is the opposite. A script-based visualization library such as Matplotlib maps every point from a concrete computed result, offering far higher precision and reproducibility. Parameters can also fix the color scheme, font size, and layout, making it easy to produce reports in a consistent style at scale.

    The same logic applies to document output: it should also follow the script route, with a program assembling the body text, tables, and images into a PDF, rather than the model “describing” what a report should look like.

    4. Numerical Consistency: A Side-by-Side Look at the Same Task

    Under the same quantile calculation task, different agent implementation paths produce visibly different results. The following is an illustrative comparison (used only to show the magnitude of the differences, and not representative of any specific product’s capability):

    Implementation path Current inventory quantile Current basis quantile
    Path A ~26% ~55%
    Path B ~21% ~55%
    Path C ~27% ~68%
    Path D ~30% ~30%

    The basis quantile column shows the widest divergence, ranging from 55% to 68% and then to 30%, a spread large enough to change the judgment of whether the value sits at a historical high or a historical low. The conclusion is not that one path is “smarter.” It is that any step that does not push the calculation into the interpreter is guessing the answer from the training distribution. The engineering response is not to swap models, but to add a deterministic computation layer.

    5. Actionable Engineering Rules

    1. Separate computation from generation: every metric must come from a script. The model may only reference script output and is forbidden from filling in numbers itself.
    2. Persist intermediate artifacts: write CSV files, images, and logs to separate directories for review and re-runs.
    3. Constrain the output shape explicitly in the prompt: state clearly that the agent should call a plotting library to produce charts and merge them into a document, so the model does not improvise.
    4. Add a validation gate: run range and monotonicity checks on key metrics, and trigger recomputation instead of accepting out-of-bounds values.
    5. Fix randomness: set a random seed and pin dependency versions so the same input reproduces the same chart.
    6. Isolate cost and quota: cloud-based AI coding environments with a UI are convenient for quick validation, but watch for the risk of free quotas running out and interrupting the workflow. Production environments should connect to your own quota with monitoring and alerts.

    6. Conclusion

    The right role for an LLM agent in data analysis is “a programmer who can write code,” not “an analyst who can do arithmetic.” Give exact calculation to the interpreter, visualization to the plotting library, and document assembly to scripts, then use a validation step to catch anomalies. Only then can you keep the efficiency of generation while holding the line on the accuracy that professional reports demand.