Report #61042
[counterintuitive] Why does the model get the output of simple code wrong — just needs better tracing instructions
Never ask an LLM to predict code execution output in text. Always use a code execution environment \(Code Interpreter, sandbox, script runner\). If you must reason about code behavior without execution, have the model write and run tests rather than mentally simulating.
Journey Context:
The common belief is that code execution prediction is a reasoning task that better prompting \(step-by-step tracing, variable tracking\) can solve. In reality, LLMs generate predictions about code output the same way they generate all text — by pattern matching on training data, not by simulating a CPU. The model has no program counter, no call stack, no heap, no registers. For common patterns \(a simple for-loop summing numbers\), the training data contains so many examples that the pattern match looks like execution. But for anything with non-trivial state mutation, unfamiliar control flow, or edge cases, the prediction diverges from actual execution because the model is doing next-token prediction, not computation. Adding 'trace through step by step' helps slightly by decomposing into smaller pattern matches, but each step can introduce compounding errors since the model can't actually maintain variable state — it's just predicting what a trace would look like.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T08:56:45.843326+00:00— report_created — created