Tokens & Embeddings
LLMs don't see words — they see tokens (small chunks of text) converted into embeddings (lists of numbers that capture meaning).
Introduction
LLMs don't see words — they see tokens (small chunks of text) converted into embeddings (lists of numbers that capture meaning). Understanding these two concepts is essential because they drive cost, context limits and search quality.
Beginner analogy: A token is a syllable; an embedding is a coordinate on a 'meaning map' where similar words sit close together. 'King' and 'queen' end up near each other; 'pizza' is far away.
Learning objective: Apply Tokens & Embeddings when shipping TechLearningPRO GenAI features — with clear model choice, retrieval/tool boundaries, and evals you can defend in review.
Treat GenAI as an engineering system: prompts, embeddings, retrieval, tools, and monitoring — not a single chat box. Prefer measurable quality and cost over demo magic.
Purpose of this lesson
Apply Tokens & Embeddings when shipping TechLearningPRO GenAI features — with clear model choice, retrieval/tool boundaries, and evals you can defend in review.
Understanding the topic
Core concepts to understand:
- Token ≈ ¾ of a word in English. 1000 tokens ≈ 750 words.
- Pricing is per token (input + output) — long prompts get expensive fast.
- Embedding = vector (e.g. 1536 numbers) representing meaning.
- Similar meanings → similar vectors → cosine similarity ≈ 1.
- Embeddings power semantic search, RAG, recommendation.
Step-by-step explanation
- Name the product job Tokens & Embeddings unlocks and the non-AI baseline it must beat.
- Map the architecture diagram to prompt layer, model, vector store, and tools.
- Build the smallest correct TechLearningPRO spike (API call, RAG chunk, or agent step).
- Measure failure modes: hallucination, latency, token cost — then add one guardrail.
Syntax reference
Visual workflow / architecture:
Text: "Hello world"│▼ tokenizer (BPE)[15496, 1917]│▼ embedding model[0.12, -0.84, 0.33, ..., 0.07] ← 1536-dim vector│▼ store in Vector DBPinecone / Chroma / pgvectorSearch: query → embed → find nearest vectors → return original text
Informative example
Minimal GenAI sketch:
# TechLearningPRO GenAI sketch — Tokens & Embeddingsfrom openai import OpenAIclient = OpenAI()resp = client.chat.completions.create(model="gpt-4.1-mini",messages=[{"role": "system", "content": "You help learners; never invent order facts."},{"role": "user", "content": "Summarize order ORD-9 status in 2 bullets."},],temperature=0.2,)print(resp.choices[0].message.content)
Real-world use
OpenAI text-embedding-3-small (1536-dim) is the workhorse for RAG. Cohere embed-v3 and Voyage AI are popular alternatives. Every modern AI search system — Perplexity, ChatGPT memory, Notion AI Q&A — runs on embeddings.
Security implications
GenAI systems handle untrusted text and often privileged tools — treat them as attack surface.
- Keep API keys in secrets managers; rotate and scope per environment.
- Isolate system instructions from user content; validate tool arguments.
- Filter or redact PII before prompts, embeddings, and trace logs.
Best practices
- Always count tokens before sending — use
tiktokenfor OpenAI. - Pick an embedding model and stick with it; mixing models breaks similarity.
- Normalise embeddings (L2) for accurate cosine similarity.
Common mistakes
- Embedding very long documents in one shot — chunk first!
- Forgetting that token cost includes output as well as input.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionQ1. What is a token?+
Answer
2IntermediateQuestionQ2. What is an embedding and what is it good for?+
Answer
3IntermediateQuestionQ3. Why does cosine similarity matter for embeddings?+
Answer
4AdvancedQuestionQ4. How would you estimate the cost of a 50-page document?+
Answer
Hands-on exercise
Hands-on + interview drill:
- Implement a minimal, evaluated spike for Tokens & Embeddings with cost and quality notes.
- Q1. What is a token?
- Q2. What is an embedding and what is it good for?
- Q3. Why does cosine similarity matter for embeddings?
- Q4. How would you estimate the cost of a 50-page document?
Summary
Tokens & Embeddings is a production GenAI skill: choose the right layer (prompt, RAG, agent), constrain risk, and prove quality with evals and cost awareness.