Inside the Neural Circuit Powering In-Context Learning

A Stanford study maps the exact attention circuit enabling in-context learning in transformers, revealing how hidden layers construct transient memory.

PUBLISHED //
DURATION // 14 MIN READ
Ultrawide monitor showing an abstract node graph on a dark desk with a closed notebook and pen
FIG.01 // VISUAL DOCUMENTATION Ultrawide monitor showing an abstract node graph on a dark desk with a closed notebook and pen

Language models possess an extraordinary ability to execute new tasks directly from prompt demonstrations without undergoing parameter updates or backpropagation passes. When a user supplies three examples of classification with randomized labels and appends an unseen query, the model outputs the matching label with high reliability. While in-context learning has anchored modern prompting practices since the initial scaling of transformer architectures, the exact sequence of computations taking place within the network during that single forward pass has remained partially understood. Early research uncovered isolated components such as induction heads, yet how these discrete elements coordinate to bind arbitrary categories to tokens remained subject to debate.

A research preprint by Andrew Gao and William Gao from Stanford University, titled A Circuit for In-Context Learning, provides a comprehensive mapping of this internal mechanism. By evaluating transformer models performing few-shot sentence classification under arbitrary and flipped label assignments, the researchers isolated the complete operational circuit responsible for in-context adaptation. Accompanied by replication scripts and an interactive visualization tool in their GitHub repository, the study establishes that transformers construct an active key-value memory directly across label positions in the residual stream. This system relies on specialized attention heads to write latent categories, read matching labels, verify label repetition through early gates, and suppress recency bias before generating output probabilities.

Mechanistic interpretability beyond isolated induction heads

To determine how a neural network processes prompt examples, interpretability research has historically focused on induction heads: two-layer attention circuits that search for previous occurrences of a current token and copy the subsequent token. While induction heads explain simple pattern matching, repetition, and prefix copying, they fail to explain complex few-shot classification. In classification tasks, the model must categorize a complex natural language sentence, map that sentence to a semantic class, and link that class to an arbitrary label symbol that shares no semantic connection with the input text.

When arbitrary labels are assigned (for example, mapping enthusiastic movie reviews to the word “zebra” and critical reviews to “tulip”), the model cannot rely on pre-trained lexical associations. It cannot retrieve a stored memory that connects “zebra” to positive sentiment. Instead, the network must perform dynamic variable binding within the boundaries of a single forward pass.

The Stanford study shows that this variable binding is accomplished by an interconnected circuit spanning multiple functional depths. The model routes this computation through a distinct set of attention heads whose roles can be causally isolated and mathematically modeled.

The four functional components of the in-context circuit

The architecture of the in-context learning circuit divides across four stages distributed through the network’s layers. Each stage fulfills a distinct sub-task necessary to complete the associative mapping:

First, middle-layer attention heads operate as key-value memory writers. When the transformer processes each demonstration example in the prompt, these middle-layer heads evaluate the semantic content of the demonstration sentence. They write the resulting latent class vector into the residual stream at the precise token position of the subsequent label. Through this write operation, the label token position is converted into an addressed storage slot. The token’s lexical representation serves as the stored value, while the sentence category written into its residual channel serves as the lookup key.

Second, upper-layer attention heads function as associative readers and copy mechanisms. When the forward pass reaches the final query sentence, these reader heads compute a query vector representing the semantic category of the unlabelled input. They attend back across earlier token positions in the prompt, comparing the query vector against the keys deposited at earlier demonstration label positions. When a reader head identifies a matching category key, it copies the corresponding label identity into the query’s residual vector, transferring the selected token representation into position for final unembedding projection.

Third, an early-layer gating signal determines whether the reading mechanism should activate. If a model executed associative retrieval unconditionally on every prompt, ordinary text generation without demonstrations would suffer from constant interference, as upper layers would attempt to match non-existent keys. The Stanford team identified a specialized sub-circuit in early layers that tracks whether any demonstration label repeats across the context window. When duplicate labels are detected, the gate emits an activation signal downstream that enables the upper-layer reader heads. If no label repeats, the gate remains closed, routing the reader heads to attend to the sequence start token (the attention sink) and preventing spurious associative lookups.

Fourth, a dedicated population of attention heads acts as recency suppression brakes. Autoregressive language models possess an inherent structural bias toward repeating tokens that appeared most recently in the prompt sequence. In few-shot classification, this tendency would cause the model to favor whichever label appeared in the final demonstration example, regardless of semantic match. The brake heads counteract this position effect by projecting negative activation vectors against the most recent label, balancing the competition among stored values and allowing semantic similarity to determine the output.

Dynamic memory in the residual stream

The realization that transformers implement key-value memory over label positions clarifies the computational role of the residual stream. In standard transformer formulations, the residual stream is often conceptualized as a passive communication bus that accumulates token embeddings, positional encodings, and successive layer outputs.

Under the mechanistic circuit model, the residual stream functions as a dynamic read-write scratchpad. Because self-attention allows tokens to communicate across positions, attention heads can treat token locations as arbitrary memory registers. The label token position is particularly suited for this purpose: it marks the boundary between consecutive demonstration examples, providing a consistent structural anchor across varied prompts.

During the forward pass, intermediate layers continuously write information into this register. The middle-layer writer heads do not modify the model parameters; instead, they alter the high-dimensional activation vectors that travel through the network. When the forward calculation completes, these activations dissipate. The model returns to its baseline state, ready to host an entirely different task in its next forward pass without retaining parameter drift or risk of weight interference.

Formalizing readout through a two-constant softmax model

Beyond qualitative identification of the circuit, the authors developed a mathematical formulation of how upper-layer reader heads perform label readout. They constructed a softmax readout model where the attention logit for each stored label position is computed as a function of query-key semantic similarity and head-specific scaling constants.

The formulation characterizes each reader head using two primary constants: a query-key projection strength parameter that governs how sharply the head distinguishes between matching and non-matching category vectors, and an offset bias that balances the head’s default attention allocation between stored memory slots and the attention sink.

When fitted on a single demonstration distribution using standard sentiment benchmarks, this two-constant model successfully predicted the output distributions of the full neural network on completely different prompt distributions, including alternative vocabulary choices and varied sentence structures. This predictability confirms that the reading heads operate according to stable, mathematically tractable decision boundaries.

Causal verification via activation patching and head ablations

To confirm that the identified heads are strictly necessary and sufficient for in-context learning, the researchers implemented a rigorous causal intervention protocol using activation patching and structured ablations across Llama-3.2-3B and SmolLM models.

Activation patching operates by running two distinct versions of a prompt through the network simultaneously: a clean prompt with consistent demonstrations, and a corrupted prompt where demonstration labels are permuted or mismatched. By intercepting specific hidden states and swapping activations between the two runs, researchers can observe whether a specific head directly controls the final prediction.

When activations from the identified writer heads in a corrupted prompt were patched into a clean run, the model immediately altered its final prediction to match the corrupted label assignment, verifying that the writer heads supply the primary information driving the decision. Conversely, patching baseline heads outside the circuit produced no measurable deflection in output probabilities.

Structured ablation experiments further established necessity. When the designated reader heads were computationally silenced, the model’s accuracy on arbitrary-label few-shot tasks collapsed to chance levels. Importantly, silencing these reader heads did not degrade the model’s standard language modeling capabilities or its perplexity on general prose, demonstrating that the ablated heads perform specialized functional duties dedicated to in-context retrieval.

The team also measured the conservation of this circuit across scales. Investigating model families from 0.6 billion parameters up to 8 billion parameters revealed that the same functional division of labor appeared at every scale. While larger models allocate more total attention heads to parallelize reading and writing, the underlying four-stage topology remains conserved. In addition, when transformers were initialized with random weights and trained from scratch on standard pre-training corpora, the optimization trajectory repeatedly converged on this exact circuit structure. The architecture also survived post-training alignment and instruction tuning without functional degradation.

Contextual entrainment and distractor vulnerability

While the in-context circuit provides reliable task adaptation under clean conditions, its reliance on associative attention creates identifiable vulnerabilities when prompts contain extraneous or conflicting data. A related study by Jinghan Zheng and Jiajun Shi, titled From Relation-Specific Heads to Shared Interventions: Contextual Entrainment in Language Models, examines what happens when models encounter conflicting context patterns.

Contextual entrainment describes a failure mode where attention heads unintentionally reuse irrelevant tokens from earlier prompt context instead of executing intended instructions. Zheng and Shi demonstrated that models rely on shared, structured intervention targets across multiple relation types instead of maintaining separate heads for every individual relation.

When a prompt includes irrelevant distractor sentences or formatting noise, those extraneous tokens compete directly for the attention capacity of writer and reader heads. If a distractor sentence contains words that resemble demonstration labels, the early-layer gating mechanism can register false duplicate-label signals, partially opening the reader gate prematurely. Middle-layer writer heads may also deposit spurious category vectors into non-label positions, polluting the residual stream and causing reader heads to retrieve incorrect values. This mechanism explains why prompt clarity, delimiter consistency, and token hygiene exert such an outsized influence on few-shot accuracy.

Intersections with probing, context windows, and inference limits

The discovery of explicit in-context circuits integrates cleanly with broader empirical findings across language model research. In our previous analysis of perturbation probing on model safety fragility, researchers discovered that complex behavioral rules and safety guardrails frequently depend on a localized cluster of attention heads and MLP layers. The observation that in-context learning similarly concentrates into a defined circuit of writers, readers, gates, and brakes reinforces the view that modern transformers organize complex cognitive functions into localized computational sub-graphs.

This organization also interacts with sequence scaling dynamics. As examined in our overview of LLM context window evolution, expanding context windows from thousands of tokens to hundreds of thousands creates significant signal-routing difficulties. As demonstration examples are placed farther away from the query position, the spatial distance across the sequence increases attention entropy. If early-layer gating signals diminish over extended sequence lengths, or if intermediate attention sinks draw excessive probability mass, upper-layer reader heads can fail to resolve the correct label register, leading to performance degradation in long-document contexts.

These circuit mechanics also inform our understanding of reasoning efficiency. In our investigation of LLM overthinking and test-time compute, evidence showed that models frequently generate redundant thinking tokens that dilute sharp internal activations. In reasoning models that output extensive intermediate reasoning traces before producing a final answer, each extra token passes through the residual stream. If intermediate reasoning steps introduce drifting semantic vectors, they risk corrupting the clean key-value bindings established during early demonstration processing. Preserving concise reasoning traces directly protects the integrity of the associative readout mechanism.

Editorial perspective: implications for prompt engineering and model architecture

The empirical verification of the in-context learning circuit marks an important transition from speculative prompt crafting to principled engineering. For years, practitioners treated prompting as an intuitive art, adjusting words and punctuation without visibility into why certain formulations worked while others failed. The Stanford findings provide concrete rules grounded in hardware-level activations.

Our analysis points to three critical implications for prompt designers:

First, label formatting must remain completely uniform across demonstrations. Because the early-layer gating signal requires label repetition to unlock downstream reader heads, any formatting inconsistency that causes labels to tokenize differently will impair the circuit. For example, if the first demonstration ends with ” Label: Positive” and the second ends with ” Label: positive” or ” Label:Positive”, the tokenizer may assign distinct token IDs to the label strings. To the early-layer duplicate detector, these tokens appear non-identical. As a consequence, the gating signal fails to fire at full strength, the reader heads remain directed toward attention sinks, and the model defaults to pre-trained lexical associations when the prompt demonstrator fails to trigger the reader gate.

Second, demonstration ordering must be actively balanced to manage recency suppression. Although transformers possess dedicated brake heads designed to inhibit the most recently seen label, these heads operate within bounded numerical ranges. If an unbalanced prompt concludes with two or three consecutive demonstrations belonging to the same category, the accumulated activation can overwhelm the brake heads, causing the model to exhibit strong recency bias. Practitioners structuring few-shot prompts should rotate categories across positions to ensure that final slots do not bias the output distribution.

Third, the discovery of explicit in-context memory raises strategic questions regarding the division between prompting and parameter fine-tuning. In-context learning provides remarkable agility: an enterprise can deploy a single base model and adapt it to dozens of specialized classification tasks purely by changing prompt templates, avoiding model proliferation and training costs. However, running the in-context circuit incurs recurring compute costs on every single forward pass. Every inference query must process the full demonstration sequence, consuming memory bandwidth and FLOPs to repeatedly write and read the same key-value bindings.

For high-volume production pipelines, maintaining thousands of identical in-context demonstrations across millions of API calls represents unnecessary overhead. A more efficient engineering paradigm involves using in-context demonstrations to validate task behavior during development, and then distilling the learned mapping into lightweight parameter adapters (such as low-rank adaptation matrices) for deployment. By baking the associative mapping directly into feedforward weights, teams can eliminate demonstration tokens entirely, reducing latency and cutting token costs without sacrificing classification accuracy.

Technical guidance for system architects

Based on the verified mechanics of the in-context circuit, engineering teams building automated agent harnesses and inference pipelines should adopt the following operational practices:

  1. Standardize structural delimiters: Use simple, consistent delimiters (such as standard colons or dedicated XML tags) between inputs and labels. Avoid decorative markdown, varied whitespace, or conversational filler between demonstration pairs, as extraneous tokens increase attention dispersion in middle-layer writer heads.

  2. Place demonstration examples in close proximity: Keep demonstration sentence-label pairs tightly packed. Inserting long explanatory notes or chain-of-thought rationales between a demonstration sentence and its label dilutes the writer head’s ability to deposit the sentence category directly into the label token’s residual stream.

  3. Preserve clean prompt prefixes: Because reader heads fall back to attending to sequence start positions when gating criteria are not met, the opening tokens of a prompt determine the baseline reference state. Overly complex or noisy system headers can distort attention sink dynamics, impairing the stability of the fallback mechanism.

  4. Decouple pattern binding from reasoning: In-context circuits are specialized for associative variable binding and category mapping, while multi-step logical deduction requires distinct computational scaffolding. When architecting complex agent workflows, developers should decompose multi-step tasks into modular stages, using clear, concise demonstrations for classification steps and offloading complex planning to structured tools.

The mapping of the in-context learning circuit demonstrates that transformer behavior, despite its immense scale, follows organized computational laws. As researchers continue to unveil the internal pathways through which these networks process information, the boundary between machine learning research and systems engineering will continue to narrow, enabling more predictable, efficient, and controllable language models.