Understanding Tokens, Embeddings and Transformers
This lesson explains the building blocks of how large language models read and generate text, why prices and limits are measured in tokens, and how semantic search and retrieval augmented generation (RAG) work.
Learning outcomes
After completing this lesson, you will be able to:
- Explain what tokens are and how the context window and pricing are measured in them.
- Explain why documents are chunked and how chunk size affects retrieval.
- Explain how embeddings and vector search find text with similar meaning.
- Describe how transformers generate text one token at a time.
Tokens and context windows
Tokens are the units a model reads and writes: words, parts of words or punctuation. One token is roughly three-quarters of an English word. These are all measured in tokens:
- The context window: how much text the model can consider at once, prompt and answer together.
- The output limit.
- The price, so long prompts cost more.
Counting in tokens
Chunking
Chunking splits long documents into passages, for example a few hundred tokens each with some overlap, so they can be embedded and retrieved individually. Small chunks are precise but may lose context; large chunks keep context but add noise and cost.
Choosing a chunk size
Embeddings and vector search
Embeddings are numeric vectors that represent meaning. Similar meanings produce vectors that lie close together, measured by cosine similarity. "How do I reset my password?" and "I forgot my login" have similar embeddings despite sharing few words.
- Uses: semantic search, clustering, recommendations, duplicate detection and Retrieval Augmented Generation (RAG).
- Embedding models on Bedrock include Amazon Titan Text Embeddings, Amazon Nova multimodal embeddings and Cohere Embed.
- Vector databases store embeddings for fast similarity search.
The following diagram shows chunks embedded and searched by meaning.

Uses of embeddings
Transformers and text generation
Large language models are based on the transformer architecture and generate text one token at a time. The following list describes how text generation works.
- Transformers are the architecture behind modern LLMs. Self-attention lets every token weigh its relationship with every other token, which is how a model knows what "it" refers to. Tokens are processed in parallel, making training on huge data practical.
- LLMs generate text by repeatedly predicting the next token.
- Temperature and Top P control that choice: low values give focused, repeatable output; high values give creative, varied output.
Inside a transformer
Why a long prompt costs more, and how embeddings find meaning
A support assistant sends each question to a model with a 3,000-token system prompt and returns about 300 tokens. With illustrative prices of $0.0008 per 1,000 input tokens and $0.0032 per 1,000 output tokens, each request costs:
Input: 3,050 tokens x $0.0008 / 1,000 = $0.00244
Output: 300 tokens x $0.0032 / 1,000 = $0.00096
Total per request = $0.0034 -> 1 million requests = about $3,400Input tokens dominate this bill. Cutting the system prompt to 1,000 tokens reduces the cost per request to about $0.0018, almost half; caching the unchanged system prompt with prompt caching, which bills cached tokens at a fraction of the normal input price, reduces it further without shortening the instructions.
The same assistant uses embeddings to find relevant help articles. The question "I cannot sign in" and the article titled "Resetting a forgotten password" share no words, but their embedding vectors are close together (high cosine similarity), so semantic search finds the article where keyword search would fail.
What is a model's context window?
Show answer
The maximum number of tokens the model can consider at once, including the prompt and its answer. Text beyond it is not seen.
Exam Tips
Remember for the exam
- Context windows, output limits and pricing are all measured in tokens.
- To find text with a similar meaning, use embeddings and a vector database (semantic search).
- Similarity between vectors is measured with cosine similarity.
- Splitting documents into passages for retrieval is called chunking.
- Self-attention is the key mechanism in transformers.
- LLMs generate text by predicting the next token.