AI Foundations0% complete
0%

Chapter 5 of 2525 chapters remaining

Saved on this device. Mark a chapter complete after reading.

MODULE 1 · LESSON 5 · 13 MIN

Tokens, Embeddings, and Context Windows Explained

By Siten Sanghvi · Published · Updated

Tokens are the units a language model processes. Embeddings are numerical representations. The context window limits what can fit in a request. They are related, but they are not interchangeable names for memory or understanding.

These distinctions explain several practical surprises: a short-looking document can use many tokens, two similar texts can retrieve an inappropriate passage, and a model can miss a fact even when it fits inside the advertised context limit.

Tokens: count with the actual tokenizer

A tokenizer maps text to a sequence of token IDs. Depending on the tokenizer, a unit can be a word, part of a word, punctuation, whitespace-containing text, bytes or a special symbol. Exact boundaries and vocabulary sizes vary.

Do not assume “100 tokens means 75 words” is a reliable conversion for your input. Language, code, numbers, formatting and the selected tokenizer all matter. The Hugging Face tokenizer guide describes different tokenization methods rather than one universal split.

If you need a count for a limit or price, use the chosen model's tokenizer or documented token-counting facility. Include the actual message formatting and special tokens required by that interface. A friendly word-sized diagram is a teaching aid, not a tokenizer trace.

Embeddings: numbers before, and after, context

In a common text-model setup, an input token ID selects a learned vector from an embedding table. The same token ID starts with the same table entry. The PyTorch Embedding reference shows that lookup operation directly.

The network then transforms those inputs, incorporating position information and permitted context. Those later hidden representations can depend on surrounding text. That is different from saying the initial lookup already contains every contextual meaning.

The famous word-vector analogy “king minus man plus woman is near queen” is a historical illustration of some learned relationships, not a universal guarantee or literal rule for truth. The word-representation paper evaluates particular models on particular similarity tasks. A vector can encode useful associations and biases without representing a statement's correctness.

For document retrieval, a dedicated embedding model may produce a vector for a passage or query. Similarity is a retrieval signal. You still have to check whether a returned passage supports the user's question and whether the user is allowed to read it.

Attention: mixing information, not proving a reason

In a transformer attention head, learned projections produce queries, keys and values. Query-key scores determine weights used to combine values. This is more specific than “similar words attract each other.” The original transformer paper specifies that mechanism and its masking.

For causal self-attention, the representation at a position cannot use later tokens. In “The bank approved the loan,” the later token “loan” can use earlier information about “bank.” The earlier “bank” position cannot look ahead to “loan” in that same causal prefix. Bidirectional models use different permitted context.

EXHIBIT 05 / HEATMAP

Inspect the available context

Which earlier tokens receive attention?

Each row shows a token routing attention across the context available at that position. Select a cell to inspect its weight.

Reading across → keys in the available context

Query ↓ThebankapprovedtheloanThebankapprovedtheloan
Smaller weightLarger weight

Handpicked, normalized weights for one illustrative causal attention head, using word-sized units. Future positions are masked. Attention weights alone do not explain a model's decision or establish factual accuracy.

Read the attention example

In this finance example, the final token “loan” assigns 45% of its illustrated attention to “bank” and 30% to “approved”. The earlier token “bank” cannot attend to the later word “loan” in a causal head. Every visible row sums to 100%.

The heatmap uses one handpicked head and word-sized units to make the mask readable. It does not measure a production model. Attention weights alone also do not establish why a model reached a decision, a limitation examined in Attention is not Explanation.

Context window: capacity is not reliable recall

The relevant input can include instructions, selected conversation history, retrieved material and tool results. Generating an answer also needs an output allowance; exactly how input, output and other token categories count depends on the model and interface.

Not all saved conversation history is necessarily supplied. When an input is too long, software may reject it, truncate some material, summarize it, or retrieve a smaller relevant selection. The documented truncation strategies are concrete examples of configurable behavior. Automatic removal of the oldest message is not a universal model rule.

Fitting is also different from using reliably. Lost in the Middle found position-dependent performance on its tested long-context tasks. It does not prove every current model fails identically; it gives you a reason to test where evidence appears in your own workflow.

Try it: budget a document answer

Use this made-up budget to practice, not as a provider specification. Suppose a request budget is 8,000 tokens, supplied input uses 5,500, and you reserve 1,500 for output. That leaves 1,000 tokens of headroom under those assumptions.

Now review the input. Which duplicated passages could you remove? Which policy version is authoritative? Does the remaining evidence answer the question? Keep the relevant evidence, reserve room for the response, and test an unsupported question too.

A saved conversation or a vector database is not automatically part of the model's present context. Application design determines what is stored, retrieved and supplied. That is why context engineering is a system-design task rather than just buying a larger window.

The takeaway

Tokens define units, embeddings provide numerical representations, and context limits constrain what is supplied and generated. None is a guarantee of truth or reliable recall. More context is not always better context: include what the task needs.

Up next

Training Data: What Models Learn From and Why It Matters: distinguish what training examples teach from the evidence supplied for a particular request.

VISUAL FIELDNOTES / 05

Keep the big picture.

A one-page visual companion. Save it, print it, or explain it to someone else.

Text pieces become numeric vectors and context-dependent attention, followed by a finite budget shared by inputs and output.View the full-size sheet ↗

Give context a shape.

Tokens are units. Embeddings are vectors. Context has limits.

One US Letter page · Vector artwork · Fits A4 when printed to fit

Read the key points as text
Tokens
Chunks of text, not necessarily whole words. Count with the selected model’s tokenizer rather than a fixed words-to-tokens ratio.
Embeddings
A token ID selects an initial vector. Later hidden representations incorporate permitted context. Similarity is not truth.
Context window
Finite request capacity. Check input/output accounting and overflow policy. Fitting does not guarantee reliable recall.

Remember: More context is not always better context. Include what the task needs.

Try it: For document Q&A, retrieve the relevant passages, remove duplicates, and reserve room for the answer. Check the provider’s accounting rules.

Recall: Which context could you remove without losing the evidence needed for the answer?

OPTIONAL CHECK

Check your understanding.

The optional check requires JavaScript. You can continue reading the lesson.

YOUR LEARNING

Keep your place.

Your progress is saved on this device.