How Do Language Models Compose Functions?

Apoorv Khandelwal, Ellie Pavlick
10/2/2025
cs.CLcs.AI

Official code: apoorvkh/composing-functions (9 stars, last commit 2026-01-30).

Abstract

While large language models (LLMs) appear to be increasingly capable of solving compositional tasks, it is an open question whether they do so using compositional mechanisms. In this work, we investigate how feedforward LLMs solve two-hop factual recall tasks, which can be expressed compositionally as $g(f(x))$. We first confirm that modern LLMs continue to suffer from the "compositionality gap": i.e. their ability to compute both $z = f(x)$ and $y = g(z)$ does not entail their ability to compute the composition $y = g(f(x))$. Then, using logit lens on their residual stream activations, we identify two processing mechanisms, one which solves tasks $\textit{compositionally}$, computing $f(x)$ along the way to computing $g(f(x))$, and one which solves them $\textit{directly}$, without any detectable signature of the intermediate variable $f(x)$. Finally, we find that which mechanism is employed appears to be related to the embedding space geometry, with the idiomatic mechanism being dominant in cases where there exists a linear mapping from $x$ to $g(f(x))$ in the embedding spaces. We fully release our data and code at: https://github.com/apoorvkh/composing-functions .

View on arXivView PDF

Code Implementations(1)

Codebase for paper `How Do Language Models Compose Functions?`

91PythonOct 2, 20258 months ago

Similar Papers

Cite this paper

@article{khandelwal2025language,
  title  = {How Do Language Models Compose Functions?},
  author = {Apoorv Khandelwal and Ellie Pavlick},
  year   = {2025},
  eprint = {2510.01685},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2510.01685v1}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2510.01685.svg)](https://codeofpaper.com/paper/2510.01685)

Discussion