Building Reliable LLM Agents with Code Interpreters: A Practical Guide to Data Analysis

    Why do large language models produce unreliable numbers when asked to compute statistics directly, and how does the Agent mode fix this? This article compares "direct answer" and "tool calling" paradigms through a hands-on data analysis experiment, showing how separating planning from execution dramatically improves accuracy. It also shares practical tips for writing prompts, validating numerical outputs, and generating reproducible documents with LLM agents.

    Building Reliable LLM Agents with Code Interpreters: A Practical Guide to Data Analysis

    From “Direct Answers” to “Tool Calling”: Two Fundamentally Different Modes

    Over the past few years, one clear thread has run through the evolution of large language models: the shift from “conversational Q&A” to “task execution.” In the early days, users asked questions and the模型 generated text directly—the quality of the answer depended on whether the training data covered similar patterns. This works well for fuzzy tasks like writing, translation, and summarization. But the moment a task involves precise computation, a fundamental problem surfaces: a language model is fundamentally a probabilistic generator, not a calculator.

    Specifically, when you ask a model to “compute the quantiles of this dataset,” it isn’t performing mathematical operations—it’s guessing a plausible-looking number based on its training corpus and context. This can lead to:

    1. Approximate values: The answer looks close to correct, but the digits after the decimal point are entirely untrustworthy;
    2. Ignored precision requirements: No matter how many decimal places you request, the model tends to output whatever format feels “comfortable”;
    3. Fabricated intermediate steps: The step-by-step derivation looks like calculation, but each step is actually a product of text generation, not strict arithmetic execution.

    This is what the industry calls “hallucination” in numerical settings. For tasks with hard accuracy requirements—data analysis, quantitative computation, report generation—asking the model to “do mental math” is simply unreliable.

    Agent mode takes a completely different approach: the model no longer produces answers itself. Instead, it acts as a “programmer,” writing scripts, calling tools, and running code, obtaining results from the deterministic outputs of those tools. Quantiles are computed by Python’s numerical libraries, charts are drawn point-by-point from real data by plotting libraries, and PDFs are generated by scripts following templates—every step is backed by a deterministic execution result. The model handles understanding and planning; the tools handle precise execution. Once responsibilities are separated this way, reliability improves significantly.

    A Side-by-Side Experiment on a Data Analysis Task

    To validate this, I designed a typical quantitative data analysis task: read a table containing inventory and basis data, compute several groups of quantile metrics, generate a daily analysis report with visualizations, and finally produce a polished report document with both text and charts.

    Most mainstream LLM products now support Agent mode: the user provides a task description in natural language, and the model automatically plans the steps, creates a task list, writes and runs Python scripts, searches the web for supporting information, and assembles the final output. I gave the same task to several model agents and made some very revealing observations.

    1. The workflows have converged.

    The Agent modes across major models are now strikingly similar: they first create a task list, breaking a large task into steps like “read data → retrieve information → compute metrics → plot charts → generate document,” then execute each step in order. Along the way, they automatically run Python code to parse tables and search the web when external information is needed. This shows that “plan → decompose → call tools → summarize” has become the de facto paradigm for Agent mode, and the cost of switching to any platform is low.

    2. Numerical results diverge noticeably.

    With the same task and the same data, different model agents produced inconsistent quantile results. Some output percentages precise to one decimal place, some gave rounded figures, and a few differed considerably. This observation is highly instructive—it confirms the earlier conclusion: even in Agent mode, if the model “gets lazy” at some step and does mental math instead of calling code, probabilistic偏差will creep into the output. The correct Agent behavior is to delegate all numerical computation to code execution, with the model only passing parameters and interpreting results.

    3. Chart quality hinges on how they’re drawn.

    Charts drawn with Python plotting libraries have every point positioned by real data, with programmatic axis ticks, legends, and annotations—highly professional and precise. But if the agent takes the lazy route of using an image-generation model, the “chart” is just a pixel image that looks like a chart: axis values may be wrong, data points may not correspond to real data, and the result is not reproducible. The gap between the two approaches is essentially the gap between “deterministic rendering” and “probabilistic generation.” This is why explicitly stating requirements like “automatically call matplotlib or similar plotting libraries to generate charts” in your prompts is an effective way to control output quality.

    Practical Recommendations for Putting Agent Mode to Work

    Based on this experiment, here are several reusable best practices:

    1. Explicitly specify the toolchain in your prompts. Don’t assume the model will automatically pick the best tool. Spell out the execution path—for example: “Please generate and run a Python script to parse the table, use matplotlib to create the charts, and finally merge the analysis conclusions and charts into a PDF.” The more precise the specification, the more stable the output.

    2. Separate “computation” from “narration.” Require the model to run the code first and obtain the numbers, then write the analysis based on the code’s output. You can add to the prompt: “All numerical values must come from script execution results; direct estimation is prohibited.”

    3. Cross-check critical numbers. Compare outputs from multiple models, or have one model compute while another verifies; recalculate any significantly divergent results with code. Even in Agent mode, a model may still skip a computation step, so manual spot checks remain a necessary quality backstop.

    4. Prefer script generation for document deliverables. Structured outputs like PDFs, spreadsheets, and charts should be produced via code libraries and templates, not “drawn” by the model. The former are reproducible, auditable, and batchable.

    5. Pay attention to context and tool permissions. When the agent needs to read local files, confirm the data paths and formats; when web retrieval is involved, be mindful of how source quality affects the conclusions.

    Conclusion

    Agent mode represents a leap for LLMs from “being able to talk” to “being able to work.” But its reliability doesn’t come from the model itself getting smarter—it comes from the architecture of “model planning + tool execution.” For any task involving precise numbers, complex structured data, or reproducible deliverables, the model should write code and run scripts first, confining probabilistic text generation to where it excels. Understanding this is what it takes to use LLMs in production without falling into common pitfalls.