Python: LangChain vs LlamaIndex vs CrewAI, AI Agent Frameworks Compared

You have three agent frameworks open in browser tabs, each with a slick demo. LangChain (and its agent layer, LangGraph) promises general-purpose Large Language Model (LLM) orchestration. LlamaIndex specializes in connecting LLMs to your own data. CrewAI focuses on multi-agent teams. So which one do you actually pick? It depends on what you are building, and this langchain vs llamaindex guide hands you the decision framework. Not marketing, just side-by-side code that solves the same problem in each one.

“The largest and most powerful applications combine LLMs with other sources of computation or knowledge.”

LangChain documentation

Last Updated: July 2026 | Tested on: Python 3.14.6, langchain 1.3.10, llama-index 0.14.22 | Difficulty: Advanced | Reading Time: 13 minutes

📋 Prerequisites:
  • AI agents tutorial
  • pip install langchain langchain-openai langchain-community langchain-text-splitters faiss-cpu llama-index crewai

LangChain and LlamaIndex are the two most popular Python frameworks for building LLM-powered apps, but they were built for different jobs. LangChain is a general-purpose orchestration framework: it wires LLMs up to tools, databases, and APIs through chains and agents. LlamaIndex is laser-focused on connecting LLMs to your own data, so it owns document loading, indexing, and retrieval-augmented generation (RAG).

Think of it like a kitchen. LangChain is the full set of pots, knives, and gadgets that lets you cook anything. LlamaIndex is the specialist rice cooker: one job, done perfectly, almost no effort. You can keep both on the counter, and plenty of teams do. This post puts them side by side, the same task written in each framework, so you judge them by the code and not the sales pitch. By the end you will know which one fits your project, and how they work together when you need both.

Head-to-Head Comparison

🔍 LangChain vs LlamaIndex🦜 LangChainGeneral-purposeAgent framework🦙 LlamaIndexData-focusedRAG frameworkChains and agentsTool orchestrationMemory managementBest for: chatbots,agents, multi-stepworkflowsData connectorsIndex typesQuery enginesBest for: RAG,knowledge bases,document QandAPython LangChain vs LlamaIndex: Core Features and Best Use Cases Compared

Tip: click the diagram to open it full screen, then use the zoom and pan controls for a closer look.

The diagram compares the LangChain and LlamaIndex architectures. LangChain gives you a general-purpose chain-and-agent framework for building LLM-powered workflows, while LlamaIndex connects LLMs to your data through indexing, retrieval, and query engines. LangChain shines at orchestrating multi-step reasoning with tools. LlamaIndex shines at retrieval-augmented generation over big document collections. Plenty of production systems run both: LlamaIndex for the data layer, LangChain for the orchestration layer.

CriteriaLangChain / LangGraphLlamaIndexCrewAI
Core strengthGeneral LLM orchestrationData indexing & retrieval (RAG)Multi-agent collaboration
ArchitectureGraph-based state machineIndex + query pipelineRole-based agent teams
RAG qualityGood (via retrievers)Excellent (purpose-built)Basic (via tools)
Agent capabilityExcellent (LangGraph)Good (agent mode)Excellent (native)
Learning curveHigh (many abstractions)MediumLow (intuitive API)
Ecosystem sizeLargest (huge integration catalog)Large (many data loaders on LlamaHub)Growing (tool catalog still expanding)
Production useWidely adoptedWidely adopted for RAGProduction-ready (v1.x)
📝 Version note (at the time of writing, mid-2026): this post is tested on LangChain 1.x, LlamaIndex 0.14.x, and CrewAI 1.x. LangChain reached 1.0 (GA October 2025), so agents are now built with create_agent, and the older AgentExecutor plus legacy chains moved to a separate langchain-classic package. The pipe-style LCEL syntax (prompt | llm | parser) you see below is not deprecated; it is still the current way to build RAG chains. These libraries ship a release every few weeks, so treat the pins here as a snapshot and check the official docs for the latest: LangChain, LlamaIndex, and CrewAI.

The pattern outlives the tool. A RAG pipeline (load, chunk, embed, retrieve, answer) and an agent loop (think, act, observe, repeat) are stable ideas; the library is the swappable part. If any framework here stalls, the same pattern maps cleanly onto the credible alternatives: Haystack for typed RAG pipelines, the OpenAI Agents SDK or plain MCP tools for agents, and Chroma, pgvector, or Qdrant in place of the FAISS store used below.

Same Task, Three Frameworks

Think of it like a test drive. The fairest way to judge two cars is to drive both on the same road, not to read their brochures. So the fairest way to compare frameworks is to make each one solve the same problem. Here is the task: build a small Q&A system that answers questions about a set of documents. Both snippets below call the OpenAI Application Programming Interface (API), so they need an API key and they cost a few cents to run. The outputs shown are illustrative, captured from a sample run, not generated at build time.

📄 langchain_rag.py: RAG with LangChain

from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser

# Aditi builds RAG with LangChain
documents = [
    "Python 3.14.6 makes free-threading officially supported via PEP 779, so the GIL can be turned off.",
    "The experimental JIT compiler from PEP 744 keeps improving in Python 3.14.6 for extra speed.",
    "Pattern matching with match/case was introduced in Python 3.10 via PEP 634.",
]

# Split, embed, and store
splitter = RecursiveCharacterTextSplitter(chunk_size=200, chunk_overlap=20)
chunks = splitter.create_documents(documents)
vectorstore = FAISS.from_documents(chunks, OpenAIEmbeddings())
retriever = vectorstore.as_retriever(search_kwargs={"k": 2})

# Build the RAG chain
template = "Answer based on context:\n{context}\n\nQuestion: {question}"
prompt = ChatPromptTemplate.from_template(template)
# gpt-5.4-mini is a current low-cost OpenAI model
# (at the time of writing; models change fast, check the provider docs)
llm = ChatOpenAI(model="gpt-5.4-mini")

chain = (
    {"context": retriever, "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)

answer = chain.invoke("What is free-threading in Python 3.14.6?")
print(f"LangChain answer: {answer}")

📄 llamaindex_rag.py: RAG with LlamaIndex

from llama_index.core import VectorStoreIndex, Document, Settings
from llama_index.llms.openai import OpenAI as LlamaOpenAI

# Prathamesh builds RAG with LlamaIndex: fewer lines, RAG-optimized
# gpt-5.4-mini is a current low-cost OpenAI model
# (at the time of writing; models change fast, check the provider docs)
Settings.llm = LlamaOpenAI(model="gpt-5.4-mini")

documents = [
    Document(text="Python 3.14.6 makes free-threading officially supported via PEP 779, so the GIL can be turned off."),
    Document(text="The experimental JIT compiler from PEP 744 keeps improving in Python 3.14.6 for extra speed."),
    Document(text="Pattern matching with match/case was introduced in Python 3.10 via PEP 634."),
]

# Two lines: index, then query
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()

answer = query_engine.query("What is free-threading in Python 3.14.6?")
print(f"LlamaIndex answer: {answer}")

▶ Output (both frameworks, illustrative, from a sample run)

LangChain answer: Free-threading in Python 3.14.6 became officially supported
through PEP 779. It lets you turn off the Global Interpreter Lock (GIL) so
threads can run truly in parallel for CPU-bound work.

LlamaIndex answer: In Python 3.14, free-threading is officially supported via
PEP 779. With the GIL turned off, Python threads can run in parallel instead of
taking turns one at a time.

What happened here: Both frameworks pulled the right answer out of the same three documents. The difference is the amount of wiring. LangChain made you build the pipeline by hand: splitter, embeddings, vector store, retriever, prompt, then a chain piped through a StrOutputParser so you get back a plain string. LlamaIndex did the whole thing in two lines, because RAG is its home turf, so chunking, embedding, and retrieval happen for you behind the scenes. For a pure RAG app, LlamaIndex wins on simplicity. The moment you need RAG plus agents plus custom branching, LangChain’s extra knobs start to earn their keep.

When to Use Which

📄 decision_guide.py: choosing the right framework

# Vinay's decision framework for choosing an AI framework
decision_tree = {
    "Primary need is RAG / document Q&A": {
        "recommendation": "LlamaIndex",
        "reason": "Purpose-built for data retrieval. Best chunking, indexing, and query optimization.",
        "alternative": "LangChain if you also need complex agent workflows",
    },
    "Building agentic workflows with tools": {
        "recommendation": "LangGraph (LangChain)",
        "reason": "Graph-based orchestration handles conditional branching and state management.",
        "alternative": "Raw function calling (LLM API and function calling tutorial) if workflow is simple",
    },
    "Multi-agent team collaboration": {
        "recommendation": "CrewAI",
        "reason": "Role-based agents with task delegation is CrewAI's core design.",
        "alternative": "LangGraph sub-graphs for more control",
    },
    "Quick prototype / proof of concept": {
        "recommendation": "LlamaIndex or CrewAI",
        "reason": "Lowest boilerplate. Get results in minutes.",
        "alternative": "Raw OpenAI/Anthropic API for maximum simplicity",
    },
    "Enterprise production system": {
        "recommendation": "LangChain + LangSmith",
        "reason": "Largest ecosystem, mature observability (LangSmith, or self-hosted Langfuse), most integrations.",
        "alternative": "Custom code if you want zero framework dependency",
    },
}

print("AI Framework Decision Guide\n")
for scenario, info in decision_tree.items():
    print(f"  If: {scenario}")
    print(f"    Use: {info['recommendation']}")
    print(f"    Why: {info['reason']}")
    print(f"    Alt: {info['alternative']}")
    print()

▶ Output

AI Framework Decision Guide

  If: Primary need is RAG / document Q&A
    Use: LlamaIndex
    Why: Purpose-built for data retrieval. Best chunking, indexing, and query optimization.
    Alt: LangChain if you also need complex agent workflows

  If: Building agentic workflows with tools
    Use: LangGraph (LangChain)
    Why: Graph-based orchestration handles conditional branching and state management.
    Alt: Raw function calling (LLM API and function calling tutorial) if workflow is simple

  If: Multi-agent team collaboration
    Use: CrewAI
    Why: Role-based agents with task delegation is CrewAI's core design.
    Alt: LangGraph sub-graphs for more control

  If: Quick prototype / proof of concept
    Use: LlamaIndex or CrewAI
    Why: Lowest boilerplate. Get results in minutes.
    Alt: Raw OpenAI/Anthropic API for maximum simplicity

  If: Enterprise production system
    Use: LangChain + LangSmith
    Why: Largest ecosystem, mature observability (LangSmith, or self-hosted Langfuse), most integrations.
    Alt: Custom code if you want zero framework dependency

What happened here: This snippet is plain Python, no API key needed, and the output above is exactly what it printed when we ran it. Read it like a cheat sheet you pin above your desk. Match your situation to the left-hand If:, then read across. Notice the pattern: when the job is mostly about your documents, LlamaIndex keeps showing up. When the job is about steps, tools, and branching, LangChain (through LangGraph) takes over. And the honest row that most marketing pages skip is the last one in spirit: sometimes the right answer is no framework at all, just a thin wrapper around raw API calls.

Common Mistakes

⚠️ Common Mistakes:
  • Framework hopping: Jumping to a new framework mid-project because it looks shinier on Twitter. Pick one, build something real, and only switch if you hit a wall you genuinely cannot climb.
  • Using LangChain for simple RAG: If all you need is document Q&A, LlamaIndex does it in about five lines. LangChain’s extra abstraction layers add ceremony without buying you anything for a use case that simple.
  • Ignoring the “no framework” option: For a lot of production systems, raw API calls with a thin wrapper of your own are easier to maintain and debug than any framework. Reach for a framework when it saves you real work, not by reflex.

Practice Exercises

  1. Exercise 1: Build the same three-document Q&A in both LangChain and LlamaIndex, then count the lines of code each one took. Where did LlamaIndex hide the work that you had to spell out by hand in LangChain?
  2. Exercise 2: Feed both frameworks a folder of your own PDFs or markdown notes and ask three real questions. Compare not just the answers, but how much setup each framework needed to load the files.
  3. Exercise 3: Go hybrid. Use LlamaIndex to index the documents and expose a retriever, then let LangGraph drive an agent that decides when to call it. This is the pattern most real production stacks land on.

More in this series:

Frequently Asked Questions

LangChain vs LlamaIndex: which one should I pick?

Pick LlamaIndex when the heart of your app is searching your own documents (RAG, knowledge bases, document Q&A). Pick LangChain when you need agents, tools, and multi-step workflows, especially through LangGraph. If you are honestly not sure, start with LlamaIndex for retrieval and add LangChain later only if your workflow grows tools and branching.

Can I use multiple frameworks together?

Yes, and many teams do. A common setup is LlamaIndex for the RAG pipeline and LangGraph for the agent orchestration layer. LlamaIndex even ships a LangChain integration. Use each framework for the part it is best at instead of forcing one to do everything.

Am I locked into a framework once I choose it?

Partly. The LLM calls themselves are portable, since they all hit OpenAI or Anthropic underneath. What gets sticky is the orchestration logic, the memory handling, and the tool definitions, which are written in each framework’s own style. To keep your options open, keep your core business logic in plain Python functions and let the framework handle only the orchestration around them.

Which framework is fastest?

For retrieval, LlamaIndex has the most tuned RAG pipeline. For tight agent loops, raw API calls win because there is no framework sitting in the middle. Frameworks like LangChain do add a little overhead per step from their abstraction layers, but in practice the model’s own thinking time (often half a second to several seconds per call) dwarfs that, so it rarely decides your architecture.

What’s Next?

You now know the real differences behind the langchain vs llamaindex question: LlamaIndex is the specialist that makes RAG almost effortless, LangChain (with LangGraph) is the general-purpose orchestrator for agents, tools, and branching workflows, and CrewAI leans into multi-agent teams. You saw the same Q&A task written in each, a decision guide you can pin above your desk, and the honest reminder that sometimes no framework at all is the right call. The biggest takeaway: pick by the shape of your problem, not by the loudest demo.

Next, in the YOLO object detection tutorial, we leave text behind and move to images, building an object detection system that spots and labels objects in real time. For the full learning path from basics to advanced AI, head back to the Python + AI/ML tutorial series home.

Interview Questions on LangChain vs LlamaIndex

Try each one aloud first. The phrasing you produce under mild pressure is what interviews measure.

Q: What is the core difference between LangChain and LlamaIndex?

LangChain is a general-purpose orchestration framework that wires LLMs to tools, APIs, and databases through chains and agents, with LangGraph handling stateful agent workflows. LlamaIndex is purpose-built for connecting LLMs to your own data, so it specializes in document loading, indexing, retrieval, and RAG. In short, LangChain is about orchestrating steps and tools, while LlamaIndex is about retrieving from your data.

Q: When would you choose LlamaIndex over LangChain?

Choose LlamaIndex when the heart of your app is searching your own documents: RAG, knowledge bases, or document Q&A. It handles chunking, embedding, indexing, and retrieval with far less boilerplate, often in about five lines versus the full pipeline LangChain makes you assemble. Reach for LangChain instead when you need agents, tool calling, memory, or multi-step branching workflows.

Q: Can LangChain and LlamaIndex be used together, and how?

Yes, and it is a common production pattern. You use LlamaIndex for the data layer, indexing documents and exposing a retriever or query engine, and LangGraph (LangChain) for the orchestration layer that decides when to retrieve, call other tools, or branch. LlamaIndex even ships a LangChain integration, so you can wrap its retriever as a LangChain tool. The rule of thumb is to let each framework do the part it is best at.

Q: How does CrewAI fit alongside these two?

CrewAI focuses on multi-agent collaboration: role-based agents that delegate tasks to each other, with a low-boilerplate, intuitive API. Its RAG capability is basic compared to LlamaIndex, and its agent control is less granular than LangGraph, but it shines when you want a small team of specialized agents working together quickly. Think of it as the fast way to prototype agent teams.

Q: Scenario: your RAG chatbot gives correct but slow answers, and profiling shows most time is spent inside the framework, not the model call. What do you check first?

First confirm where the time actually goes, because model latency (often half a second to several seconds per call) usually dwarfs framework overhead. If the framework really is the bottleneck, look at how many retrieval and LLM steps the chain makes per query, whether embeddings are recomputed instead of cached, and how large your retrieved context is. Over-chunking, a high top-k retriever, or extra unnecessary chain steps are common culprits. Trimming the pipeline, caching embeddings, and lowering k usually recover most of the time before you blame the framework itself.

Q: Scenario: a teammate wants to rewrite a working LlamaIndex RAG app in LangChain because it looks more powerful. How do you respond?

Push back on framework hopping unless there is a concrete need the current stack cannot meet. If the app is pure document Q&A and working, LangChain’s extra abstraction layers add ceremony without buying anything. The right trigger for adding LangChain is a genuine new requirement, such as agents, tool orchestration, or complex conditional branching, and even then you can keep LlamaIndex for retrieval and add LangGraph on top rather than rewriting from scratch.

Q: How do you avoid getting locked into one framework?

Keep your core business logic in plain Python functions and let the framework handle only the orchestration around them. The LLM calls themselves are portable since they all hit OpenAI or Anthropic underneath; what gets sticky is the orchestration, memory, and tool definitions written in each framework’s style. By isolating those framework-specific pieces behind your own interfaces, you can swap or drop a framework later with far less pain.

Previous: Build an Model Context Protocol (MCP) Server in Python (FastMCP, Step by Step)

Next: AI Agent Evaluation: Trajectories, Tool Errors, Success

Series Home: Python + AI/ML Tutorial Series

RahulAuthor posts

Avatar for Rahul

Rahul is a passionate IT professional who loves to sharing his knowledge with others and inspiring them to expand their technical knowledge. Rahul's current objective is to write informative and easy-to-understand articles to help people avoid day-to-day technical issues altogether. Follow Rahul's blog to stay informed on the latest trends in IT and gain insights into how to tackle complex technical issues. Whether you're a beginner or an expert in the field, Rahul's articles are sure to leave you feeling inspired and informed.

No comment

Leave a Reply

Your email address will not be published. Required fields are marked *