Executable Counterfactuals: Improving LLMs' Causal Reasoning Through Code
On the map: LLM reasoning → Causal reasoning in LLMs
Abstract (arXiv)
Counterfactual reasoning, a hallmark of intelligence, consists of three steps: inferring latent variables from observations (abduction), constructing alternatives (interventions), and predicting their outcomes (prediction). This skill is essential for advancing LLMs' causal understanding and expanding their applications in high-stakes domains such as scientific research. However, existing efforts in assessing LLM's counterfactual reasoning capabilities tend to skip the abduction step, effectively reducing to interventional reasoning and leading to overestimation of LLM performance. To address this, we introduce executable counterfactuals, a novel framework that operationalizes causal reasoning through code and math problems. Our framework explicitly requires all three steps of counterfactual reasoning and enables scalable synthetic data creation with varying difficulty, creating a frontier for evaluating and improving LLM's reasoning. Our results reveal substantial drop in accuracy (25-40%) from interventional to counterfactual reasoning for SOTA models like o4-mini and Claude-4-Sonnet. To address this gap, we construct a training set comprising counterfactual code problems having if-else condition and test on out-of-domain code structures (e.g. having while-loop); we also test whether a model trained on code would generalize to counterfactual math word problems. While supervised finetuning on stronger models' reasoning traces improves in-domain performance of Qwen models, it leads to a decrease in accuracy on OOD tasks such as counterfactual math problems. In contrast, reinforcement learning induces the core cognitive behaviors and generalizes to new domains, yielding gains over the base model on both code (improvement of 1.5x-2x) and math problems. Analysis of the reasoning traces reinforces these findings and highlights the promise of RL for improving LLMs' counterfactual reasoning.
Key facts. Cited by 10 papers in the map (data through 7 Oct 2026). Placed in Causal reasoning in LLMs, one of 17 directions in LLM reasoning. Builds on 37 of its 62 references that are in the map. First posted on arXiv 2 Oct 2025.
Builds on
37 of 62 references.
Closest ancestors · 7
Same direction, newest first
- 2025On the Eligibility of LLMs for Counterfactual Reasoning: A Decompositional Study17 May
- 2025CounterBench: Evaluating and Improving Counterfactual Reasoning in Large Language Models16 Feb
- 2024Reasoning Elicitation in Language Models via Counterfactual Feedback2 Oct
- 2024Teaching Transformers Causal Reasoning through Axiomatic Training10 Jul
- 2023CLadder: Assessing Causal Reasoning in Language Models7 Dec
- 2023CLOMO: Counterfactual Logical Modification with Large Language Models29 Nov
- 2023Causal Reasoning and Large Language Models: Opening a New Frontier for Causality28 Apr
Other references · 30
- 2024DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Modelscited by 8,851
- 2025DeepSeek-R1: Incentivizing Reasoning Capability in LLMs via Reinforcement Learningcited by 6,146
- 2024HybridFlow: A Flexible and Efficient RLHF Frameworkcited by 1,980
- 2020Beyond Accuracy: Behavioral Testing of NLP models with CheckListcited by 979
- 2025SFT Memorizes, RL Generalizes: A Comparative Study of Foundation Model Post-trainingcited by 655
- 2019Leveraging Procedural Generation to Benchmark Reinforcement Learningcited by 478
- 2019Learning the Difference that Makes a Difference with Counterfactually-Augmented Datacited by 428
- 2023Understanding the Effects of RLHF on LLM Generalisation and Diversitycited by 386
- 2021Brax -- A Differentiable Physics Engine for Large Scale Rigid Body Simulationcited by 375
- 2025Cognitive Behaviors that Enable Self-Improving Reasoners, or, Four Habits of Highly Effective STaRscited by 373
- 2023Can Large Language Models Infer Causation from Correlation?cited by 142
- 2025On the Generalization of SFT: A Reinforcement Learning Perspective with Reward Rectificationcited by 121
18 more
- 2024Physics of Language Models: Part 2.1, Grade-School Math and the Hidden Reasoning Processcited by 107
- 2020Generate Your Counterfactuals: Towards Controlled Counterfactual Generation for Textcited by 75
- 2019WIQA: A dataset for "What if..." reasoning over procedural textcited by 71
- 2023Integrating Large Language Model for Improved Causal Discoverycited by 54
- 2025OMEGA: Can LLMs Reason Outside the Box in Math? Evaluating Exploratory, Compositional, and Transformative Generalizationcited by 48
- 2023Causal Order: The Key to Leveraging Imperfect Experts in Causal Inferencecited by 46
- 2021An Investigation of the (In)effectiveness of Counterfactually Augmented Datacited by 42
- 2023Prompting Large Language Models for Counterfactual Generation: An Empirical Studycited by 36
- 2025Retro-Search: Exploring Untaken Paths for Deeper and Efficient Reasoningcited by 32
- 2023On Evaluating and Mitigating Gender Biases in Multilingual Settingscited by 31
- 2024LLMs for Generating and Evaluating Counterfactuals: A Comprehensive Studycited by 30
- 2025NaturalThoughts: Selecting and Distilling Reasoning Traces for General Reasoning Taskscited by 29
- 2023IfQA: A Dataset for Open-domain Question Answering under Counterfactual Presuppositionscited by 25
- 2025Finding Flawed Fictions: Evaluating Complex Reasoning in Language Models via Plot Hole Detectioncited by 24
- 2024CausalBN-Bench: A Comprehensive Benchmark for Causal Learning Capability of LLMscited by 20
- 2025Abduct, Act, Predict: Scaffolding Causal Inference for Automated Failure Attribution in Multi-Agent Systemscited by 14
- 2025Through the Valley: Path to Effective Long CoT Training for Small Language Modelscited by 10
- 2025Can LLMs Reconcile Knowledge Conflicts in Counterfactual Reasoningcited by 7
Built on by
Citing papers per year: 2025: 1; 2026: 9.
10 papers.
- 2026MedGuideX: Internalizing Decision Logic from Executable Guidelines into Large Language Models for Clinical Reasoningcited by 3
- 2025TempoBench: Reasoning Execution Without Causal Attribution Is Just Simulationcited by 1
- 2026Towards Generalizable Reasoning: Group Causal Counterfactual Policy Optimization for LLM Reasoningcited by 1
- 2026DeepImagine: Clinical Trial Outcome Prediction via Stepwise Local Counterfactual Imaginationscited by 0
- 2026Knowledge Vector of Logical Reasoning in Large Language Modelscited by 0
- 2026CauSim: Scaling Causal Reasoning with Increasingly Complex Causal Simulatorscited by 0
- 2026Towards a Universal Causal Reasonercited by 0
- 2026Robust Asynchronous Planning via Auto-Formalizationcited by 0
- 2026The Illusion of textitWhat If: Evaluating the Breakdown of Counterfactual Reasoning in LLMscited by 0
- 2026COUNTERMEM: World-Model Verified Counter-Factual Memory for Language Agentscited by 0
- 2026COUNTERMEM: World-Model Verified Counter-Factual Memory for Language Agents25 Sep 2026
- 2026The Illusion of textitWhat If: Evaluating the Breakdown of Counterfactual Reasoning in LLMs28 Aug 2026
- 2026Robust Asynchronous Planning via Auto-Formalization31 May 2026
- 2026MedGuideX: Internalizing Decision Logic from Executable Guidelines into Large Language Models for Clinical Reasoning26 May 2026
- 2026Towards a Universal Causal Reasoner24 May 2026
- 2026CauSim: Scaling Causal Reasoning with Increasingly Complex Causal Simulators9 May 2026
- 2026Knowledge Vector of Logical Reasoning in Large Language Models26 Apr 2026
- 2026DeepImagine: Clinical Trial Outcome Prediction via Stepwise Local Counterfactual Imaginations24 Apr 2026
- 2026Towards Generalizable Reasoning: Group Causal Counterfactual Policy Optimization for LLM Reasoning6 Feb 2026
- 2025TempoBench: Reasoning Execution Without Causal Attribution Is Just Simulation31 Oct 2025
Similar, not cited
Close in wording, no citation either way
- 2026Better Think Thrice: Learning to Reason Causally with Double Counterfactual Consistency
- 2025Flying Pigs, FaR and Beyond: Evaluating LLM Reasoning in Counterfactual Worlds
- 2023Counterfactual Collaborative Reasoning
- 2026Counterfactual Simulation Training for Chain-of-Thought Faithfulness
- 2025Counterfactual reasoning: an analysis of in-context emergence
- 2022Counterfactual reasoning: Do language models need world knowledge for causal understanding?
- 2023What If the TV Was Off? Examining Counterfactual Reasoning Abilities of Multi-modal Language Models
- 2025Can LLMs Explain Themselves Counterfactually?