LLM Agent for Data Analysis and Research Report Generation: A Practical Guide

    This article explains how to use LLM agents for data analysis and automated research report generation without relying on the model to "do the math." It covers the agent workflow, tool orchestration, code-based visualization, cross-model consistency checks, and execution environment choices, along with a practical checklist for building reliable, auditable pipelines. Key principles include letting the model orchestrate tasks while deterministic scripts handle all calculations.

    1. The Problem: Why Data Analysis Cannot Rely on Text Generation Alone

    When introducing large language models into data analysis and research report writing, the most common mistake is treating them as a “search engine that writes answers.” Tasks such as table parsing, quantile calculation, and factor backtesting demand high numerical precision and reproducible calculation paths. However, a language model’s core mechanism is predicting the next token based on context and training distribution; it does not inherently follow strict mathematical rules. Once you let the model “mentally calculate” decimal places, you get approximate outputs, definition drift, and even structural hallucinations.

    A more robust engineering paradigm is to let the model act as a “programmer” and “analysis orchestrator,” delegating actual numerical computation to a deterministic script runtime. Python serves at this layer as a verifiable computation engine: the same input always produces the same output, with no probabilistic memory bias. The model is responsible for understanding requirements, breaking down tasks, writing scripts, and interpreting results; the runtime is responsible for precise calculation. This division of responsibility is the foundation of the entire solution’s reliability.

    2. Core Mechanism: How the Agent Pattern Turns “Saying” into “Calculating”

    The key shift in the agent pattern is upgrading the model from single-turn Q&A to an execution entity capable of tool invocation. A typical workflow looks like this:

    1. Requirement parsing: Read output requirements, data scope, and format constraints from the user prompt.
    2. Task list generation: Automatically create a to-do list, breaking complex goals into independently verifiable subtasks.
    3. Tool orchestration: Invoke capabilities such as code execution, table reading, web search, or local knowledge base retrieval in sequence.
    4. Result aggregation: Merge intermediate artifacts (data tables, charts, conclusion paragraphs) into the final deliverable.

    An illustrative prompt constraint can be written as:

    1. Please complete the daily report following these steps:
    2. 1) Use a script to read the attached table and validate fields;
    3. 2) Calculate the historical quantiles of current inventory and current basis;
    4. 3) Automatically call a plotting library to generate charts; every point in the chart must be computed by the script;
    5. 4) Merge the text conclusions with the charts and output a PDF document.

    The most important constraint here is the third one: explicitly prohibit using generative image methods to “draw” data charts. Data visualization must be rendered by a plotting library based on calculation results, with every point traceable to a specific value. Only then do the charts have professionalism and auditability.

    3. Visualization and Deliverables: Precision Comes from Computation, Not Pixels

    In research report scenarios, charts are not decoration but evidence. Probability-based pixel generation may be aesthetically pleasing, but it cannot guarantee that axes, proportions, and annotations are consistent with the underlying data, and professional readers will spot the problem immediately. In contrast, charts produced by code-based plotting have three advantages:

    • Reproducible: With fixed script and data versions, the chart is always identical;
    • Verifiable: Any data point can be traced back to its calculation process;
    • Batch-capable: The same logic can generate charts for different instruments and different periods in bulk.

    For delivery, an agent can merge text, tables, and charts into a PDF document within the same workflow, achieving an integrated “analysis—visualization—final draft” pipeline. From an engineering perspective, it is recommended to store intermediate artifacts in object storage and hand task status and logs over to logging services and monitoring/alerting systems, making retries and result tracing easier.

    4. Cross-Model Consistency Verification: Don’t Trust a Single Model’s “Precise Decimals”

    An easily overlooked fact is that different models will produce different results on the same calculation task, and both may be only approximations. Taking quantile calculation as an example, four execution approaches produced “current inventory quantile” values spread across the 21% to 30% range for the same dataset, while “current basis quantile” showed a significant difference between 55% and 68%. This shows that model-generated numbers cannot be trusted directly; a verification mechanism must be established.

    The recommended verification framework has three layers:

    1. Definition layer: Clarify the quantile definition (interpolation method, sample interval, whether to exclude missing values), and write the definition into both the prompt and script comments.
    2. Computation layer: All numbers may only be produced by scripts; the model must not rewrite calculation results and may only cite them.
    3. Regression layer: Fix a baseline dataset with expected outputs, run a regression pass after every prompt or model change, and record deviations.
    1. import numpy as np
    2. import pandas as pd
    3. def quantile_report(series: pd.Series, qs=(0.25, 0.5, 0.75)):
    4. s = pd.to_numeric(series, errors="coerce").dropna()
    5. return {q: float(np.quantile(s, q, method="linear")) for q in qs}

    Solidifying such functions as “trusted computation units,” with the model only responsible for invoking and interpreting them, is the most effective way to suppress hallucinations.

    5. Trade-offs Between Cloud Execution Environments and Local Capabilities

    Agents need to run code, which raises the question of execution environment choice. Local environments are close to the real development experience and can reuse already configured skills and scripts. Cloud execution hosts with a graphical interface lower the barrier to entry—register and you get a trial quota—making them suitable for quick validation. The shared risk of both is quota and permission constraints: once the quota is exhausted or permissions are restricted, the workflow breaks and modifications become hard to reproduce. Therefore, in the production stage, execution environments should be containerized, relying on a container platform to unify runtime dependencies and using images to pin versions, avoiding the “it works on my machine” problem.

    6. Implementation Checklist

    • Write “must be calculated by scripts” as a hard prompt constraint, prohibiting generative plotting as a substitute for data charts;
    • Solidify trusted computation functions for each type of analysis, with a baseline regression set;
    • Structure tasks as a list so subtasks can be rerun independently and failures can be located;
    • Centralize management of intermediate artifacts and logs, and connect to monitoring and alerting;
    • Maintain the boundary of “cite rather than rewrite” for model outputs to prevent numerical drift;
    • On cloud platforms such as Baidu AI Cloud, combine object storage, container platforms, logging services, and monitoring/alerting to build a complete observable data pipeline.

    7. Conclusion

    The value of LLM agents in data analysis and research report generation lies not in doing arithmetic for you, but in automating the chain of “understand requirements—write code—invoke tools—aggregate into a final draft.” Only by adhering to the four principles of “model orchestration, script computation, traceable charts, and regressible results” can you lock the uncertainty of probabilistic models in a cage and produce professional, precise, and auditable deliverables.