Cloud Explainers

How RAG Works

LLM infrastructure9 min video

Ask a language model about your own business and it guesses: here a help desk model invents a 25% restocking fee when the store's own policy says 15%. RAG (retrieval-augmented generation) fixes that by looking the answer up first. On a real lab with local models (Ollama, nomic-embed-text and llama3.2), we build it step by step: chunk the documents, embed them into vectors, search by meaning, put the closest chunks in the prompt, and let the model answer from them, then watch a stale index miss a new rule until it is rebuilt.

Made up

Ask an AI about your own rules, and it makes up an answer. Ask a store's help desk bot about the restocking fee on an opened drill: twenty-five percent.

Kari: ~/helpdesk
ask "What is the restocking fee on an opened drill?"According to our store policy, if an item is
opened or damaged, we charge a restocking
fee of 25% of the original purchase price.

But a search of the store's own policy files finds fifteen. The AI has never read them. It was very sure anyway.

Kari: ~/helpdesk
grep -i restocking docs/*.mddocs/returns.md:Power tools that have been opened or used, such as drills, sanders and saws, can be returned within 14 days only if they still work and every part is back in the box. A 15% restocking fee is taken off the refund.

Kari owns this hardware store, and Tom is the customer who asked. Every rule her bot invents is an argument she has to settle at the counter.

Kari's Hardware: diagram of the drill, says 25%, returns: 15%

The fix is called RAG, retrieval augmented generation: look it up first, then answer.

The fix has a name: diagram of Retrieval-Augmented Generation, Retrieval = find the right pages, Augmented = add them to the question, Generation = then the model answers, Look it up first, then answer

By the end, her help desk quotes her own policy, and when the answer isn't in it, it says so.

By the end of this video: cards of Quotes her policy, Or says so

An open book

First: why does the model guess, instead of saying it doesn't know? A language model learned from a huge pile of public text, once. Kari's policy was never in that pile. So the model does what it was trained to do: it writes the most likely sounding answer.

What the model learned, and what it never saw: diagram of Public text, The model, Kari's policy, Likely answer

It's a closed-book exam on a book it never read. RAG turns it into an open-book exam: before the model answers, her program looks up the right pages in her own documents, and hands them over with the question.

Closed book, then open book: diagram of Tom's question, Help desk, Her documents, The model, Guessed, Reads first

Here's the whole thing she's going to build, in two parts. Ahead of time, once, her program cuts the documents into small pieces, turns each piece into numbers, and stores them. Then, for each customer question, it finds the pieces closest in meaning, and hands them to the model together with the question. Retrieve, augment, generate: the three words in the name, in that order.

RAG: look it up, then answer: diagram of Store documents, Chunks, Embedding model, Index, Question, Search, Prompt, The model, Once, ahead of time, For every question

Chunk

Step one: a real store's files are too long to hand over whole, so her program cuts them into small pieces called chunks. She has five small files, one per topic, like returns and rentals. Her chunk command makes one chunk per paragraph, each one tagged with its file and number. Sixteen chunks from five files. Size matters: too big, and one chunk mixes several topics; too small, and a rule gets cut in half. A paragraph is a good place to start.

Kari: ~/helpdesk
ls docshours.md  keys.md  paint.md  rentals.md  returns.md
Kari: ~/helpdesk
chunkrentals#1   We rent a carpet cleaner, a ...
rentals#2   A rental day runs from openi...
rentals#3   Late returns are charged a f...
returns#1   Most items can be returned w...
returns#2   Power tools that have been o...
returns#3   Paint that has been tinted o...
returns#4   Without a receipt we give st...
16 chunks from 5 files

Why not just paste all five files into every question? Here, you could: they're four hundred and ten words. But a real store has hundreds of pages, more than a model can read at once. Later, the search will hand the model only the three closest chunks, plus the question: about one hundred and sixty-two words.

Words the model reads per question: chart of All 5 files: 410 words, 3 chunks + question: 162 words, A real store: hundreds of pages

Embed

Step two, and the hard part: customers rarely use the store's words. A customer says bring back; the policy says returned. So her program has to match meaning, not spelling. An embedding model reads some text and turns it into a long list of numbers. That list is called a vector, and it stands for what the text means. It's a separate, smaller model from the one that writes answers. Short text or long, it always gives the same number of values. Feed it opened drill: two words in, seven hundred and sixty-eight numbers out.

Kari: ~/helpdesk
embed "opened drill"768 numbers: [-0.036, -0.013, -0.178, -0.038, ...]

Think of each vector as a spot on a map of meaning. Texts about the same thing land near each other, even when they share no words. Return a drill, refund for a sander: close together. Cut a spare key is about something else, so it lands further away. The real map has seven hundred and sixty-eight directions, not two, but the idea is the same.

A map of meaning: diagram of return a drill, refund for a sander, cut a spare key, paint a wall, about returns, about other things

She can measure it. Her similar command embeds two texts and scores how close their meanings are, from zero up to one: the higher, the closer. She compares return a drill with bring back a power tool, and then with rent a tile saw. Guess first: which pair scores closer? The power tool, zero point six six, against zero point five seven for the tile saw. Texts about one store always score a bit alike, so the exact number matters less than which one is higher. Not one shared word, and the power tool still wins.

Kari: ~/helpdesk
similar "return a drill" "bring back a power tool"0.66  'return a drill'
      vs 'bring back a power tool'
Kari: ~/helpdesk
similar "return a drill" "rent a tile saw"0.57  'return a drill'
      vs 'rent a tile saw'

Now she embeds every chunk, once, and saves the vectors in a file: the index. Sixteen chunks in under a second, and the index is a hundred and sixty-four kilobyte file of numbers. A big store might use a vector database, a database built for searching vectors; for sixteen chunks, a file is plenty.

Kari: ~/helpdesk
indexEmbedded 16 chunks, 768 numbers each
in 0.8 s -> index.json
Kari: ~/helpdesk
ls -lh index.json-rw-r--r-- 1 kari kari 164K Nov 28 04:27 index.json

Augment and generate

Back in the drill search, two of the three chunks were about rentals. So how does the model know to ignore them? Step four is augment: adding the chunks to the question, with clear rules. Her program builds one prompt for the model. On top go the rules: answer only from these documents. If they don't have the answer, say you don't know. And name the chunk you used. Then the three chunks, then Tom's question.

One prompt: instructions, chunks, question: diagram of Instructions, [returns#2], [rentals#3], [rentals#2], Tom's question

Step five is generate: the model writes the answer. Her rag command runs the last three steps, search, augment and generate. With the show option, it also prints what it found along the way. The answer: fifteen percent, and the chunk it came from. Same model as at the start. The only change: this time, it took the exam with the book open.

Kari: ~/helpdesk
rag --show "What is the restocking fee on an opened drill?"Top 3: returns#2, rentals#3, rentals#2
Prompt: 162 words (3 chunks + question)
Answer:
The restocking fee on an opened drill is 15%
of the original price. [returns#2]

And when the answer isn't there? Do you rent kayaks? Under the hood, the search still found its three closest chunks, because it always returns the closest three. They're about rentals, and none mention kayaks. So the model follows its rules: I don't know, please ask at the counter. Better than a made-up kayak.

Kari: ~/helpdesk
rag "Do you rent kayaks?"I don't know, please ask at the counter.

The stale index

But store rules change. What happens when her files change and the help desk doesn't notice? In December, Kari adds a holiday rule to the returns file: anything bought from mid-November to Christmas Eve can come back until January thirty-first. The file's last lines show it's saved. A customer got a ladder as a gift on December tenth, and wants to return it on January twentieth. Guess first: does rag find the new rule now? No. It still says: I don't know.

Kari: ~/helpdesk
tail -n 2 docs/returns.mdHoliday returns: anything bought from November 15 to December 24 can be returned until January 31, with the receipt or a gift receipt.
Kari: ~/helpdesk
rag --show "Got a ladder as a gift Dec 10. Return it Jan 20?"Top 3: returns#2, rentals#3, returns#1
Prompt: 153 words (3 chunks + question)
Answer:
I don't know, please ask at the counter.

Why? The search never reads the files. It reads the index. And the index is a snapshot from the last time she ran her index command. It still holds sixteen chunks, and none of them is the new paragraph. Real systems re-index on a schedule, or each time a document is saved.

The search reads the index, not the files: diagram of Store documents, Index, Search

So whenever the documents change, she runs index again. Seventeen chunks now. Same question: this time the top chunk is chunk five of the returns file, the holiday rule, and the answer is yes, until January thirty-first.

Kari: ~/helpdesk
indexEmbedded 17 chunks, 768 numbers each
in 0.7 s -> index.json
Kari: ~/helpdesk
rag --show "Got a ladder as a gift Dec 10. Return it Jan 20?"Top 3: returns#5, returns#2, rentals#3
Prompt: 155 words (3 chunks + question)
Answer:
Since the gift was received on December
10th, it falls under the holiday return
period, which is from November 15th to
December 24th. However, the return window is
until January 31st. Since January 20th is
before January 31st, the return is allowed.
[returns#5]

Saturday

Saturday morning, Tom is back with one more: can he bring back a sander he opened? This time the help desk answers from Kari's own policy: yes, if it still works and every part is in the box, minus the fifteen percent fee, not the twenty-five it made up before. And it names the chunk it read.

Kari: ~/helpdesk
rag "Can I bring back a sander I already opened?"You can return a sander that has been
opened, but it must still work and every
part must be back in the box. A 15%
restocking fee will be taken off the refund.
[returns#2]

Tom reads it, nods, and goes to find the box. No argument at the counter.

The help desk reads first: diagram of her policy, says 15%

Recap

So that's RAG. Once: chunk your documents, embed them, store the index. For each question: search for the closest chunks, put them in the prompt, and let the language model answer from them. And when the documents change, index again.

RAG: look it up, then answer: diagram of Store documents, Chunks, Embedding model, Index, Question, Search, Prompt, The model, Once, ahead of time, For every question

If your own assistant should look things up before it answers, a like helps more beginners find this, and subscribing brings the next one, built and tested in a real lab.

Every command in this video ran with local models, on our own computer, not a cloud service, and the lab files are linked in the description. Thanks for watching Cloud Explainers.

Sources

Demos: two containers on our own Mac, local models only (no account or API key); Kari's hardware store and its documents are made up. Every command and output was real. Watch the full video on YouTube.

More videos

AWS8 min

Amazon Bedrock vs OpenRouter: Which One Should You Use?

Go (Golang)5 min

Golang Channels (chan) in 5 Minutes

Homelab & self-hosting21 min

Tailscale: Full Tutorial for Beginners

Git11 min

Every Git Setting to Change on Day One

Linux9 min

WSL vs a Linux Virtual Machine: Which One Should You Use?

PostgreSQL9 min

Every PostgreSQL Setting to Change After You Install It

Docker9 min

What Happens When You Install Docker on Ubuntu

Python8 min

How to Fix Python 'Address Already in Use' Errors

Go (Golang)5 min

Golang Interfaces and Mocks in 5 Minutes

Git7 min

How to Fix Git 'Detached HEAD' State

Linux21 min

Linux Firewall with ufw: Full Tutorial for Beginners

Go (Golang)5 min

Golang defer Statements in 5 Minutes

Auth (OAuth, JWT)9 min

How to Fix OAuth 'invalid_grant' Errors

Databases & SQL11 min

The SQL Roadmap for Beginners

Go (Golang)5 min

Golang make() for Slices and Maps in 5 Minutes

Networking7 min

How to Fix Network 'No Route to Host' Errors on Linux

Linux6 min

How to Fix Linux 'Too Many Open Files' Errors

Django (Python web framework)11 min

Every Django Concept a Beginner Needs to Know

Linux24 min

SSH on Linux: Full Tutorial for Beginners

Go (Golang)5 min

Golang for range Loops in 5 Minutes

Databases & SQL10 min

SQL Databases: SQLite vs PostgreSQL

Django (Python web framework)10 min

The Django Roadmap for Beginners

Go (Golang)5 min

Golang Structs in 5 Minutes

Caching & Redis22 min

Redis: Full Tutorial for Beginners

Go (Golang)5 min

Golang context.Context in 5 Minutes

Databases & SQL7 min

How to Fix MySQL Database Error 1045: Access Denied for User

Go (Golang)5 min

Golang Method Receivers in 5 Minutes

Python8 min

How to Fix Python SSL: CERTIFICATE_VERIFY_FAILED Errors

Django (Python web framework)9 min

How to Fix Django Static Files Not Loading in Production

OpenRouter (one API for many AI models)8 min

OpenRouter vs Direct API Keys: Which One Should You Use?

Go (Golang)5 min

Golang if err != nil Checks in 5 Minutes

Go (Golang)8 min

How to Fix Golang 'go.mod File Not Found' Errors

AI agents & MCP22 min

MCP Servers: Full Tutorial for Beginners

Go (Golang)5 min

Golang Error Return Values in 5 Minutes

Python8 min

How to Fix Python ModuleNotFoundError: No Module Named

Go (Golang)5 min

Golang Pointers in 5 Minutes

Kubernetes8 min

How to Fix a Kubernetes Pod Stuck in CrashLoopBackOff

Homelab & self-hosting9 min

How to Fix a Homelab ESXi Host Not Responding

PostgreSQL24 min

PostgreSQL: Full Tutorial for Beginners

Load balancing & Nginx9 min

How a Load Balancer Works with HAProxy

Python9 min

How Python Virtual Environments Work

Homelab & self-hosting9 min

How to Fix Cloudflare Tunnel Errors in a Homelab

APIs9 min

How to Fix API CORS Errors: No 'Access-Control-Allow-Origin'

Auth (OAuth, JWT)9 min

How Authenticator App Login Codes Work (TOTP)

Git22 min

Git and GitHub: Full Tutorial for Beginners

Homelab & self-hosting10 min

How a Homelab Hypervisor Works (VMware ESXi)

Kubernetes9 min

How to Fix Kubernetes ImagePullBackOff Errors

DNS8 min

How to Fix DNS 'Temporary Failure in Name Resolution' on Linux

AWS8 min

How to Fix AWS CLI 'Unable to Locate Credentials'

Homelab & self-hosting10 min

Proxmox vs ESXi for Your Homelab: When Each One Wins

PostgreSQL10 min

PostgreSQL UUID vs Auto Increment: Which Primary Key Should You Use?

Kubernetes21 min

Kubernetes: Full Tutorial for Beginners

Networking9 min

How TCP Sockets Work

PostgreSQL7 min

How to Fix PostgreSQL 'Could Not Connect to Server'

Claude & the Claude Developer Platform9 min

Claude Code Hooks Will Finally Make Sense After This

DevOps & CI/CD9 min

DevOps with Ansible Will Finally Make Sense After This

Networking9 min

How SNMP Network Monitoring Works

Git8 min

How to Fix GitHub 'Permission Denied (publickey)' Errors

Docker21 min

Docker: Full Tutorial for Beginners

PostgreSQL7 min

How to Fix PostgreSQL 'Password Authentication Failed'

Linux8 min

Why One Linux Server Handles More Users Than You Think

Networking7 min

How to Fix Network 'Connection Refused' Errors

DevOps & CI/CD9 min

DevOps Pipelines: Jenkins vs GitHub Actions

Linux11 min

Linux Nginx Reverse Proxy: Full Tutorial for Beginners

Networking11 min

Every Networking Term a Beginner Needs in 12 Minutes

Docker6 min

How to Fix Docker 'Permission Denied' on docker.sock

Git7 min

How to Fix Git 'Updates Were Rejected' When You Push

Linux7 min

How to Fix nginx 502 Bad Gateway Errors on Linux

Linux8 min

How to Fix Linux apt 'Could Not Get Lock' Errors (Ubuntu)

Networking9 min

How Email Works on the Internet (SMTP) in 10 Minutes

Python8 min

The Python Roadmap for Beginners

Go (Golang)21 min

Golang REST API with Gin: Full Tutorial for Beginners

Networking10 min

How a WireGuard VPN Network Works

Linux9 min

Linux Makefiles Will Finally Make Sense After This

Auth (OAuth, JWT)9 min

SAML vs OAuth vs OpenID Connect: What's the Difference?

Python7 min

How to Fix Python cron Jobs That Never Run

Linux22 min

Linux Bash Scripting: Full Tutorial for Beginners

Networking10 min

How PXE Network Boot Works

Active Directory9 min

How Active Directory Works

Linux10 min

How to Fix a Linux systemd Service That Won't Start

Linux9 min

Linux Logs: syslog vs journald

Auth (OAuth, JWT)9 min

JWT vs Session Cookies: Which One Should You Use?

Docker10 min

Docker vs Docker Compose: Which One Should You Use?

Docker9 min

How to Fix Docker Compose Containers That Can't Talk to Each Other

Django (Python web framework)22 min

Django Full Tutorial for Beginners

Linux9 min

How Linux systemd Works

Networking8 min

How to Fix 'Windows Cannot Access' Network Share Errors (SMB)

APIs10 min

REST vs gRPC: Which One Should You Use?

Certificates & PKI (ADCS)9 min

How to Fix SSL Certificate Chain Errors (Missing Intermediate)

Python9 min

Every Python Concept for Beginners in 10 Minutes

Databases & SQL9 min

Every SQL Join Type in 10 Minutes

OpenRouter (one API for many AI models)9 min

Why OpenRouter Free Models Hit Their Limit So Fast

Gemini API & Google AI Studio9 min

Why Your Gemini API Free Tier Runs Out So Fast

OpenRouter (one API for many AI models)10 min

OpenRouter Will Finally Make Sense After This

Caching & Redis10 min

How Redis Caching Works

Gemini API & Google AI Studio10 min

Gemini API vs Vertex AI: Which One Should You Use?

Gemini API & Google AI Studio8 min

How to Fix a Leaked Gemini API Key

Go (Golang)9 min

Why Golang Uses Goroutines Instead of Threads

Gemini API & Google AI Studio9 min

How to Fix Gemini API Error 429: Resource Exhausted

Claude & the Claude Developer Platform10 min

Claude Token Pricing Will Finally Make Sense After This

Python11 min

Why Python Web Scraping Returns Nothing (and the Fix)

Python22 min

Python Automation: Full Tutorial for Beginners

Auth (OAuth, JWT)9 min

How OAuth Login Works

Python10 min

Python uv vs pip: Which One Should You Use?

Gemini API & Google AI Studio10 min

Google AI Studio Will Finally Make Sense After This

Docker10 min

What Happens When You Run a Docker Container

Claude & the Claude Developer Platform8 min

How to Fix Claude Code Billing Your API Key Instead of Your Plan

Auth (OAuth, JWT)9 min

Why Passkeys Can't Be Phished Like Passwords

PowerShell10 min

PowerShell vs Bash: Which One Should You Learn?

Linux10 min

The Linux Roadmap for Beginners

Django (Python web framework)9 min

Django vs FastAPI: Which One Should You Use?

Databases & SQL10 min

Why SQLite Is the Most Used Database in the World

Databases & SQL11 min

How a NoSQL Database Like MongoDB Works

Python11 min

How to Fix Python pip error: externally-managed-environment

Databases & SQL9 min

How to Fix MySQL Too Many Connections Database Errors

Go (Golang)9 min

Golang Concurrency Will Finally Make Sense After This

AWS10 min

How Amazon S3 Works

Kubernetes10 min

How Kubernetes Works

Git10 min

How Git Works

Docker10 min

How to Fix Docker Port Already Allocated Errors

PostgreSQL10 min

How PostgreSQL Works

Git8 min

How to Fix Git Merge Conflicts

Linux9 min

How to Fix No Space Left on Device in Linux

Networking10 min

How the Internet Works

Databases & SQL9 min

SQL vs NoSQL: When Each One Wins

Auth (OAuth, JWT)8 min

How to Fix JWT Token Expired Errors (401 Unauthorized)

Claude & the Claude Developer Platform9 min

Claude API vs Claude Pro: Why Your Subscription Doesn't Include It

AI agent skills9 min

Why AI Agent Skills Beat Giant Prompts

Linux10 min

Linux Cron Jobs Will Finally Make Sense After This

Cybersecurity10 min

Security Logs Will Finally Make Sense After This

Cybersecurity8 min

How to Fix a Security Leak: An API Key Pushed to GitHub

DNS9 min

DNS Will Finally Make Sense After This

Cybersecurity9 min

Never Ignore a Security Update

Auth (OAuth, JWT)9 min

Two-Factor Authentication Will Finally Make Sense

Networking8 min

Never Expose a Database to the Internet

Cybersecurity10 min

SSH Keys vs Passwords: Which Is Safer for Security?

Linux10 min

The 5 Levels of Linux Boot: From Power On to Login

Networking12 min

The 5 Levels of Network Firewalls

Cybersecurity10 min

Password Security: Why Hackers Crack Weak Passwords

Linux9 min

Why Your Linux Server Runs Out of Memory

Networking10 min

HTTP vs HTTPS: What Actually Changes on Your Network

Linux12 min

Every Linux File Permission You Need to Know

Linux10 min

Linux Processes vs Threads: When Each One Wins

Networking11 min

The OSI Model Will Finally Make Sense After This

Git11 min

The 7 Levels of Git

Networking10 min

Every Network Troubleshooting Command You Need

Cybersecurity10 min

Hashing vs Encryption in Cybersecurity: When Each One Wins

Networking11 min

Why the Internet Ran Out of IP Addresses

Linux11 min

Linux Package Managers: apt vs Snap vs Flatpak

Python9 min

Why Python Is Slow (and When It Doesn't Matter)

Networking11 min

TCP vs UDP: When Each One Wins

Linux8 min

Stop Running Linux as Root (Do This Instead)

Linux11 min

Linux Users and Groups Will Finally Make Sense

Cybersecurity9 min

Why Phishing Beats Good Security (and How to Spot It)

Docker12 min

The 7 Levels of Docker

AI agents & MCP12 min

AI Agents Will Finally Make Sense After This

Git8 min

Stop Using git push --force (Use This Instead)

AI agent skills10 min

Agent Skills vs MCP: When Each One Wins

Networking12 min

Networking Will Finally Make Sense After This

Linux12 min

Every Linux Command You Actually Need

Cybersecurity12 min

The 7 Levels of Cybersecurity

System design12 min

Every System Design Concept You Need, in One Diagram

PostgreSQL10 min

Why PostgreSQL Ignores Your Index