Engineering Notes

LangChain vs. LlamaIndex: Choosing a Framework and Building Production RAG

A practical comparison of LangChain, LangGraph, and LlamaIndex, followed by an end-to-end production RAG architecture covering ingestion, hybrid retrieval, reranking, citations, evaluation, and access control.

HOUHUIYANG.COM

Scan to continue reading

Generating…

LangChain vs. LlamaIndex: Choosing a Framework and Building Production RAG

houhuiyang.com/en/notes/langchain-vs-llamaindex-production-rag

LangChain and LlamaIndex are often compared because both connect models, vector stores, data sources, and tools, and both can build RAG systems and agents. “LangChain for agents, LlamaIndex for RAG” is a useful first approximation, but it is no longer a complete technical conclusion.

LangChain is now primarily a high-level agent framework with model, tool, middleware, and prebuilt agent-loop abstractions. LangGraph provides the lower-level runtime for durable, stateful orchestration. LlamaIndex remains data-centric, organizing Document, Node, Ingestion Pipeline, Index, Retriever, and Query Engine abstractions, while also offering Workflows, FunctionAgent, ReActAgent, and multi-agent coordination.

The useful question is not which framework is universally stronger. It is: Does the system's dominant complexity live in data ingestion, retrieval, and evidence assembly, or in tool decisions, state transitions, and long-running orchestration?

The capability boundaries of LangChain, LangGraph, and LlamaIndex

A more accurate comparison

DimensionLangChain / LangGraphLlamaIndex
Core positionAgent framework plus a stateful orchestration runtimeContext engineering, indexing, retrieval, querying, and data workflows
Main abstractionsModel, Tool, Middleware, Agent; State, Node, EdgeDocument, Node, Transformation, Index, Retriever, Query Engine, Workflow
RAGLoaders, splitters, vector stores, retrievers, and 2-step/agentic/hybrid RAGConcentrated abstractions for ingestion, metadata, indexing, retrieval, synthesis, and evaluation
AgentsHigh-level create_agent; LangGraph adds persistence, streaming, human review, and recoveryFunctionAgent, ReActAgent, AgentWorkflow, and event-driven Workflows
Best-fit complexityTool use, branches, loops, approvals, and long-lived stateHeterogeneous data, sophisticated retrieval, document relationships, and data agents
Selection signalBusiness workflow and agent behavior dominatePrivate-data quality and retrieval behavior dominate

LlamaIndex is not merely a vector-index utility, and LangChain is not merely the old idea of joining chains. Both cover overlapping territory. Choose the abstractions that match the part of the system your team must change and debug most often.

Choose by scenario

For an enterprise policy or documentation assistant, the hard problems are usually parsing, versions, permissions, section structure, hybrid retrieval, citations, and evaluation. LlamaIndex is often a natural center because its ingestion and retrieval concepts are cohesive.

For a support agent that reads knowledge, queries orders, checks refund rules, requests approval, calls payments, and records an outcome, the core is a recoverable state machine with controlled tool access. LangChain agents on LangGraph fit that shape well.

For research and report generation, LlamaIndex can expose a high-quality retrieval tool while LangGraph manages research steps, state, approval, and recovery. Keep the boundary explicit: the orchestration layer consumes a structured Retriever or Query Engine result rather than manipulating index internals.

If retrieval itself is the product—tenant-aware filters, temporal rules, parent-child retrieval, graph retrieval, or multimodal documents—build the data layer as a platform and expose it as a tool to whichever agent framework you use.

RAG has two lifecycles, not one arrow

Offline ingestion:
Sources → Parse/OCR → Clean and restore structure → Metadata/ACL → Chunk
        → Embeddings + lexical index → Versioned storage → Quality checks

Online serving:
Question → Safety/ACL → Query understanding → Routing → Hybrid retrieval
         → Fusion/dedup → Rerank → Context assembly → Grounded generation
         → Citation validation/abstention → Response

Continuous loop:
Real queries + labeled set → Retrieval eval → Generation eval
                           → Production monitoring → Failure replay

An end-to-end production RAG pipeline

A diagram containing only documents, embeddings, a vector database, and an LLM omits the production failure points: versions, authorization, query rewriting, lexical recall, reranking, context budgets, citation mapping, abstention, and evaluation.

1. Ingestion: make the evidence correct first

PDF ingestion must preserve more than continuous text. Heading hierarchy, page anchors, tables, captions, footnotes, and reading order can determine whether an answer is correct. Scanned files need OCR. LlamaParse, Unstructured, and specialized parsers are candidates, but evaluate them on your corpus for field completeness and layout fidelity.

Every document should carry a stable ID, source, tenant, ACL labels, version, effective period, update time, section path, page/anchor, and content hash. Updates and deletions must remove obsolete chunks precisely. An ingestion job should be idempotent, replayable, versioned, and observable.

2. Chunking: there is no universal 512-1024 setting

A fixed token window is a baseline, not a best practice. Good size depends on document structure, query type, the embedding model, and generation context.

Overlap reduces boundary loss but increases index size, duplicate retrieval, and context waste. Increase it only when evaluation shows a measurable benefit.

3. Embeddings and indexes: semantic search is one channel

Choose embeddings by language, domain, dimensions, cost, and deployment constraints. Multilingual models such as BGE-M3 can be candidates, not automatic answers. Build a retrieval set containing bilingual text, abbreviations, product names, identifiers, rare entities, and hard negatives; measure Recall@K, MRR, or NDCG.

Most systems benefit from at least dense vector search and BM25/lexical search. Dense retrieval handles paraphrase; lexical retrieval handles identifiers, error codes, names, and rare exact terms. Fuse rankings with a stable method such as Reciprocal Rank Fusion.

Apply tenant, ACL, validity-time, and data-type filters before or during retrieval. Never retrieve unauthorized content and merely remove it before prompting.

4. Query understanding and routing

Conversation turns may need rewriting into standalone queries. Complex questions may need decomposition. Identifier-heavy searches should favor lexical retrieval. Structured facts should route to SQL or an API rather than being forced through a vector store.

Rewriting must preserve user constraints. Record the original query, rewritten query, route, and model version so failures can be localized.

5. Recall, fusion, and reranking

First-stage retrieval optimizes coverage and can gather a larger pool across channels. A cross-encoder or specialized reranker then scores query-document pairs before evidence enters the context.

Top-K = 3-5 is not a universal rule. Retrieve broadly, rerank and deduplicate, then cut dynamically by token budget, score threshold, and evidence coverage. A reranker improves relevance; it does not enforce authorization, validity, or business eligibility. Deterministic filters own those constraints.

6. Context assembly

Do not concatenate raw Top-K chunks. Deduplicate them, restore section paths, merge useful neighbors, retain stable source IDs, control per-document dominance, and avoid cutting critical statements at a token boundary.

Each evidence block should carry a citation ID, title, safe source URL or filename, page/anchor, update time, and text. Ask the model to ground claims, separate fact from inference, abstain when evidence is insufficient, and return parseable citations.

A prompt alone cannot eliminate hallucinations. Abstention needs retrieval confidence, evidence coverage, and post-generation citation validation.

7. Generation, citations, and abstention

Support three outcomes: answer with sufficient evidence; answer the supported portion and disclose gaps; or abstain and request clarification or human review.

Validate that every material claim has a citation and that the cited passage actually entails it. High-risk workflows may require deterministic rules, structured-output validation, or approval. Store retrieval traces, scores, filter reasons, versions, cost, and latency internally without exposing sensitive metadata to users.

8. Evaluation

LayerMetricsQuestion
Parsing/chunkingField completeness, structure fidelity, chunk coverageDid valid evidence enter the index?
RetrievalRecall@K, MRR, NDCG, filter accuracyWas the evidence found and ranked well?
GenerationFaithfulness, citation accuracy, completeness, abstention accuracyIs the answer supported?
SystemP50/P95 latency, cost, errors, cache hit rate, ACL violationsIs it safe and operable?

Use real questions, including unanswerable, ambiguous, cross-document, version-conflict, and permission-isolation cases. Online feedback should include reformulation, repeated queries, escalation, and citation clicks—not just thumbs-up rates.

When to combine the frameworks

A clean hybrid exposes LlamaIndex ingestion and retrieval as a typed knowledge-search tool. LangGraph decides when to retrieve, whether to refine the query, which other tools to call, and how to handle approval and recovery.

LangGraph / LangChain Agent
  ├─ KnowledgeSearchTool → LlamaIndex Retriever / Query Engine
  ├─ SQLTool
  ├─ BusinessAPITool
  └─ HumanApproval

Do not combine them for a fixed two-step RAG application that one framework can express in a few dozen lines. Two abstraction stacks create version, tracing, type-conversion, and debugging costs. Separate them only when the data layer and orchestration layer have independently earned that complexity.

Final guidance

Framework choice affects developer experience, not the quality ceiling by itself. Evidence quality, authorization, retrieval and reranking, abstention, evaluation, and observability decide whether RAG is production-ready.

References

Back to Engineering Notes