Agent Mode for Data Analysis: A Practical Guide from Code Execution to Automated Research Reports

    Agent mode changes LLM interaction from conversational answers to autonomous task planning and code execution. This article explains why executing generated Python code is more reliable than direct text generation for data analysis, outlines a typical workflow from task decomposition to PDF report synthesis, compares precision across models and toolchains, and offers engineering practices for reproducible, auditable analysis.

    1. Background: From Chat Models to Agent Mode

    Large language model interaction is undergoing an important shift: from a question-and-answer chat pattern to an Agent mode that can autonomously plan tasks, call tools, execute code, and deliver final artifacts. Mainstream models from multiple vendors now support this type of capability. Their common characteristic is that the model no longer directly “states” an answer. Instead, it plays the role of a programmer or analyst, writing and running Python scripts to perform data processing, visualization, document generation, and other tasks.

    This shift matters significantly for data analysis. Traditional text-only output relies on the model’s memory and probabilistic reasoning, and it can easily deviate when facing precise numerical calculations. Agent mode executes code for real, so calculation results are produced by deterministic programs, significantly reducing the risk of hallucination.

    2. Why Code Execution Is More Reliable Than Direct Generation

    2.1 Probabilistic Generation vs. Deterministic Computation

    When a model directly answers a question such as “what historical percentile is current inventory at,” it is essentially making a probabilistic guess based on training data and context. It does not strictly follow mathematical rules to handle precise decimal places, and the output can only be regarded as an approximate estimate.

    The process under Agent mode is completely different: the model generates a Python script, the script is actually executed, and the percentile is strictly calculated by a computation library. Every number and every data point in a chart corresponds to a concrete data calculation result. This is fundamentally different from how image generation models produce pixels probabilistically. The former offers clearly higher precision and professionalism.

    2.2 Handling Complex Structured Information

    When dealing with complex tables such as Excel and CSV files, asking an AI to “read and restate” the data can easily produce structural hallucinations, such as misalignment, missing rows, or misread fields. By automatically generating and calling Python scripts to parse tables, the data always flows through the program in its original form, and the AI intervenes only at the level of “writing logic.” This is a more robust engineering approach to structured information.

    A typical task instruction can be organized as follows:

    1. Read the local data file, calculate the historical percentiles of current inventory and basis, automatically call matplotlib to generate charts, and finally merge the analysis conclusions and charts into a PDF report with text and figures.

    3. Anatomy of a Typical Agent Mode Workflow

    Observing the actual operation of Agent mode on various platforms reveals a nearly consistent general workflow:

    1. Task decomposition: The Agent first automatically creates a task list, similar to a to-do list, breaking a complex goal such as “generate a research report” into sub-steps such as reading data, calculating metrics, drawing charts, and assembling documents.
    2. Data acquisition: It runs Python code in sequence to read local tables, or retrieves relevant information online or from a local knowledge base.
    3. Computation and plotting: It executes statistical calculations according to established logic, such as rules in factor calculation source code, and calls visualization libraries such as matplotlib to generate charts.
    4. Artifact synthesis: It merges textual conclusions with charts and directly outputs final deliverables such as PDFs, with no manual layout or assembly required.

    The value of this chain lies in being end-to-end: the user only needs one prompt describing the output requirements, and the Agent autonomously handles coding, execution, and assembly.

    4. Insights from Horizontal Comparison: Same Mode, Different Precision

    Comparing multiple models’ Agent mode on the same data analysis task, including the hosted environment of a cloud-based coding tool, yields two important findings.

    First, percentile calculation results differ noticeably. For the same inventory percentile task, for example, outputs from different models roughly fall in the 20%–30% range, while basis percentiles may be close to 55% in some cases and reach 68% in others. Sources of difference include context understanding, code logic construction, and data splitting methods.

    Second, text-only “memory-based output” is inconsistent with code execution results. The model does not process precise decimal places and only provides approximate output. This again confirms that any conclusion involving precise numerical values must be based on code execution results, and numbers directly provided by the model can only serve as a reference.

    Toolchain selection also deserves attention. Programmatic plotting represented by matplotlib has data support for every point and is suitable for professional research reports. “Illustrative diagrams” generated by image generation models may be aesthetically pleasing, but they cannot guarantee numerical accuracy, and once compute quotas are limited, iterative correction becomes difficult. They should be used cautiously in production environments.

    5. Engineering Recommendations

    Based on the above practice, here are several actionable recommendations:

    • Numerical conclusions must be reproducible: Require the Agent to output calculation code and retain execution results so they can be rerun and verified during audits.
    • Explicitly declare the output form in the prompt: For example, “call matplotlib for charts and merge them into a PDF” can significantly improve task completion.
    • Choose programmatic rendering for charts: Avoid using image generation models as a substitute for data visualization.
    • Establish a result validation mechanism: Independently recalculate key percentiles and statistics with separate scripts.
    • Monitor quotas and costs: Cloud-hosted Agents usually have resource limits, and important tasks should reserve room for iteration.

    6. Conclusion

    Agent mode evolves large models from “analysts who can talk” into “engineers who can write code.” In data analysis and research report automation, the determinism of code execution is the core defense against hallucination. Mastering the general chain of task decomposition, code execution, visualization, and document synthesis makes it possible to build reliable and auditable intelligent analysis workflows regardless of how the underlying models change.