Build Your Own AI Agents

Lesson 4 of 7

Memory & Knowledge (RAG)

Agents forget everything between chats unless you give them memory. Learn short-term memory, long-term memory and retrieval.

🟡 Intermediate 65 XP

Learn it

Short-term memory is the current conversation.

Long-term memory saves useful facts (like 'prefers Python') to reuse later.

Retrieval lets an agent look things up in your own documents before answering, so it makes fewer things up.

Key terms

Context window
The maximum text a model can consider at once.
Embedding
A list of numbers representing meaning, so similar texts are close together.
RAG
Retrieving relevant documents and adding them to the prompt.
Hallucination
When an AI states something false confidently.

RAG in 4 steps

  1. 1Chunk: Split the class textbook into paragraphs.
  2. 2Embed: Turn each paragraph into a vector.
  3. 3Retrieve: For 'What is photosynthesis?', find the 3 closest paragraphs.
  4. 4Answer: The model answers using only those paragraphs and cites them.

Toy retrieval by keyword overlap

pythonnotes = ["Photosynthesis makes glucose using light", "Mitosis is cell division", "Light travels at 300,000 km/s"]
q = "how does photosynthesis use light"
score = lambda n: len(set(n.lower().split()) & set(q.split()))
best = max(notes, key=score)
print(best)

Real systems use embeddings instead of word overlap, but the idea is the same: find the most relevant text first.

Try it

Memory facts

Agents remember every past chat automatically.

RAG can reduce hallucinations.

It's fine for an agent to save a user's password in memory.

Challenge

Decide what your agent should remember long-term (3 things) and what it must never store (3 things). Then name one document collection it could search with RAG.

Pick whichever way suits you — every mode earns the same bonus XP.

Write at least 40 more characters to submit.

Mark your own work

Guided walkthrough — 0/2 clues revealed

  1. Clue 1 locked — reveal it only if you get stuck.
  2. Clue 2 locked — reveal it only if you get stuck.

Each clue costs 4 XP (never below 17 XP). You'd earn 33 XP right now.

Quiz time

Question 1 of 2Score 0

What does RAG add to the prompt?