← All topics
Knowledge

RAG: ground answers in retrieved knowledge

RAG fetches the most relevant chunks from your knowledge store and drops them into context, so the agent answers from retrieved facts with citations instead of guessing from training memory.

What it is

RAG is how you make an agent answer from facts it can look up instead of facts it half-remembers from training.

  • You keep a knowledge store (docs, tickets, code) chunked and indexed.
  • At question time you retrieve the few chunks most relevant to the query.
  • You paste those chunks into the prompt so the model answers from them, with citations.

The model stops guessing and starts quoting. That's the whole trick.

When to use it

Reach for RAG when the answer lives in data the model never saw or that changes often.

  • Private or internal knowledge: your docs, your codebase, your support history.
  • Fast-moving facts: pricing, policies, anything stale a week after training.
  • Anywhere you need a citation so the user can verify the source.
  • When hallucination is expensive and you'd rather the agent say "not in the docs."

When NOT to use it: skip RAG for reasoning or general-knowledge tasks where retrieval just adds noise and latency. See tool design when the answer should come from a live API call instead.

How it works

Two phases: build the index once, then retrieve per query.

  • Chunk: split each doc into passages (say, a few hundred tokens). Too big buries the answer; too small loses context.
  • Embed: run each chunk through an embedding model to get a vector, store it in a vector index alongside the raw text.
  • Retrieve: embed the user's question the same way, find the top-k nearest chunks (e.g. top_k=5).
  • Ground: drop those chunks into the prompt — Answer using only the context below — and ask the model to cite which chunk it used.

Example: query "what's our refund window?" pulls the billing-policy chunk, and the model answers "30 days" straight from that text.

Watch out for

  • Retrieval is the bottleneck, not the model. If the right chunk isn't in the top-k, no prompt can save the answer. Measure retrieval quality first — make it part of your evals.
  • Grounding is a request, not a guarantee. The model can still answer from memory and ignore your chunks. Instruct it to say "not found" when context is thin, and treat that as a guardrails check.
  • Chunk boundaries cut answers in half. A fact split across two chunks may surface neither cleanly. Overlap chunks slightly.
  • Stale index, wrong answers. If your store drifts from reality, RAG confidently serves old facts. Re-index on a schedule.

Related patterns

Ask the swarm