On this page7 min left
At a glance
RAG is two systems, not one: an offline indexing pipeline and an online answer pipeline.
Retrieval chooses which external evidence enters the prompt; attention decides how tokens inside that prompt influence generation.
Production quality depends more on permissions, retrieval evaluation, freshness, and failure handling than on the vector database brand.
01The boundary
Why RAG exists
A language model can generate fluent text from patterns encoded in its parameters. It cannot automatically inspect your current refund policy, private repository, incident log, or product catalogue. Even when relevant facts were present during training, the model may not reproduce them accurately or show where they came from.
Retrieval-Augmented Generation adds an explicit knowledge path. Before generation, the application searches an external corpus, selects useful evidence, and supplies that evidence with the question. The original RAG paper described a model combining parametric memory with a dense, non-parametric index. In production engineering, “RAG” now commonly describes the wider retrieve-then-generate system around an LLM.
This guide uses a company refund-policy question as a running example. The architecture is the same for support assistants, code search, compliance workflows, research tools, and internal knowledge systems.
02The system map
Think in two pipelines
A clean RAG design separates work that prepares knowledge from work that answers a request. The indexing pipeline can run on a schedule or react to source changes. The answer pipeline must execute within a user-facing latency budget.
Loading diagram…
- Offline: ingest source documents.
- Parse and normalize their structure.
- Chunk the content and attach metadata and access controls.
- Create embeddings and write searchable records to a hybrid index.
- Online: receive a user question and rewrite or embed it.
- Retrieve candidates the caller may access.
- Rerank and filter those candidates.
- Assemble a bounded context with citations.
- Generate a grounded answer and return its sources.
- Offline indexing: ingest sources, preserve structure, attach metadata and access controls, create searchable representations, and update or delete them when the source changes.
- Online answering: understand the question, retrieve candidates the user may access, rerank them, assemble a bounded context, generate an answer, and return inspectable sources.
This boundary is operationally useful. You can re-index a source without changing the answer API, replace a reranker without re-parsing documents, and evaluate retrieval separately from generation.
03Offline path
Indexing: make knowledge retrievable
The job of indexing is not to turn every file into equal-sized strings. It is to preserve enough meaning, provenance, and permission data that the right passage can be found later.
- Parse with structure. Keep headings, lists, tables, page references, code boundaries, and source identifiers. A format-aware parser is usually more valuable than another prompt tweak.
- Chunk around meaning. Use section boundaries, semantic units, or task-specific rules. There is no universal “few hundred tokens” setting; chunk size is an evaluation parameter.
- Attach metadata and ACLs. Store source, revision, owner, timestamp, document type, and access-control attributes with every retrievable unit.
- Create complementary indexes. Dense embeddings help with semantic similarity. Lexical search protects exact names, identifiers, error codes, and domain language. Many production systems combine both.
- Treat updates and deletion as first-class. A stale chunk with a strong similarity score is still the wrong answer.
for document in source.changed_documents():
parsed = parser.parse(document)
for chunk in chunker.split(parsed):
index.upsert(
id=stable_id(document, chunk),
text=chunk.text,
vector=embed(chunk.text),
metadata={
"source": document.url,
"revision": document.revision,
"acl": document.allowed_principals,
},
)
for document_id in source.deleted_documents():
index.delete(where={"document_id": document_id})04Online path
Retrieval and generation
When a user asks “What is our refund policy?”, the application turns that request into one or more search queries. A first-stage retriever searches broadly. A second-stage reranker spends more compute on a smaller candidate set. Context assembly then selects the best passages that fit the model’s input budget.
Loading diagram…
- Expand the user question into search queries.
- Use dense and lexical search to gather a broad candidate set.
- Merge results and enforce permission and metadata filters.
- Rerank candidates for the actual question.
- Remove duplicates and low-value passages.
- Pack the strongest evidence into the available context budget.
The funnel matters because nearest-neighbour search is only a candidate generator. A high cosine-similarity score does not prove that a passage is current, authoritative, complete, or appropriate for the task.
def answer(question: str, principal: Principal) -> Answer:
queries = query_planner.expand(question)
candidates = retriever.search(
queries=queries,
filters={"acl": principal.effective_groups},
limit=40,
)
ranked = reranker.rank(question, candidates)
context = context_builder.pack(ranked, token_budget=6_000)
return generator.generate(
question=question,
evidence=context,
require_citations=True,
)05A common confusion
Retrieval is not attention
RAG and Transformer attention operate at different boundaries. Retrieval runs outside the generator and decides which external evidence becomes input. Self-attention runs inside the Transformer and computes how the tokens already in that input influence one another during representation and generation.
Loading diagram…
- The application receives a user question.
- Retrieval searches an external corpus for relevant, allowed passages.
- The application combines the question and selected passages into a prompt.
- Inside the Transformer, self-attention relates tokens in that prompt.
- The generator produces an answer from those token representations.
The distinction is practical. If the policy document never reaches the prompt, changing attention cannot recover it. If retrieval returns the correct passage but the response ignores or misinterprets it, the failure is in context construction, prompting, or generation rather than search.
Technical deep dive: where Q, K, and V fit
In scaled dot-product attention, a query is compared with keys to produce weights over values. The Transformer computes softmax(QKᵀ / √dₖ)V across token representations. Multi-head attention repeats this operation in learned representation subspaces. The mechanism comes from Attention Is All You Need; it explains how supplied context is processed, not how an application discovers external documents.
06Topology choices
Beyond naive RAG
Advanced RAG patterns are responses to specific failure modes. They are not maturity levels that every system must climb.
- Naive RAG uses one query, one retrieval pass, and one generation step. It is a strong baseline when the corpus and questions are narrow.
- Hybrid and fusion retrieval combine semantic and lexical candidates, sometimes using multiple query formulations and reciprocal-rank fusion before reranking.
- Corrective RAG evaluates retrieval quality and chooses a corrective action when evidence is weak. The original CRAG paper describes confidence-based retrieval actions, web-search augmentation, and knowledge refinement.
- Agentic RAG lets a planner decide when and where to retrieve, inspect results, and issue follow-up searches. It adds flexibility along with more latency, cost, and failure states.
- Graph RAG represents entities and relationships explicitly for questions that require connections or corpus-level summaries. Microsoft’s GraphRAG documentation describes an indexing pipeline that extracts entities, relationships, claims, and community summaries.
07Build boundary
Choose your abstraction level
The useful decision is not which library wins. It is where your team wants to own complexity. These categories are representative examples as of July 2026, not endorsements.
| Decision | Maximum controlRaw SDKs and primitives | Composable middleOrchestration framework | Fastest assemblyPackaged RAG stack |
|---|---|---|---|
| Best for | Teams with unusual retrieval logic, strict latency goals, or strong platform engineering. | Teams that want reusable retrievers, document loaders, tool interfaces, and fast experimentation. | Teams validating a standard knowledge workflow before investing in custom infrastructure. |
| You own | Parsing, indexing, retries, observability, evaluation, prompt assembly, and provider integration. | Product behavior and production controls; the framework supplies common composition primitives. | Configuration, data governance, evaluation, and the parts where the product differs. |
| Trade-off | More code and operational surface, but the fewest hidden decisions. | Quicker iteration, with an abstraction layer that can complicate debugging. | Strong defaults reduce setup time but constrain architecture and upgrade timing. |
| Examples | Provider SDKs, search clients, queues, and your own interfaces. | LangChain and similar orchestration libraries. | OpenRAG and other self-hosted or managed platforms. |
Maximum control
Raw SDKs and primitives
- Best for
- Teams with unusual retrieval logic, strict latency goals, or strong platform engineering.
- You own
- Parsing, indexing, retries, observability, evaluation, prompt assembly, and provider integration.
- Trade-off
- More code and operational surface, but the fewest hidden decisions.
- Examples
- Provider SDKs, search clients, queues, and your own interfaces.
Composable middle
Orchestration framework
- Best for
- Teams that want reusable retrievers, document loaders, tool interfaces, and fast experimentation.
- You own
- Product behavior and production controls; the framework supplies common composition primitives.
- Trade-off
- Quicker iteration, with an abstraction layer that can complicate debugging.
- Examples
- LangChain and similar orchestration libraries.
Fastest assembly
Packaged RAG stack
- Best for
- Teams validating a standard knowledge workflow before investing in custom infrastructure.
- You own
- Configuration, data governance, evaluation, and the parts where the product differs.
- Trade-off
- Strong defaults reduce setup time but constrain architecture and upgrade timing.
- Examples
- OpenRAG and other self-hosted or managed platforms.
Whichever layer you choose, keep your application contracts explicit: a parser produces structured chunks, a retriever returns evidence with provenance, a reranker returns scores, and a generator receives a bounded evidence set. Those seams make replacements and evaluation possible.
08Operating reality
Production is an evaluation loop
A demo is complete when it answers one question. A production RAG system must keep answering across changing documents, permissions, user language, and provider failures.
- Retrieval quality: measure recall at k, ranking quality, empty-result rate, and which source types are consistently missed.
- Answer quality: measure groundedness, citation correctness, completeness, task success, and abstention when evidence is insufficient.
- Freshness and deletion: track ingestion lag, failed syncs, tombstones, and the revision attached to every cited passage.
- Security: propagate identity and ACLs, isolate tenants, defend against prompt injection in retrieved text, and avoid logging sensitive context by default.
- Reliability: define timeouts and fallbacks for parsers, embedding services, indexes, rerankers, and generators. A partial answer with clear limits is better than invented certainty.
- Cost and latency: observe each stage independently. Candidate count, reranking depth, context size, and model choice are product-quality controls as well as cost controls.
09Working vocabulary
Glossary
- Chunk
- A retrievable unit derived from a source document, stored with provenance and metadata.
- Embedding
- A numeric representation used to compare semantic similarity between text items.
- Hybrid search
- Candidate retrieval that combines semantic vector search with lexical or keyword search.
- Reranker
- A second-stage model or scoring function that reorders a smaller candidate set for a specific query.
- Context assembly
- Selecting, ordering, annotating, and fitting retrieved evidence into the generator’s input budget.
- Groundedness
- The degree to which an answer’s claims are supported by the evidence supplied to the model.
10Sources and further reading
References
- Retrieval Augmented Generation (RAG 101)
Abhishek Choudhury
Original article, published February 2025. - Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks
Patrick Lewis et al.
The 2020 paper that introduced the RAG formulation. - Attention Is All You Need
Ashish Vaswani et al.
The Transformer architecture and scaled dot-product attention. - Corrective Retrieval Augmented Generation
Shi-Qi Yan et al.
Retrieval evaluation and corrective knowledge actions. - GraphRAG indexing overview
Microsoft Research
A current reference implementation of graph-based indexing. - What is OpenRAG?
OpenRAG
A representative packaged, open-source RAG stack.


