# How AI Agents Use Code Execution to Produce Accurate Data Analysis and Research Reports

    Large language models approximate numbers instead of computing them, and that single fact decides whether an agent can be used in production for finance, operations, risk, or research work. This article explains the fix: split responsibilities so the model plans and interprets while code does the arithmetic. It covers the three capabilities that make up a data analysis agent — task planning, tool use, and code execution — how code-driven charting keeps every plotted point tied to a real value, how one prompt can produce an illustrated PDF report, a comparison of code plotting versus image generation models, the engineering requirements for running an agent in the cloud (sandboxing, capability access, resource quotas, artifact management), and a practical checklist for drawing the line between probabilistic and deterministic components.

    1. Why Data Analysis Needs to “Let Code Do the Talking”

    A large language model’s core mechanism is predicting the next token by probability. When it tries to “compute in its head” a statistic — a percentile, a mean, a variance, a period-over-period change — it is not executing arithmetic. It is approximating based on training data and the surrounding context. As a result, decimal places, boundary values, and rounding rules can all drift. The output is neither reproducible nor easy to verify.

    Code execution solves this at the engineering level: the model takes on the role of a programmer, decomposes the data processing task into runnable scripts, and lets an interpreter produce the numbers deterministically. The difference comes down to two categories:

    • Probabilistic generation: results are non-deterministic, hard to trace, and prone to hallucination — the same input can yield different output across runs.
    • Code execution: results are deterministic, every step is auditable, and the same input always produces identical output.

    For precision-sensitive domains such as finance, operations, risk control, and research, this distinction is often the line between “usable in production” and “not usable.” The governing principle is one sentence: if something can be calculated, never rely on a guess.

    2. Three Pillars of an AI Agent Workflow

    A mature data analysis agent is usually built on three capabilities working together:

    1. Planning — turning a vague goal such as “produce a daily report” or “write a research brief” into an ordered task list: read the table → compute metrics → search the web for recent information → generate charts → merge and export the document.
    2. Tool use — executing the items on that list by calling scripts, web search, local knowledge base retrieval, file read/write, and similar capabilities, which effectively gives the model hands and feet in the outside world.
    3. Code execution — running scripts inside a sandbox to read data, compute results, render charts, and write output files.

    Agent workflows in current mainstream LLMs have converged on the same shape: build a task list automatically, work through it by running code and retrieving information, then emit results in a predefined output format. The real differentiator is not whether the workflow runs end to end, but whether the numbers are exact, the process is controllable, and the output is reproducible.

    3. Code-Driven Data Visualization: Every Point Comes from a Calculation

    In the visualization stage the agent plays the programmer. It writes code to read a table, compute values, and draw. Unlike an image generation model, code-based plotting does not “synthesize pixels by probability” — it derives the coordinates of every point in the chart from real numbers. That produces three concrete advantages:

    • Accuracy: axis ticks, value labels, and the underlying data correspond exactly. There is no “looks right but the values are shifted” distortion.
    • Professionalism: standard chart types — line, bar, heatmap, distribution — render consistently, with controllable color schemes and annotations.
    • Reproducibility: the same data and the same code produce an identical figure on every run.

    Illustrative code below (function names stand for generic capability categories, not a specific library):

    1. # --- Data layer: executed by code, results reproducible and verifiable ---
    2. data = load_table("portfolio.xlsx") # load the structured table
    3. q25_current = percentile(data["inventory_level"], 25) # exact percentile
    4. q25_baseline = percentile(data["baseline"], 25)
    5. # --- Visualization layer: generic plotting capability, driven by real values ---
    6. chart = plot_comparison(data["inventory_level"], data["baseline"])
    7. export_chart(chart, "chart.png")
    8. # --- Document layer: merge text and figures, export in one step ---
    9. report = render_report(analysis_text=build_summary(q25_current, q25_baseline),
    10. figures=[chart])
    11. save_as_pdf(report, "research_report.pdf")

    The key point: percentiles, means, and other statistics come from numeric computation, not from the model blurting them out. This also removes, at the root, the memory bias and hallucination models exhibit on complex structured information.

    4. From Separate Text and Charts to a One-Step Report

    In a conventional workflow, writing prose, producing charts, and assembling the document are three disconnected steps. In agent mode, one sentence in the prompt’s output requirements is enough — for example, “generate charts from the data automatically, then merge the body text and the charts into a single illustrated document” — and the agent connects the whole chain:

    1. Read the data sources (tables, logs, local knowledge base).
    2. Compute metrics and render charts organized by factor logic.
    3. Draft the report body, embedding each chart in its corresponding section.
    4. Merge everything into a document in the requested format, such as PDF.

    Data analysis and report writing collapse into a single conversation, and human effort shifts from repetitive production to review and decision-making.

    5. Two Approaches to Chart Generation Compared

    In hands-on testing, the two approaches to chart generation differ significantly and deserve attention:

    Dimension Code plotting (visualization library) Image generation model
    How it works Coordinates computed from real data Pixels generated probabilistically
    Numeric accuracy High — coordinates map one-to-one to data Low — axis and labels easily distorted
    Professionalism Supports standard chart types Aesthetics-oriented, weak on technical charts
    Reproducibility Fully consistent May differ every run

    The conclusion is clear: prefer the code plotting route for data visualization. Image generation is better suited to covers, decorative imagery, and other contexts that do not involve exact values. The two serve different purposes and should not be used interchangeably.

    6. The Precision Trap: LLMs Are Bad at “Counting Exactly”

    An easily overlooked fact: large language models are inherently poor at handling exact decimal places. What they output is an approximation derived from context, not an operation that follows strict mathematical rules. Take percentiles: ask a model to answer directly what percentile the current inventory and the baseline sit at, and different models may return contradictory numbers, sometimes far from the true value. Change the instruction to “write code and compute it,” and the result converges quickly and stays reproducible.

    This leads to a design principle: anything involving exact numbers belongs to code execution. The model is responsible only for orchestrating logic and interpreting results. Let the model do what it is good at — understanding intent, organizing the workflow, writing the prose — and hand computation to deterministic tools.

    7. Engineering Considerations for Cloud Agent Execution Environments

    Taking a data agent from prototype to production also means solving execution-environment and operational problems:

    • Sandboxing and isolation: code runs in a controlled environment so mistakes cannot affect the host.
    • Capability access: web search, local knowledge base, and file I/O should be wrapped behind standard interfaces rather than depending on individual credentials.
    • Resources and quotas: both script execution and model inference consume resources, so quotas and monitoring need to be in place.
    • Artifact management: outputs such as charts and PDFs should be stored and archived in a unified way so they can be traced later.

    For enterprise deployment, general-purpose platform capabilities can absorb much of this work — for example, using object storage to keep report and chart artifacts, compute resources to run the agent sandbox, and log and monitoring services with alerting to trace the execution chain. Baidu AI Cloud and similar platforms generally offer model services, object storage, compute, and monitoring as building blocks for moving a data analysis agent from demo to production.

    8. Implementation Checklist

    • Compute whenever possible: statistics, financial calculations, and similar work must go through scripts. Never let the model quote numbers directly.
    • Make the process explicit: use the prompt to define the output format and artifacts precisely (for example, “merge and export as PDF”).
    • Keep artifacts traceable: retain the data, code, and charts for every run so results can be audited.
    • Separate hot from cold: the model handles orchestration and text; deterministic tools handle numbers and charts.
    • Validate continuously: spot-check critical metrics and build regression tests for numerical trustworthiness.

    9. Conclusion

    The value of AI agents in data analysis is not “letting the model compute faster.” It is making computation deterministic, reproducible, and auditable. When an agent writes and runs scripts to fetch data, calculate, plot, and generate documents, the model’s “guessing” is replaced by the certainty of code, and both accuracy and professionalism improve materially. For developers, the real design problem is not prompt technique but drawing the boundary between “the model understands, the code calculates” — and that boundary is the core of building a reliable data analysis agent.