In-Context Algebra — part 1/5

Cleaned-text code points [0, 15856). Bibliography omitted here; source.md includes it.

# In-Context Algebra

###### Abstract

We investigate the mechanisms that arise when transformers are trained to solve arithmetic on sequences where tokens are variables whose meaning is determined only through their interactions in-context. While prior work has studied transformers in settings where the answer relies on fixed parametric or geometric information encoded in token embeddings, we devise a new in-context reasoning task where the assignment of tokens to specific algebraic elements varies from one sequence to another. Despite this challenging setup, transformers achieve near-perfect accuracy on the task and even generalize to unseen groups. We develop targeted data distributions to create causal tests of a set of hypothesized mechanisms, and we isolate three mechanisms models consistently learn: commutative copying where a dedicated head copies answers, identity element recognition that distinguishes identity-containing facts, and closure-based cancellation that tracks group membership to constrain valid answers. Our findings show that the kinds of reasoning strategies learned by transformers are dependent on the task structure and that models can develop symbolic reasoning mechanisms when trained to reason in-context about variables whose meanings are not fixed.

## 1 Introduction

The hallmark of abstract reasoning is the ability to work with words and symbols whose meaning is unknown ahead of time ( Newell, 1980 ; Lampinen et al., 2024 ) . Yet much of the performance of language models (LMs) can be attributed to the power of the token embedding, for example pre-encoding the attribute female in the embedding for “ Queen ,” ( Mikolov et al., 2013 ) or pre-encoding divisible-by-two within the embedding of the token “ 108 ” ( Zhou et al., 2024 ; Hu et al., 2025 ; Kantamneni & Tegmark, 2025 ; Nikankin et al., 2025 ) . What mechanisms can a transformer language model employ if it is unable to pre-encode solutions in the embeddings of the words?

In this work, we devise a simple in-context learning setting where tokens serve as pure variables, acquiring meaning only through their interactions within each sequence. This allows us to ask: What computational strategies do transformers develop when deprived of meaningful embeddings?

We adopt a familiar arithmetic problem setting, training small transformers to predict answers to arithmetic problems sampled from finite groups. What makes our setting unique is that each token is a variable that can represent any algebraic element: the meaning of each token is only fixed within a single sequence. Complementary to previous studies of emergent arithmetic reasoning ( Power et al., 2022 ; Zhang et al., 2022 ; Nanda et al., 2023 ; Zhong et al., 2023 ) , solving problems in this setting forces models to infer structure solely from observations of contextual relationships ( Piantadosi & Hill, 2022 ) rather than relying on fixed information encoded within token embeddings.

Surprisingly, we find that models trained on this task develop fundamentally different reasoning strategies than those previously observed when LMs solve arithmetic in fixed-token settings. Rather than learning geometric representations of a Fourier basis, we find that models acquire symbolic reasoning mechanisms based on sparse relational patterns ( Zucchet et al., 2025 ) . We identify three primary algorithmic strategies models employ beyond verbatim copying: commutative copying, identity element recognition, and closure-based cancellation. These findings suggest that the kinds of reasoning strategies learned by transformers are dependent on the task structure, with symbolic, context-based strategies emerging when tokens carry no consistent meaning across contexts.

## 2 Task Description

In this section, we describe our in-context learning task. At a high level, our task involves simulating a mixture of finite groups. 1 1 1 While not imperative for understanding our task setup, we provide a brief review of relevant topics from group theory in Appendix A . Each task sequence presents several examples of products between elements in a group and the model is trained on the ordinary next-token language modeling objective, with the goal that it will learn to predict the outcome of unseen group products (Figure 1 ).

More formally, we have a set of m m groups 𝒢 = { G 1 , G 2 , … , G m } \mathcal{G}=\{G_{1},G_{2},\ldots,G_{m}\} that the model is trained to simulate. Recall that for any finite group G G , the product of two elements x , y ∈ G x,y\in G is written as z = x ⋅ y ∈ G z=x\cdot y\in G . We call each such product “ x ⋅ y = z x\cdot y=z ” a fact . Training data consists of sequences of k k facts written using a vocabulary of variable tokens v i ∈ V v_{i}\in V whose meaning may vary between sequences. In practice, the vocabulary is small, with N = | V | < ∑ i | G i | N=|V|<\sum_{i}|G_{i}| . A typical sequence s s takes the form shown in Equation 1 , where individual facts consist of four tokens. s = “ v x ​ 1 v y ​ 1 = v z ​ 1 , v x ​ 2 v y ​ 2 = v z ​ 2 , ⋯ , v x ​ k v y ​ k = v z ​ k ” s=\text{``}v_{x1}v_{y1}=v_{z1},\,v_{x2}v_{y2}=v_{z2},\,\cdots,v_{xk}v_{yk}=v_{zk}\text{"} (1) We describe the positions of a fact with the following terminology: The first element v x ​ i v_{xi} , occupies the “left-slot”; v y ​ i v_{yi} is the “right-slot”; = = is the “predictive token”; and v z ​ i v_{zi} is in the “answer-slot”.

To generate a training sequence s s , we first sample 2 2 2 For more details about how we sample groups to construct 𝒢 s \mathcal{G}_{s} , see Appendix C.2 . a set of groups 𝒢 s \mathcal{G}_{s} from 𝒢 \mathcal{G} whose total number of elements is less than or equal to the number of variable tokens N N . We define the set of all sampled group elements to be H s = ⋃ 𝒢 s H_{s}=\bigcup\mathcal{G}_{s} , where | H s | ≤ N |H_{s}|\leq N . We then construct a one-to-one latent mapping φ s : H s → V \varphi_{s}:H_{s}\rightarrow V that randomly assigns all elements of H s H_{s} to distinct tokens in V V . We ensure that each group in 𝒢 s \mathcal{G}_{s} is assigned a non-overlapping set of variables so that the meaning of each variable within a given sequence is determined by the underlying group structure (Figure 1 a).

Given this latent mapping, we then assemble s s by sampling facts from the groups in 𝒢 s \mathcal{G}_{s} , converting them to variable statements via φ s \varphi_{s} , and concatenating them together (Figure 1 b). The statement “ φ s ​ ( x ) ​ φ s ​ ( y ) = φ s ​ ( z ) \varphi_{s}(x)\varphi_{s}(y)=\varphi_{s}(z) ” only appears in s s when there is a corresponding valid fact “ x ⋅ y = z x\cdot y=z ” among the sampled groups in 𝒢 s \mathcal{G}_{s} . Importantly, while the mapping φ s \varphi_{s} is fixed within a sequence, it varies between sequences, ensuring that vocabulary tokens v i ∈ V v_{i}\in V act as variables without fixed global meaning (Figure 1 c).

## 3 Can transformers learn in-context algebra?

We train transformer models on the in-context algebra task (§ 2 ) and evaluate both their in-distribution performance and their ability to generalize across contexts. We report results for one representative model throughout, but observe qualitatively similar patterns across multiple training runs (see Figure 7 in Appendix C ). Our main model is a 4-layer autoregressive transformer with 8 attention heads per layer and hidden size 1024, trained with next-token prediction on sequences of k = 200 k{=}200 algebra facts ( ∼ 1000 {\sim}1000 tokens). The training distribution 𝒢 = { C 3 , … , C 10 , D 3 , D 4 , D 5 } \mathcal{G}=\{C_{3},\dots,C_{10},D_{3},D_{4},D_{5}\} includes cyclic ( C i C_{i} ) and dihedral groups ( D i D_{i} ) of up to order 10, with sequences written using N = 16 N{=}16 variable tokens plus the special tokens ‘ = ’ and ‘ , ’. Because group-to-variable assignments are randomized per sequence, tokens act as placeholders whose meaning must be inferred from context.

Performance increases with context length. Accuracy increases monotonically with the number of in-context facts k k , but the rate of improvement depends on the group order (Fig. 2(a) ). Smaller groups (e.g., C 4 C_{4} , C 6 C_{6} ) reach high accuracy with only a few facts, whereas larger groups (e.g., C 8 C_{8} , C 10 C_{10} ) require substantially more context to achieve similar performance (see Fig. 10 in Appendix C.3 ).

Phase transition on non-copyable queries. At large k k , many queries are trivially solvable by copying a previously seen fact (about 90 % 90\% of queries are copyable at k = 200 k{=}200 versus 45 % 45\% at k = 50 k{=}50 ). To address this, we evaluate the model with held-out data where the final fact “ x ​ y = xy{=} ” and its commutative pair “ y ​ x = yx{=} ” never appear elsewhere in the sequence. In this setting, the model still achieves near perfect accuracy, and we observe an abrupt improvement during training (a phase transition) on non-copyable queries (Fig. 2(b) ), suggesting the emergence of strategies beyond verbatim retrieval.

Generalization across algebraic structure. The model also transfers to unseen groups: on the complete set of order-8 groups (for groups excluded from training), the model also achieves near-perfect performance (Fig. 2(c) ). Interestingly, hold-out performance is good for non-group structures such as semigroups, but is worse for quasigroups and collapses on magmas (Fig. 11 b in Appendix C.3 ). The model still achieves non-trivial accuracy for quasigroups, particularly on cancellation data (Fig. 12 in Appendix C.3 ), though generalization remains consistently stronger for groups than non-groups.

## 4 Hypothesizing Model Mechanisms

When analyzing a random in-context algebra sequence, it is possible that multiple algorithms could theoretically produce correct predictions. That can make it challenging to identify which mechanisms the model actually implements. Consider the sequences shown in Equation 2 and Equation 3 that differ only by which fact is bolded. The model could correctly predict “ d ​ p = p dp{=}\emph{\textbf{p}} ” by either copying the answer from the duplicate fact appearing earlier (i.e., d ​ p = p dp{=}p ) or by recognizing d d is an identity element (i.e., d ​ c = c dc{=}c ) and applying the identity rule. “ k b = i , d c = c , c l = p , j p = l , dp = p ¯ , e n = e , b b = n , p j = l , d p = ” \displaystyle\text{``}kb=i,dc=c,cl=p,jp=l,\underline{\textbf{dp}=\textbf{p}},en=e,bb=n,pj=l,dp=\text{"} (2) “ ​ k ​ b = i , dc = c ¯ , c ​ l = p , j ​ p = l , d ​ p = p , e ​ n = e , b ​ b = n , p ​ j = l , d ​ p = ” \displaystyle\text{``}kb=i,\underline{\textbf{dc}=\textbf{c}},cl=p,jp=l,dp=p,en=e,bb=n,pj=l,dp=\text{"} (3) To disambiguate between potential mechanisms, we design five targeted data distributions to test specific algorithms that can solve algebra sequences when a corresponding set of facts is present in the context. We describe each data distribution and the hypothesized algorithm it tests below:

1. Verbatim Copying ( 𝒟 copy \mathcal{D}_{\text{copy}} ). This data tests whether the model copies exact facts from the context. We construct sequences s ∈ 𝒟 copy s\in\mathcal{D}_{\text{copy}} to contain at least one duplicate of the final fact.

2. Commutative Copying ( 𝒟 commute \mathcal{D}_{\text{commute}} ). In many groups, knowing that a ​ b = c ab{=}c often implies that b ​ a = c ba{=}c . This data tests whether the model copies these commutative facts from the context. We construct s ∈ 𝒟 commute s\in\mathcal{D}_{\text{commute}} to contain at least one instance of the commutative fact (e.g., y ​ x = z yx{=}z for final fact x ​ y = xy{=} ), and no duplicate facts.

3. Identity Element Recognition ( 𝒟 identity \mathcal{D}_{\text{identity}} ). This data tests whether the model can recognize and apply the identity rule. We construct s ∈ 𝒟 identity s\in\mathcal{D}_{\text{identity}} such that the final fact contains an identity element (e.g., x ​ y = x xy{=}x ) and that at least one prior fact in the context reveals the identity element (e.g., z ​ y = z zy{=}z ). We remove any duplicate or commutative facts.

4. Associative Composition ( 𝒟 associate \mathcal{D}_{\text{associate}} ). This data tests whether the model can chain fact results together to answer a new fact via associativity. Given a final fact x ​ y = z xy=z , we construct s ∈ 𝒟 associate s\in\mathcal{D}_{\text{associate}} so that it contains a minimum set of facts that would enable a solution via association. For example, the three facts x ​ g = f xg{=}f , g ​ d = y gd{=}y , f ​ d = z fd{=}z can be composed (i.e., ( x ​ g ) ​ d = f ​ d ⇒ x ⁡ ( g ​ d ) = z ⇒ x ​ y = z (xg)d{=}fd\Rightarrow x(gd){=}z\Rightarrow xy{=}z ) to compute x ​ y = z xy{=}z . We make sure to only use triples without duplicate or commutative facts.

5. Closure-Based Cancellation ( 𝒟 cancel \mathcal{D}_{\text{cancel}} ). This data tests whether the model can track group membership and appropriately apply the cancellation law to eliminate invalid answers (e.g., x ​ b = g xb{=}g eliminates g g as an answer to “ x ​ y = xy{=} ”). Given a final fact x ​ y = z xy{=}z , we construct s ∈ 𝒟 cancel s\in\mathcal{D}_{\text{cancel}} by including all the facts that share x x in the left-slot (e.g., x ​ b = g xb{=}g ) or y y in the right-slot (e.g., c ​ y = e cy{=}e ), and removing duplicate and commutative facts.

### 4.1 Measuring Coverage and Performance on Targeted Distributions

We seek to answer two questions: (1) What fraction of in-context algebra sequences can theoretically be solved by these hypothesized algorithms? and (2) Does the model successfully solve sequences that algorithmic strategies can solve when presented with the appropriate facts in-context?

Algorithmic Coverage. To understand the breadth of data that our hypothesized mechanisms might explain, we implement Python equivalents of all five algorithms (Appendix G ) and measure their coverage , i.e., the percentage of sequences they can theoretically solve. We apply the algorithms sequentially in the following order: verbatim copying, commutative copying, identity recognition, closure-based cancellation, and associative composition, where each algorithm is only applied to sequences unsolved by previous mechanisms. We compute algorithmic coverage over both random training sequences (Figure 3 a) and random hold-out sequences where neither form of copying is possible (Figure 3 b), using 2000 sequences for each evaluation.

We find that verbatim copying can solve a large percentage of the training data, with its area under the curve (AUC) being 67.9 % 67.9\% (Figure 3 a, green). Commutative copying accounts for an additional 12.1 % 12.1\% of cases (purple), with the identity solving 4.2 % 4.2\% (yellow), closure-based cancellation solving 2.7 % 2.7\% (red), and associativity solving 3.6 % 3.6\% (blue) for total coverage AUC of 90.4 % 90.4\% . In contrast, the model accuracy (black) achieves an AUC of 92.4 % 92.4\% . While the hypothesized algorithms can explain most of the model’s empirical training performance, they do not explain everything the model has learned ( ∼ 2.0 % {\sim}2.0\% AUC, gray); there may be other interesting mechanisms this analysis misses.

When a sequence cannot be solved via copying or commutative copying, we see a very different trend (Figure 3 b). In this more challenging setting, the model achieves a slightly lower AUC of 87.3 % 87.3\% (black). Identity recognition is able to solve 28.7 % 28.7\% of hold-out cases (yellow), closure-based cancellation can solve another 39.1 % 39.1\% (red), and associativity solves 16.9 % 16.9\% (blue) bringing the total hold-out coverage AUC to 84.7 % 84.7\% . Here, the AUC gap between the model’s empirical performance and our algorithmic coverage is 2.6 % 2.6\% (gray), and is primarily for algebra sequences with fewer facts.