How K-Composition and Induction Circuits Drive In-Context Learning in AI
Mechanistic interpretability researchers have isolated the exact two-layer attention mechanism that allows language models to recognize and copy patterns. By passing information through the residual stream, these circuits enable models to adapt to novel prompts without updating their underlying weights.
By Harper Lane
In short
- Induction circuits allow language models to perform in-context learning by detecting and copying token sequences directly from the prompt.
- The mechanism relies on K-composition, where a second-layer attention head uses information written by a first-layer head to find historical patterns.
- This process operates entirely within the residual stream, enabling the model to adapt to novel instructions without altering its frozen neural weights.
In this article
The binding constraint of any deployed large language model is that its memory is frozen. Once training finishes, the billions of neural weights that define the model cannot change, meaning it cannot learn new facts by updating its parameters. The architecture is entirely static during inference.
Yet, when a user types a prompt with a completely made-up word or a novel formatting pattern, the model learns it instantly. It adapts to the context without a single parameter update, a phenomenon known as in-context learning. This capability allows models to perform tasks they were never explicitly trained to do.
For years, how a static matrix of numbers could dynamically learn on the fly remained a black box to developers. Now, mechanistic interpretability researchers have isolated the exact mathematical engine responsible: a two-layer mechanism called an induction circuit. This discovery provides a physical explanation for an emergent behavior.[1][3]
By tracing the flow of data through these circuits, researchers found that models use a process called K-composition to temporarily rewire their own attention. It allows the model to detect and copy token sequences directly within the prompt, effectively building a temporary memory bank.[2]
The Anatomy of the Residual Stream
To understand how induction circuits work, one must first look at the transformer's central highway, known as the residual stream. This is a high-dimensional vector space where every token in the prompt maintains its own running state, constantly updated as it passes through the network.[2]
As information flows forward through the model's layers, attention heads read from this stream and write new information back into it. The stream acts as a shared memory buffer, accumulating context and enriching the mathematical representation of each word as it moves deeper into the architecture.
"All components of a transformer communicate with each other by reading and writing to different subspaces of the residual stream," explains the foundational 2021 paper by Elhage et al. on transformer circuits. This additive writing is the foundation that makes complex multi-layer behaviors possible.[2]
Crucially, attention heads operate in two parallel tracks to manage this flow. The Query-Key (QK) circuit determines where the head looks by calculating attention scores, while the Output-Value (OV) circuit determines what specific information it copies from the target back to the current token's residual stream.[2]
Step One: The Previous Token Head
The induction circuit requires a minimum of two attention layers to function, making it impossible for single-layer models to learn in-context. The process begins in the first layer with a specialized component known as a "previous token head," which performs a highly specific copying task.[1][2]
When the previous token head looks at a sequence, its QK circuit is hardwired to attend exactly one position backward. If the prompt contains the sequence "cat sat," the head at the "sat" position looks exclusively at the preceding "cat" token, ignoring all other context.[1]
Once it focuses on the preceding token, the head's OV circuit copies the identity of that token into the current token's residual stream. After this layer executes, the token "sat" carries a mathematical label advertising that its predecessor was "cat," fundamentally altering its representation.[1][2]
This operation enriches the data passing through the network. The model is no longer looking at isolated words; each position now holds a composite representation of both itself and the token that came immediately before it, setting the stage for complex pattern matching.[4]
Step Two: K-Composition in Action
The magic of in-context learning happens in the second layer, where the actual "induction head" resides. This head uses the enriched data from the first layer to perform a sophisticated pattern-matching operation called K-composition, linking the two layers into a single functional circuit.[1][3]
K-composition occurs when a layer-two head uses the output written by a layer-one head to construct its Key matrix. Instead of just looking at the raw token, the induction head's keys now include the predecessor information, changing what each position advertises about itself.[2]
Imagine the prompt later repeats the word "cat" and asks for the next word. The induction head's Query at this new "cat" position searches backward through the sequence, looking for any token whose Key explicitly advertises the label "predecessor = cat."[1]
Because the previous token head already wrote "predecessor = cat" into the residual stream of the original "sat" token, the induction head finds a perfect mathematical match. The QK circuit locks onto "sat" as the target, ignoring the rest of the sequence.[1][4]
Copying the Future
With the attention pattern locked onto the correct historical token, the induction head executes its final move. Its OV circuit copies the identity of that target token—"sat"—and writes it into the current position's residual stream, preparing the model to generate its prediction.[1]
The model then translates this updated residual stream into its final output, predicting "sat" as the next word. It has successfully recognized a pattern, retrieved the historical continuation, and copied it, all without altering a single static weight in its billions of parameters.[1][3]
"Induction heads search the context for previous instances of the present token, attend to the token which would come next if the pattern repeated, and increase its probability," notes the 2022 Anthropic study by Olsson et al., which first formalized the mechanism.[1]
This mechanism explains why large language models are so adept at few-shot prompting. When a user provides three examples of translating English to French, the induction circuits simply match the formatting prefix and copy the corresponding structural output, executing the task seamlessly.[1][4]
The Phase Change in Training
The discovery of induction circuits also solved a lingering mystery about how language models learn during their initial training phase. Researchers observed that models do not acquire in-context learning capabilities gradually over millions of training steps, as one might expect from a neural network.[1]
Instead, models undergo a sudden "phase change" early in their training run. Over a very narrow window of optimization steps, the model's ability to learn from its context window spikes dramatically, visible as a sharp drop in training loss when processing repeated sequences.[1][3]
By tracking the internal weights of the network, researchers proved that this phase change occurs exactly when the previous token heads and induction heads align to form K-composition circuits. The capability emerges the moment the two layers connect and begin sharing information.[1][2]
In models with only a single attention layer, this phase change never happens. Because K-composition requires one head to read the output of another, a one-layer architecture physically cannot form an induction circuit, permanently capping its reasoning abilities regardless of how long it trains.[1][2]
Measuring the Circuit's Strength
To prove that K-composition is the primary driver of this behavior, researchers do not just observe the model; they actively intervene. By mathematically ablating, or deleting, specific induction heads during inference, they can measure the exact drop in the model's performance.[1]
When the induction heads are removed from a small attention-only model, its ability to perform in-context learning vanishes almost entirely. The model reverts to acting like a basic statistical predictor, unable to follow novel prompt instructions or recognize repeated patterns in the text.[1][3]
Furthermore, researchers can quantify the exact strength of the K-composition between any two heads. They calculate the Frobenius norm of the matrix product between the first head's OV circuit and the second head's QK circuit, providing a concrete metric for how much information flows between them.[2]
High composition scores reliably identify which heads are working together across layers. This mathematical precision allows engineers to map the computational graph of a transformer just as an electrician would trace the wiring in a physical circuit board, moving AI from alchemy to engineering.[2][4]
Bypassing the Weight Bottleneck
Understanding K-composition fundamentally shifts how researchers view artificial intelligence. The neural weights are not merely a database of memorized facts; they are a collection of algorithms designed to manipulate the residual stream in real time, creating dynamic programs on the fly.[4]
By offloading the pattern-matching to the context window, the model bypasses the bottleneck of its frozen parameters. The induction circuit acts like a dynamic RAM module, storing and retrieving temporary variables without needing to write them to the permanent hard drive of its weights.[1][4]
This mechanistic transparency is crucial for AI safety and alignment. If engineers know exactly which attention heads are responsible for copying text, they can monitor those specific circuits to prevent the model from repeating toxic or classified information, intervening before the output is generated.[3][4]
How we did this
- Method
- We traced the matrix multiplication path across two attention layers to isolate how the Query-Key (QK) and Output-Value (OV) circuits interact during K-composition, comparing the residual stream state before and after the previous-token head writes to it.
- What we found
- By composing the output of the first layer directly into the key-computation of the second, the transformer creates a dynamic, context-specific lookup table in the residual stream, effectively bypassing the need to update static model weights to learn new associations during inference.
- What we worked from
- Layer 1 previous-token head output (OV circuit): Residual stream enrichment — Transformer Circuits Thread
- Layer 2 induction head key matrix (QK circuit): Predecessor matching — arXiv
- Limits of this analysis
- This analysis is based on simplified attention-only models; in frontier models with hundreds of layers and MLP blocks, induction heads interact with other components in ways that are not yet fully mapped.
Key terms
- Induction Circuit
- A two-layer mechanism in transformer models that detects repeated patterns in a prompt and copies the subsequent tokens to predict the next word.
- K-composition
- A mathematical operation where an attention head in a later layer uses the output of an earlier layer to construct its Key matrix, allowing it to search based on context.
- Residual Stream
- The central data pathway in a transformer model where every token maintains a running state that attention heads read from and write to.
- In-context Learning
- The ability of a language model to adapt to new tasks or patterns provided in the prompt without updating its permanent neural weights.
- Attention Head
- A component of a neural network that calculates which parts of the input sequence are most relevant to the current token being processed.
Frequently asked
Can a single-layer transformer perform in-context learning?
No. Because K-composition requires one attention head to read the output of another, induction circuits can only form in models with at least two layers. Single-layer models are restricted to basic statistical predictions.
Do induction circuits change the model's weights?
No. The model's neural weights remain completely frozen during inference. Induction circuits operate entirely by manipulating the temporary data in the residual stream, allowing the model to adapt without permanently learning.
How do researchers know which heads are induction heads?
Researchers identify them by calculating the Frobenius norm of the matrix product between heads across layers. They also mathematically ablate, or delete, specific heads during testing to see if the model loses its ability to copy patterns.
Viewpoints in depth
Mechanistic Interpretability Researchers
Focus on reverse-engineering neural networks into understandable algorithms.
This camp argues that large language models should not be treated as black boxes. By breaking down the architecture into discrete, understandable components like induction circuits, they believe we can mathematically prove how a model will behave. They prioritize finding the exact weights and matrices responsible for specific tasks, arguing that true AI safety is impossible without this level of circuit-level transparency.
Capabilities Researchers
View induction circuits as foundational building blocks for scaling advanced reasoning.
For researchers focused on advancing AI performance, induction circuits are proof that transformers naturally develop efficient algorithms for in-context learning. They argue that as models scale, these simple copy-and-paste circuits compose into much more complex reasoning engines. Their focus is on how architectural tweaks—like adjusting the number of layers or attention heads—can encourage the formation of these circuits earlier in training.
AI Safety Advocates
See circuit-level understanding as essential for preventing harmful model outputs.
Safety advocates emphasize the control that mechanistic interpretability provides. If engineers know exactly which attention heads are responsible for copying text, they can actively monitor or ablate those circuits to prevent the model from repeating toxic, biased, or classified information. They argue that relying on behavioral testing is insufficient, and that direct intervention at the circuit level is necessary to align models with human values.
- Mechanistic Interpretability Researchers
- Focus on reverse-engineering neural networks into understandable algorithms to guarantee safety.
- Capabilities Researchers
- View induction circuits as foundational building blocks for scaling advanced reasoning.
- AI Safety Advocates
- See circuit-level understanding as essential for preventing harmful model outputs.
Perspectives this story doesn't cover
- Hardware Engineers
- Commercial AI Deployers
Sources
[1]arXivCapabilities ResearchersIn-context Learning and Induction Heads
Read on arXiv →
[2]Transformer Circuits ThreadMechanistic Interpretability ResearchersA Mathematical Framework for Transformer Circuits
Read on Transformer Circuits Thread →
[3]OpenReviewAI Safety AdvocatesInduction Heads in Transformers
Read on OpenReview →
[4]Factlen Editorial TeamMechanistic Interpretability ResearchersSynthesis by Factlen editorial team
Read on Factlen Editorial Team →
More in Artificial Intelligence
See all →Model Architecture
How Layer Normalization Stabilizes Transformer Training by Standardizing Input Across the Feature Dimension
7 sources
Model Training
How Group Relative Policy Optimization Eliminates the Memory Bottleneck in AI Reasoning Training
6 sources
Model Architecture
How Mixture of Experts Routing Networks Decouple LLM Parameter Count From Compute Cost
5 sources
Structured Generation
How Constrained Decoding Forces Large Language Models to Output Valid JSON
6 sources
Comments
Every angle. Every day.
Get Artificial Intelligence stories with full source coverage and perspective breakdowns, free every day.




