LLM Agent Mode: Automating Data Analysis and Visualization for Research Reports
Large language models (LLMs) excel at open-ended writing, but they struggle with exact numbers, structured tables, and multi-step reasoning. Agent mode closes this gap by giving a model the ability to call external tools: it can write and run code, query knowledge sources, read and write files, and generate charts and documents. This article explains why computing numbers through code is more reliable than letting the model answer directly. It also walks through a typical agent workflow, compares programmatic charts with AI-generated images, and outlines practical engineering practices for building end-to-end pipelines. These pipelines turn data analysis into research reports that are both accurate and reproducible.
1. From “Direct Generation” to Agent Mode: A Paradigm Shift
Traditional LLM conversations are essentially “probabilistic continuation” based on the model’s training data and the current context. When you ask a data analysis question, the model tries to produce the answer that seems most plausible. This works well for open-ended writing, concept explanations, and brainstorming. However, its limitations become apparent as soon as a task involves exact figures, structured tables, or multi-step reasoning.
Agent mode extends this paradigm. Rather than having the model “answer out of thin air,” it gives the model the ability to call external tools, such as:
- Writing and executing code
- Querying external or local knowledge bases
- Reading and writing files
- Generating visualizations and documents
The model’s role shifts from “answer provider” to “task executor.” It plans the steps, writes scripts, runs them, and adjusts its next action based on the results.
This shift is especially valuable for data analysis. The ultimate goal of analysis is correctness, and correctness typically depends on a real, deterministic computation rather than a probabilistic guess.
2. Why Letting AI Write Code Beats Letting AI Answer Directly
Accuracy is the first requirement of data analysis. However, LLMs have an inherent characteristic: they are probabilistic models that output the “most likely” answer rather than a precisely calculated one.
For example, when asked for the percentile of a given metric, a model typically returns an approximation inferred from its training data and context rather than a value derived from the data at hand. It does not strictly follow mathematical rules, so errors can creep into the number of decimal places, rounding, or boundary handling.
A more reliable approach is to have the agent:
- Generate Python code to parse the tables and perform the calculations.
- Read the script’s actual output.
A Python interpreter does not guess. It computes deterministically and follows the rules exactly, which eliminates the risk of probabilistic recall errors.
This “generate code → execute code → read results” pipeline hands work that demands precision to a deterministic engine. It leaves interpretation and planning to the LLM, so each plays to its strengths.
The sample code below illustrates this approach. The percentiles are calculated by a deterministic function instead of being “computed in the model’s head”:
# Illustrative example: the agent writes and runs this code itself,# rather than relying on the model to guessimport data_viz_lib as viz # Placeholder for any Python data visualization librarydata = [120, 135, 128, 142, 155, 148, 160]# Deterministic percentile calculation, free of probabilistic biasdef percentile(sorted_data, q):idx = (len(sorted_data) - 1) * q / 100lo = int(idx)frac = idx - lohi = min(lo + 1, len(sorted_data) - 1)return sorted_data[lo] + frac * (sorted_data[hi] - sorted_data[lo])s = sorted(data)p25, p50 = percentile(s, 25), percentile(s, 50)print(f"P25={p25:.2f} P50={p50:.2f}")# Programmatic chart: every point comes from real, computed dataviz.bar([str(x) for x in s], s)viz.save("report_chart.png")
3. A Typical Agent Workflow
When given a complex task, a mature agent usually follows a workflow similar to that of a coding assistant. This workflow can be broken down into four stages:
- Task breakdown and planning: The agent first creates a to-do list. It decomposes a goal such as “analyze the data and produce a research report” into sub-steps, including reading data, calculating metrics, retrieving relevant information, drawing charts, and assembling a document.
- Staged execution: The agent runs code for each item on the list, starting with reading local spreadsheets or data files and then running calculation scripts. Each step takes the actual output of the previous step as its input.
- Information retrieval and supplementation: As the agent works, it searches online or pulls relevant information from a local knowledge base. This fills the gaps in the timeliness and accuracy of relying solely on training data.
- Result integration and output: The agent brings together the numerical results, charts, and retrieved information, then uses document generation capabilities to produce the final deliverable.
This closed loop of “plan → execute → retrieve → integrate” enables agents to handle end-to-end, real-world tasks rather than just answering isolated questions.
4. Programmatic Visualization vs. Generative Images: Precision Determines Credibility
Visualization tasks can follow two fundamentally different technical approaches. Understanding the differences between them is essential for professional use cases.
- Programmatic visualization: This approach relies on Python visualization libraries. Every bar, scatter point, and axis segment is derived from the data through deterministic computation. Values can be verified point by point, and axes, labels, and proportions are accurate.
- Generative imagery: An image generation model “draws” a picture that looks like a chart. This is essentially pixel-level probabilistic generation. Axis labels may be pseudo-characters that merely look like text, and specific values cannot be mapped one-to-one to the source data.
For rigorous scenarios such as quantitative and financial analysis, programmatic visualization is clearly more precise and professional. When the chart itself forms part of the data-driven conclusion, generative models introduce an uncontrollable risk of distortion.
5. End-to-End Research Reports and Multi-Format Output
Another major value of agents is their end-to-end output capability. Once the analysis is complete, an agent can use document generation capabilities in the same workflow. It combines the written conclusions with programmatically generated charts to produce a polished PDF report with both text and visuals, or a structured spreadsheet.
The key is that the “combine” step should be performed by deterministic code, not assembled as text by the model.
A practical way to orchestrate this is to declare the output requirements explicitly in the prompt. For example:
Please complete the following tasks:1. Read the tabular data and calculate the specified metrics (percentiles, means, etc.). Leave all calculations to code.2. Use a visualization library to create charts, making sure the values shown match the calculation results.3. Combine the analysis conclusions and charts into a PDF report with both text and visuals.4. Note the data sources and calculation scripts in the report so the analysis can be reproduced.
Clear, verifiable output contracts help agents reliably produce deliverables with consistent structure and full traceability.
6. Numerical Precision: A Common Issue Worth Watching
In practice, when different implementations are left to “compute” percentiles from memory, their results often disagree with one another and deviate from the true values.
For example, when calculating percentiles for a set of inventory or basis data, different implementations might return anything from “just over 20%” to “nearly 30%,” even though there is only one true value. Answers for the basis percentile may vary in the same way.
This is not a flaw unique to any one implementation. It is a common problem in how LLMs handle precise decimals, since they all produce probabilistic approximations.
This finding reinforces the conclusion of Section 2: any task that requires numerical accuracy should be handled by code, not by a model’s direct output.
Rather than debating “which implementation calculates more accurately,” developers are better off establishing an architectural principle: numerical computation goes to code, while interpretation and planning stay with the model. This approach addresses accuracy risks at their root.
7. Practical Engineering Recommendations
To deploy agent mode reliably in production, consider the following practices:
- Define clear output contracts: Specify the required fields, formats, and chart requirements in prompts so that downstream systems can consume the output reliably.
- Enforce code-based validation: All key numbers must be calculated and printed by scripts. The model’s role is to interpret the results, not to recalculate them in its head.
- Separate retrieval from computation: Assign time-sensitive information to retrieval and exact numbers to computation, keeping the two clearly distinct.
- Plan the infrastructure: Running code, generating documents, and executing long workflows typically require stable compute resources and a reliable runtime environment. In general, teams use container platforms to schedule these tasks, object storage to hold input data and output files, and logging and monitoring/alerting services to track the status and exceptions of each run. General-purpose cloud products, such as those offered by cloud providers like Baidu AI Cloud, are commonly used to run and govern tasks like these.
- Keep results reproducible: Save calculation scripts, raw data, and random seeds so that every analysis can be independently re-run and audited.
Conclusion
The value of agent mode lies not in making models “know more,” but in making them “deliver more reliably.”
By delegating precise calculations to code, visualization to programmatic charting, and output to document generation tools, LLMs can evolve from giving probabilistic answers to performing deterministic execution.
By understanding and making good use of this paradigm, developers can build automated solutions for serious scenarios such as data analysis and research report generation. These solutions are both efficient and trustworthy.
