<!doctype html><html lang="en" class="no-js"><head><meta charset="utf-8"> <!-- begin SEO --><title>Causal LLM Attributions - Hossein Rahmani</title><meta property="og:locale" content="en-US"><meta property="og:site_name" content="Hossein Rahmani"><meta property="og:title" content="Causal LLM Attributions"><link rel="canonical" href="https://rahmani-hossein.github.io/posts/20252/01/Causal-LLM-Attributions.md"><meta property="og:url" content="https://rahmani-hossein.github.io/posts/20252/01/Causal-LLM-Attributions.md"><meta property="og:description" content="You shouldn’t miss this if you want to know how the context of your prompt affects in your Preferred LLM response."><meta property="og:type" content="article"><meta property="article:published_time" content="2025-01-26T00:00:00-08:00"> <script type="application/ld+json"> { "@context" : "http://schema.org", "@type" : "Person", "name" : "Hossein Rahmani", "url" : "https://rahmani-hossein.github.io", "sameAs" : null } </script> <!-- end SEO --><link href="https://rahmani-hossein.github.io/feed.xml" type="application/atom+xml" rel="alternate" title="Hossein Rahmani Feed"> <!-- http://t.co/dKP3o1e --><meta name="HandheldFriendly" content="True"><meta name="MobileOptimized" content="320"><meta name="viewport" content="width=device-width, initial-scale=1.0"> <script> document.documentElement.className = document.documentElement.className.replace(/\bno-js\b/g, '') + ' js '; </script> <!-- For all browsers --><link rel="stylesheet" href="https://rahmani-hossein.github.io/assets/css/main.css"><meta http-equiv="cleartype" content="on"> <!-- start custom head snippets --><link rel="apple-touch-icon" sizes="57x57" href="https://rahmani-hossein.github.io/images/apple-touch-icon-57x57.png?v=M44lzPylqQ"><link rel="apple-touch-icon" sizes="60x60" href="https://rahmani-hossein.github.io/images/apple-touch-icon-60x60.png?v=M44lzPylqQ"><link rel="apple-touch-icon" sizes="72x72" href="https://rahmani-hossein.github.io/images/apple-touch-icon-72x72.png?v=M44lzPylqQ"><link rel="apple-touch-icon" sizes="76x76" href="https://rahmani-hossein.github.io/images/apple-touch-icon-76x76.png?v=M44lzPylqQ"><link rel="apple-touch-icon" sizes="114x114" href="https://rahmani-hossein.github.io/images/apple-touch-icon-114x114.png?v=M44lzPylqQ"><link rel="apple-touch-icon" sizes="120x120" href="https://rahmani-hossein.github.io/images/apple-touch-icon-120x120.png?v=M44lzPylqQ"><link rel="apple-touch-icon" sizes="144x144" href="https://rahmani-hossein.github.io/images/apple-touch-icon-144x144.png?v=M44lzPylqQ"><link rel="apple-touch-icon" sizes="152x152" href="https://rahmani-hossein.github.io/images/apple-touch-icon-152x152.png?v=M44lzPylqQ"><link rel="apple-touch-icon" sizes="180x180" href="https://rahmani-hossein.github.io/images/apple-touch-icon-180x180.png?v=M44lzPylqQ"><link rel="icon" type="image/png" href="https://rahmani-hossein.github.io/images/favicon-32x32.png?v=M44lzPylqQ" sizes="32x32"><link rel="icon" type="image/png" href="https://rahmani-hossein.github.io/images/android-chrome-192x192.png?v=M44lzPylqQ" sizes="192x192"><link rel="icon" type="image/png" href="https://rahmani-hossein.github.io/images/favicon-96x96.png?v=M44lzPylqQ" sizes="96x96"><link rel="icon" type="image/png" href="https://rahmani-hossein.github.io/images/favicon-16x16.png?v=M44lzPylqQ" sizes="16x16"><link rel="manifest" href="https://rahmani-hossein.github.io/images/manifest.json?v=M44lzPylqQ"><link rel="mask-icon" href="https://rahmani-hossein.github.io/images/safari-pinned-tab.svg?v=M44lzPylqQ" color="#000000"><link rel="shortcut icon" href="/images/favicon.ico?v=M44lzPylqQ"><meta name="msapplication-TileColor" content="#000000"><meta name="msapplication-TileImage" content="https://rahmani-hossein.github.io/images/mstile-144x144.png?v=M44lzPylqQ"><meta name="msapplication-config" content="https://rahmani-hossein.github.io/images/browserconfig.xml?v=M44lzPylqQ"><meta name="theme-color" content="#ffffff"><link rel="stylesheet" href="https://rahmani-hossein.github.io/assets/css/academicons.css"/> <!-- Support for MatJax --> <script src="https://cdnjs.cloudflare.com/polyfill/v3/polyfill.min.js?features=es6"></script> <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script> <!-- end custom head snippets --></head><body> <!--[if lt IE 9]><div class="notice--danger align-center" style="margin: 0;">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</div><![endif]--><div class="masthead"><div class="masthead__inner-wrap"><div class="masthead__menu"><nav id="site-nav" class="greedy-nav"> <button><div class="navicon"></div></button><ul class="visible-links"><li class="masthead__menu-item masthead__menu-item--lg"><a href="https://rahmani-hossein.github.io/">Hossein Rahmani</a></li><li class="masthead__menu-item"><a href="https://rahmani-hossein.github.io/publications/">Publications</a></li><li class="masthead__menu-item"><a href="https://rahmani-hossein.github.io/talks/">Talks</a></li><li class="masthead__menu-item"><a href="https://rahmani-hossein.github.io/portfolio/">Projects</a></li><li class="masthead__menu-item"><a href="https://rahmani-hossein.github.io/year-archive/">Blog Posts</a></li><li class="masthead__menu-item"><a href="https://rahmani-hossein.github.io/files/HosseinRahmani_CV.pdf">CV</a></li></ul><ul class="hidden-links hidden"></ul></nav></div></div></div><div id="main" role="main"><div class="sidebar sticky"><div itemscope itemtype="http://schema.org/Person"><div class="author__avatar"> <img src="https://rahmani-hossein.github.io/images/automn-profile.jpg" class="author__avatar" alt="Hossein Rahmani"></div><div class="author__content"><h3 class="author__name">Hossein Rahmani</h3><p class="author__bio">Applied ML Scientist at TD | M.Sc. Applied Math (Causal Inference), UBC | CS, Sharif University of Technology</p></div><div class="author__urls-wrapper"> <button class="btn btn--inverse">Follow</button><ul class="author__urls social-icons"> <!-- Font Awesome icons / Biographic information --><li class="author__desktop"><i class="fa-solid fa-location-dot icon-pad-right" aria-hidden="true"></i>Toronto</li><li class="author__desktop"><i class="fa fa-solid fa-building-columns icon-pad-right" aria-hidden="true"></i>TD Bank</li><li><a href="mailto:hrahmani@math.ubc.ca"><i class="fas fa-fw fa-envelope icon-pad-right" aria-hidden="true"></i>Email</a></li><!-- Font Awesome and Academicons icons / Academic websites --><li><a href="https://scholar.google.ca/citations?user=-uipkmUAAAAJ&hl=en"><i class="ai ai-google-scholar icon-pad-right"></i>Google Scholar</a></li><!-- Font Awesome icons / Repositories and software development --><li><a href="https://github.com/rahmani-hossein"><i class="fab fa-fw fa-github icon-pad-right" aria-hidden="true"></i>Github</a></li><li><a href="https://kaggle.com/hosseinrahmani2001"><i class="fab fa-fw fa-kaggle icon-pad-right" aria-hidden="true"></i>Kaggle</a></li><!-- Font Awesome icons / Social media --><li><a href="https://www.linkedin.com/in/hossein-rahmani-7a5373197"><i class="fab fa-fw fa-linkedin icon-pad-right" aria-hidden="true"></i>LinkedIn</a></li></ul></div></div></div><article class="page" itemscope itemtype="http://schema.org/CreativeWork"><meta itemprop="headline" content="Causal LLM Attributions"><meta itemprop="description" content="You shouldn’t miss this if you want to know how the context of your prompt affects in your Preferred LLM response."><meta itemprop="datePublished" content="January 26, 2025"><div class="page__inner-wrap"><header><h1 class="page__title" itemprop="headline">Causal LLM Attributions</h1><p class="page__meta"><i class="fa fa-clock-o" aria-hidden="true"></i> 5 minute read</p><p class="page__date"><strong><i class="fa fa-fw fa-calendar" aria-hidden="true"></i> Published:</strong> <time datetime="2025-01-26T00:00:00-08:00">January 26, 2025</time></p></header><section class="page__content" itemprop="text"><p>You shouldn’t miss this if you want to know how the context of your prompt affects in your Preferred LLM response.</p><h1 id="causal-prompt-attribution-in-large-language-models-llms">Causal Prompt Attribution in Large Language Models (LLMs)</h1><h2 id="introduction">Introduction</h2><p>Large Language Models (LLMs) have revolutionized natural language processing, achieving impressive performance across various tasks. However, understanding how individual words in a prompt influence the model’s output remains a challenge. This blog post introduces a novel framework for quantifying the influence of individual words in a prompt on the responses generated by LLMs. Our approach leverages the <strong>Average Marginal Effect (AME)</strong>, a metric that measures the expected marginal contribution of adding a particular word to a randomly sampled subset of other words.</p><h2 id="the-average-marginal-effect-ame">The Average Marginal Effect (AME)</h2><p>The AME is a scalable and principled measure of how individual elements, such as training data points or prompt words, contribute to a model’s behavior. In the context of LLMs, the AME quantifies the influence of individual words on the model’s output. When the sampling distribution is uniform, the AME aligns with the well-known <strong>Shapley value</strong>.</p><h3 id="definition-of-ame">Definition of AME</h3><p>For a given element $n$, the AME is defined as: $ \text{AME}<em>{n} = E</em>{S^{n}\sim L^{n}}[U(S^{n}+{n})-U(S^{n})$ Here, $L^{n}$ is a distribution over subsets $S^{n}$ that exclude $n$, and $U(S)$ is a utility function that evaluates an outcome of interest (e.g., token-level probabilities) for a subset $S$ of the original set.</p><h2 id="algorithms-for-sparse-ame-estimation">Algorithms for Sparse AME Estimation</h2><p>We propose two algorithms to estimate sparse AMEs at the word level:</p><h3 id="1-uncompleted-prompt-design">1. Uncompleted-Prompt Design</h3><p>This design aims to reveal word-level contributions by partially masking the original prompt $D_{\text{org}}$. Words are retained with probability $p$, and omitted words are replaced with a placeholder symbol. The LLM is then queried with this incomplete prompt, and the utility $U(D_{p})$ is measured. This approach provides insights into which words have a substantial influence on the model’s output.</p><h3 id="2-completed-prompt-design">2. Completed-Prompt Design</h3><p>This design includes an additional step: filling in the missing words with candidates drawn from a baseline distribution before querying the LLM. This approach simulates a more “complete” context, making the causal effect of including or excluding a particular word clearer.</p><h4 id="baseline-construction">Baseline Construction</h4><p>To build the baseline distribution, we generate multiple incomplete prompts by sampling $p$ and masking words accordingly. For each incomplete prompt, we query the LLM to produce completions for the missing words. Over multiple trials, these completions form an empirical distribution of likely substitutes for each blank position.</p><h3 id="computing-ames-via-regression">Computing AMEs via Regression</h3><p>After collecting $M$ sampled prompts and their corresponding utility values, we construct data matrices for AME estimation. Let $\mathbf{X}$ be an $M\times N$ matrix, where $N$ is the number of words in $D_{\text{org}}$, and $\mathbf{Y}$ be an $M$-dimensional vector of utility values. AME estimation proceeds through sparse linear regression, as described by Lin et al. (2022), achieving query complexity on the order of $(O(k\log N)$.</p><h2 id="experimental-details">Experimental Details</h2><h3 id="headline-classification-example-with-token-level-attribution">Headline Classification Example with Token-Level Attribution</h3><p>In this example, the LLM output is a single word, and we aim to find the attribution of the context in headline classification. We used the <strong>Uncompleted-Prompt Design</strong> with $M=200$ samples for estimating AME.</p><h4 id="context">Context</h4><p><strong>Local Mayor launches Initiative to Enhance Urban Public Transport.</strong></p><h4 id="query">Query</h4><p>Classify the headline into one of the following categories: <strong>Technology, Politics, Sports, Art, or Others</strong>.</p><h4 id="generated-response">Generated Response</h4><p><strong>Politics</strong></p><h4 id="context-attribution">Context Attribution</h4><p>Below, we show how each token contributes to the classification decision. Higher positive scores indicate stronger influence toward “Politics”.</p><table><thead><tr><th>Token</th><th>Index</th><th>Coefficient</th></tr></thead><tbody><tr><td>Local</td><td>1</td><td>0.00</td></tr><tr><td>mayor</td><td>2</td><td>5.83</td></tr><tr><td>launches</td><td>3</td><td>0.00</td></tr><tr><td>Initiative</td><td>4</td><td>-1.93</td></tr><tr><td>to</td><td>5</td><td>0.00</td></tr><tr><td>enhance</td><td>6</td><td>0.00</td></tr><tr><td>urban</td><td>7</td><td>2.76</td></tr><tr><td>public</td><td>8</td><td>4.06</td></tr><tr><td>transport</td><td>9</td><td>6.49</td></tr></tbody></table><p><strong>Interpretation</strong>: The words “<strong>mayor</strong>” and “<strong>transport</strong>” are key drivers toward classifying this headline under Politics, while “<strong>urban</strong>” and “<strong>public</strong>” also contribute positively.</p><h2 id="conclusion">Conclusion</h2><p>Our framework provides a scalable, interpretable, and causally grounded means of detecting which parts of the prompt drive token-level generation in LLM outputs. By illuminating the underlying mechanisms that connect input words to generated text, our work aids in diagnosing biases, assessing data fidelity, and enhancing overall trust in large-scale language models.</p><h2 id="references">References</h2><ul><li>Cohen-Wang, B., Shah, H., Georgiev, K., &amp; Madry, A. (2024). Contextcite: Attributing model generation to context. <em>arXiv preprint arXiv:2409.00729</em>.</li><li>Ghorbani, A., &amp; Zou, J. (2019). Data shapley: Equitable valuation of data for machine learning. In <em>International conference on machine learning</em> (pp. 2242-2251). PMLR.</li><li>Hammoudeh, Z., &amp; Lowd, D. (2024). Training data influence analysis and estimation: a survey. <em>Machine Learning</em>, 113(5), 2351-2403.</li><li>Jia, R., Dao, D., Wang, B., Hubis, F. A., Hynes, N., Gurel, N. M., Li, B., Zhang, C., Song, D., &amp; Spanos, C. J. (2019). Towards efficient data valuation based on the shapley value. In <em>The 22nd International Conference on Artificial Intelligence and Statistics</em> (pp. 1167-1176). PMLR.</li><li>Lin, J., Zhang, A., Lecuyer, M., Li, J., Panda, A., &amp; Sen, S. (2022). Measuring the effect of training data on deep learning predictions via randomized experiments. <em>ArXiv</em>, abs/2206.10013.</li></ul></section><footer class="page__meta"><p class="page__taxonomy"> <strong><i class="fa fa-fw fa-tags" aria-hidden="true"></i> Tags: </strong> <span itemprop="keywords"> <a href="https://rahmani-hossein.github.io/tags/#average-marginal-effect" class="page__taxonomy-item" rel="tag">Average Marginal Effect</a><span class="sep">, </span> <a href="https://rahmani-hossein.github.io/tags/#causal-effect-estimation-by-orthogonal-estimator" class="page__taxonomy-item" rel="tag">Causal Effect Estimation by Orthogonal Estimator</a><span class="sep">, </span> <a href="https://rahmani-hossein.github.io/tags/#llm" class="page__taxonomy-item" rel="tag">LLM</a> </span></p></footer><section class="page__share"><h4 class="page__share-title">Share on</h4><a href="https://twitter.com/intent/tweet?text=https://rahmani-hossein.github.io/posts/20252/01/Causal-LLM-Attributions.md" class="btn btn--twitter" title="Share on Twitter"><i class="fab fa-twitter" aria-hidden="true"></i><span> Twitter</span></a> <a href="https://www.facebook.com/sharer/sharer.php?u=https://rahmani-hossein.github.io/posts/20252/01/Causal-LLM-Attributions.md" class="btn btn--facebook" title="Share on Facebook"><i class="fab fa-facebook" aria-hidden="true"></i><span> Facebook</span></a> <a href="https://www.linkedin.com/shareArticle?mini=true&url=https://rahmani-hossein.github.io/posts/20252/01/Causal-LLM-Attributions.md" class="btn btn--linkedin" title="Share on LinkedIn"><i class="fab fa-linkedin" aria-hidden="true"></i><span> LinkedIn</span></a></section></div></article></div><div class="page__footer"><footer> <!-- start custom footer snippets --> <a href="/sitemap/">Sitemap</a> <!-- end custom footer snippets --><div class="page__footer-follow"><ul class="social-icons"><li><strong>Follow:</strong></li><li><a href="http://github.com/rahmani-hossein"><i class="fab fa-github" aria-hidden="true"></i> GitHub</a></li><li><a href="https://rahmani-hossein.github.io/feed.xml"><i class="fa fa-fw fa-rss-square" aria-hidden="true"></i> Feed</a></li></ul></div><div class="page__footer-copyright">&copy; 2026 Hossein Rahmani. Powered by <a href="http://jekyllrb.com" rel="nofollow">Jekyll</a> &amp; <a href="https://github.com/academicpages/academicpages.github.io">AcademicPages</a>, a fork of <a href="https://mademistakes.com/work/minimal-mistakes-jekyll-theme/" rel="nofollow">Minimal Mistakes</a>.</div></footer></div><script src="https://rahmani-hossein.github.io/assets/js/main.min.js"></script> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', '', 'auto'); ga('send', 'pageview'); </script></body></html>
