LearnFrenzy
Sign in Take a free test

Tools, Memory & RAG · Lesson 4 of 6

Agent Memory and RAG: Giving AI Agents Context and Knowledge

Models forget everything between calls and know nothing about your private documents. Learn the types of agent memory, how retrieval-augmented generation (RAG) works with embeddings and vector search, and the mistakes that make RAG answers wrong.

11 min read Intermediate Updated Nov 9, 2026 Reviewed by Saurabh Samir

Here is something that surprises many beginners: a language model has no memory at all between calls. Every time your code calls it, it sees only what you send in that request. Everything that feels like memory — remembering your name, the earlier steps, your college's rules — is something the application puts into the context on purpose.

Three kinds of memory an agent needs

TypeWhat it holdsWhere it lives
Working memoryThe current task: goal, steps taken, tool resultsThe context window of each call
Long-term memoryFacts that should survive between sessions — "prefers Python", "final year, ECE"A database or file, loaded when relevant
KnowledgeDocuments the agent should answer from — policies, notes, manualsA search index, used through RAG

Working memory: managing the context window

As an agent loops, the conversation grows: tool calls, results, more calls. Long runs hit the context limit or simply get slow and expensive. Common techniques:

  • Trim tool results to the fields that matter before adding them.
  • Summarise old steps — replace ten finished steps with a short "so far we have…" note.
  • Keep a scratchpad — a small, structured notes object (plan, findings, open questions) the agent updates, instead of rereading everything.

RAG: answering from your own documents

Suppose you want an assistant that answers questions about your college's academic rules — attendance, re-exams, grace marks. The model was never trained on that PDF, and even if it had been, rules change. Retrieval-Augmented Generation (RAG) solves this: before the model answers, you retrieve the most relevant passages and put them in the prompt.

Step 1 — Prepare the documents (once)

  1. Split each document into chunks of a few hundred words, with a little overlap so sentences are not cut in half.
  2. Embed each chunk: an embedding model turns text into a list of numbers (a vector) that captures its meaning. Texts with similar meaning get similar vectors.
  3. Store the vectors with their text in a vector database or search index.

Step 2 — Answer a question (every time)

  1. Embed the user's question.
  2. Find the chunks whose vectors are closest to it (semantic search).
  3. Send the question plus those chunks to the model with an instruction like: "Answer only from the context below. If the answer is not there, say you don't know."
# The core of RAG, without any framework
question = "How much attendance do I need to sit for the end-semester exam?"
chunks   = search_index(embed(question), top_k=4)          # most similar passages

context = "\n\n".join(f"[{c['source']}, p.{c['page']}]\n{c['text']}" for c in chunks)
prompt  = f"""Answer the student's question using ONLY the context.
Quote the rule and give the source in brackets. If the context does not answer it,
reply: "I couldn't find this in the academic rules."

<context>
{context}
</context>

Question: {question}"""
answer = call_model(prompt)

Why RAG matters: it lets a model use private and up-to-date information without retraining, and it makes answers checkable — you can show the source passage next to every answer.

Basic RAG searches once for every question. In an agent, retrieval becomes just another tool — search_documents(query). The agent can search, notice the result is about the wrong semester, search again with a better query, and combine two documents. This handles harder questions, at the cost of more calls.

Why RAG answers go wrong (and fixes)

  • Retrieval missed the right chunk. Most "the model is wrong" bugs are actually search bugs. Test retrieval separately: for 20 real questions, is the right passage in the top results?
  • Chunks are too big or too small. Huge chunks bury the answer; tiny ones lose context. Split along headings when you can.
  • Keyword-heavy questions. Course codes, roll numbers and exact names are better found by keyword search. Many systems combine keyword and semantic search (hybrid search).
  • Old documents. If the 2023 rules and the 2026 rules are both in the index, the model may quote the wrong one. Store dates and filter on them.

Privacy: anything you put in the index can end up in an answer. Do not index documents some users should not see, unless you filter results by the user's permissions before they reach the model.

Try it yourself: Take one long document you know — a syllabus or an internship policy. Split it into chunks by heading, write five questions, and for each question note which chunk should be retrieved. This "retrieval test set" is exactly how teams evaluate RAG.

Common interview questions

Try answering each one aloud before you open it.

1What is RAG and why is it used?

Retrieval-Augmented Generation retrieves relevant passages from a document collection and includes them in the prompt so the model answers from them. It adds private or recent knowledge without retraining and reduces hallucinations because answers can be grounded and cited.

2What is an embedding?

A list of numbers produced by an embedding model that represents the meaning of a piece of text. Similar meanings produce vectors that are close together, which enables semantic search.

3Do LLMs remember previous conversations?

Not by themselves. Each call only sees what is sent in its context. Applications create memory by including chat history, summaries or stored facts in the prompt.

Practise this

Build something with it

FAQs

Do I always need a vector database for RAG?

No. For a few hundred chunks, an in-memory list and a similarity function are enough. Vector databases help when you have many documents, need filters or need fast search at scale.

RAG or fine-tuning — which should I use?

Use RAG to give a model knowledge (facts, documents that change). Use fine-tuning to change behaviour or style. Many real systems use RAG alone.

References & further reading

Finished this lesson?

Mark it complete. Create a free account to save it on every device.

Next lesson

Search LearnFrenzy

Esc

Quick links