Building an LLM Agent Workflow for Data Analysis and Report Generation with a Code Interpreter

    Why LLMs fail at precise numerical tasks, how Agent mode fixes it by delegating computation to a Python interpreter, and how to design prompts that produce polished, chart-rich PDF reports.

    Building an LLM Agent Workflow for Data Analysis and Report Generation with a Code Interpreter

    Why “Just Ask the AI to Analyze a Spreadsheet” Often Fails

    When you hand an LLM an Excel file and ask it to output metrics that require exact computation—such as inventory quantiles or basis quantiles—the results are often unreliable. The reason is fundamental: a language model is a probabilistic generator. It doesn’t perform mathematical operations; it “guesses” a plausible answer based on similar examples in its training data and surrounding context.

    For simple arithmetic, the model’s accuracy is acceptable. But once the task involves quantiles, regression, or rolling-window statistics—calculations that must strictly follow mathematical definitions—the error amplifies quickly.

    This is a key insight that’s easy to overlook: LLMs excel at understanding intent and organizing language, not executing precise calculations. Every digit after the decimal point is “made up” by the model, not “computed.” For any task requiring exact numerical output, having the model answer out of thin air carries a structural risk.

    The Agent-Mode Approach: The Model Writes Code, the Interpreter Computes

    The core idea of Agent mode is decoupling “understanding the task” from “executing the task”:

    1. The model acts as the programmer: it understands the data structure, breaks down the task, and writes the processing script;
    2. The execution environment acts as the runtime: the Python interpreter reads the file, runs the computation, and strictly outputs results according to the code logic;
    3. The toolchain extends capabilities: plotting libraries, document generation tools, web search, and local knowledge base retrieval are all invoked by the agent on demand.

    Python code is a deterministic sequence of instructions—the same input always produces the same output, with no probabilistic memory drift. The model translates data-processing requirements into code, the interpreter executes it, and accuracy is guaranteed by code quality rather than the model’s “feel for language.” This structurally eliminates the hallucination problem.

    A professional agent workflow typically looks like this: after receiving a task, the agent first generates a task list; then it executes each item—running Python to read the spreadsheet, retrieving supplementary information from the web or a local knowledge base, and computing metrics according to business logic; finally, it consolidates the results into a polished, chart-rich report document. The flow resembles common AI coding terminals, except it targets data analysis rather than codebase refactoring.

    Visualization: Why Plotting Libraries Beat Image Generation Models

    When it comes to report charts, many people think of asking a model to “draw a picture.” But image generation models are also probabilistic pixel generators—they cannot guarantee that every data point in the chart strictly corresponds to the real value. In a quantitative context, such charts are not just unprofessional; they can actively mislead readers.

    The correct approach is to have the agent call Python visualization libraries (such as open-source plotting components):

    1. import pandas as pd
    2. df = pd.read_excel("inventory.xlsx")
    3. quantile = df["inventory"].quantile(0.7)
    4. df["baseline"].plot(kind="line")

    In charts like these, every point and every bar is the mapped result of an actual calculation, and axes, legends, and tick marks are all precisely controlled by code. The accuracy and professionalism are clearly superior to image-generation output. When models of similar capability operate in Agent mode, the variance in their quantile results can shrink from the percentile level to the decimal-point level—exactly the order-of-magnitude difference between “code execution” and “probabilistic output.”

    The agent can go further by using document processing libraries to merge the written content and generated charts into a complete PDF report automatically. One prompt yields a finished, deliverable document with both text and visuals.

    Prompt Design: One Sentence Unlocks the Full Pipeline

    To get the agent to complete the “data analysis → plotting → report assembly” loop, the prompt must state the output requirements explicitly, for example by appending to the task description: “Automatically call the plotting library to generate charts based on the data logic, then merge the report body and charts into a PDF report.” Practical experience shows that:

    1. Explicitly specifying “process data with code and plot with a plotting library—do not use image generation” prevents the agent from taking the wrong technical path;
    2. Requiring it to “list a task plan before executing” improves completion rates and traceability for multi-step tasks;
    3. Specifying that charts should be drawn “according to business logic” rather than as arbitrary decoration significantly improves their professionalism.

    Practical Takeaways and Conclusion

    Taken together, cloud-hosted code execution environments, the Agent modes offered by various vendors, and locally deployed coding terminals all follow the same underlying methodology: let the LLM do what it’s good at—understanding, decomposition, and writing code—and hand the parts that demand precision to a deterministic execution environment. A few rules of thumb worth remembering:

    1. Whenever exact numbers are involved, always go through the “code execution” path—never let the model report figures directly;
    2. Use plotting libraries for visualization, not image generation models, so that every pixel corresponds to real data;
    3. When designing prompts, spell out the “process requirements” clearly—an agent’s execution quality depends on how explicit the instructions are;
    4. For report-type tasks, make full use of document assembly capabilities so the agent delivers a finished product, not intermediate results.

    “Assign the probabilistic to the model, and the deterministic to the code”—only then can you truly unlock the value of LLM agents in professional data work.