The persistent issue of Large Language Models hallucinating numerical results stems from a fundamental architectural mismatch: transformers predict tokens, they do not perform arithmetic. A recent technical breakdown argues that scaling model parameters is a misguided solution to this problem. Instead, the author proposes a deterministic pipeline that routes every numerical claim through a sandboxed Python evaluator and verifies the output with an automated auditor.
The Autocomplete Fallacy
When an LLM is asked for a calculation, such as a tax amount, it generates a sequence of tokens that statistically resembles a correct answer. This results in 'plausible magnitude' and 'believable decimals' without any underlying guarantee of correctness. The core failure mode is twofold: the value itself drifts due to probabilistic generation, and the formatting often includes binary floating-point artifacts, such as returning 87.50000000000001 instead of 87.50. The proposed remedy converts the task from 'produce a number' to 'ask a program for a number,' ensuring the request remains machine-checkable.
Implementing a Safe Evaluator with Decimal
The technical implementation relies on a Python script that parses expressions into an Abstract Syntax Tree (AST), explicitly avoiding the dangerous eval() function. By allowing only five specific operators—addition, subtraction, multiplication, division, and power—the system prevents code injection from malformed model outputs. Crucially, the evaluator utilizes Python’s Decimal module with a precision set to 28. This specific configuration eliminates the binary-float tail, ensuring that 1250 * 0.07 returns exactly 87.50. A character whitelist further restricts inputs to digits and basic operators, rejecting function names like round() before parsing even begins.
The System Prompt as a Contract
Code alone is insufficient; the model must be constrained by a rigorous system prompt that functions as a contract. The prompt explicitly forbids 'mental arithmetic,' requiring the model to call the calculate tool for every number appearing in the final answer, even for trivial cases like 2+2. It mandates one expression per tool call and requires the model to copy the returned string verbatim, prohibiting any re-rounding or reformatting. Furthermore, the prompt enforces a specific JSON output format at the end of every response, listing all 'claims' made by the model for later verification.
Automated Verification and CI Integration
The final piece of the architecture is a verification script that audits the model's output before it reaches a human reader. This script extracts the JSON claims block from the model's response and recomputes each expression using the same AST-based evaluator. If the recomputed value does not match the string provided by the model, the script exits with code 1. This non-zero exit code is designed to integrate directly into Continuous Integration (CI) pipelines or pre-publish hooks, effectively blocking any response containing fabricated or incorrect figures from being deployed.
Key Takeaways
- Determinism over Probability: LLMs should not calculate; they should orchestrate calls to deterministic programs.
- Precision Matters: Using Python's
Decimalmodule with 28-digit precision solves common floating-point formatting errors. - Strict Prompting: The system prompt must explicitly ban mental arithmetic and enforce verbatim copying of tool results.
- Automated Auditing: Verification scripts with non-zero exit codes allow math errors to be caught in CI pipelines.
The Bottom Line
Stop trusting your LLM's arithmetic. If you aren't routing numbers through a verified, deterministic tool chain, you're shipping hallucinations.