In-Context Algebra

Eric Todd, Jannik Brinkmann, Rohit Gandikota, David Bau

TL;DR

Researchers have demonstrated that transformers can learn to solve abstract algebra problems using tokens as "pure variables" with no fixed meaning. Instead of relying on pre-encoded embedding information, these models develop sophisticated symbolic reasoning mechanisms—such as identity recognition and group cancellation—to infer mathematical structure entirely in-context.

The Problem: Stripping Away Semantic Anchors

Most of the "reasoning" we see in Large Language Models (LLMs) is heavily anchored by the semantic meaning of token embeddings. For example, a model knows "108" is divisible by two because that property is baked into the vector representing "108."

The authors ask: What happens when you strip away those anchors? If a token like "A" represents a different algebraic element in every new sequence, can a transformer still learn the underlying "rules of the game"? This tests whether transformers are capable of true symbolic manipulation—reasoning about relationships between variables—rather than just performing high-dimensional pattern matching on fixed concepts.

Methods: The In-Context Algebra Task

To investigate this, the authors trained small, 4-layer autoregressive transformers on a novel task called In-Context Algebra.

  1. The Task: The model is given a sequence of "facts" from a finite group (e.g., $ab=c, de=f$). It must then predict the answer to a new query (e.g., $ad=?$).
  2. Variable Assignment: Crucially, the mapping of vocabulary tokens to group elements is randomized for every sequence. "Token A" might be the identity element in one example and a non-identity element in the next.
Illustration of the In-Context Algebra task setup, showing how abstract group elements are randomly assigned to vocabulary tokens for different sequences.

To understand how the model solves this, the authors used mechanistic interpretability techniques, including causal interventions (patching activations), PCA, and subspace analysis to identify how the model represents abstract concepts like "group membership."

Key Insights: Mechanisms of Symbolic Logic

Previous work on "grokking" modular arithmetic showed models learning geometric rotations in embedding space. This paper finds that when tokens are variables, models instead learn sparse relational patterns—essentially a form of symbolic logic.

  • The Identity Circuit: The model solves identity problems ($e \cdot x = ?$) through a two-part mechanism. First, a "Query Promotion" head promotes both variables in the query as potential answers. Then, an "Identity Demotion" head identifies which variable is the identity based on previous context and suppresses its logit, leaving only the correct answer.
  • Closure-Based Cancellation: The model learns to track "closure"—which variables belong to the same group. It uses a "cancellation" strategy to eliminate variables that have already appeared in other equations, narrowing down the valid possibilities for the current answer.
  • Ordered Skill Acquisition: The model doesn't learn everything at once. It follows a strict "curriculum" of phase transitions: first learning the syntax ($=$ and $,$), then group closure, then simple copying, and finally complex elimination and associativity.

Key Results and Generalization

Despite the randomized variable assignments, the model achieves nearly 100% accuracy on both in-distribution and unseen groups of the same order.

Training and generalization performance showing near-perfect accuracy on held-out data and zero-shot generalization to unseen algebraic structures like Q8 and semigroups.

Remarkably, the model exhibits zero-shot generalization, successfully applying its reasoning to entirely new algebraic structures (like the quaternion group $Q_8$ or non-group structures like semigroups) that it never saw during training. Mechanistic analysis confirmed that specific attention heads are responsible for these generalized logic rules, and a small 16-dimensional subspace in the final layer is sufficient to capture the model's understanding of group closure.

Limitations and Future Work

While the model mastered copying and identity, it struggled with "Associative Composition" (chaining facts like $ab=c$ and $cd=e$ to solve $a(bd)$), reaching only about 60% accuracy. Furthermore, the study used small 4-layer models on finite groups. Future work will need to explore whether these specific symbolic "circuits" scale to billion-parameter LLMs and whether transformers can handle more complex algebraic structures like rings, fields, or infinite groups using similar in-context variable manipulation.

Summary updated

BibTeX
@inproceedings{todd2026context,
  author = {Eric Todd and Jannik Brinkmann and Rohit Gandikota and David Bau},
  title = {{In-Context Algebra}},
  booktitle = {International Conference on Learning Representations},
  year = {2026},
  url = {https://openreview.net/forum?id=J2peqXPQbB},
}
Figure