Papers › Counterfactual Token Generation in Large Language Models

Counterfactual Token Generation in Large Language Models

25 Sep 2024arXiv:2409.17027archive 2025-07-28

Ivi Chatzi, Nina Corvelo Benz, Eleni Straitouri, Stratis Tsirtsis, Manuel Gomez-Rodriguez

"Sure, I am happy to generate a story for you: Captain Lyra stood at the helm of her trusty ship, the Maelstrom's Fury, gazing out at the endless sea. [...] Lyra's eyes welled up with tears as she realized the bitter truth - she had sacrificed everything for fleeting riches, and lost the love of her crew, her family, and herself." Although this story, generated by a large language model, is captivating, one may wonder -- how would the story have unfolded if the model had chosen "Captain Maeve" as the protagonist instead? We cannot know. State-of-the-art large language models are stateless -- they maintain no internal memory or state. Given a prompt, they generate a sequence of tokens as an output using an autoregressive process. As a consequence, they cannot reason about counterfactual alternatives to tokens they have generated in the past. In this work, our goal is to enhance them with this functionality. To this end, we develop a causal model of token generation that builds upon the Gumbel-Max structural causal model. Our model allows any large language model to perform counterfactual token generation at almost no cost in comparison with vanilla token generation, it is embarrassingly simple to implement, and it does not require any fine-tuning nor prompt engineering. We implement our model on Llama 3 8B-Instruct and Ministral-8B-Instruct and conduct a qualitative and a quantitative analysis of counterfactually generated text. We conclude with a demonstrative application of counterfactual token generation for bias detection, unveiling interesting insights about the model of the world constructed by large language models.

PaperPDFCodeCode Syntology ran

In Syntology Open this paper in Syntology's Atlas, the map of the papers in Syntology's graph and their citations.

For agents, Syntology's MCP tool lists every function and class Syntology harvested from this paper and whether it ran (how to connect): get_harvested_code_for_paper(arxiv_id="2409.17027")

Code

Syntology Ran 10 of 10 code samples harvested from 1 repository linked to this paper; 0 have no recorded run. Of those that ran: 1 ran · fixture could not drive it; 9 ran with no contract checked.

By repository: official repository: 10 samples from 1 repository, 10 ran. The run record, sample by sample. “Ran” means executed on a synthesized input, not that the code is correct or reproduces the paper.

networks-learning/counterfactual-llms officialmentioned in papermentioned on GitHubpytorch report

Repository list and official/mentioned flags are the archive's, frozen 2025-07-28. Reachability, where shown, is from one Syntology probe window (2026-09-16 to 2026-09-18); repositories not probed show nothing. GitHub stars are not tracked.

Code Syntology ran Syntology

10 samples harvested; 10 ran; 0 honoured the contract we drafted; 0 have no recorded run. Read from Syntology's graph 2026-09-24; that is when this build read the record, not when the samples ran.

1ran · fixture could not drive it
9ran

Licence: 10 of the 10 samples are pointer only, meaning Syntology does not serve that copy's text. This page shows no code text for any sample; each one links to its file in the repository.

Harvested from networks-learning/counterfactual-llms. “Ran” means the sample executed on a synthesized input. It does not mean the output is correct, and nothing here reproduces the paper's results. “Honoured” and “violated” refer to a contract Syntology drafted from the code itself; “our draft was wrong” and “fixture could not drive it” are failures of Syntology's instrument, not of the code.

Each sample ends with its code_sha256, Syntology's identity for that exact code. An agent fetches the stored sample with Syntology's MCP tool get_code(code_sha256="…") (how to connect); click an identity to copy that call.

Repository labels, per sample. official repository: The archive marks this repository official for the paper. named in the paper: The archive records that the paper mentions this repository; it is not marked official. community (archive-listed): In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper. found in paper text by Syntology: Syntology found this repository in the paper's own text; whether it is the authors' implementation is not asserted. community: Not in the archive's code links for this paper; a community repository Syntology harvested. Samples from a repository marked official are listed first. Licence labels name the repository's licence as recorded at harvest. “Pointer only” means Syntology does not serve that copy's text, for one of four reasons: no licence file was found; the licence was not identified; the licence is recorded as permissive but that copy's record is not marked cleared; or the licence is outside the permissive list Syntology serves text under (MIT, Apache-2.0, BSD and similar). Some licences outside that list permit redistribution, such as WTFPL, and GPL-3.0 under its conditions; they are simply not on the list. Hover a licence label for the reason. File links open the file on GitHub at the default branch, which may have changed since the harvest.

apply_rotary_emb networks-learning/counterfactual-llms/src/mistral-inference/moe_one_file_ref.py official repository ran no licence file found · pointer only · 22426d7eecb70410 · report
fix_income networks-learning/counterfactual-llms/src/bias.py official repository ran fingerprinted no licence file found · pointer only · 7e3a2dbb3c9579a1 · report
get_fig_dim networks-learning/counterfactual-llms/src/utils.py official repository ran fingerprinted no licence file found · pointer only · 1c1a16c4896a4f25 · report
get_where_to_intervene_dict networks-learning/counterfactual-llms/src/bias.py official repository ran no licence file found · pointer only · 7c2a58be8462ca91 · report
get_where_to_intervene_dict_direct networks-learning/counterfactual-llms/src/bias.py official repository ran no licence file found · pointer only · 58d741b58f7d0059 · report
precompute_freqs_cis networks-learning/counterfactual-llms/src/mistral-inference/moe_one_file_ref.py official repository ran no licence file found · pointer only · 99e0ca21ba709107 · report
repeat_kv networks-learning/counterfactual-llms/src/mistral-inference/moe_one_file_ref.py official repository ran no licence file found · pointer only · 3fdd4f66367ca608 · report
repeat_kv networks-learning/counterfactual-llms/src/mistral-inference/one_file_ref.py official repository ran no licence file found · pointer only · e8e39c9933cb102c · report
reshape_for_broadcast networks-learning/counterfactual-llms/src/llama3/llama/model.py official repository ran · fixture could not drive it fingerprinted no licence file found · pointer only · 70bf6ebaafd266c4 · report
sample_top_p networks-learning/counterfactual-llms/src/llama3/llama/generation.py official repository ran no licence file found · pointer only · 841e0eb44df294a1 · report

Tasks

Bias DetectionLanguage ModelingLanguage ModellingLarge Language ModelPrompt Engineering

1 archive task tag without a task page not shown.

Results from the paper archive 2025-07-28

No leaderboard rows for this paper in the archive.

Methods

AMLLaMA

Report a problem or propose a change · a person checks every report against the paper or source before anything changes; decisions are listed on /corrections