Lesson 1 of 2 How Generative AI Models Work

Understanding Tokens, Embeddings and the Transformer Architecture

This lesson explains how generative language models turn text into numbers, process those numbers with a transformer and turn the result back into text. The three building blocks, tokens, embeddings and attention, explain how models use context and why they are measured and billed in tokens.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain what tokens are and how the context window is measured.
  • Explain how embeddings represent meaning as vectors.
  • Describe how attention lets a transformer use context.
  • Estimate whether a document fits in a model's context window.

Tokens

A model does not read words directly. A tokenizer splits text into tokens, which are common words, parts of words, punctuation or spaces, and maps each to an integer ID. Frequent words are usually one token; rare or long words are split into several. In English, 100 tokens correspond to roughly 75 words, but the ratio differs between languages.

Tokens matter in practice because models have a context window, the maximum number of tokens they can consider at once (the prompt plus the response), and because Foundry Models pricing and quota are measured in tokens: input tokens in the prompt, output tokens in the response and tokens per minute (TPM) for deployment quota.

The following diagram shows a sentence split into tokens and what a context window must hold.

Tokens and the context window. A tokenizer splits text into tokens, such as common words, parts of words and punctuation, and maps each to an integer ID; a rare word such as forklift becomes two tokens. In English 100 tokens are roughly 75 words. The context window is the maximum number of tokens the model considers at once, and it must hold the instructions, any earlier turns, the input and the response. Pricing and quota are also measured in tokens.
Token IDs are illustrative; each tokenizer has its own vocabulary.
True or false

Counting in tokens

Exam skills 1.2.1

Check five statements about tokens, context windows and quota.

Log in to try this activity Interactive activities are free with an account.

Embeddings

An embedding is a vector, a list of numbers, that represents the meaning of a token, sentence or document as a position in a many-dimensional space. Items with similar meaning have vectors that are close together, which is measured with cosine similarity. Inside a language model, each token ID is converted to an embedding before processing, and the model's layers refine these vectors to reflect context, so that bank in river bank and bank account end up with different representations.

Separate embedding models, such as text-embedding-3-small, produce one vector for a whole piece of text. Applications store these vectors to find similar content, which is the basis of semantic search and of retrieval-augmented generation (RAG), where relevant passages are found and added to a prompt.

The following diagram shows embeddings as positions in a space and how search uses them.

Embeddings as positions in a space. Each item becomes a vector, and items with similar meaning lie close together: animals in one area, billing terms in another. The same word in different contexts, such as bank in river bank and bank account, ends up in different places. An embedding model turns a query into a vector, the closest stored vectors by cosine similarity are the most similar content, and retrieval-augmented generation adds those passages to a prompt.
Positions are illustrative; real embeddings have hundreds or thousands of dimensions.

The transformer and attention

Most modern language models use the transformer architecture. Its key mechanism, attention (more precisely self-attention), lets the model calculate, for each token, how much every other token in the context should influence its representation. In The trophy did not fit in the suitcase because it was too big, attention helps the model relate it to trophy. Attention runs over all tokens in parallel, which made it practical to train very large models on very large datasets.

The original transformer had an encoder, which builds representations of input, and a decoder, which generates output. Generative models such as GPT are decoder-only: they generate text one token at a time, each time attending to the prompt and to everything generated so far. Embedding models are encoder-style models that produce representations rather than text.

Scenario questions

Embeddings and attention at work

Exam skills 1.2.1

Explain behaviour you see in applications with embeddings and the transformer's attention mechanism.

Log in to try this activity Interactive activities are free with an account.
Worked example

Estimating tokens and context

A developer wants a model to summarize 300-page contracts in one request. The chosen model has a 128,000-token context window. Each page has about 500 words.

300 pages × 500 words is 150,000 words, which is about 200,000 tokens, more than the context window before any instructions or output are added.

The developer can choose a model with a larger context window, summarize the contract in sections and then combine the section summaries, or retrieve only the clauses relevant to a question. Raising the maximum output tokens would not help, because the limit is on the total of input and output.

Check yourself

What is an embedding?

Show answer

A vector of numbers that represents meaning, so that texts with similar meanings are placed close together.

Exam Tips

Remember for the exam

  • Models process tokens; about 100 tokens equal 75 English words.
  • The context window limits prompt plus response, measured in tokens.
  • Embeddings are vectors that place similar meanings close together.
  • Attention lets each token use the relevant context; GPT models are decoder-only transformers.