Profile

Settings

Loading…

← Papers

Help

FAQ

Which features require signing in?

You can read the commentary, paper info, Model Structure and related papers of public papers without signing in. Sign in to manage your account or contact us about your inquiries.

Can I upload a PDF file?

Upload papers with an arXiv or DOI link; the commentary is public to everyone. Papers whose source and terms of use can't be verified, so the commentary can't be made public, can't be uploaded. Uploading PDF files and keeping papers private are for subscribers only, and a paper uploaded as a file is saved as private only. Searching for official info by title runs only when you press the button.

Are the figures and AI commentary accurate?

Figures come only from the original paper, and only those whose terms of use are verified are shown. The commentary is written by AI and may be wrong. Check important claims, numbers and figure context against the evidence in the original paper.

I want to report a copyright issue.

Terms of use and the source of the original paper are on the Paper info tab. If there's a problem, choose "Paper content, figures and copyright" under Contact us and send the paper link, the part in question, and evidence that you hold the rights. Receiving your report doesn't confirm any rights or remove anything right away. You can see the result of the review under My inquiries.

I forgot my password.

If you signed up with email, use Forgot password in the sign-in window to reset it with your verified email. If you signed up with Google, Naver or Kakao, change your password with that service.

What should I include in an inquiry?

Tell us which screen you were on and what you were doing when the problem happened, along with the paper link. Don't include personal information such as passwords, API keys, verification codes or phone numbers. Inquiries and replies are stored in Refivu and you can see them under My inquiries.

How do I delete my account?

In Settings, under My account, press Delete account. Review what will be deleted and complete identity verification to delete your account. For other requests, choose "Account" under Contact us.

Paper commentary

A Circuit for In-Context Learning

Researchers trace how in-context learning composes into a gated key-value lookup circuit

  • Problem Prior work found candidate attention heads behind in-context learning but not how they compose. With arbitrary labels the model cannot have memorized the mapping, so it must learn it from the prompt in one forward pass.
  • Method The authors write each demonstration's class into its label token as a key, then read it with a gated lookup. Middle-layer heads store the class key, and upper-layer readers match the query's class against those keys and copy out labels.
  • Results The named heads alone reproduce the behaviour, unlike a random head set of the same size.

Figure 2: The circuit for few-shot in-context learning in Qwen3-4B-Base. Prompt tokens run down
the left (sentences Si, labels Li, the query Q and its arrow EN…

Figure 2 : The circuit for few-shot in-context learning in Qwen3-4B-Base. Prompt tokens run down the left (sentences Si, labels Li, the query Q and its arrow END). Boxes are classes of attention heads, with layer.head numbers beneath. Writer heads give each label token a key (the class of its sentence) alongside its value (the label). At END, reader heads match the query’s class against those keys and copy the matching labels, copier heads write the logits, and brake heads write against the most recent label. The gate g, assembled by the pooling heads, opens the read by lowering the readers’ attention-sink logit (dashed). Sections 3–8 give the intervention behind each arrow.

Original paper · CC BY 4.0 · Andrew Gao, William Gao · A Circuit for In-Context Learning · Original paper figure · Extracted the figure area from page 5 of the original PDF.

Why the lookup is hard to pin down

With arbitrary labels, a model can't have memorized the mapping, so it has to pick the mapping up from the prompt within one forward pass. Earlier work had found candidate parts, such as heads that copy tokens and heads that carry a compact task description to the query. Label tokens were known to be where task information accumulates, and the recency bias was traced to query–key similarity with label positions.

Several questions stayed open, and the authors list them directly. It was also unclear which heads read that content and what switches the read on and off. Finally, it was unknown how the result reaches the logits, and which parts are single nameable heads versus populations that act in sum. The paper studies two-class label retrieval and function selection in Qwen3-4B, SmolLM3-3B and Llama-3.2-3B, each a base model of three to four billion parameters.

The authors answer these questions with a key-value memory built inside the prompt, which they describe as the working picture. Each label token becomes a slot, with its value set to the label's identity and its key set to the class of the sentence it followed. About twenty middle-layer heads deposit that key by writing the sentence's class into the label token's residual stream.

Upper-layer readers then compare the query's class against those keys, so the lookup is keyed on class rather than on a single token. A separate early-layer gate, pooled from duplicate-token heads, decides whether that read is switched on at all.

A key-value memory built inside the prompt

The model stores each demonstration as a slot at its label token, with the label identity as the value and the sentence class as the key. About twenty attention heads in the middle layers read each sentence and write its class into the residual stream of the following label token. The paper cites K-composition in the sense of Elhage et al.

2021 for this carry-forward of the class into the slot. Nothing in this step is trained; the heads simply carry the class forward into the slot.

At the query, about ninety upper-layer readers compare the query's class with the stored keys through an ordinary softmax over positions. The readers then copy out the labels whose keys match, so they act like induction heads that match on a class feature rather than on a token. The authors count the slots in a twelve-demonstration prompt as the place where this matching is tested, with the attention sink at position zero taking part in the same softmax.

The paper traces these stages on a single twelve-demonstration prompt, with rows as layers and columns as tokens, in Figure 1.

Figure 1: One twelve-demonstration prompt, stage by stage. Columns are tokens (sentences si,
arrows, labels and the query q) and rows are layers of Qwen3-4B. E…

Figure 1 : One twelve-demonstration prompt, stage by stage. Columns are tokens (sentences si, arrows, labels and the query q) and rows are layers of Qwen3-4B. Each coloured block marks the tokens and layers where a stage acts: the duplicate detectors and the encoder early on, the writers into the label tokens in the middle layers, and then, at the query arrow, the query code, the gate, the readers, the brakes and the readout band. Later sections give the intervention behind each block.

Original paper · CC BY 4.0

That direction lowers the logit the readers assign to the attention sink, and the authors call the resulting outcome the null action. A separate population of about two dozen brake heads writes against the most recently seen label, which removes a recency bias.

Each step is tested with a matched control: the authors build twin prompts that differ in one span and move activations between them. They also knock out heads against random-head baselines and steer the residual stream at the strength the model itself uses. Before the readers, the paper also checks that the contributions of all heads and MLPs add up to the model's logit to within 0.05 nats.

Testing a softmax law for the readers

The readers are ordinary softmax heads, so the attention a reader pays to matching slots should follow a closed-form expression. The expression uses the number of slots N, the number of matching slots nm, the matching-slot logit L and the sink logit Lsink. Two of the constants enter only through their sum, which is why only two of the three per-head numbers are identifiable.

The formula gives the matching mass m as nm times e to the L, divided by nm times e to the L plus N minus nm plus nrest plus e to the Lsink.

Keeping each head's three constants and setting nm to 1 predicts the battery with one slot per class. That held-out prediction has a median error of 0.07 in Qwen, with 69% of heads within 0.1, and 0.04 in Llama, with 83% within 0.1. Predicting in the other direction breaks down, as the eighteen-demonstration test shows. On eighteen-demonstration prompts, with three to nine slots per class, the observed medians were 0.15 in Qwen and 0.13 in Llama.

The authors set a criterion of a median error below 0.07, and 92 to 94% of heads were over-predicted.

The attention patterns explain the failure, because each head's sink mass stays fixed as slots are added. A head's total attention to the matching slots is also nearly constant, so extra matching slots divide a fixed class budget. L29h11 puts 0.18 on the matching slots with six of them and 0.19 with nine.

Figure 9: Within a regime the read follows a softmax law, and the law transfers to fewer slots per class
but not to longer prompts. Each point is one head at o…

Figure 9 : Within a regime the read follows a softmax law, and the law transfers to fewer slots per class but not to longer prompts. Each point is one head at one slot count, showing the measured matching- slot mass against the prediction of Equation 1 from twelve-demonstration constants. (a) Qwen, in the fitted regime. (b) Qwen and Llama, one slot per class (held out). (c) Qwen and Llama, eighteen demonstrations (held out; the prediction fails). Sharp readers sit on the line and diffuse heads fall below it.

Original paper · CC BY 4.0

The failure also fixes two facts the rest of the paper depends on. Because each head's class budget is fixed, the readers must act together to produce a large answer. The next question is what switches the read on, which the authors answer with a repeat signal.

A repeat signal opens the gate

The gate is a direction computed from whether any label has repeated, and it is pooled from duplicate-token heads. Sixteen random heads move it by at most 0.7, so the effect is specific to the duplicate-token heads. The rest of the separation comes from somewhere the authors have not located, so half of it stays unexplained.

The gate doesn't act on the slots themselves; it acts on the readers' sink logit.

The sink is the position where a reader puts the attention it doesn't spend on the slots, so lowering its logit pushes the read onto the slots. A reader that puts most of its attention on the sink copies nothing, which is the null action the gate controls.

Cutting connections between the parts

The earlier sections showed the parts side by side, so the authors cut the connections directly in Qwen3-4B. Patching the twenty readers' outputs at the query from the class-flipped twin into the intact run carries −1.08 of the twin's −1.80 on rare-word labels. On symbol labels the same patch carries −0.54 of −0.95, so sixty percent of the difference passes through those twenty heads.

Holding the inputs of every late attention head in layers 30 to 35 at their intact values drops the effect to −0.81 and −0.35. Holding the late MLP inputs as well brings it to −0.39 and −0.19.

Figure 16: The answer as a sum of bands. Left: the intact twelve-demonstration margin in Qwen,
built up band by band; the copier band of layers 30–35 writes al…

Figure 16 : The answer as a sum of bands. Left: the intact twelve-demonstration margin in Qwen, built up band by band; the copier band of layers 30–35 writes almost all of it. Right: the change under the exact class flip. The copier band carries most of the swing and the readers a quarter, and the late MLPs push the other way.

Original paper · CC BY 4.0

The copier band of layers 30 to 35 writes almost all of the intact twelve-demonstration margin in Qwen. The gate also acts partly through the readers' queries, so the authors knocked out the sixteen duplicate-token heads and added g back at layer 23. Removing g from the twenty readers' query inputs alone lowers the rise to +0.43 and +0.61.

A random direction removed from the same queries changes nothing, so a quarter to a third of g's effect travels through the readers' queries.

The authors multiplied the attention output of three readers, 24:21, 29:11 and 22:20, by two, on prompts drawn with a fresh seed. That raised the margin by +0.40 on rare labels and +0.25 on symbol labels, against +0.04 for three random heads. Knocking out the other seventeen readers costs −0.89 and −0.50, and amplifying the three by four recovers only to −0.46 and −0.44.

With all 189 named heads kept, the circuit alone scores +0.89 on symbol labels and +1.26 on rare labels under resampling. A random set of the same size gives −0.64 and −0.98, so the named set, not the head count, carries the function.

Scale and post-training in one model family

The authors ran the same census on Qwen3 base models at 0.6B, 1.7B, 4B and 8B, giving each size its own census with its own head identities and gate direction. The authors had predicted a monotone rise in the brakes, so that part of the prediction missed.

The meaning-following part tracks the Wei et al. result: P/R falls from 1.03 at 0.6B to 0.73 at 8B, while the flipped-label margin rises from −0.03 to +1.05 nats.

After post-training, Qwen3-4B instruct keeps the circuit: the base model's 189-head set, carried over unchanged, keeps 79% of the demonstration-following part R. Both R and P are about 2.5 times larger in the instruct model, with R at 7.30 and P at 6.75, against base values of 2.87 and 2.52. The unnamed heads of layers 16 to 27 now raise P by 4.9 nats when removed, against about 2 in the base model.

The authors read this as post-training changing the gain and adding braking around the readers, not rewiring them.

Limitations

They didn't test binding among many classes or longer demonstration lists beyond what the law covers.

The read law is the first prediction that failed, and it fails when prompts get longer. Fitted on twelve-demonstration constants, the law was tested on eighteen-demonstration prompts with three to nine slots per class. The median errors were 0.15 in Qwen and 0.13 in Llama, against a criterion below 0.07, and 92 to 94% of heads were over-predicted.

The cause is that a head's null mass and its total matching mass stay roughly constant as slots are added, so new slots share a fixed budget. The authors can describe this saturation but can't yet explain it, and the fitted parameters are not separately identifiable.

A second failed prediction concerns training: the authors expected many-shot fine-tuning to strengthen the brakes. So where the brakes come from in training remains open. The authors also expected the double gate dose to beat the single dose at every size, and at 8B the two tied.

The failures of the account point to a shared lesson about how much of the mechanism is still unexplained. The authors read the link between missing keys and failed retrieval as correlational, so it is a pointer rather than proof that a missing key is the cause. For classes that aren't linearly readable at the arrow, such as parity and digit rules, writing the key in by hand raised accuracy from 50 to 71%.

Paper info

Authors
Andrew Gao, William Gao
Venue
arXiv · Preprint
First published
2026-11-3
Added
2026-11-6
Updated
2026-11-3
Version
v1
arXiv
2611.05344
Category
cs.CL · cs.LG
Original paper terms
CC BY 4.0
What we show
Paper info, commentary and permitted figures from the original paper

AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.

Abstract

In-context learning (ICL) lets a pretrained language model perform a task specified by demonstrations in its prompt, with no parameter updates. With arbitrary labels the model must acquire a mapping it cannot have memorized, so the learning happens within a single forward pass. Prior work found candidate parts but not how they compose. We give the composition for few-shot sentence classification with arbitrary labels in three 3-4B-parameter transformers. The model implements a key-value memory over label-token positions: each label token's residual stream encodes the label's identity, and middle-layer attention heads write into it the class of the preceding demonstration as its key. At the query, upper-layer heads match the query's class against the stored keys and copy out the matching labels. An early-layer signal encoding whether any label has repeated gates this readout; in its absence the readers attend to the start of the prompt and no label is favored. A separate set of heads suppresses the most recently seen label. Each step is established causally by activation patching, head ablations against random-head baselines, and steering with the model's own signals. A softmax model of the readout with two constants per head, fit on one prompt distribution, predicts another. The computation is conserved across the three models, the named heads alone reproduce the behaviour, and a transformer trained from scratch grows the same parts. The circuit transfers to semantic concepts and to labels with meaning, and the same parts, with a causal gate, appear from 0.6B to 8B in one family and survive instruction tuning. ICL in this setting is a gated associative lookup built from a few identifiable component types, traced causally from demonstrations to prediction.

Quote from the paper

Andrew Gao, William Gao (2026). A Circuit for In-Context Learning. arXiv:2611.05344v1. https://arxiv.org/abs/2611.05344v1

BibTeX
@misc{gao2026circuit,
  title = {{A Circuit for In-Context Learning}},
  author = {Andrew Gao and William Gao},
  year = {2026},
  eprint = {2611.05344},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2611.05344v1}
}

See related papersBrowse other papers

Paper preview
Account security

Connect Google account

If you connect a Google account, you can sign in with it too.

Refivu

Delete account

Refivu
Refivu

Sign up