Why the Last Token Isn't Your LLM's Best Embedding

New analysis challenges the standard practice of extracting embeddings from the final token of decoder-only LLMs, showing intermediate layers and alternative pooling strategies often produce richer semantic representations.

Share
Why the Last Token Isn't Your LLM's Best Embedding

For years, practitioners building retrieval, classification, and clustering systems on top of decoder-only large language models have followed a near-universal convention: extract the hidden state of the final token from the final layer and call it the sentence embedding. A growing body of analysis suggests this convention is leaving substantial semantic quality on the table.

The Last-Token Convention and Why It Exists

Decoder-only transformers like LLaMA, Mistral, and GPT-style models are trained with causal attention masks. Only the final position can attend to every prior token, so it intuitively contains the most "complete" view of the sequence. Combined with the fact that next-token prediction objectives concentrate gradient signal at the last position, it became standard to treat that final hidden state as the canonical pooled representation.

The problem: training objective and representation quality are not the same thing. A vector optimized to predict the next token is not necessarily a vector that maximally separates semantically distinct sentences in cosine space.

What the Analysis Reveals

Probing studies across decoder-only models consistently find that intermediate layers — often somewhere between 50% and 80% of the way through the network — produce embeddings that outperform the final layer on tasks like semantic textual similarity (STS), retrieval, and clustering. The final layers tend to specialize for the next-token prediction head, collapsing semantic distinctions that earlier layers preserve.

This mirrors a well-documented phenomenon in encoder models like BERT, where middle layers carry the richest syntactic and semantic information, while top layers drift toward task-specific signals from the pretraining objective.

Beyond Last-Token Pooling

Three alternative pooling strategies repeatedly outperform last-token extraction:

  • Mean pooling across all token positions, which averages information distributed throughout the sequence rather than concentrating on one position.
  • Weighted mean pooling, where later tokens receive higher weights — a compromise that respects the causal structure while still aggregating distributed semantics.
  • Layer-wise pooling, where embeddings are taken from a selected intermediate layer (or a learned weighted combination of layers) rather than only the final one.

Models like E5-Mistral, GTE-Qwen, and NV-Embed have explicitly engineered around these findings. NV-Embed, for instance, introduces a latent attention layer that learns how to pool across positions rather than relying on the last token, achieving state-of-the-art MTEB scores.

Why This Matters for Synthetic Media and Authenticity

Embedding quality has direct downstream consequences for systems Skrew AI News readers care about. Retrieval-augmented generation pipelines that detect manipulated content, cross-modal search systems that match synthetic audio to source training data, and provenance verification tools all depend on dense vector representations. A retrieval system using suboptimal embeddings will miss semantically related deepfake variants, fail to cluster coordinated inauthentic content, and produce weaker similarity signals for forensic analysis.

For practitioners building detection systems on top of open-source LLMs, switching from last-token to mean-pooled intermediate-layer embeddings can yield measurable improvements without retraining a single parameter.

Practical Recommendations

If you are extracting embeddings from a decoder-only LLM for any downstream task:

  • Probe before you commit. Run a quick STS benchmark across layers — extract the hidden state at layer N for a range of N and measure Spearman correlation against a reference dataset. The optimum is rarely the final layer.
  • Try mean pooling. For sequences longer than a few tokens, mean pooling over non-padding positions almost always beats last-token extraction.
  • Consider instruction-tuned embedding variants. Models specifically fine-tuned for embeddings (E5-Mistral, GTE-Qwen2, NV-Embed) outperform raw base models by wide margins on retrieval benchmarks.
  • Don't trust the default. Many libraries return last-token embeddings by default. Audit your pipeline.

The Bigger Picture

The takeaway is broader than a single pooling trick: the geometry of representations inside an LLM is not uniform, and the position where the training loss is computed is not the position where semantic meaning is best encoded. As open-source LLMs become the backbone of detection, retrieval, and authenticity systems, understanding where in the network the useful signal actually lives is becoming an essential engineering skill.


Stay informed on AI video and digital authenticity. Follow Skrew AI News.