How RAG Works: The Two-Stage Architecture
In short
RAG works in two stages. First, a retriever converts the user's query into a vector embedding and searches a vector database for the most relevant document chunks. Second, a generator (LLM) receives the query plus the retrieved chunks as context and produces a grounded response.
Every RAG system has the same fundamental flow, regardless of framework or provider:
- Indexing (offline). Documents are split into chunks (typically 200–1,000 tokens), each chunk is converted into a vector embedding using an embedding model (e.g., OpenAI text-embedding-3-large, Cohere embed-v3), and the vectors are stored in a vector database alongside the original text and metadata.
- Retrieval (at query time). The user's query is embedded using the same model. The vector database performs a similarity search (cosine similarity or approximate nearest neighbor) and returns the top-k most relevant chunks.
- Generation (at query time). The retrieved chunks are injected into the LLM's prompt as context. The model generates a response grounded in those chunks, often with inline citations back to the source documents.
This separation of retrieval and generation is what makes RAG powerful: the retriever handles knowledge lookup (fast, updatable, scalable), while the LLM handles reasoning and language generation.
Vector Databases: The RAG Knowledge Store
In short
A vector database stores document embeddings and enables fast similarity search — the core retrieval mechanism in RAG. Leading options include Pinecone (managed), Weaviate (open-source, hybrid search), Qdrant (open-source, Rust-based), Chroma (lightweight, Python-native), and Milvus (open-source, large-scale).
The vector database is where your knowledge lives in RAG. It stores embeddings (dense numerical representations of text) and supports fast approximate nearest neighbor (ANN) search.
| Database | Type | Best for |
|---|---|---|
| Pinecone | Managed SaaS | Teams wanting zero infra management, fast setup |
| Weaviate | Open-source + cloud | Hybrid search (vector + keyword), multi-modal |
| Qdrant | Open-source (Rust) | High performance, advanced filtering, self-hosted |
| Chroma | Open-source (Python) | Prototyping, small-to-medium datasets, simplicity |
| Milvus | Open-source | Large-scale enterprise, billion-vector workloads |
Most production RAG systems also use metadata filtering alongside vector search. For example, filtering by document date, department, or access level before running the similarity search dramatically improves relevance for enterprise use cases.
RAG vs Fine-Tuning: When to Use Each
In short
RAG retrieves external knowledge at query time without modifying the model. Fine-tuning modifies the model's weights on a custom dataset. RAG is better when knowledge changes frequently, needs to be traceable, or the corpus is large. Fine-tuning is better when you need to change the model's behavior, tone, or reasoning patterns.
This is the most common architectural decision teams face. The two approaches solve different problems:
| Dimension | RAG | Fine-tuning |
|---|---|---|
| What it changes | The context the model sees | The model's weights |
| Knowledge updates | Instant — update the document store | Requires retraining (hours to days) |
| Traceability | High — can cite source documents | Low — knowledge baked into weights |
| Cost | Retrieval infra + longer prompts | Training compute + model hosting |
| Best for | Factual Q&A, knowledge bases, documentation | Style, tone, domain-specific reasoning |
In practice, the two are not mutually exclusive. Many production systems use a fine-tuned model as the generator inside a RAG pipeline — getting both behavioral adaptation and grounded knowledge.
Need a production RAG system?
Alice Labs builds enterprise RAG pipelines — from chunking strategy and vector database selection to evaluation harnesses and access control — for companies across Sweden and Europe.
Talk to a RAG engineerEnterprise RAG: Challenges and Best Practices
In short
Production RAG systems face challenges in chunking strategy, metadata filtering, evaluation (faithfulness, relevance, completeness), access control, and latency. The difference between a demo and a production RAG system is almost entirely in how well these challenges are handled.
A basic RAG demo takes an afternoon. A production-grade RAG system that enterprise teams trust takes engineering discipline across several dimensions:
- Chunking strategy. Fixed-size chunks are a starting point. Production systems use semantic chunking (split at paragraph or section boundaries), parent-child retrieval (retrieve the chunk, return the parent section), or recursive summarization for long documents.
- Metadata filtering. Pre-filter by date, department, document type, or access level before vector search. Without this, retrieval quality degrades rapidly as the corpus grows.
- Evaluation. Measure faithfulness (does the answer match the retrieved documents?), relevance (did retrieval return the right chunks?), and completeness (did the answer address the full question?). Frameworks like RAGAS and DeepEval automate these metrics.
- Access control. Enterprise RAG must enforce document-level permissions. A user should never receive an answer grounded in a document they don't have access to. This requires permission-aware indexing and retrieval.
- Hybrid search. Combine vector search (semantic similarity) with keyword search (BM25) for better recall. Weaviate and Elasticsearch support this natively; others require a custom fusion layer.
RAG as a Tool for AI Agents
In short
In agentic AI systems, RAG is implemented as a tool the agent can invoke — not a static pipeline. The agent decides when to retrieve, what query to send, and whether the retrieved context is sufficient. Anthropic's 'Building effective agents' guide (Dec 2024) recommends RAG as a core tool-use pattern.
RAG started as a standalone architecture pattern, but in modern AI systems it is increasingly used as one tool among many that an AI agent can invoke. The agent decides dynamically whether to retrieve documents, and can reformulate the retrieval query if initial results are poor.
Anthropic's December 2024 engineering guide on building effective agents explicitly recommends RAG as a core tool-use pattern. In this model, the agent's tool list might include: a RAG retrieval tool, a web search tool, a code execution tool, and domain-specific API tools. The agent reasons about which tool to call based on the user's query.
- Static RAG pipeline. User query goes directly to retrieval, then to generation. No decision-making. Good for simple Q&A use cases.
- Agentic RAG. An AI agent decides when to retrieve, what to retrieve, whether to re-rank or re-query, and when the answer is good enough to return. Better for complex, multi-step research tasks.
Most production RAG systems in 2026 are moving toward agentic RAG — where the retrieval step is a tool call within a broader agent loop, not a hardcoded pipeline stage.
About the Authors & Reviewers

Co-Founder, Alice Labs
Co-Founder at Alice Labs. Author of 7 research reports on AI adoption, governance and labor markets cited across EU, OECD and US benchmarks.
- 8+ years in AI strategy & implementation
- Top-5 AI Speaker, Sweden (Mindley 2025)
- 100+ enterprise AI engagements

Co-Founder, Alice Labs
Co-Founder at Alice Labs. Builds AI automation, agent workflows and integration systems that hold up in real business operations.
- AI automation & agent systems lead
- Workflow design across 100+ deployments
- Specialist in RAG, integrations & APIs
Frequently Asked Questions
What is RAG in simple terms?
RAG (Retrieval-Augmented Generation) is a way to give an AI model access to external documents before it generates an answer. Instead of relying only on what the model learned during training, RAG searches a knowledge base for relevant information and includes it in the prompt — so the answer is grounded in real, up-to-date sources.
What problem does RAG solve?
RAG solves two core LLM limitations: hallucination (making up facts) and stale knowledge (training data has a cutoff date). By retrieving relevant documents at query time, RAG grounds responses in verifiable sources and enables real-time knowledge updates without retraining the model.
Who invented RAG?
RAG was introduced by Patrick Lewis and colleagues at Facebook AI Research (now Meta AI) in their 2020 paper 'Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks' (arXiv:2005.11401). The paper demonstrated that combining a dense passage retriever with a sequence-to-sequence generator significantly improved performance on knowledge-intensive tasks.
What is the difference between RAG and fine-tuning?
RAG retrieves external documents at query time and injects them into the prompt — the model's weights stay unchanged. Fine-tuning modifies the model's weights by training on a custom dataset. RAG is better for factual knowledge that changes often; fine-tuning is better for changing the model's behavior, tone, or reasoning patterns. Many production systems combine both.
What is a vector database in RAG?
A vector database stores document embeddings — dense numerical representations of text — and enables fast similarity search. When a user asks a question, the query is embedded and the vector database finds the most similar document chunks. Popular options include Pinecone, Weaviate, Qdrant, Chroma, and Milvus.
How do you evaluate RAG quality?
RAG quality is measured across four dimensions: faithfulness (does the answer match the retrieved documents?), answer relevance (does it address the question?), context precision (are the retrieved chunks relevant?), and context recall (were all necessary chunks retrieved?). The RAGAS and DeepEval frameworks automate these evaluations.
Can RAG work with private enterprise data?
Yes — that is the primary enterprise use case. RAG connects an LLM to your internal knowledge base (wikis, policy documents, support articles, product catalogs) so employees or customers get answers grounded in your actual data. Access control must be enforced at the retrieval layer to prevent unauthorized information exposure.
What is agentic RAG?
Agentic RAG is when an AI agent uses RAG as one of its tools rather than running a fixed retrieval pipeline. The agent decides when to retrieve, what query to send, whether to re-rank or reformulate, and when the answer is sufficient. Anthropic's December 2024 'Building effective agents' guide recommends this pattern for complex knowledge tasks.
Why AI Projects Fail: 7 Root Causes & How to Avoid Them
Next in AI ImplementationWhat Is MLOps? Machine Learning Operations Explained
Further reading
- Lewis et al. — Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks (2020)· arxiv.org
- Anthropic — Building effective agents (Dec 2024)· anthropic.com
- Gao et al. — Retrieval-Augmented Generation for Large Language Models: A Survey (2024)· arxiv.org
- RAGAS — Evaluation framework for RAG pipelines· docs.ragas.io
- LangChain — RAG documentation· python.langchain.com
Related services
Related reading
What Is an AI Agent?
RAG is a core capability of AI agents — understand the full agent architecture.
7 min deep diveWhy AI Projects Fail: 7 Root Causes
Data quality and evaluation gaps are top RAG failure modes too.
10 min pillarAI Search Optimization: Complete Guide for 2026
How RAG-powered search engines decide which sources to cite.
14 minSources
- Lewis, Perez, Piktus, Petroni, Karpukhin, Goyal, Küttler, Lewis, Yih, Rocktäschel, Riedel, Kiela — Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks (arXiv:2005.11401, Facebook AI Research, 2020)(accessed 2026-04-16)
- Anthropic — Building effective agents (engineering guidance, December 2024)(accessed 2026-04-16)
- Gao, Xiong, Gao, Jia, Pan, Bi, Dai, Sun, Wang, Wang — Retrieval-Augmented Generation for Large Language Models: A Survey (arXiv:2312.10997, 2024)(accessed 2026-04-16)
- RAGAS — Evaluation framework for RAG pipelines (documentation)(accessed 2026-04-16)
- LangChain — RAG tutorial and documentation(accessed 2026-04-16)
- LlamaIndex — RAG documentation and guides(accessed 2026-04-16)
Next scheduled review: