Skip to content
47% off: the complete course for $79 (reg. $149)Order now →
Agent Builder
Module 3Full course13 min read

Retrieval-Augmented Generation for Agents

Ground agents in your own documents: chunking, retrieval, citations, and the agentic RAG patterns that beat a single lookup.

Ask a general-purpose model about your company's refund policy and you'll get a confident, plausible, completely generic answer. The model has never seen your policy. You could paste the policy into every conversation, but that stops working when "the policy" is really 400 help articles, three years of meeting notes, and a product manual that changes every month.

Retrieval-augmented generation (RAG) is the standard fix: find the few passages that matter for this question, put them in front of the model, and have it answer from them, with citations you can check. This lesson covers the whole pipeline: how to chunk documents, how to retrieve the right chunks, how to get answers with verifiable citations, and why letting the agent drive retrieval beats a single lookup. You'll finish with a working TypeScript agent that answers questions from your own documents and cites its sources.

How RAG works

RAG has two phases that run at different times.

Indexing happens ahead of time, whenever your documents change:

  1. Load your documents (help articles, PDFs converted to text, wiki pages).
  2. Chunk them into passages small enough to retrieve precisely.
  3. Embed each chunk and store it with its source and other metadata (see Embeddings and Semantic Search, Explained).

Answering happens on every question:

  1. Retrieve the chunks most relevant to the question.
  2. Generate an answer from those chunks, citing which ones it used.

Classic RAG runs steps 4 and 5 exactly once, in a fixed pipeline: question in, top five chunks pasted into the prompt, answer out. That works for simple questions. It breaks when the question needs two pieces of information, uses different words than the documents, or has no answer in the documents at all. That's why most modern systems hand retrieval to the agent as a tool. You'll get there after the fundamentals.