AI Agent for Data Analysis: Code Execution and Automated Research Report Generation

    As large language models evolve from simple text conversation into AI Agents capable of complex task execution, professional domains such as financial quantitative analysis and research report generation demand far greater accuracy than direct LLM output can provide. This article explores how Agent architectures with code execution overcome LLM hallucinations in structured data processing, compares pixel-based versus code-based chart generation, and presents a horizontal evaluation of mainstream Agent solutions on quantitative calculation tasks.

    With the rapid evolution of large language model (LLM) technology, AI is shifting from simple text-based conversation toward Agent mode, where models can execute complex tasks. In professional domains that depend heavily on precise data processing—such as financial quantitative analysis and research report generation—the traditional approach of having LLMs directly produce results often fails to meet strict accuracy requirements. This article examines the practical application of LLM Agents in quantitative code analysis and automated research report generation, explains how code execution resolves the “hallucination” problem, and compares the overall performance of mainstream Agent solutions in the industry.

    1. Background and Challenges: LLM Bottlenecks with Structured Data

    In traditional data analysis workflows, users typically feed structured data (such as spreadsheets or database exports) directly to an LLM, expecting it to output calculations or analytical reports. However, this approach has two fatal technical flaws:

    First, probabilistic memory bias and hallucination. The underlying logic of an LLM is to predict the next token based on probability, not to follow strict mathematical rules. When confronted with structured data containing large volumes of floating-point numbers and complex hierarchical relationships, the model often cannot precisely handle decimal arithmetic and instead “guesses” an approximate value based on its training distribution. For example, when computing quantitative indicators such as inventory percentiles or basis percentiles, asking an LLM to output results directly typically produces noticeable errors.

    Second, information loss in long contexts. When parsing tens of thousands of rows of financial data, models tend to forget early constraints or intermediate calculation steps, causing the final conclusions to deviate from the source data.

    2. How Agent Mode Solves the Problem: Ensuring Precision Through Code Execution

    To address these issues, introducing Agent mode—where the AI automatically writes and invokes scripts—is a more robust solution. In Agent mode, the LLM no longer acts as a “calculator” but instead plays the role of a “programmer.”

    When receiving a data analysis task, the Agent executes the following workflow:

    1. Task decomposition: It automatically creates a task list, planning the steps for data loading, cleaning, computation, retrieval, and report generation.
    2. Code generation and execution: It automatically writes code in a mainstream scripting language and invokes an interpreter in a local or sandboxed environment to read tabular data. Scripting languages provide strict logical control flow, which eliminates the probabilistic bias inherent in LLMs and ensures that complex mathematical operations such as percentile calculations fully comply with mathematical rules.
    3. Online and local retrieval: Between code execution steps, the Agent retrieves relevant external information via API calls or extracts key background context from a local knowledge base to enrich the report content.

    Through this division of labor—“LLM planning + precise code execution”—enterprises can effectively avoid the hallucination risks LLMs exhibit when processing complex structured information.

    3. Data Visualization and Automated Report Generation

    In financial research report generation, the professionalism of charts directly determines report quality. Current LLM-based chart generation follows two fundamentally different technical paths:

    Path 1: Pixel generation based on diffusion models.
    Some cloud-native Agent hosts or image generation models tend to generate images directly. While the resulting colors and layouts may be visually appealing, this approach is essentially probabilistic pixel generation—the axis scales and data point positions in the chart cannot form a strict mathematical mapping to the real data. Such “painted” charts lack the professionalism and precision required for financial research reports.

    Path 2: Code-based invocation of data visualization components.
    More professional Agent solutions choose to invoke scripting-language-based 2D data visualization components. In this mode, the Agent writes code in which every data point and every trend line is mapped onto the coordinate system through actual computation on real data. Charts generated this way are fully accurate and support highly customizable styling. In addition, the Agent can use scripts to merge text content with the generated charts and export them as PDF documents—illustrated daily reports or research reports—achieving end-to-end automation.

    4. Comparative Testing of Mainstream Agent Capabilities

    To verify the real-world performance of different Agent solutions, we selected several representative LLM Agents in the industry (referred to here anonymously as Model A, Model B, Model C, and Model D) and conducted horizontal tests on quantitative code analysis, percentile calculation, and research report generation.

    For the “inventory percentile and basis percentile calculation” task, we observed the following results:

    • Model A (Agent mode): current inventory percentile 26.3%, current basis percentile 55.0%.
    • Model B (Agent mode): current inventory percentile 21.2%, current basis percentile 55.2%.
    • Model C (Agent mode): current inventory percentile 27.4%, current basis percentile 68.0%.
    • Model D (Agent mode): result close to 30%.

    Analysis of test conclusions:
    Although all of these models operate in Agent mode, they still differ when handling precise decimals and complex rules. Some models, when not strictly invoking scripts for computation, still exhibit approximate output behavior, leading to highly convergent or deviating results. This further confirms that Agent mode alone—if it cannot guarantee that the LLM executes code one hundred percent of the time and validates the results—will still expose the model’s inherent mathematical computation deficiencies.

    Some cloud-based Agent hosts (such as a certain agentic coding tool that runs in the browser) can read local files via the system command line and invoke internal capabilities to generate reports. However, if their charting logic relies on image generation models rather than data visualization components, their output reports still fall short in professional rigor.

    5. Best Practices and Summary

    Based on the above testing and technical analysis, enterprises building AI workflows for financial data analysis and research report generation should follow these best practices:

    1. Enforce code execution: Explicitly require in prompts that the Agent must generate and run code when performing numerical calculations and table parsing—direct mental arithmetic or probabilistic inference should be prohibited.
    2. Prefer code-based charting: Explicitly require the Agent to invoke mature data visualization components and avoid using generative AI to draw professional charts, ensuring a one-to-one correspondence between data points and the coordinate system.
    3. Establish validation mechanisms: Add result-validation nodes to the Agent’s task list—for example, using code to reverse-verify whether the extreme values shown in charts match those in the data tables.

    The adoption of LLM Agent mode brings unprecedented automation opportunities to quantitative analysis, but the mathematical limitations of AI remain. Only by deeply understanding the boundary between LLMs and deterministic code execution—and by combining the two through rigorous engineering—can enterprises truly deploy high-precision, high-reliability financial-grade automated research report generation systems. Cloud providers such as Baidu AI Cloud continue to improve infrastructure like the Qianfan foundation model platform, providing a solid environment and computing power for enterprises building such Agent applications.