A Circuit for In-Context Learning
This paper identifies and causally validates a specific, conserved neural circuit in transformer models that enables in-context learning for few-shot sentence classification by implementing a gated associative memory mechanism where middle-layer heads store label-class mappings and upper-layer heads retrieve them based on query matches.
Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer
Imagine a language model as a vast library of human knowledge, trained on billions of sentences. When you ask it a question, it usually answers based on what it has memorized. But there is a special trick it can perform called "in-context learning." If you show the model a few examples of a new task right before asking your question—like showing it a dozen sentences paired with made-up labels—it can instantly learn the pattern and apply it to your new sentence, without ever changing its internal weights or being retrained. It figures out the rule in a single moment of reading. For years, scientists have known this happens, but they did not know how the machine actually does it. They could see the result, but the gears turning inside the dark box remained a mystery.
A team of researchers has now opened that box and mapped the exact machinery behind this ability. By studying three different large language models, they discovered that the model does not "learn" a rule in the traditional sense. Instead, it builds a temporary memory inside the prompt itself, using a specific set of attention heads—specialized processing units within the network—to act as writers, readers, and gatekeepers. They found that the model treats the examples you give it as a key-value memory system. When you show a sentence followed by a label, the model writes the identity of that label into a specific slot in its memory, while simultaneously writing a "key" that describes the type of sentence it just saw. Later, when the model encounters the final question, it computes a code for that question and uses it to search its temporary memory, matching the question's type against the keys it stored earlier to retrieve the correct label.
The researchers traced this process step by step, using a method that allowed them to swap parts of the model's internal activity between different prompts to see which pieces were essential. They found that about twenty middle-layer heads act as writers, taking the class of each demonstration sentence and depositing it into the memory slot of the following label. Then, about ninety upper-layer heads act as readers. These readers scan the memory, looking for slots where the stored key matches the class of the current question. When they find a match, they copy the label from that slot to the final answer. However, this retrieval system is not always active. The researchers discovered a "gate" that decides whether to read the memory at all. This gate checks if any label in the examples has appeared more than once. If a label repeats, the gate opens, allowing the readers to search the memory. If no label repeats, the gate stays closed, and the model defaults to ignoring the examples, a behavior that prevents it from guessing randomly when no clear pattern exists.
To ensure the model answers correctly, another set of about two dozen heads acts as "brakes." These heads work against the most recently seen label, preventing the model from simply copying the last example it read, a common bias known as recency. Without these brakes, the model would often ignore the pattern and just repeat the final label it saw. The researchers also found that the model relies on a "sink," a default position where attention goes when there is nothing specific to focus on. The gate works by lowering the attraction to this sink, forcing the readers to look at the actual memory slots instead.
What makes this discovery particularly robust is that the researchers did not just observe the behavior; they proved it by cutting connections. When they blocked the writers, the model could not store the keys. When they blocked the readers, it could not find them. When they removed the gate, the model tried to read the memory even when no pattern existed, leading to errors. They even built a tiny, hand-coded version of this circuit from scratch, and it worked exactly as predicted, confirming that these specific parts are sufficient to perform the task. The study also revealed that while the specific heads performing these jobs vary slightly between different models, the overall architecture of the circuit remains the same.
Perhaps the most surprising finding concerns how the model handles the size of the task. The researchers found that the model does not rely on a single powerful head to do all the work. Instead, the task is shared among many heads working together. No single reader head is strong enough to produce the final answer on its own; the model needs the combined output of dozens of them to generate a confident prediction. This explains why the model uses such large populations of heads for this task. Furthermore, the study showed that as models get larger, they do not invent new parts to solve the problem. Instead, they simply strengthen the existing populations. The "brakes" that stop the model from being biased by the most recent example become stronger in larger models, which is why bigger models are better at following the pattern in the examples rather than relying on their own pre-trained knowledge of what the words usually mean.
This work changes our understanding of how artificial intelligence learns from context. It suggests that the model is not magically deducing rules or performing complex mathematical updates. Instead, it is performing a highly organized, mechanical lookup. It writes information into a temporary memory, checks if a pattern exists, and then retrieves the answer based on that pattern. The researchers showed that this mechanism is consistent across different model sizes and even survives when the model is fine-tuned for other tasks. The ability to learn from a few examples is not a mysterious emergent property, but a concrete, traceable circuit built from identifiable components that can be measured, manipulated, and understood.
Drowning in papers in your field?
Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.