Agentic LLM Workflows for Data Analysis and Automated Report Generation

    This article explains why reliable data analysis should move from direct language-model generation to code execution. It covers the Agent workflow for reading data, computing metrics, creating data-driven visualizations, and exporting reports, while also discussing prompt design, numerical accuracy limitations, and the cloud infrastructure needed for reproducible execution. The central principle is simple: let code handle precise computation, and let the model handle understanding, orchestration, and communication.

    1. From Direct Generation to Code Execution: A Shift in Data Analysis

    One of the most overlooked risks in practical data analysis is that large language models do not truly calculate numbers. At their core, they predict the probability of the next token. Based on their training data and the current context, they infer an answer that appears reasonable.

    When a model is asked to calculate a precise metric—such as the current percentile of a factor, a benchmark percentile, or a historical statistical range—it may produce an approximation rather than a result derived by strictly following the relevant mathematical rules.

    This creates two common problems:

    • Probabilistic memory bias: The model may transfer familiar numerical patterns from its training data to a specific task, producing values that look plausible but do not match the real data.
    • Hallucinations in structured information: When processing tables or multi-level reports, the model may skip rows, misalign fields, or invent information.

    For this reason, a more reliable approach is to have the Agent automatically generate and execute Python scripts to parse and analyze tables. The key advantage is that code does not introduce probabilistic memory bias. When a script reads the source data and performs the calculation, the result is reproducible, verifiable, and auditable. This fundamentally reduces hallucinations when the model works with complex structured information.

    2. How Agent Mode Works: A Programmer Rather Than an Answer Generator

    The defining change in Agent mode is the model’s role. In a standard chat, the model acts as an answer generator. In Agent mode, it acts more like a programmer: it writes code, calls tools, organizes data, and produces deliverables.

    A mature coding or data-analysis Agent typically follows a workflow like this:

    1. Task decomposition: It creates a task list or todo list based on the user’s objective. For example, “generate a daily data analysis report” can be divided into several executable subtasks.
    2. Data ingestion: It runs scripts to read structured data from local storage or object storage, including tables, CSV files, and Excel workbooks.
    3. Information retrieval: When necessary, it searches for the latest information online or retrieves relevant material from a local knowledge base or news repository.
    4. Data processing: It writes and executes code for cleaning, aggregation, metric calculation, and statistical analysis.
    5. Visualization: It calls Python visualization libraries to generate charts.
    6. Report assembly: It combines written conclusions and charts into a formatted document such as a PDF report.

    The value of this workflow is traceability. Each step has corresponding code and intermediate artifacts. Users can see how a number was calculated and which logic produced a chart, instead of receiving an unverifiable answer from a black box.

    3. Visualization Accuracy: Code-Generated Charts vs. Pixel-Based Guessing

    Data visualization is one of the clearest demonstrations of an Agent’s value in analytical work. Two fundamentally different generation methods should be distinguished:

    • Code-based visualization: Every data point, curve, and axis tick is mapped from a specific value to a coordinate through calculation. The chart is an accurate projection of the data.
    • Image-generation visualization: A diffusion or similar image-generation model produces an image that looks like a chart. It is essentially pixel-level probabilistic sampling and is not necessarily connected to real data.

    The difference is fundamental in professional settings. In finance, risk management, and operations, a chart with incorrect coordinates or distorted values can directly lead to poor decisions. By contrast, a chart generated by an Agent through a plotting script can trace every point back to the source data, providing substantially greater accuracy and credibility.

    The following example illustrates the data-driven plotting workflow. The function names are illustrative and are intended only to demonstrate the process:

    1. # 1. Read structured data
    2. df = load_table("factor_data.xlsx")
    3. # 2. Calculate the factor series and percentile using real data
    4. factor_series = df["factor"].rolling(window=20).mean()
    5. current_quantile = compute_quantile(factor_series, level=0.05)
    6. # 3. Plot the series: every point is mapped from the calculated values
    7. plot_series(factor_series, title="Factor Trend", output="chart_trend.png")
    8. # 4. Combine written conclusions and charts into a PDF report
    9. merge_to_pdf(
    10. text_blocks=report_sections,
    11. charts=["chart_trend.png"],
    12. output="daily_report.pdf",
    13. )

    4. Driving an Agent with Prompts: Specify the Deliverables Clearly

    An Agent’s capabilities depend heavily on how its instructions are written. When the required output is clearly defined in the prompt, the Agent can automatically organize the workflow described above.

    For example, a request such as the following can guide the Agent through the entire process:

    Automatically call a visualization library to generate charts, combine the daily report text with the generated charts, and export the result as a visual PDF report.

    This single requirement can be enough for the Agent to complete the loop:

    read data → calculate metrics → generate charts → assemble a PDF

    The main principles for improving prompts include:

    • Specify the output format: State whether the deliverable should be a PDF, PNG, spreadsheet, or Markdown document.
    • Specify the calculation logic: Require a reproducible algorithm or defined methodology instead of asking the Agent to “estimate” a value.
    • Specify the data source: Indicate whether the data should come from local storage, object storage, or online retrieval to reduce fabricated information.
    • Require verification: Ask the Agent to include calculation notes or code snippets alongside important numerical results.

    5. Recognize the Accuracy Limitation: Do Not Delegate Numerical Calculation to Guesswork

    Even when using Agent mode, one fact must remain clear: large language models are strong at understanding, orchestration, and communication, but not at precise arithmetic.

    When calculating metrics that require multiple decimal places, a model may still return an approximation. Its output is based on learned distributions and contextual inference rather than guaranteed mathematical computation.

    In engineering practice, it is therefore useful to follow a clear division of responsibilities:

    • Tasks involving precise numerical calculations, statistical definitions, or financial modeling should always be delegated to code execution. Scripts should read the actual data and perform the calculations.
    • The model should be responsible for understanding the requirement, writing the correct calculation code, and organizing the results into a report—not for manually calculating the final number itself.

    Different models and Agents may perform differently on precise metrics such as percentiles, but the underlying limitation is the same: numbers generated directly by the model are only approximations. Recognizing this limitation is essential for designing a workflow that is both efficient and reliable.

    6. Cloud Collaboration: Give the Agent a Stable Execution Environment

    A controlled execution environment is a prerequisite for allowing an Agent to reliably read data, run scripts, and generate reports. General-purpose cloud services can provide the foundation for such a system, including:

    • Object storage: Centralize raw tables, scripts, and generated PDF files to support version management and sharing.
    • Containers or compute instances: Provide an isolated and reproducible execution sandbox for the Agent, reducing failures caused by differences between local environments.
    • Logging and monitoring: Record each script execution and its results, making it easier to diagnose calculation errors and trace the origin of generated artifacts.
    • Message queues: Decouple task distribution from execution when generating reports in batches, improving throughput.

    Some cloud-based Agent hosts provide an out-of-the-box experience without requiring users to bring their own interfaces or infrastructure. They may also include limited free usage, which can be useful for rapid validation. However, these environments often impose usage limits. In addition, if chart generation depends on an image-generation model, the resulting visuals may not be accurate enough for professional analysis.

    For serious production workloads, a controlled pipeline built around code execution and data-driven visualization is more dependable. General cloud capabilities such as object storage, containers, and logging services—including those provided by platforms such as Baidu AI Cloud—can serve as the infrastructure layer for this type of pipeline.

    7. Conclusion

    The most important transformation brought by Agent mode is not simply that a large language model produces more intelligent answers. It is that the model changes from a probabilistic generator into an orchestratable programmer.

    The core conclusions are:

    1. Code execution is better than direct generation for precise analysis: Calculating from real data reduces memory bias and structured-data hallucinations while making results reproducible and auditable.
    2. Data-driven visualization is better than pixel-based image generation: Each point on a code-generated chart can be traced back to source data, providing a fundamental improvement in accuracy and professionalism.
    3. Precise arithmetic remains a weakness of language models: Numerical computation should be delegated to scripts, while the model handles understanding, orchestration, and communication.

    With this architecture, combined with cloud storage, containers, logging, and other infrastructure, an Agent can become a practical, trustworthy, and maintainable assistant for data analysis and automated report generation.