Module 0 · 10 lessons

AI basics, in plain English

How a model learns, then what a prompt, a token, an embedding and RAG are, for someone who has never used an AI API.

Before you start

Nothing else on the platform. This module starts from zero.

Later, to go deeper: Search indexes and autocomplete, LLM serving and retrieval (RAG)

1. What a model is: learned from examples

The picture

A model learns from examples

Training adjusts a model's numbers using marked examples, also called labelled data, and a test on new examples shows whether it learned.

Scene 1 of 5 · Examples, not rules

A spam filter starts from 10,000 emails, each one marked spam or not spam by a person.

Marked examplesMarked examples10,000 marked emailsTrainingTrainingThe modelThe modelYour appYour app
  • Marked examples — healthy — 10,000 marked emails
  • Training — healthy
  • The model — healthy
  • Your app — healthy
  1. Examples, not rules. A spam filter starts from 10,000 emails, each one marked spam or not spam by a person.
  2. Nudge the numbers. Training compares each guess with its mark and shifts the model's numbers towards the right answer.
  3. Test on new emails. Scored on fresh emails, it is right 96 times in 100, close to its 99 on old ones.
  4. Memorising is not learning. A model that memorised its examples scores 100 on them but 71 on new ones.
  5. Inference: use it. Your app sends a new email and gets a label back, and the numbers no longer change.
  6. Now you try. Pick a task, and see whether it suits plain code or a trained model. For example: Plain code. The rule is known and must be exact every time, so a guess that is usually right is not good enough.

Scene 1 of 5: Examples, not rules. A spam filter starts from 10,000 emails, each one marked spam or not spam by a person.

Key points

  1. A model's rules come from examples, not from a person writing them.
  2. Training nudges its numbers until its guesses match the marked answers.
  3. Only a score on unseen examples shows that it learned.
  4. Your app runs the finished model; its numbers stay fixed while it answers.

Most software follows rules a person wrote, such as "orders over $50 ship free". A model is different: nobody writes its rules. It learns patterns from many examples.

shows the model examples, such as emails a person marked as spam or not spam. Examples marked like this are called labelled data. It compares its guesses with the marked answers, and adjusts its internal numbers, called , so the next guesses are a little better.

Training is slow and costly, and it happens before anyone uses the model. Using the finished model to get an answer is called . Most AI features call a model someone else trained, so they run inference only.

A model is judged on examples it never saw. One that memorised its training examples does well on them and badly on new ones, which is called . Where the rules are known and must be exact, such as tax or permissions, plain code is the better tool.

Example. Illustrative: a spam filter learns from 10,000 marked emails. On 1,000 emails it never saw, it labels 960 correctly. Mistakes like those 40 are normal for a model, so costly cases need a rule or a person.

In system design: Treat a model as a part that is usually right, not always. Keep exact rules, such as prices and permissions, in plain code.

Where it goes wrong

  • A model trained on old examples drifts as the world changes. A spam filter from last year misses this year's tricks, so test it again on fresh data.
  • A model can learn a shortcut instead of the real pattern, such as flagging every email from one domain. Test it on varied examples it never saw.

In an interview

  • Say which parts of the design use a model and which stay in plain code, such as prices, tax and permissions. Then say how you measure the model and what happens when it is wrong.

Check yourself

A spam filter scores 99 in 100 on the emails it trained on, but 70 on new emails. What does that suggest?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

A spam filter scores well on its test set, but all the spam in that set came from one domain. What should you do before trusting it?

Judge a design

A bank plans to let a model decide which staff may open which accounts, since a model learns fast. What is the best judgement?

Sources and further reading (2)

First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.

Open this lesson on its own page

2. What a large language model is

The picture

A model guesses the next piece

A language model scores every piece of text that could come next, then picks one, usually one of the highest scores.

Scene 1 of 5 · Send some words

Send "The capital of France is": the model gives a score to each piece that could come next.

YouYouLanguage modelLanguage model
  • You — healthy
  • Language model — healthy
Chances for the next piece, out of 100 (made-up numbers)
  • "Paris"92 in 100
  • "a"5 in 100
  • "the"3 in 100
  1. Send some words. Send "The capital of France is": the model gives a score to each piece that could come next.
  2. One piece stands out. "Paris" gets 92 chances in 100, since that pattern filled its training text again and again.
  3. A fact it never read. Now send "Our shop's refund window, in days, is", a fact about your shop that was never in its training.
  4. Only a guess. Here "90" leads with only 36 in 100, so another run could as easily write "30".
  5. It sounds equally confident. Both answers come back in the same confident voice, so a reader cannot spot the guess.
  6. Now you try. Send different words, and see whether one next piece stands out. For example: Top piece: "blue", with 86 chances in 100. This pattern was common in its training text, so one piece stands far above the rest.

Scene 1 of 5: Send some words. Send "The capital of France is": the model gives a score to each piece that could come next.

Key points

  1. For any words you send, the model scores what could come next.
  2. A pattern it read many times in training gets one clear winner.
  3. A fact it never read gets a likely-sounding guess, with no evidence behind it.
  4. A guess comes out in the same sure voice as a known fact.

A , or LLM, is a program trained on a huge amount of text: books, websites and code. From all that text it learned one skill very well. Given some words, it guesses which piece of text most likely comes next. It picks with a little randomness, so the same question can get different answers.

A chat model then gets a second round of training, on examples of requests paired with good replies, often with people rating its answers. That is why it answers a question instead of only continuing your text.

To write an answer, it makes that guess again and again. It adds one small piece, then guesses the next one, until the answer is complete. That is why answers appear a few words at a time, when the app shows them as they are written.

A model on its own does not look anything up while it writes. It works from what it learned in training and the text you send. It can sometimes work out a fact it was never told. It cannot reliably know your private data or recent news: send that text, or give it a tool to look it up.

Example. Send the words "The capital of France is" and the model continues with "Paris". It has seen that pattern many times. Ask about your own shop's refund policy, and it has never seen it, so it can only guess.

In system design: Your backend treats the model as a slow, costly function: text goes in, text comes out. Most of the design work is everything around that call.

Where it goes wrong

  • A model learned only from text up to a cut-off date. Ask about anything newer, such as this week's prices, and it guesses unless you send the facts.
  • The same can give different answers. A feature that needs one fixed result must check or limit the output.

In an interview

  • Treat the model as a slow, costly function that is sometimes wrong. Then name what wraps it: timeouts, checks and a .

Check yourself

You send the same question twice and get two different answers. Why can that happen?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

A shop asks a chat model, with no extra text, what the shop's own returns policy is. What is the answer based on?

Judge a design

A team plans to show today's exchange rates by asking a model, with no rates in the prompt. What is the best judgement?

Sources and further reading (2)

First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.

Open this lesson on its own page

3. What a prompt is

The picture

Three parts, one request

Your backend builds each prompt from instructions, context and a question, then sends it all in one request.

Scene 1 of 5 · The instructions

First come the instructions, which say how to behave: "Answer only from the policy below."

InstructionsInstructionsPolicyPolicyQuestionQuestionYour backendYour backendLanguage modelLanguage model
  • Instructions — healthy
  • Policy — healthy
  • Question — healthy
  • Your backend — healthy
  • Language model — healthy
The prompt your backend builds
PartText
Instructions (highlighted)Answer only from the policy below.
  1. The instructions. First come the instructions, which say how to behave: "Answer only from the policy below."
  2. The context. Next comes the context: the shop's refund policy, which allows returns for 30 days.
  3. The question. Last comes what the customer asked: may they return shoes after 40 days?
  4. One request. Your backend joins the three parts into one prompt and sends it as a single request.
  5. Answer from the policy. The model answers from the policy: no, since 40 days is past the 30-day limit.
  6. Now you try. Choose which parts go into the prompt, and see what the model can answer. For example, tapping "Instructions" and "Question" gives this: A guess: "Yes, returns are fine for 90 days". With no policy in the prompt, the model never saw your rule, so it guesses a common number.

Scene 1 of 5: The instructions. First come the instructions, which say how to behave: "Answer only from the policy below."

Key points

  1. Instructions say how to behave, such as "use only the policy".
  2. Context is the material to answer from, such as a document.
  3. The model sees only what is in this one prompt.
  4. Leave out the context, and the answer becomes a guess.

A prompt is everything you send to the model in one request. It usually has three parts: instructions that say how to behave, context such as documents or earlier messages, and the question itself.

The model sees only what is in the prompt. It does not remember earlier requests by itself. A chat app that seems to remember is sending the earlier messages again, inside each new prompt. Some provider APIs can store a conversation and add the earlier messages for you: the same work, done on their side.

Better context gives better answers. That is why much of the work in an AI feature is choosing what goes into the prompt, and what stays out.

Example. A support bot's prompt might hold three parts. Instructions: "Answer only from the policy below." Context: the shop's refund policy. Question: "Can I return shoes after 40 days?"

In system design: Your backend builds the prompt. It decides which documents, user details and limits go in, and it pays for all the text it adds.

Where it goes wrong

  • Instructions that pull against each other, such as "be brief" and "explain fully", make answers swing from one to the other. Keep one clear rule for each choice.
  • Text pasted into the prompt from users or documents can hold instructions, and the model may follow them. The security module covers this.

In an interview

  • Say what goes into the prompt, where each part comes from, and how many each part may use.

Check yourself

A chat app seems to remember what you said five messages ago. How does it do that?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

A support bot's prompt has instructions and the question, but no refund policy. It answers refund questions badly. What is missing?

Judge a design

A prompt says "Be brief" at the top and "Explain every step fully" further down. What will most likely happen?

Sources and further reading (2)

First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.

  • Official docsPrompt engineering overviewAnthropic docs (one provider)What goes in a prompt, and which techniques to try first.
  • Official docsMessages APIAnthropic docs (one provider)The exact shape of one request: the system prompt, the earlier turns, and the new message.

Open this lesson on its own page

4. Tokens: the pieces a model reads

The picture

Our·support·bot·answers·questions·about·refunds.

7 words · 10 tokens · 1.43 tokens per word

At $1 per million input tokens, 1,000 questions like this cost $0.01. Prices vary by model and change often.

What this simplifies. This splitter is written for teaching, with fixed rules for where to cut. Real models learn about 100,000 pieces from data, so they cut in different places. Their counts differ, sometimes a lot for code, numbers or other languages. The rule of thumb still holds: in English, a token is roughly three quarters of a word. The cost counts only the text you typed. A real request also pays for the instructions and documents in the prompt, and for the answer, whose tokens often cost more. A dot (·) marks a space that belongs to the token.

A model reads and writes small pieces of text called tokens. It reads in pieces, not whole words, and a common word is often only one piece. A rare or long word is split into two or more tokens.

Every request counts tokens, and the prompt you send and the answer you get are counted apart. Limits and prices are set per token, so a longer prompt or a longer answer costs and counts for more.

English text averages roughly three-quarters of a word per token, close to 1.33 tokens for every word. This ratio is rough: it shifts with the language, the words used and the model itself.

Example. A 12-word sentence is about 16 tokens. English runs roughly 1.33 tokens per word, so 12 words times about 1.33 comes to about 16.

In system design: Token counts set your prompt budget, your answer length limit and your bill. A backend estimates tokens before sending a request, to avoid errors or surprise costs.

Where it goes wrong

  • Many languages need more tokens than English for the same meaning. The same message can then cost more and fill the window sooner.
  • Counting words instead of tokens leads to requests that are refused as too long. Use the provider's token counter for exact counts.

In an interview

  • Turn users and messages a day into tokens, then into cost and context use, and show the sum.

Check yourself

Why do model providers count tokens instead of words?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

About how many tokens is a 300-word message in English?

Judge a design

A backend checks whether a message fits by counting its words against the model's token limit. What is wrong?

Sources and further reading (2)

First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.

  • Official docsSummary of the tokenizersHugging Face Transformers docsHow text is split into tokens, and why a rare word becomes several pieces.
  • Official docstiktokenOpenAI on GitHubA real tokenizer you can run to count the tokens in any text.

Open this lesson on its own page

5. Inside the model: how it picks the next token

The picture

How the next token is picked
Tokens, prompt in pieces, NOWTokensprompt in pieces5 tokensNOWVectors, numbers per tokenVectorsnumbers per tokenAttention, weighs past tokensAttentionweighs past tokensLayers, repeat many timesLayersrepeat many timesScores, every known tokenScoresevery known token
  • Tokens — prompt in pieces — healthy — NOW — 5 tokens
  • Vectors — numbers per token — healthy
  • Attention — weighs past tokens — healthy
  • Layers — repeat many times — healthy
  • Scores — every known token — healthy
  • Tokens
  • Vectors
  • Weighed mix
  • Scores
Step 1 of 6

Step 1 of 6 · Tokens

Done: The prompt is split into 5 tokens

What happens
"The cat sat on the" becomes "The", "cat", "sat", "on", "the".
Why
A model works on tokens, and each token has its own number in the list of tokens the model knows.
What changes
Next, each token becomes a list of numbers.
Next
Each token becomes a vector

Event log

5 more to go. Press a line to go back to that step.

Tokens read
5
Numbers per token
–
not yet
Next token
not yet

Tokens: The·cat·sat·on·the

Step 1 of 6: Tokens. The prompt is split into 5 tokens. "The cat sat on the" becomes "The", "cat", "sat", "on", "the".

What this simplifies. This is a small made-up example. The weights and chances are round values chosen to add up as real ones do, not output from a real model. Real models use thousands of numbers per token and run many attention heads side by side in each layer.

A language model writes one token at a time. For each one, the prompt so far goes through the same parts in order. The model ends with a score for every token it could write next.

First, each token becomes a : a list of numbers the model learned in training. Then attention compares the tokens. Each token makes a query, a key and a value. The query of the newest token is matched against the key of every token so far, and the best matches get the most weight.

A layer is one round of attention, plus a network that works on each token alone. A large model stacks dozens of layers. At the end, the vector of the last token becomes a score for every token the model knows. The scores become chances that add up to 100%.

One token is then picked. At 0 the top score wins. At a higher temperature the token is drawn by chance, so answers vary. The picked token joins the prompt, and the whole run starts again for the next token.

Example. For "The cat sat on the", attention gives "cat" the most weight, 40% in the picture. The scores put "mat" at 42% and "floor" at 18%. At temperature 0 the model writes "mat". At temperature 1 it might write "floor".

In system design: Each new token is one more run, so a longer answer takes longer. The saves earlier keys and values, which spares work but takes memory.

Where it goes wrong

  • Attention compares each token with every earlier one, so the work grows faster than the prompt does. Very long prompts are slow to read.
  • At temperature 0 the top token wins, but small differences in the server's sums can still change a word now and then.

In an interview

  • Explain why a long answer takes longer than a long prompt. The prompt is read in one pass, but each answer token needs its own run.

Check yourself

The prompt has already been read. Why does a 400-token answer still take longer to arrive than a 40-token one?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

The next-token chances are "Paris" 70%, "Lyon" 20% and "the" 10%. What does the model write at temperature 0, and at a higher one?

Judge a design

A team says: "The model reads the prompt in one pass, so a 100,000-token prompt costs no more work than a 1,000-token one." What is wrong?

Sources and further reading (2)

First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.

Open this lesson on its own page

6. The context window

The picture

One window for prompt and answer

The prompt and the answer share one fixed space, so a long prompt leaves little room to answer.

Scene 1 of 4 · A fixed limit

This example model reads at most 128,000 tokens per request, counting the prompt and the answer together.

One request, in tokens
  • Context window (example model) (highlighted)128,000 tokens
  • Your prompt0 tokens
  • Room left to answer128,000 tokens
Pages that fit
about 300
  1. A fixed limit. This example model reads at most 128,000 tokens per request, counting the prompt and the answer together.
  2. A long prompt. A prompt of 120,000 tokens leaves room for only 8,000 tokens of answer.
  3. A whole manual. A 400-page manual is about 170,667 tokens, more than the whole window, so it cannot go in.
  4. Send what matters. Send only the 20 pages that matter, about 8,533 tokens, and most of the window is free.
  5. Now you try. Set the prompt size, and see how much room is left for the answer. For example: Room left for the answer: 8,000 tokens. The prompt and the answer share one window, so a shorter prompt leaves more room to answer.

Scene 1 of 4: A fixed limit. This example model reads at most 128,000 tokens per request, counting the prompt and the answer together.

Key points

  1. A model reads only so many tokens per request.
  2. The prompt and the answer count against the same limit.
  3. A document bigger than the window has to be cut down.
  4. Send only the parts that matter, and room is left over.

A model cannot read an unlimited amount of text in one request. The is the most tokens it can read at once, counting the prompt you send and the answer it writes together.

If a document is longer than the context window, it will not fit. It must be shortened, split into pieces, or chosen from carefully, so only the most relevant part is sent.

The size of the window depends on the model. It ranges from a few thousand tokens to a million or more, and many models also set a smaller limit on the answer alone. Check the limits of the model you use.

A bigger context window lets you send more background at once. Sending more text costs more, since you pay for every token the model reads, and a long request can also take longer to answer.

Example. Take an example model with a 128,000-token context window. It holds roughly 96,000 English words, about 300 pages of a book. A user manual longer than that cannot go into the prompt whole; it must be cut down first.

In system design: Your backend keeps the prompt, and any documents it adds, under the model's context window. Going over it means shortening text or sending fewer documents.

Where it goes wrong

  • A model may not use every part of a long prompt equally well. Facts in the middle are missed more often, so put key facts near the start or the end.
  • Filling the window costs money and time on every call, even when a short passage would have answered the question.

In an interview

  • Say how you would handle a document too big for the window. Search for the parts that matter, summarise, or split the job into several calls.

Check yourself

A model's context window is 8,000 tokens, and your prompt is 7,900 tokens. What does that leave for the answer?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

A model has a 128,000-token context window, and a document is 150,000 tokens. What can you do?

Judge a design

A team puts its one key rule in the middle of a 90,000-token prompt, and the model sometimes ignores it. What is the best change?

Sources and further reading (2)

First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.

Open this lesson on its own page

7. Why models make things up

The picture

A wrong guess, said with confidence

With no source in the prompt, a model fills the gap with a likely guess that sounds every bit as sure as a fact.

Scene 1 of 5 · Only the question

The bot sends the model only the customer's question, with no refund policy in the prompt.

CustomerCustomerSupport botSupport botLanguage modelLanguage modelRefund policyRefund policy
  • Customer — healthy
  • Support bot — healthy
  • Language model — healthy
  • Refund policy — healthy
The refund window the bot gave
What the prompt heldAnswer
The question only (highlighted)not yet
  1. Only the question. The bot sends the model only the customer's question, with no refund policy in the prompt.
  2. A likely number. The model writes "90 days", because that is a common refund window in its training text.
  3. Fluent but false. This is a hallucination: the answer sounds sure, but the real policy says 30 days.
  4. Add the policy. Now the bot puts the real policy in the prompt, with a rule to answer only from it.
  5. The true answer. The model answers 30 days and names the policy as its source, so anyone can check it.
  6. Now you try. Choose what the bot sends, and see what the model answers. For example: The bot answers "60 days", but the real policy says 30 days. Sources stop guessing only when they are right: an out-of-date policy gives a sure but wrong answer.

Scene 1 of 5: Only the question. The bot sends the model only the customer's question, with no refund policy in the prompt.

Key points

  1. The model judges what sounds likely, not what is true.
  2. A missing fact gets filled with a common guess.
  3. Real sources in the prompt, plus a rule to use them, cut this down.
  4. The source must be current, or the answer is wrong in a new way.

A model writes by guessing the next likely piece of text, one piece at a time. It has no built-in way to check whether a sentence is true; it only judges whether the words sound likely.

Without the right information in the prompt, the model can still write a fluent, confident sentence that is false. This is called a : a false statement written as if it were true.

A hallucination is not a lie, since lying needs intent, and the model has none. It is a wrong guess, stated in the same confident voice the model uses for a correct answer.

Four habits cut this down. Give the model real sources in the prompt, and tell it to answer only from them. Ask it to cite which source each claim came from, and check its answers before a user sees them.

Example. A support bot with no real policy in its prompt invents this answer: "You can get a refund within 90 days." The real policy says 30 days. The model guessed a common number, not the true one.

In system design: Any feature that answers from company data needs sources in the prompt and an instruction to use only them. It also needs citations and answer checks.

Where it goes wrong

  • An out-of-date source in the prompt gives a sure but wrong answer, so keep sources current.
  • A model can cite a source that does not say what it claims. Check that each quoted line really appears in the passage it cites.

In an interview

  • Do not claim made-up answers can be removed. Say how you make them rarer and how you catch the rest: sources, citations and checks.

Check yourself

Which change most directly cuts made-up answers about your own refund policy?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

A bot says "refunds within 30 days" and cites a policy passage that never mentions 30 days. What should your check do?

Judge a design

A product page says: "Our assistant never makes things up, because it answers from our documents." What is wrong with that claim?

Sources and further reading (2)

First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.

  • Research paperWhy Language Models Hallucinate (2025)Kalai et al., OpenAIArgues that training and grading reward a guess over saying “I don’t know”.
  • Official docsReduce hallucinationsAnthropic docs (one provider)Steps that lower the risk: allow “I don’t know”, ask for quotes, check the answer against them.

Open this lesson on its own page

8. Embeddings: meaning as numbers

The picture

Search by meaning (closest 3)

  1. I was charged twice for the same order this month.closeness 0.90 out of 1, an example score
  2. My package arrived damaged and I need a refund for it.closeness 0.86 out of 1, an example score
  3. The item does not match the listing, so I'd like a reimbursement.closeness 0.84 out of 1, an example score

Search for the same words

No sentence shares a word with "I want my money back".

Closest by meaning: I was charged twice for the same order this month.; My package arrived damaged and I need a refund for it.; The item does not match the listing, so I'd like a reimbursement..

  • Signing in
  • Refunds
  • Delivery
  • Closing an account

What this simplifies. Real embeddings have hundreds of numbers, not two, and a model computes them; this map was placed by hand to show the idea. Real systems usually compare two lists of numbers by the angle between them, but the idea is the same: close means similar in meaning. "Search for the same words" is also a crude stand-in. It matches exact word forms of four or more letters, with no ranking, so "refund" does not match "refunds".

An model turns a piece of text into a list of numbers called an embedding. It does not write answers. Many apps call a separate embedding model for search, apart from the model that writes answers. The exact set-up depends on the system.

Texts with a similar meaning get embeddings that land close together, even when they share no words at all. Texts with different meanings get embeddings that land far apart.

This lets software compare meaning with plain arithmetic: measuring the distance between two lists of numbers, instead of matching exact words. It says nothing about whether either text is true. Close also means the same topic, not the same claim: "refunds are allowed" and "refunds are not allowed" can land close together.

Example. "I can't log in" and "sign-in keeps failing" share no words, but their embeddings land close together, since both describe the same problem. "Please refund me" lands far from both.

In system design: Your backend calls an embedding model once per , a short passage cut from a document. It calls it again for each question, then compares the numbers.

Where it goes wrong

  • Embeddings from two different models cannot be compared. Mixing them in one gives matches that make no sense.
  • Close in meaning is not the same claim. A question about refunds may match a page that says refunds are not allowed.

In an interview

  • Say which embedding model embeds both the documents and the questions, and how you would test a switch to a new one.

Check yourself

"Refunds are allowed" and "refunds are not allowed" get very close embeddings. What does that tell you?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

Which pair of texts gets the closest embeddings?

Judge a design

A team embeds its old documents with model A and new ones with model B, all in the same index. What is wrong?

Sources and further reading (2)

First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.

Open this lesson on its own page

9. Vector databases: search by meaning

The picture

Search by meaning, fast

A vector database skips most of the work: one common kind of index sorts meanings into groups, then checks only nearby groups.

Scene 1 of 5 · Meanings stored

The database holds 2,000,000 help articles, each stored as an embedding of its meaning.

Help appHelp appVector databaseVector database2,000,000 articles
  • Help app — healthy
  • Vector database — healthy — 2,000,000 articles
Items checked for one question
  • Check every article?
  • Check nearby groups?
True closest article found?
not shown yet
  1. Meanings stored. The database holds 2,000,000 help articles, each stored as an embedding of its meaning.
  2. Check every one. Comparing the question with every article finds the true closest one, but takes 2,000,000 checks.
  3. Sort into groups. Ahead of time, one common kind of index sorts the articles into 1,000 groups of similar meaning.
  4. Check nearby groups. It finds the 2 nearest groups, then checks their articles: 5,000 checks in all, 400 times fewer.
  5. Now and then, a miss. Checking only the single nearest group is faster still, but here the closest article sat in the next one.
  6. Now you try. Choose how much to check, and see the work and whether the closest article is found. For example: 17,000 checks; the true closest article was found. That is 118 times less work than checking everything, and it still finds the closest one here.

Scene 1 of 5: Meanings stored. The database holds 2,000,000 help articles, each stored as an embedding of its meaning.

Key points

  1. Each stored item is an embedding: a list of numbers for its meaning.
  2. Checking every item is exact, but slow with millions of them.
  3. An index checks only a few nearby groups, far faster.
  4. Now and then the closest item sits in a group it skipped.

A stores embeddings, the lists of numbers that stand for meaning, and finds the ones closest to a given embedding. A list like this is also called a vector. This lets an app search by meaning instead of by exact words.

Comparing a question's embedding against every stored item works, but it turns slow once there are millions of them. Checking each one, one at a time, does not scale.

Most vector databases use instead: it checks only part of the data, so it runs much faster. Now and then, it misses a match that was truly closest.

Example. Finding the 3 help articles closest in meaning to a question, among 2 million stored articles, would take too long to check one by one. So the database searches approximately instead.

In system design: A vector database sits beside your normal database. Your backend embeds the question, asks the vector database for the closest matches, then fetches the full text for each one.

Where it goes wrong

  • Approximate search can miss the true closest match. Measure how many of the right matches it finds on real questions, and tune the index.
  • Filtering after the search, such as by customer, can leave too few results. Filter inside the search where the database allows it.

In an interview

  • Give the number of vectors, how fast a search must be, and how the index trades finding every match for speed.

Check yourself

Why do most vector databases search approximately?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

The vector database returns the 3 article vectors closest to a question. What does the backend do next?

Judge a design

A shared index holds help articles for many shops. The app takes the top 3 results, then drops those from other shops. Users sometimes get none. Why?

Sources and further reading (2)

First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.

Open this lesson on its own page

10. RAG: look it up, then answer

The picture

  1. FindSearch your own documents by meaning for passages that match the question.
  2. Build the promptPut the instructions, those passages and the question together.
  3. AnswerThe model writes its answer, told to use only those passages.
Flow

Runs for every question a user asks.

Question
Chunk size
2

On: the search returns 5 chunks, and a second check picks which to keep.

User, NOWUserNOWApplication, runs each stepApplicationruns each stepLanguage model, writes the answerLanguage modelwrites the answerEmbedding model, text to vectorEmbedding modeltext to vectorVector index, chunks and vectorsVector indexchunks and vectorsReranker, turned off, IDLERerankerturned offIDLE
  • User — healthy — NOW
  • Application — runs each step — healthy
  • Language model — writes the answer — healthy
  • Embedding model — text to vector — healthy
  • Vector index — chunks and vectors — healthy
  • Reranker — turned off — IDLE
  • Request
  • Search
  • Response
Step 1 of 6

Step 1 of 6 · User

Done: The user asks a question

What happens
The user sends a question to the application.
Why
The model was not trained on this shop's help pages. The application has to look the answer up first.
What changes
The application now holds the question: "Can I return a sale item after 20 days?"
Next
Turn the question into a vector

Event log

5 more to go. Press a line to go back to that step.

Chunks in the index
12
Chunks found
–
Passages in the prompt
–
Prompt size
–

The chunks found, the prompt and the answer show here as the run reaches them.

Step 1 of 6: User. The user asks a question. The user sends a question to the application.

Things to try

  • Keep 1 passage for the sale question. The closest chunk is the 30-day rule, so the answer is wrong.
  • Now turn the reranker on. The 14-day chunk moves to place 1 and the answer is right again.
  • Ask about price matching. The pages do not cover it, yet the search still returns its closest chunks.
  • Set the chunk size to 4 sentences. Whole pages go into the prompt, and its size grows.
  • Ask about Canada with chunks of 2 sentences and keep 1. The Canada sentence shares a chunk with the address rule, which pulls that chunk away. The chunk that says 3 to 5 days wins, so the answer is wrong.
  • Now keep 2 chunks, or turn the reranker on. The Canada chunk reaches the prompt and the answer is right.

What this simplifies. The help pages, the map places and the answers are made up for teaching. A real embedding has hundreds of numbers, worked out by a model. Here a chunk's place is the average of its sentences' places, which were set by hand. The reranker uses grades set by hand, where a real one is a second model. The answer follows a fixed rule: it is right when the passage with the answer is in the prompt. A real model usually follows its passages, but not always. Token counts come from this track's teaching splitter.

The full design adds a cache, limits and output checks. See it on the LLM serving and retrieval page.

, or RAG, answers questions from your own documents instead of from the model's memory alone. It works in three steps: find, build the prompt, and answer.

First, the app searches your documents by meaning for the passages that match the question, using the embeddings from the last two lessons. Only the closest few are kept.

Second, the app builds a prompt from the instructions, those passages and the question. Third, the model writes its answer, told to use only those passages. It usually does, but not always, so answers still need checks.

RAG is the most common way to give a model your own data. It needs no retraining, and an answer can point back to the real passages it was built from. The answer is only as good as the search: given the wrong passage, the model answers from that.

Example. A support bot searches the real refund policy, puts the matching passage in the prompt, and answers from it. That is better than guessing the policy from what it saw in training.

In system design: RAG adds document search before every model call. Cutting text into chunks (short passages), embeddings, a vector database and prompt limits all become part of your system's design.

Where it goes wrong

  • The search returns the wrong passages, and the model answers from them with confidence. Measure the search on its own.
  • The index falls behind the documents, so answers quote old prices or pages that were deleted.

In an interview

  • Draw two paths: indexing documents ahead of time, and the question path at request time. Then say how each one fails.

Check yourself

A RAG bot answers wrongly, and the passages it was given were about a different topic. What should you fix first?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

A RAG bot quotes a price that changed last month, and the model is working fine. What most likely failed?

Judge a design

A team plans to answer from company documents by training a model on them once a year, with no search. What is the main weakness?

Sources and further reading (2)

First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.

Open this lesson on its own page