Get a quote
Designveloper / Blog / AI/Machine Learning / Build A RAG Agent With LangChain: Step-By-Step Tutorial

Build A RAG Agent With LangChain: Step-By-Step Tutorial

Written by Khoa Ly Reviewed by Ha Truong 16 min read May 29, 2026

Table of Contents

KEY TAKEWAYS:

  • LangChain RAG is best understood as an application workflow pattern that connects retrieval, prompts, model calls, grounding, evaluation, and orchestration.
  • MCP does not replace RAG; it can provide a cleaner integration boundary for approved tools, resources, files, databases, and APIs.
  • Production RAG depends on chunking, metadata, permissions, freshness, citations, evaluation, observability, and fallback behavior, not vector search alone.
  • The safest architecture separates retrieval logic from tool and system access so LangChain can coordinate the workflow while MCP standardizes reusable integrations.

LangChain RAG is a practical way to build an AI agent that answers from your own documents instead of relying only on a model’s training data. The basic flow is simple: index trusted content, retrieve relevant chunks at runtime, pass that context to a chat model, and make the agent answer with clear boundaries.

A useful RAG agent is not just a demo around embeddings. It needs clean source data, chunking rules, metadata, a vector store, a retriever tool, prompt controls, evaluation cases, logging, and a handoff path when the retrieved evidence is weak. This tutorial uses an internal policy assistant as the running example so each step has a concrete starting point.

LangChain is a good fit because the current LangChain Python overview positions the framework around agents, models, tools, and context-aware workflows. For RAG, that means you can connect retrieval, prompting, tools, and evaluation in one maintainable Python application.

Xem thêm:

Overview of building a LangChain RAG agent from trusted documents to cited answers.

RAG/MCP update: LangChain RAG and MCP solve adjacent layers. RAG decides how a product retrieves, ranks, grounds, and evaluates context for a model. MCP standardizes how approved tools, resources, prompts, files, databases, and APIs are exposed to AI clients. In practice, a LangChain RAG workflow can use MCP servers as a cleaner integration boundary when the same retrieval or tool access must be reused across agents, assistants, and developer tools.

How Does LangChain RAG Work?

LangChain RAG works by separating knowledge preparation from answer generation. The system prepares searchable document chunks before users ask questions, retrieves relevant context when a question arrives, and asks the model to answer from that retrieved evidence.

Xem thêm:

Indexing Data Before Users Ask Questions

Indexing is the offline or background stage. The application loads documents, cleans them, splits them into chunks, attaches metadata, converts chunks into embeddings, and stores them in a vector database. For an internal policy assistant, source files might include leave-policy.md, remote-work.md, and benefits.md.

Good indexing makes retrieval possible. Each chunk should carry metadata such as source filename, department, version, owner, publish date, and access level. Without metadata, the agent may retrieve outdated or unauthorized text and still sound confident.

Retrieving Relevant Context At Runtime

Retrieval happens when a user asks a question. The user’s query is embedded or otherwise matched against indexed content, and the retriever returns the most relevant chunks. The official LangChain retrieval documentation describes this pattern as a way to search external data and supply relevant context to the model.

Retrieval quality is the heart of RAG. If the retriever returns the wrong chunks, the model may answer fluently from the wrong evidence. That is why teams should test retrieval separately from generation before judging the full agent.

Generating Answers From Retrieved Context

Generation is the final stage. The chat model receives the user question, retrieved context, and prompt instructions. A strong RAG prompt tells the model to answer only from retrieved sources, cite filenames or document titles, ask for clarification when needed, and refuse unsupported claims.

The model should not hide uncertainty. If retrieved context does not answer the question, the RAG agent should say that the source material does not confirm the answer and route the user to the right owner.

How LangChain RAG prepares knowledge, retrieves context, and generates bounded answers.

What You Need Before You Start

A LangChain RAG agent needs a small but complete Python setup. The safest first build is a narrow internal assistant that answers from three or four documents and refuses questions outside that scope.

RequirementWhat to prepareAcceptance check
Basic Python And API KnowledgeVirtual environments, packages, environment variables, JSON, and API calls.A developer can run a local script and keep API keys outside source control.
LangChain And Supporting LibrariesLangChain packages, document loaders, text splitters, provider integration, vector store library, and test tooling.The project can load one file, split it, and print chunks with metadata.
A Chat Model, Embeddings Model, And Vector StoreA model provider, an embedding provider, and a vector database such as FAISS, Chroma, Pinecone, Weaviate, or a managed cloud option.The retriever returns the right policy chunk for a known question.

Use a minimal folder structure so each part can be tested:

langchain-rag-agent/  .env  data/    leave-policy.md    remote-work.md    benefits.md  src/    settings.py    ingest.py    retriever.py    agent.py    prompts.py    api.py  tests/    test_retrieval.py    test_agent_answers.py

The first user story should also be narrow: “Employees can ask HR policy questions, and the agent answers only from approved policy files with a source filename.” That single sentence gives the team a concrete scope and a clear refusal rule.

Required setup for a LangChain RAG agent before development starts.

The Core Components Of A LangChain RAG Agent

A RAG agent has several components that should stay separate. Separating the components makes the system easier to debug when an answer is wrong.

  • Chat Model: The model that writes the final answer. LangChain’s chat model documentation explains the standard interface for provider-backed models.
  • Embeddings: Numeric representations of text chunks and user questions. Embeddings make semantic search possible.
  • Documents And Chunking: Source text split into retrievable chunks with metadata. Chunking affects relevance more than many teams expect.
  • Vector Store: The search layer that stores embeddings and returns similar chunks.
  • Retriever Tool: The callable retrieval function the agent can use when it needs evidence.
  • Prompt And Agent Logic: The instructions and orchestration that decide when to retrieve, how to answer, and when to refuse.

A production team should be able to inspect each component. If the answer is weak, the trace should show whether the failure came from stale documents, poor chunking, irrelevant retrieval, prompt ambiguity, or model behavior.

Xem thêm:

Core components that make a LangChain RAG agent work.

Build A RAG Agent With LangChain Step By Step

The safest path is to build retrieval first, then wrap retrieval as a tool, then connect the tool to an agent. This avoids the common mistake of building an impressive chat interface before proving that the retriever can find correct evidence.

Step 1: Set Up The Project And Install The Required Libraries

Start with a clean Python environment and install only the packages needed for the first version. A local prototype can use FAISS or Chroma, while a production system may use a managed vector database with access control, backups, and observability.

python -m venv .venv.venv\Scripts\activatepip install langchain langchain-openai langchain-community langchain-text-splitters faiss-cpu python-dotenv pytest

Store API keys in .env locally and in a managed secret store for staging or production. Do not hard-code model keys, vector database credentials, or internal document paths in the repository.

Step 2: Load And Split Your Source Documents

Load source documents from a controlled folder, then split them into chunks. For policy and support documents, chunks should be large enough to preserve meaning but small enough to retrieve precise answers.

from langchain_community.document_loaders import DirectoryLoaderfrom langchain_text_splitters import RecursiveCharacterTextSplitter loader = DirectoryLoader("data", glob="*.md")docs = loader.load() splitter = RecursiveCharacterTextSplitter(    chunk_size=700,    chunk_overlap=120,)chunks = splitter.split_documents(docs) for chunk in chunks:    chunk.metadata["owner"] = "HR"    chunk.metadata["status"] = "approved"

Chunking should be tested with real questions. If a user asks about remote-work approval and the retriever returns a generic onboarding paragraph, the chunking or metadata needs work before the model is blamed.

Step 3: Create Embeddings And Store Them In A Vector Database

Embeddings let the retriever compare a user question with document chunks by meaning. The example below creates a local FAISS index for a prototype.

from langchain_openai import OpenAIEmbeddingsfrom langchain_community.vectorstores import FAISS embeddings = OpenAIEmbeddings()vectorstore = FAISS.from_documents(chunks, embeddings)vectorstore.save_local("storage/faiss_hr_policy")

For production, choose the vector store based on access control, persistence, filtering, cost, scale, backup needs, and deployment environment. The vector database is not just a cache. It becomes part of the application’s knowledge infrastructure.

Step 4: Build A Retriever Tool To Fetch Relevant Context

The retriever should return a small set of relevant chunks with source metadata. LangChain tools are useful because an agent can call retrieval only when it needs evidence. The official LangChain tools documentation shows how Python functions can become tools for agent workflows.

from langchain.tools import tool retriever = vectorstore.as_retriever(search_kwargs={"k": 4}) @tooldef search_hr_policy(question: str) -> str:    """Search approved HR policy documents for relevant context."""    docs = retriever.invoke(question)    lines = []    for doc in docs:        source = doc.metadata.get("source", "unknown")        lines.append(f"SOURCE: {source}\n{doc.page_content}")    return "\n\n".join(lines)

The first acceptance test is simple: a known question should retrieve the known source. For example, “How many remote work days can I request?” should retrieve remote-work.md before generation begins.

Step 5: Connect Retrieval To Your Prompt And Agent Flow

After the retriever tool works, connect it to the agent. The current LangChain agents documentation explains how agents can use tools while following model-driven workflows.

from langchain.agents import create_agentfrom langchain_openai import ChatOpenAI model = ChatOpenAI(model="gpt-4.1-mini", temperature=0.2) system_prompt = """You are an HR policy RAG agent.Use search_hr_policy before answering policy questions.Answer only from retrieved context.Cite the source filename.If retrieved context does not support the answer, say you cannot confirm it and route the employee to HR.""" agent = create_agent(    model=model,    tools=[search_hr_policy],    system_prompt=system_prompt,) response = agent.invoke({"messages": [{"role": "user", "content": "How many remote days can I request?"}]})

The prompt makes the retrieval rule explicit. The agent should not answer HR policy questions from memory, generic web knowledge, or a model’s training data.

Step 6: Test, Refine, And Improve Response Quality

Testing should cover retrieval and generation separately. Retrieval tests check whether the right chunks appear. Agent tests check whether the final answer follows source, refusal, format, and handoff rules. LangSmith’s evaluation documentation gives teams a way to compare outputs and track changes over time.

Test caseExpected behaviorFailure to watch
Known policy questionAnswer with correct source filename.Right answer but no citation.
Missing policy questionRefuse and route to HR.Model invents a policy.
Ambiguous questionAsk a clarifying question.Agent guesses the user’s region or employment type.
Prompt injection in a documentIgnore malicious retrieved instructions.Agent follows untrusted text from a source chunk.

Add a new test whenever a reviewer catches a bad answer. RAG quality improves fastest when failures become regression cases instead of one-off fixes.

Step by step LangChain RAG agent build process from setup to testing.

RAG Chains Vs RAG Agents In LangChain

A RAG chain follows a fixed sequence: retrieve context, format prompt, call model, return answer. A RAG agent can decide when to retrieve, whether to call another tool, and how to proceed through a more flexible workflow.

ApproachBest fitMain tradeoff
RAG chainFAQ bots, documentation search, policy Q&A, and narrow answer flows.Predictable but less flexible.
RAG agentMulti-step assistants that may retrieve, call APIs, summarize, ask follow-up questions, or hand off.More flexible but harder to test and govern.

Use a RAG chain when the workflow is simple and repeatable. Use a RAG agent when the assistant needs tool choice, conditional steps, or workflow actions. Many production systems start as chains and become agents only after the team understands the real user paths.

Comparison of RAG chains and RAG agents in LangChain workflows.

How To Make A LangChain RAG Agent More Reliable

Reliability comes from retrieval quality, prompt boundaries, evaluation, logging, and error handling. A RAG agent should be designed as a software system, not a single prompt.

Improve Chunking And Retrieval Quality

Improve retrieval before changing the model. Review chunk size, overlap, metadata, filters, document freshness, and duplicate content. Use metadata filters for status, version, department, permission, and date when the knowledge base contains similar documents.

Tune Prompts And Response Boundaries

The prompt should define source rules, refusal behavior, citation format, and escalation. A good boundary is explicit: answer from retrieved context, cite sources, and refuse when evidence is missing. Do not rely on vague instructions such as “be accurate.”

Add Logging, Evaluation, And Error Handling

Logging should capture the user request, retrieved source IDs, tool calls, model name, latency, errors, and final answer where policy allows. LangSmith’s observability documentation is useful for tracing LLM application behavior and diagnosing weak answers.

Error handling should prevent silent failure. If the vector store is unavailable, the model provider times out, or no sources are retrieved, the agent should return a controlled fallback instead of a confident unsupported answer.

A useful release gate is to compare three answers for every critical question: the answer from the current system, the expected answer written by a human owner, and the retrieved source chunks. If the final answer is wrong but the chunks are right, improve the prompt or model choice. If the chunks are wrong, improve indexing, metadata, filters, or source quality. If the expected answer is unclear, the business rule needs clarification before the agent can be trusted.

Teams should also watch cost and latency. Larger context, more retrieved chunks, reranking, and agent loops can improve quality, but they also add delay and usage cost. Start with a simple retrieval configuration, measure failure cases, then add reranking or agentic behavior only where the evidence says it helps.

Reliability loop for improving retrieval, prompts, evaluation, and error handling.

Security And Quality Risks In LangChain RAG

RAG reduces some hallucination risks, but it also introduces new risks because retrieved content becomes part of the model context. Security and quality review should start before the first public release.

Indirect Prompt Injection From Retrieved Content

Indirect prompt injection happens when retrieved content contains malicious instructions that try to override the system prompt. The OWASP Top 10 for LLM Applications treats prompt injection as a major LLM application risk, and 2026 research on indirect prompt injection in RAG systems shows why retrieved content must be treated as untrusted data.

Defenses include source allowlists, content scanning, tool permission limits, prompt separation, retrieval provenance, human review for sensitive actions, and refusal rules for instructions found inside retrieved documents.

Poor Source Data And Weak Retrieval Quality

Weak source data creates weak answers. Duplicate, outdated, contradictory, or poorly chunked documents can make the agent retrieve the wrong context. Every RAG project needs source ownership, versioning, review dates, and deletion rules.

Why Grounding Still Needs Evaluation

Grounding is not a guarantee. The answer can cite a source and still misunderstand it. Evaluation should test factual accuracy, source relevance, refusal behavior, latency, and user usefulness. A RAG agent is trustworthy only when its behavior is measured over realistic questions.

Security and quality risks in LangChain RAG systems.

Where LangChain RAG Agents Create The Most Value

LangChain RAG agents create the most value when the answer must come from business-specific content. Generic questions may not need RAG. Company policies, product documents, support histories, implementation notes, and internal procedures usually do.

Internal assistants can search policies, onboarding docs, engineering runbooks, sales playbooks, and project knowledge. The value is faster access to approved knowledge without forcing employees to search several systems manually.

Customer Support And FAQ Automation

Support RAG agents can retrieve help-center articles, product docs, policy pages, and known issue notes. The safest support bot cites sources, asks clarifying questions, and hands off edge cases rather than pretending every answer is certain.

AI Assistants Connected To Business Content

Business assistants become useful when retrieval connects to real workflows. A RAG agent can prepare an answer, draft a ticket, summarize a document, or recommend a next step, but risky actions should still use permissions, validation, and human approval.

Business use cases where LangChain RAG agents create the most value.

What Changes When A LangChain RAG Agent Meets Real Applications

Building a RAG agent is only the first step. The harder challenge is maintaining clean source data, reliable retrieval, prompt control, and production-ready interfaces so the system stays useful over time.

Real applications need ownership. Someone must decide which documents are approved, when they expire, who can access them, how retrieval is evaluated, and how bad answers are corrected. Without ownership, the vector store becomes a stale knowledge dump.

Real applications also need product engineering. The RAG agent may need authentication, user roles, web or mobile interfaces, API boundaries, monitoring, CI/CD, rollback plans, and support workflows. Designveloper’s AI development services and web application development services focus on turning prototypes like this into maintainable systems with discovery, architecture, testing, monitoring, and post-launch iteration.

A production-readiness checklist should include source ownership, chunking review, retrieval tests, prompt boundaries, security review, logging, user feedback, fallback behavior, and human handoff. If those pieces are missing, the RAG agent may work in a demo but fail in real operations.

For a real release, define a review cadence before users arrive. A policy assistant may need monthly source review, weekly failed-answer review, and immediate re-indexing when HR changes a policy. A product support assistant may need a content owner for each product area, a changelog trigger, and a rollback path when a bad article reaches the index.

The operating model should also define what the agent is allowed to do. A read-only RAG assistant can answer and cite sources. A workflow-connected agent may create tickets, draft replies, or update records. Once the agent can act, the system needs role-based access, approval gates, audit logs, and rate limits outside the model prompt.

Production areaDecision to makePractical control
Knowledge ownershipWho approves source documents?Assign owner, review date, and status metadata.
Retrieval qualityHow do we know the right chunks are found?Maintain known-question tests and retrieval-only checks.
SecurityWhat content and tools can the agent access?Use authentication, metadata filters, and narrow tool permissions.
OperationsHow do support teams diagnose bad answers?Log retrieved source IDs, traces, model settings, and handoff reasons.

This is where LangChain RAG becomes normal software engineering. The first notebook proves that retrieval can work. The production system proves that retrieval keeps working when documents, users, permissions, prompts, and business rules change.

Production readiness checklist for real LangChain RAG applications.

FAQs About LangChain RAG

How To Create A RAG Application Using LangChain?

Create a RAG application using LangChain by loading documents, splitting them into chunks, creating embeddings, storing chunks in a vector database, building a retriever, and passing retrieved context into a prompt or agent. Test retrieval before testing the full answer flow.

Can You Build A Chatbot With A LangChain RAG Agent?

Yes. A LangChain RAG agent can power a chatbot that answers from approved documents, cites sources, asks clarifying questions, and hands off uncertain requests. This pattern works well for support, internal knowledge, policies, and product documentation.

How To Build RAG Pipeline Using LangChain?

Build a RAG pipeline with LangChain by creating an indexing pipeline and a runtime pipeline. The indexing pipeline loads, chunks, embeds, and stores documents. The runtime pipeline retrieves relevant chunks and asks a model to generate an answer from those chunks.

How To Use A LangChain Retriever?

Use a LangChain retriever by creating a vector store, calling as_retriever(), setting search parameters such as k, and invoking the retriever with a user question. Wrap the retriever as a tool when an agent needs to decide when retrieval is useful.

Can We Use RAG Without LangChain?

Yes. RAG can be built without LangChain using direct calls to document loaders, embedding models, vector databases, and model APIs. LangChain is useful when the team wants standardized components for retrieval, tools, agents, tracing, and integration, but it is not mandatory for every RAG system.

Also published on

Share post on

Insights worth keeping.
Get them weekly.

Related Articles

name
name
15 Best AI No-Code App Builders In 2026 (No Coding Skills Required)
15 Best AI No-Code App Builders In 2026 (No Coding Skills Required) Published July 06, 2026
Best ChromaDB Alternatives For RAG And Vector Search
Best ChromaDB Alternatives For RAG And Vector Search Published July 06, 2026
Gemini Vs ChatGPT: Complete AI Assistant Comparison
Gemini Vs ChatGPT: Complete AI Assistant Comparison Published July 06, 2026
name name
Got an idea?
Realize it TODAY