Introduction

Tokens and embeddings together form the very first stage of the transformer pipeline, converting raw text into the numerical representations a model can actually compute with. While tokens (covered in an earlier topic) break text into discrete pieces, embeddings take the crucial next step: transforming each token into a dense vector of numbers that captures meaningful information about that token's role and relationships in language.

Understanding embeddings specifically — beyond just tokenization — is essential for grasping how a transformer builds meaning from raw text, since these vectors are what self-attention actually operates on throughout every layer of the model.

Why Do Tokens and Embeddings Matter?

Tokens and embeddings help to:

  • Convert raw text into a numerical format neural networks can process
  • Capture semantic meaning and relationships between words as geometric relationships
  • Provide the actual input representation that self-attention operates on
  • Enable the model to recognize similarity between related words and concepts
  • Serve as the foundation upon which every subsequent transformer layer builds
  • Support downstream techniques like semantic search, covered in earlier vector database topics

From Tokens to Embeddings: The Pipeline

Whiteboard
Whiteboard diagram

Quick Recap: Tokens

Tokens are the discrete pieces text is broken into (often subwords),
each assigned a unique numeric ID from the tokenizer's vocabulary.

"unbelievable" → ["un", "believ", "able"] → [1234, 5678, 9012]

These numeric IDs are just index positions in a lookup table —
they don't yet carry any meaning on their own.

What an Embedding Actually Is

An embedding is a dense vector of numbers (e.g., 768 or 4096
dimensions) that represents a token's meaning in a continuous,
learned space.

Token ID 1234 ("un") → [0.12, -0.45, 0.88, ..., 0.03]
                         (hundreds or thousands of numbers)

Unlike the arbitrary token ID, this vector is learned during
training, positioned so that tokens with related meanings end
up located near each other in this high-dimensional space.

The Embedding Layer

The embedding layer is essentially a large, trainable lookup table — one row per vocabulary entry — where each row is the learned vector representation for that specific token.

Embedding Matrix shape: [vocabulary_size × embedding_dimension]

Example: 50,000 vocabulary entries × 768 dimensions each
= 38.4 million learnable parameter values just for this layer

Why Embeddings Capture Meaning

During training, embeddings are adjusted so that tokens
appearing in similar contexts end up with similar vectors.

Famous illustrative example (from earlier word embedding research):
vector("king") - vector("man") + vector("woman") ≈ vector("queen")

This shows embeddings can capture not just similarity,
but meaningful relationships and analogies between concepts,
purely as a byproduct of learning from large amounts of text.

Embeddings and Semantic Similarity

ConceptHow Embeddings Represent It
Similar MeaningTokens/words placed close together in vector space
Different MeaningTokens/words placed farther apart in vector space
Relationships/AnalogiesDirections and distances between vectors can reflect relationships
Measuring SimilarityTypically calculated using cosine similarity between vectors

(This is the same core concept explored earlier in the Sentence Transformers and vector database topics, applied here at the individual token level within a transformer.)

Static Embeddings vs Contextual Embeddings

AspectStatic Embeddings (e.g., early Word2Vec)Contextual Embeddings (Transformers)
RepresentationOne fixed vector per word, regardless of contextVector changes based on surrounding context
Handling AmbiguityCannot distinguish different meanings of the same wordCan represent different meanings based on usage
Example"bank" always has the same vector"river bank" vs "savings bank" get different contextual representations
Used InEarlier NLP techniques (Word2Vec, GloVe)Modern transformer-based models

How Transformers Make Embeddings Contextual

The initial embedding for a token is just its starting,
context-free representation from the embedding layer.

As this vector passes through the transformer's self-attention
layers, it's continuously updated based on its relationship to
surrounding tokens — by the final layer, each token's
representation reflects its specific meaning in that specific context.

This is why the same word can end up with different final
representations depending on how it's used in a sentence.

Key Properties of Tokens and Embeddings

  • Tokens are discrete, symbolic units; embeddings are the dense numerical vectors representing them.
  • The embedding layer is a large, trainable lookup table mapping token IDs to learned vectors.
  • Embeddings position semantically related tokens closer together in a high-dimensional space.
  • Transformers produce contextual embeddings, which change based on surrounding text, unlike older static embedding methods.
  • Embedding dimensionality (e.g., 768, 4096) is a key architectural choice affecting model capacity and size.

Where Do Tokens and Embeddings Matter Most?

ContextWhy It Matters
Language Model Input ProcessingThe essential first step converting text into model-usable form
Semantic Search / RAG SystemsEmbeddings enable finding conceptually similar content
Multilingual ModelsShared embedding spaces can help relate concepts across languages
Model Interpretability ResearchStudying embedding spaces reveals what relationships a model has learned
Fine-Tuning and Transfer LearningPre-trained embeddings provide a valuable starting point for new tasks

Advantages

  • Provides a mathematically usable, meaningful representation of language for neural networks
  • Captures semantic relationships as measurable geometric relationships
  • Contextual embeddings in transformers handle word ambiguity far better than static approaches
  • Forms a reusable foundation that supports both generation and retrieval/search use cases
  • Learned automatically during training, without requiring manual feature engineering

Limitations

  • Embedding layers can be very large, contributing significantly to a model's total parameter count
  • Static embeddings (used in some simpler systems) can't handle words with multiple meanings well
  • Embedding quality depends heavily on the diversity and quality of training data
  • High-dimensional embedding spaces can be difficult to interpret directly
  • Rare or out-of-vocabulary concepts may have less well-refined embedding representations

Real-World Examples

ApplicationTokens & Embeddings Use
Large Language ModelsInitial input representation before self-attention processing
Semantic Search SystemsComparing embeddings to find conceptually similar documents
Recommendation SystemsEmbedding-based similarity between items or user preferences
Machine TranslationEmbeddings enable meaningful cross-lingual representations
Sentiment AnalysisEmbeddings capture nuanced meaning beyond simple keyword matching

Best Practices

  • Understand that raw token IDs carry no inherent meaning — only their learned embeddings do.
  • Remember that transformer embeddings are contextual, changing based on surrounding text, unlike older static methods.
  • Consider embedding dimensionality as a meaningful architectural tradeoff between capacity and efficiency.
  • Leverage existing pre-trained embeddings rather than training them from scratch when possible.
  • Use cosine similarity as the standard way to measure relatedness between embedding vectors.

Interview Tip

A common interview question is:

"What is the difference between a token and an embedding, and why are transformer embeddings described as 'contextual'?"

A strong answer is:

A token is a discrete piece of text — often a subword — identified by a numeric ID from the tokenizer's vocabulary, while an embedding is the dense vector of numbers that represents that token's meaning in a continuous, learned space, positioned so related concepts end up near each other. Transformer embeddings are described as contextual because, unlike older static embedding methods where a word always had the same fixed vector, a transformer continuously updates each token's representation through self-attention based on its surrounding context — so the same word can end up with different final representations depending on how it's actually used in a sentence.

Explaining the static-vs-contextual distinction with a concrete example makes your answer stronger.

Conclusion

Tokens and embeddings together form the essential bridge between raw human language and the numerical representations a transformer actually computes with, with embeddings specifically capturing meaning as learned, contextual relationships in a high-dimensional space. With this foundation established, the next topic explores positional encoding — the mechanism that restores the sense of token order that embeddings alone don't inherently provide.