RAG framework guide: how retrieval-augmented generation works and which tools to use

Learn how a RAG framework connects LLMs to external data. Covers RAG architecture, tools like LangChain and LlamaIndex, and how to build a pipeline.

Aron Schuhmann

Written by

Aron Schuhmann

Sam Bhagwat

Reviewed by

Sam Bhagwat

Jun 22, 2026

·

16 min read

Your LLM knows a lot, but it doesn't know your data. It can't cite last week's support tickets, parse your internal docs, or ground answers in a proprietary knowledge base. According to the Databricks State of AI report, 70% of organizations using generative AI are now using vector databases and retrieval techniques to customize LLMs with their own data. A RAG framework solves this by connecting a language model to external information at query time, so the model retrieves relevant context before it generates a response.

This guide walks you through how retrieval-augmented generation works, the components inside a RAG system, the major open-source tools available, and how to evaluate a RAG pipeline once it's running in production.

What is retrieval-augmented generation (RAG)?

Retrieval-augmented generation is an architecture that pairs a language model with an information-retrieval layer, so responses are grounded in real, external data rather than training-set memory alone. If you've worked with an LLM and wanted it to reference your own documents instead of guessing, you already understand the problem that RAG solves.

The core idea: grounding LLM responses in external data

Your model doesn't need to memorize everything. Instead of baking knowledge into billions of parameters, a RAG system fetches the relevant slice of your knowledge base at query time and injects it into the prompt. The model then synthesizes an answer from that context. Think of it as giving the LLM an open-book exam rather than asking it to recall facts from training.

Why RAG emerged as an alternative to retraining

Fine-tuning a foundation model on domain-specific data is expensive, slow, and creates a static snapshot that goes stale. You have to retrain whenever your data changes. RAG sidesteps this: you update the knowledge base, and the model's next retrieval picks up the new information automatically. For most production use cases, a RAG AI system delivers better accuracy-per-dollar than retraining because you avoid repeated GPU cycles every time your corpus changes.

Why RAG matters: key benefits

You get several concrete advantages when you add a retrieval layer in front of your LLM.

Reduced hallucinations and more accurate answers

You can significantly cut hallucination rates by supplying retrieved context alongside the user query. A RAG framework constrains the model's generation to factual, source-backed content, so it is far less likely to invent an answer when the real answer is sitting in its prompt.

Access to current and domain-specific information

Your training data has a cutoff date. Your indexed documents and knowledge bases, on the other hand, can be updated continuously. Connect your pipeline to internal documentation, live databases, or API feeds, and the model always works with current information. Teams that manage frequently changing RAG files, such as product changelogs or compliance docs, benefit most from this pattern.

Cost-efficient implementation compared to fine-tuning

You avoid the GPU time, curated datasets, and ongoing retraining cycles that fine-tuning demands. A RAG workflow reuses a general-purpose model and swaps in new data through the retrieval layer. The compute cost difference is significant, especially when you need to support multiple domains.

Greater developer control and data security

You control exactly which documents the model can see. Sensitive data stays in your vector store behind your access controls, never mixed into model weights. If your data changes or needs to be revoked, you update the index without touching the model itself.

How RAG works step by step

Your RAG pipeline moves through four phases every time a user submits a query. Understanding each phase helps you debug and optimize performance.

  • Create and maintain the external knowledge base: Split your source documents into chunks, generate vector embeddings for each chunk, and store them in a vector database. Data ingestion at this stage is where chunking strategies matter: recursive, token-aware, and format-specific approaches for Markdown, HTML, or JSON each handle the context window differently.

  • Retrieve relevant information at query time: Convert the user's query into an embedding and run a similarity search against the vector store. Cosine similarity is the most common distance metric. Hybrid queries combine vector similarity with metadata filters like date ranges or categories.

  • Augment the LLM prompt with retrieved context: The integration layer assembles a new prompt containing the original query plus the top-K retrieved chunks. Prompt engineering techniques structure this context so the model knows what's grounding material versus what's the user's question.

  • Generate and return a grounded response: The LLM synthesizes an answer from the augmented prompt. Optionally, a reranking step between retrieval and generation applies more sophisticated scoring to improve the ordering of results before they enter the prompt.

Core components of a RAG system

You can think of a RAG architecture as four interlocking pieces. Each one is independently swappable, which is what makes the pattern so flexible.

The knowledge base and vector store

Your knowledge base is the raw corpus: PDFs, database records, API responses, Markdown docs. The vector store is the indexed, queryable version of that corpus, where each chunk becomes a vector embedding representing its semantic meaning. Popular vector databases include pgvector (on Postgres), Chroma, Weaviate, and Turbopuffer.

The retriever converts the user's query into an embedding and finds the closest matches in the vector store. Embedding models from OpenAI, Voyage, or Cohere handle the transformation. The retriever returns a ranked list of chunks, and the quality of this ranking directly determines response quality downstream.

The integration layer

This is the orchestration code that ties everything together. It accepts the user query, calls the retriever, assembles the augmented prompt, and sends it to the generator. In a RAG workflow, the integration layer also handles concerns like token budgets, context window management, and fallback logic when retrieval returns low-confidence results.

The generator (LLM)

The generator is any large language model that receives the augmented prompt and produces a final answer. You can use hosted models from OpenAI, Anthropic, or Google Gemini, or run an open-source model like Llama. The generator doesn't need to be RAG-aware; it simply responds to the prompt it receives.

Your choice of RAG framework shapes how quickly you can build, iterate, and ship. The three most established open-source RAG frameworks each target a different sweet spot. Some teams also evaluate managed RAG-as-a-service offerings from cloud providers, but self-hosted open-source tools remain the most popular starting point for teams that want control over their data pipeline.

LangChain

LangChain provides a modular architecture for chaining LLM-driven workflows. You compose retrievers, memory modules, prompt templates, and agents into pipelines. It supports multiple vector stores and LLM providers out of the box, making it a popular default for teams that want maximum flexibility. LangGraph, LangChain's graph-based orchestration layer, extends this with stateful multi-agent workflows and built-in persistence, which is useful when your RAG pipeline needs to branch or loop based on retrieval results.

The tradeoff is complexity: LangChain's abstraction layers can feel heavy for straightforward retrieval tasks.

LlamaIndex

LlamaIndex (formerly GPT Index) focuses specifically on connecting LLMs to structured and unstructured data. It excels at indexing diverse sources like PDFs, SQL databases, APIs, and Notion pages, then exposing them through a query interface. LlamaIndex also ships with natural language processing utilities for document parsing, which reduces the amount of custom code you need to write during data ingestion. If your primary problem is document retrieval over a large, heterogeneous corpus, LlamaIndex gives you a more focused toolset than LangChain.

Haystack

Haystack, built by deepset, is a full-stack RAG framework with built-in components for retrieval, reading, ranking, and generation. It ships with REST API integration and a UI layer, which makes it well-suited for teams that need production-ready QA systems. Haystack also supports evaluation pipelines out of the box, a feature many frameworks still lack.

Other tools in the ecosystem

Beyond the three major frameworks, several specialized tools are worth knowing:

  • DSPy: a programming model from Stanford that replaces hand-crafted prompt templates with optimized pipelines, useful when you want to systematically improve RAG prompt quality through automated tuning

  • txtai: a lightweight Python framework that combines embeddings, vector search, and generative AI in a single package, well-suited for teams that want a smaller surface area than LangChain or LlamaIndex; txtAI supports vector search, document indexing, and LLM queries without requiring separate service deployments

  • RAGFlow: an open-source RAG engine designed around document understanding, with built-in support for tables, figures, and complex layouts; among open-source RAG frameworks, RAGFlow is particularly useful for business intelligence use cases where documents contain structured data like financial reports or data pipelines from ERP systems

Comparing frameworks: when to use each

The table below summarizes the main differences to help you pick the right RAG software for your project.

ConsiderationLangChainLlamaIndexHaystack
Primary strengthFlexible agent and chain compositionData indexing and document retrievalProduction QA with built-in eval
Best forMulti-step agent workflowsHeterogeneous data sourcesEnterprise search and QA systems
Learning curveSteeper (many abstractions)ModerateModerate
LanguagePython, JS/TSPython, TSPython
Built-in evaluationLimitedLimitedYes

Build a TypeScript RAG pipeline on Mastra

If you're building in TypeScript, Mastra gives you a complete RAG pipeline without switching to Python. Mastra is an open-source TypeScript framework (Apache 2.0) that provides chunking, embedding, vector storage, and retrieval as typed, composable primitives under the @mastra/rag package.

The core workflow looks like this: initialize a document with MDocument, chunk it using a strategy (recursive, sliding window, or custom), generate embeddings via ModelRouterEmbeddingModel, and upsert into a supported vector store. Mastra supports pgvector, Pinecone, Qdrant, and MongoDB out of the box.

import { MDocument } from "@mastra/rag";
import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
import { PgVector } from "@mastra/pg";
import { embedMany } from "ai";
/* 1. Initialize and chunk the document */ const doc = MDocument.fromText(
  "Your document text here...",
);
const chunks = await doc.chunk({
  strategy: "recursive",
  size: 512,
  overlap: 50,
});
/* 2. Generate embeddings */ const { embeddings } = await embedMany({
  values: chunks.map((chunk) => chunk.text),
  model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
});
/* 3. Store in vector database */ const pgVector = new PgVector({
  connectionString: process.env.POSTGRES_CONNECTION_STRING,
});
await pgVector.upsert({ indexName: "embeddings", vectors: embeddings });

At retrieval time, use createVectorQueryTool to give your agent semantic search as a callable tool. Mastra also ships a GraphRAG class for knowledge-graph-based retrieval, which traverses entity relationships in your document corpus. This is useful when documents have rich relational structure like product hierarchies or technical dependencies.

Every query, embedding lookup, and reranking step is traced with full OTel-compatible spans, so you can see which documents were retrieved, how they scored, and whether the agent used them in the final response. Mastra deploys to Vercel, Netlify, Cloudflare Workers, or standalone Node, so your RAG pipeline runs wherever your app does.

Build your first TypeScript RAG pipeline on Mastra.

RAG use cases across industries

You'll find RAG pipelines behind many production AI features today, not just chatbots.

Specialized chatbots and virtual assistants

If you run a support team, you can ground your chatbot in product docs, policy documents, and ticket history so it gives accurate, source-backed answers. A RAG app replaces the generic chatbot with one that actually knows your product.

Research and knowledge engines

If you work in legal, medicine, or finance, you can use RAG to query large document corpora with natural language. Your model retrieves specific passages from contracts, patient records, or filings and synthesizes an answer with citations.

Market analysis and business intelligence

You can connect a RAG pipeline to customer feedback databases, competitor reports, and social media feeds. The model surfaces relevant patterns from your data pipeline without requiring you to manually analyze dozens of data sources, making this pattern popular for business intelligence applications where analysts need to query large, heterogeneous document stores in natural language.

Content generation and recommendation services

RAG powers recommendation systems that combine user behavior data with current catalog information. You can use it to generate personalized summaries, email digests, and contextual recommendations based on your content catalog.

You don't have to choose one technique exclusively. Understanding how these approaches relate helps you design a more effective system.

RAG vs. fine-tuning: complementary, not competing

Fine-tuning changes a model's weights to internalize domain knowledge. RAG leaves the model unchanged and provides knowledge at inference time. You can combine both: fine-tune a model so it understands your domain's vocabulary and reasoning patterns, then use RAG to inject current, specific facts. Fine-tuning is better for teaching style and structure; RAG is better for grounding answers in training data that changes.

RAG vs. semantic search: retrieval as one layer

Semantic search finds relevant documents. RAG takes those documents and feeds them to a generative AI model for synthesis. Semantic search is a component inside RAG, not a competitor. If your users just need to find documents, semantic search alone may suffice. If they need synthesized answers, you want the full RAG pipeline.

The main types of RAG

Your RAG implementation can range from a simple retrieve-and-prompt loop to a fully autonomous multi-agent system. The taxonomy is still evolving, but these are the architectures you're most likely to encounter or build.

  • Naive RAG: You start with the simplest version: chunk documents, embed them, retrieve top-K results at query time, concatenate them into the prompt, and generate. It works well for many use cases, but it struggles with complex queries that require reasoning across multiple documents.

  • Advanced RAG: You add pre-retrieval and post-retrieval optimization on top of naive RAG. On the pre-retrieval side, you improve chunking strategies, add metadata enrichment, and refine query rewriting. Post-retrieval, you add reranking models and context compression. This is where most production RAG framework deployments land.

  • Modular RAG: You break the pipeline into independently swappable components. You might use one retriever for structured data and another for unstructured docs, or route different query types to different generators. The modular approach lets you optimize each stage independently.

  • Graph RAG: You structure your knowledge base as a knowledge graph rather than flat chunks, which preserves relationships between entities and lets the retriever traverse connections. This is useful when your data has rich relational structure, like organizational hierarchies or product dependency trees.

  • Hybrid RAG: You combine dense vector search with sparse keyword search (like BM25) in a single pipeline. Hybrid approaches handle both semantic similarity and exact-match queries, which improves recall for technical or domain-specific terminology. Hybrid search is particularly effective for codebases, API documentation, and other content where exact terminology matters as much as semantic similarity.

  • Agentic RAG: You give the model the ability to decide when and how to retrieve. Instead of a fixed retrieve-then-generate flow, an agent can issue multiple retrieval calls, refine its query based on initial results, and call external tools. This is the most powerful variant but also the hardest to evaluate and debug.

  • Multimodal RAG: You extend retrieval beyond text to include images, tables, audio, or video. Your pipeline needs embedding models that handle multiple modalities and a generator capable of reasoning across them.

Before building a full RAG pipeline, consider simpler approaches: give your agent search tools, let it run code, or feed it full context directly. Use RAG as a fallback when those simpler patterns don't scale.

Choosing the right RAG variant for your use case

Start with naive RAG. If retrieval quality is the bottleneck, add reranking and query rewriting (advanced RAG). If you need to support multiple data types or query patterns, go modular.

Only reach for agentic RAG when your use case genuinely requires multi-step reasoning across retrievals. Each layer adds complexity and makes evaluation harder.

Testing, evaluating, and monitoring your RAG pipeline

Your RAG system will degrade silently if you don't measure it. Retrieval quality, answer faithfulness, and latency all shift as your knowledge base grows and user queries change.

Retrieval quality metrics: precision, recall, and MRR

You need to know whether your retriever is returning the right chunks. Precision measures how many retrieved chunks are actually relevant. Recall measures how many relevant chunks you found out of all relevant chunks in the store. Mean reciprocal rank (MRR) captures how high the first relevant result appears. Track all three, but prioritize precision if your generator is sensitive to noisy context.

Vector embeddings quality matters here too. If your embedding model produces poor representations of domain-specific terminology, similarity search will return irrelevant chunks regardless of how good the rest of your pipeline is.

Answer faithfulness and relevance evals

Evaluate your outputs even when retrieval metrics look strong, because retrieval quality alone doesn't guarantee good answers. Use LLM-as-judge evals to score whether the generated answer is faithful to the retrieved context and relevant to the original query. Pass the output, the retrieved chunks, and the user query to a judge model with a scoring rubric. Pick a judge from a different model family to reduce self-preference bias.

Tracing retrieval and generation steps in production

In production, you need to see exactly what happened on every query: which chunks were retrieved, what the assembled prompt looked like, and how the model responded. A trace is a tree of spans, following the OpenTelemetry (OTel) standard, showing latency, token counts, and input/output at each step. Mastra provides built-in tracing that surfaces this data during local development, making it straightforward to catch regressions before they reach users.

Getting started

A RAG framework gives you grounded, accurate LLM responses without the cost and rigidity of fine-tuning. Start with naive RAG, measure retrieval precision and answer faithfulness from day one, and add complexity only when your metrics tell you to.

Frequently asked questions

How do I choose between a RAG framework and building retrieval from scratch?

A RAG framework gives you pre-built chunking, embedding, vector store connectors, and retrieval logic, which saves meaningful engineering time on the parts of the pipeline that don't differentiate your product. Build from scratch when you have highly specific retrieval requirements that frameworks don't support, or when you need to minimize dependencies. For most teams, starting with a framework and customizing the integration layer is the faster path to production.

What are the main types of RAG architectures?

The main RAG architectures are naive RAG, advanced RAG, modular RAG, graph RAG, hybrid RAG, agentic RAG, and multimodal RAG. The boundaries between these categories are fluid. In practice, most production systems are advanced or modular RAG with selective agentic capabilities layered on top.

What is the most popular RAG framework?

LangChain is the most widely adopted RAG framework by community size and GitHub activity. LlamaIndex leads for data-indexing-specific use cases. For TypeScript teams, Mastra provides a native RAG pipeline without requiring Python. Your best choice depends on your language ecosystem and whether you need broad agent orchestration or focused document retrieval.

How is RAG different from an LLM?

An LLM is a model trained on a static dataset. RAG is an architecture that adds a retrieval layer in front of an LLM, giving it access to external, updatable data at inference time. RAG uses an LLM as its generator component but extends it with retrieval and prompt augmentation.

When should I use RAG instead of fine-tuning?

Use RAG when your knowledge base changes frequently, when you need source citations, or when you want control over what the model can access. Fine-tuning is better when you need to change the model's reasoning style, domain vocabulary, or output format. Many production systems combine both: fine-tuning for behavior and RAG for knowledge.

Share:
Aron Schuhmann
Aron SchuhmannHead of Demand Generation

Aron Schuhmann is the Head of Demand Generation at Mastra. A career-long B2B SaaS marketer, he has worked at the intersection of AI and developer tools since 2015, serving as an early growth and demand-generation hire at MightyAI (acquired by Uber), Gatsby (acquired by Netlify), and OctoAI (acquired by NVIDIA).

All articles by Aron Schuhmann
Sam Bhagwat

Sam Bhagwat is the founder and CEO of Mastra. He co-founded Gatsby, which was used by hundreds of thousands of developers. A Stanford graduate and veteran of web development, he authored 'Principles of Building AI Agents' (2025).

All articles by Sam Bhagwat