What is RAG (retrieval-augmented generation)?
Retrieval-augmented generation is open-book mode for AI: find the right pages first, then answer from them. It is how chatbots answer about your docs without retraining a model.

Key takeaways
- RAG retrieves relevant passages from your own content and gives them to a language model as context for its answer.
- It keeps answers current and specific without retraining, and lets you show sources.
- Most RAG failures are retrieval failures: bad chunks, missing documents or the wrong passages found.
RAG, short for retrieval-augmented generation, is a technique where an app first searches your own content for the passages most relevant to a question, then sends those passages to a language model along with the question so it answers from them. The model brings the language skills; your documents bring the facts.
An everyday comparison is an open-book exam. A student who memorized the course a year ago will misremember details. The same student allowed to flip to the right page first writes a precise answer and can point to where it came from.
The pipeline, step by step
Ahead of time (indexing):
- 01Collect the sources: help articles, policies, product specs, past support answers.
- 02Split them into chunks of a few paragraphs, each keeping its title and link.
- 03Create an embedding for each chunk and store it with the text (what embeddings are).
At question time:
- 01Embed the user's question and fetch the closest chunks, often 3 to 8 as a starting point (many systems also run a keyword search and merge the results).
- 02Build the prompt: instructions ("answer only from the sources below, say so if they do not cover it"), the retrieved chunks with their titles, then the question.
- 03Generate the answer and show the source links beside it.
Why RAG instead of a bigger prompt or retraining
- Fresh and editable: update an article and the next answer uses it. Retraining a model for every policy change is slow and costly.
- Fewer invented answers: grounding the model in real text is the main defense against hallucination.
- Fits the window: even a large context window is wasted and expensive if you paste the entire knowledge base into every call; sending only relevant passages is cheaper and often more accurate.
- Permissions: retrieval can filter by who is asking, so a customer never gets an answer built from an internal document.
Building RAG in a Startbase app
The parts map onto Startbase Cloud: documents in the database or file storage, server-side functions for indexing and answering, and the App AI gateway's `invokeLLM` to generate the reply without API keys in your app (AI features docs). The embedding step needs an embedding provider; plan where that runs before building. The docs chatbot prompt is a full, paste-ready brief, and building an AI chatbot app covers the product around it. A shorter starting brief:
Build a help assistant that answers from the articles in my help_articles table. Split each article into chunks of about three paragraphs that keep the article title and URL, and re-index an article whenever it is edited. For each question, retrieve the five most relevant chunks, answer with invokeLLM using only those chunks, and list the source links under the answer. If the chunks do not answer the question, say so and show the contact form. Log each question, the chunks used and whether the user clicked "helpful".
Common confusions and failure points
- "RAG trains the model on my data." It does not change the model at all; your content is only passed in at question time.
- Bad chunks. A chunk that splits a table or a step list in half loses its meaning. Chunk along headings.
- Missing content. If the answer is not in the sources, retrieval cannot find it. The question log shows what to write next.
- Stale index. Edited documents must be re-embedded, or answers quote the old version.
Frequently asked questions
Is RAG the same as fine-tuning?
No. Fine-tuning changes a model's weights with training examples, which shapes style and behavior. RAG leaves the model unchanged and supplies facts at question time. For answering from documents, RAG is usually the first choice.
How many documents do I need for RAG to be worth it?
If all your content fits comfortably in one prompt, you can just include it. RAG pays off once the content is too large, changes often, or needs per-user permissions.

