Extracting Algorithms in Pre-trained LLMs: A Case on Hidden Markov Models

Yijia Dai, Zhaolin Gao, Yahya Sattar, Jennifer J. Sun, Sarah DeanarXiv 2026

TL;DR

Researchers from Cornell University have peeked inside the "black box" of Large Language Models (LLMs) to understand how they learn to predict sequences with complex hidden structures. By studying Hidden Markov Models (HMMs), they discovered that LLMs don't use statistically optimal iterative inference; instead, they implement efficient, finite-window approximations called "soft n-grams" that are causally driven by low-dimensional linear signals in their internal activations.

What Problem Does This Paper Address?

LLMs possess a remarkable ability called In-Context Learning (ICL)—they can pick up on patterns in a prompt and continue them without any further training. While we know that they do this, we don't really know how.

Hidden Markov Models (HMMs) provide a perfect "laboratory" for this question. In an HMM, the actual state of the system is hidden, and we only see noisy "emissions." To predict the next token perfectly, a model must integrate all past observations to track the most likely hidden state (a process called belief tracking). Prior theories suggested LLMs might be using spectral methods or gradient-based feature learning, but these ideas hadn't been validated against the model's actual internal "brain waves"—its activations.

Figure 1: Overview of our study.

Methods

The authors tackled this using a three-stage pipeline:

  1. Behavioral Benchmarking: They tested 12 open-weight LLMs (like Llama, Qwen, and Gemma) against classical algorithms to see which one best matched the LLMs' external predictions.
  2. Theoretical Construction: They proved mathematically that Transformers are capable of emulating these algorithms (specifically Gradient Descent and Spectral learning) in-context.
  3. Principal Activations Probe (PAP): This is the paper's core innovation. It combines PCA (to find the most important directions in the model's activations), Ridge Regression (to map those directions to algorithmic states), and Activation Patching. The latter is a causal intervention: they "hot-swap" a specific representation from one sequence into another to see if it forces the model to change its prediction in a predictable way.

Datasets

The study used synthetic HMM sequences rather than natural language to maintain strict control over the underlying "ground truth." * 75 HMM Configurations: They varied transition entropy (how predictable the state changes are), emission entropy (how noisy the observations are), and alphabet size. * Scale: They sampled 4,096 sequences per configuration, evaluating context lengths from 4 up to 4,096 tokens.

Properties of HMMs used to vary task difficulty, including entropy and mixing rates.

Key Insights and Ideas

  • The "Soft n-gram" Strategy: LLMs don't seem to perform the complex, iterative math required for optimal Bayesian inference. Instead, they learn a "soft n-gram" approach—a finite-window approximation that looks at the last few tokens and applies a learned linear map.
  • Regime-Shifting Representations: The model's internal strategy changes based on the data. In "observation-dominated" regimes (where the current token tells you almost everything), it uses simple bigram-like statistics. In "belief-dominated" regimes (where you must track the hidden state over time), it recruits a much richer representation.
  • The Decodability Trap: A major "aha!" moment was finding that algorithmic information is often present in early layers (decodable) but isn't actually used by the model until much later layers. This proves that simply "finding" a concept in a model doesn't mean the model is actually using it yet.

Key Results

  • Convergence: Across almost all settings, LLM performance converges toward the "Oracle" (the theoretical mathematical optimum) as the context length increases.
  • Causal Sufficiency: Using the PAP method, the authors showed that a tiny subspace (often just 8 dimensions) is enough to causally steer the model's predictions.
  • Model Differences: The Qwen family showed the most consistent convergence to optimal predictors. In contrast, OLMo models often failed to use the information they had internally, showing high "decodability" but failing causal intervention tests.
  • Layer Specialization: Computation follows a U-shape; error in recovering the internal "operator" (the logic) is low in early and late layers but high in the middle, suggesting the "thinking" happens in the mid-to-late stages.

Limitations and Future Work

The authors acknowledge that their work is restricted to synthetic HMMs with small alphabets. While this provides a clear "ground truth," natural language is far more complex and hierarchical.

Future research directions include:

  • Extending the PAP method to natural language tasks with latent structures.
  • Moving from layer-level analysis to circuit-level analysis, identifying the specific "neurons" or "heads" responsible for these algorithmic steps.
  • Investigating why some models (like OLMo) fail to causally utilize the information they clearly represent internally.

Summary updated

BibTeX
@misc{dai2026extracting,
  author = {Yijia Dai and Zhaolin Gao and Yahya Sattar and Jennifer J. Sun and Sarah Dean},
  title = {{Extracting Algorithms in Pre-trained LLMs: A Case on Hidden Markov Models}},
  year = {2026},
  eprint = {2607.22646},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2607.22646},
}
Figure