Skip to main content
LearnMLAcademy
Intermediate10 min read

Tokens, Embeddings & Context Windows

Master Byte-Pair Encoding (BPE) tokenization, rotary embeddings, and context, with token walkthroughs, prompt design tasks, and retrieval exercises.

What you will master in this lesson
Intuitive core mechanisms & practical real-world intuition
Step-by-step mathematical breakdown & visual diagrams
Runnable Python implementation & practical trade-offs

How does a sentence get inside an LLM?

You type “I love machine learning!”. A person sees words and meaning. A neural network works with numbers. The missing bridge is: split the text into manageable pieces, identify those pieces, then turn them into learned numerical representations.

Those pieces are called tokens. A token may be a whole word, part of a word, punctuation, a byte-like unit, or a special symbol depending on the tokenizer used by the model.

Step 1 — split text into tokens

For teaching, we might imagine “playing!” becoming play | ing | !. A real tokenizer may split it differently. Tokenization is model-specific, so visible word count is not a reliable token count.

Step 2 — give each token an ID

A vocabulary assigns each token an integer ID. Think of the ID as a catalogue number. If one token has ID 4217 and another has ID 4218, that does not mean their meanings are close.

Illustrative vocabulary lookup

Illustrative vocabulary lookup
TokenToken IDWhat the number means
machine2714Index used to look up this token
learning6221Another index; 6221 is not a semantic score
!9Vocabulary index for punctuation in this toy example

Step 3 — turn IDs into learned vectors

The model uses each ID to look up an embedding: a vector, or ordered list of learned numbers. Training adjusts these numbers so the model can represent useful patterns and relationships.

A helpful mental model is a coordinate system. One point may end up closer to another because the model learned that they appear in related contexts. But the individual coordinates are not manually labelled “animal”, “positive”, or “technology”.

Now follow one piece of text through the complete conversion below. Splitting, indexing and looking up a vector are three different operations: the next stage uses the result of the previous stage.

How written text becomes numerical model input

Text

I love machine learning!

Tokens

I | love | machine | learning | !

Token IDs

41 | 908 | 2714 | 6221 | 9

Embeddings

[…] learned number vectors

Model

Processes numerical representations

Token IDs identify vocabulary entries. Embeddings are the learned numerical representations; the ID number itself does not carry meaning.

A five-stage flow converts text into tokens, then token IDs, then learned embedding vectors, then passes the representations into the model.

How does the model know order?

The same words in a different order can mean something different: “dog bites man” is not the same as “man bites dog.” LLMs therefore combine token information with positional information so the network can distinguish where pieces appear in the sequence.

Context window — the model's current worktable

The context window is the finite amount of tokenized material the model can work with for a request. Instructions, conversation history, the current question, retrieved evidence, and room for the generated answer all consume that budget.

A context window is a limited worktable

Instructions220 tokens
Question180 tokens
Retrieved evidence1,100 tokens
Reserved output500 tokens
Planned total = 220 + 180 + 1,100 + 500 = 2,000. With a 1,600-token toy limit, the request is 400 tokens over budget.
Everything the model must read now—and the space reserved for its answer—has to fit inside the request budget.

A 1,600-token toy budget contains instructions, the user's question, retrieved evidence and an output allowance. The planned total shown is 2,000 tokens, which is 400 tokens over the budget.

The 1,600-token value is deliberately small and only for this worked example. Real model limits vary. The skill to learn is the budgeting: 2,000 planned − 1,600 available = 400 tokens that must be removed or reallocated.

Context is not long-term memory

A context window is temporary working material for the current interaction. If an application remembers your preference next week, software outside the base model typically stored that information and inserted it into a later request.

Do not confuse these

Do not confuse these
ConceptPlain-English meaning
TokenA model-specific piece of text
Token IDVocabulary index used to locate a token's representation
EmbeddingLearned numerical vector used by the network
Context windowFinite current working text budget
Persistent memoryApplication-managed information stored and reintroduced later

Why the next lesson matters

Now the model has numerical token representations. How can each token decide which other tokens matter?

Transformer Architecture & Attention →

Summary / Key Takeaways

  • LLMs first split text into model-specific pieces called tokens.
  • A token ID is a vocabulary index, not a measure of meaning.
  • Embeddings are learned numerical vectors that represent tokens inside the model.
  • Positional information helps the model distinguish token order.
  • The context window is a finite current working budget and must include room for the answer.
  • A large context window is not the same as persistent application memory.

Apply this concept in a real hands-on project

You have studied the idea. Now use it in working Python code, see the calculation and output, and explain what changed. Each project includes step-by-step instructions, full source and tests.

Practice challenge: predict an outcome first, run the code, and explain the difference.

Track Your Progress0% curriculum done

Finished "Tokens, Embeddings & Context Windows"?

Mark this lesson as completed to update your progress bar and track your milestones across the full curriculum.

Overall Progress0 / 167 lessons

Was this tutorial helpful?

Your feedback helps us continuously refine and improve our open-access tutorials.

Knowledge Check

Progress: 0/5

Test your active recall. Choose one answer for each question, then check it to reveal the step-by-step reasoning.

Question 1 of 5conceptual
Which statement about tokens is correct?
Question 2 of 5interpretation
What does token ID 4217 primarily mean?
Question 3 of 5numerical/formula
The example request uses 220 instruction tokens, 180 question tokens, 1,100 evidence tokens, and a 500-token output reserve. With a 1,600-token toy budget, how far over budget is it?
Question 4 of 5debugging/diagnostic
A developer assumes a 1,000-word document will always use about 1,000 tokens. What is the main problem?
Question 5 of 5application/scenario
A chat application wants to remember a user's preference next week. Why is a large context window alone insufficient?

Knowledge Check Review & Explanations

Review the questions, verified correct answers, and conceptual explanations for this lesson.

1Which statement about tokens is correct?▼

Correct Answer: Tokens are model-specific pieces that may be words, subwords, punctuation, bytes, or special symbols

Tokenization is model-specific, and token units can take several forms rather than mapping one-to-one with words.

2What does token ID 4217 primarily mean?▼

Correct Answer: It is a vocabulary index used to look up the token's representation

Token IDs are arbitrary vocabulary indexes; semantic relationships come from learned representations, not numeric closeness of IDs.

3The example request uses 220 instruction tokens, 180 question tokens, 1,100 evidence tokens, and a 500-token output reserve. With a 1,600-token toy budget, how far over budget is it?▼

Correct Answer: 400 tokens

Planned total is 2,000 tokens, so 2,000 − 1,600 = 400.

4A developer assumes a 1,000-word document will always use about 1,000 tokens. What is the main problem?▼

Correct Answer: Token counts depend on the actual tokenizer and the text's language, punctuation, code, and vocabulary patterns

Visible word count is not a reliable token count because tokenization is model- and text-dependent.

5A chat application wants to remember a user's preference next week. Why is a large context window alone insufficient?▼

Correct Answer: Context is temporary for a request; persistent memory requires application-managed storage and reinsertion

A context window is request-time working context, not durable application memory.

Keep moving through the path

Continue Learning

Free Download: ML Interview Cheatsheet

100 essential Machine Learning interview questions and answers covering foundations, algorithms, evaluation, deep learning, Python, system design and interview strategy. Enter your email to download it instantly.

Next