Executable Counterfactuals: Improving LLMs' Causal Reasoning Through Code

Aniket Vashishtha, Qirun Dai, Hongyuan Mei, Amit Sharma, Chenhao Tan, Hao Peng

Current Large Language Models (LLMs) often appear adept at reasoning about "what if" scenarios. However, this proficiency is frequently an illusion. When tested, models are usually performing simpler "interventional" reasoning by skipping a crucial cognitive step: abduction (inferring hidden states).

This paper introduces a novel framework using executable code to force models to perform true, three-step causal reasoning. The findings reveal a significant gap in current LLM capabilities and demonstrate that Reinforcement Learning (RL) is far more effective than Supervised Fine-Tuning (SFT) at teaching models to generalize causal reasoning skills to entirely new domains.

The Missing Step in Causal Reasoning

Counterfactual reasoning represents "Level 3" of Judea Pearl’s Causal Ladder—the ability to imagine alternative realities (e.g., "Would the patient have recovered if we had used Treatment B instead of A?"). True counterfactual reasoning requires three distinct steps:

  1. Abduction: Inferring the hidden state of the world based on actual observations.
  2. Intervention: Changing a specific variable in that world.
  3. Prediction: Calculating the new outcome based on the intervention.
The three-step counterfactual reasoning process applied to code, math, and medical domains.

Most existing benchmarks inadvertently skip the "Abduction" step by providing all variables to the model upfront. This reduces the task to simple interventional reasoning (Level 2), leading to an overestimation of LLM capabilities.

The "Abduction Gap"

To expose this flaw, the researchers propose Executable Counterfactuals, a framework that operationalizes causal models as Python code.

In this setup, a Python function contains a latent (hidden) variable $r$. The model is given a "factual" observation (e.g., "When input $x=10$, the output was $y=36$"). The model must then answer a counterfactual question: "What would $y$ have been if $x=8$, keeping $r$ the same?" To solve this, the model must first work backward to find the value of $r$ (Abduction) before it can simulate the new input.

Performance drop between interventional and counterfactual reasoning across state-of-the-art LLMs.

When the hidden state is truly unknown, a massive "Abduction Gap" appears. Even state-of-the-art reasoning models like Claude-3.5-Sonnet and o4-mini are nearly perfect at executing code when given all variables, but their accuracy plummets by 25–40% when they have to infer a hidden variable first.

Training for True Causal Reasoning: SFT vs. RL

To improve this skill, the authors compared two training strategies on Qwen models:

  • Supervised Fine-Tuning (SFT): Training on reasoning traces distilled from a larger teacher model.
  • Reinforcement Learning (RL): Using the Group Relative Policy Optimization (GRPO) algorithm, where the model is rewarded solely for getting the correct final answer, forcing it to "discover" the optimal reasoning path.

The study utilized synthetic code datasets (in-distribution if-else logic, out-of-distribution while-loops and multiple latent variables) as well as a new GSM Math Counterfactuals benchmark to test domain transfer.

Key Findings

  1. RL is a Generalizer, SFT is a Memorizer: Models trained via SFT become highly proficient at the specific code structures seen during training but fail when the logic changes (e.g., moving from if statements to while loops). In contrast, RL-trained models internalize the underlying strategy of abduction.
  2. Massive Performance Gains: RL-trained Qwen-7B models achieved 1.5x to 2x accuracy gains over base models on code tasks.
  3. Cross-Domain Transfer: Remarkably, a model trained only on code using RL showed massive improvements on natural language math word problems. On the GSM Math Counterfactual task, the RL-trained model reached 39.0% accuracy, compared to just 8.5% for the SFT-trained version.
  4. Scaling is Not a Silver Bullet: Simply making a model larger does not inherently fix the abduction gap. A 7B model trained with RL often outperformed a 72B base model on these specific causal tasks.

Limitations and Future Work

While RL models achieved high "planning" scores (successfully following the three-step causal process), they are still occasionally bottlenecked by simple calculation errors during the "execution" phase. Furthermore, this work focuses on support-set inference—finding a specific value for a hidden variable—rather than dealing with full probabilistic counterfactual distributions.

Future research will look toward improving the execution capabilities of these models and scaling the executable framework to encompass more complex, real-world causal graphs beyond basic code and math.

Summary updated

BibTeX
@inproceedings{vashishtha2026executable,
  author = {Aniket Vashishtha and Qirun Dai and Hongyuan Mei and Amit Sharma and Chenhao Tan and Hao Peng},
  title = {{Executable Counterfactuals: Improving LLMs' Causal Reasoning Through Code}},
  booktitle = {International Conference on Learning Representations},
  year = {2026},
  url = {https://openreview.net/forum?id=Lm46gJA0q8},
}
Figure