AI State of the Art
Resources/Resources/10 min
Section 308

LLM Frameworks: LangChain, LlamaIndex, Haystack, DSPy

Listen as a podcast →

More Than Just a Chatty AI

An LLM on its own is a nice talker in an empty room. It can tell you stuff, but it can't actually do anything concrete. Add a database? A calculation tool? A source of truth? Now it's useful.

The real power of LLMs is when they're connected to the real world: when they can search through your documents, call APIs, execute code, and chain reasoning steps together. And for that, you need an orchestration framework.

LangChain reigns over prototyping. LlamaIndex dominates RAG (Retrieval-Augmented Generation). Haystack shines in production. DSPy reinvents prompt engineering. Each has its strength, and knowing which one to use when is a key skill.

The need for orchestration

A typical LLM application doesn't just call an API. It needs to: retrieve relevant documents (RAG), manage multi-turn conversations, call external tools, handle errors and retries, structure outputs, and chain multiple reasoning steps. Without a framework, you're reinventing the wheel on every project.

LangChain

Overview

LangChain is the most popular LLM orchestration framework, created by Harrison Chase in late 2022. It's the framework everyone knows and many love (or hate, opinions run strong).

  • LLM Abstraction: unified interface for OpenAI, Anthropic, local models, etc.
  • Chains: sequential composition of LLM operations
  • Agents: LLMs that decide which tools to use and when
  • Retrieval: integration with vector databases for RAG
  • LangGraph: agent framework based on state graphs (essentially the v2 of LangChain)

Key Components

  • LLM/Chat Models: wrappers for all model providers
  • Prompts: prompt templates with variables
  • Output Parsers: structured extraction from responses (JSON, Pydantic)
  • Tools: tool integration (web search, computation, APIs)
  • Vector Stores: integration with Pinecone, Weaviate, Chroma, FAISS

Documentation: docs.langchain.com GitHub: langchain-ai/langchain (90K+ stars)

LangSmith and LangGraph

  • LangSmith: monitoring and debugging platform for LLM applications. Essential for understanding what's happening in your chains in production.
  • LangGraph: stateful agent framework with flow graphs. More controllable than classic LangChain agents, and everything is converging toward it.
LangChain vs LangGraph

Classic LangChain uses sequential "chains" and autonomous "agents." LangGraph offers more control with explicit state graphs, enabling complex workflows with loops, conditional branching, and parallelism. For production applications, LangGraph is now recommended by the LangChain team itself.

LlamaIndex

Overview

LlamaIndex (formerly GPT Index) specializes in RAG and connecting LLMs to data. If your use case is "I want my LLM to answer based on MY documents," LlamaIndex is what you need.

  • Data Connectors: data ingestion from 160+ sources (PDF, databases, APIs, Slack, Notion)
  • Indexing: creating vector indexes and structures on your data
  • Query Engine: query engine combining search and generation
  • Agents: information retrieval agents capable of navigating through data

Strengths

  • RAG Specialization: the most comprehensive framework for retrieval-augmented generation
  • Data agents: agents specialized in querying structured and unstructured data
  • Multi-modal: support for documents with images
  • LlamaParse: intelligent parsing of complex documents (tables, charts)

Documentation: docs.llamaindex.ai GitHub: run-llama/llama_index (35K+ stars)

Haystack

Overview

Haystack, developed by deepset (Germany), is an NLP framework oriented toward pipelines and production:

  • Pipelines: modular composition of components (retrievers, readers, generators)
  • Production-ready: designed from the start for production deployment, not just prototyping
  • Flexible: support for many backends (Elasticsearch, FAISS, Weaviate)
  • Evaluation: built-in evaluation tools for RAG pipelines

Haystack 2.0

Version 2.0 was rewritten with a much more flexible pipeline architecture:

  • Components connectable via input/output types
  • Native LLM support (OpenAI, Anthropic, local)
  • Integration with Hugging Face models
  • Visual pipeline builder

Documentation: docs.haystack.deepset.ai GitHub: deepset-ai/haystack (17K+ stars)

Haystack vs LangChain

Haystack stands out with its more structured and production-ready approach. Where LangChain offers great flexibility (sometimes at the cost of complexity and "spaghetti code"), Haystack enforces a clear pipeline structure that facilitates deployment and maintenance. If you're building for production, Haystack deserves a serious look.

DSPy

Overview

DSPy (Declarative Self-improving Language Programs), created by Omar Khattab at Stanford, takes a radically different approach from the other frameworks. The idea: what if we stopped writing prompts by hand?

  • Declarative programming: you declare the program structure, not the prompts
  • Automatic optimization: prompts and few-shot examples are optimized automatically
  • Composable modules: ChainOfThought, ReAct, RAG as reusable modules
  • Compilers: transform a declarative program into optimized prompts

The DSPy Paradigm

Instead of manually writing prompts (and spending hours tweaking them), DSPy:

  1. Defines signatures (expected inputs/outputs)
  2. Composes modules (CoT, RAG, multi-hop reasoning)
  3. Compiles the program with an optimizer that finds the best prompts and examples
  4. Automatically evaluates on a development dataset

It's like going from SQL to machine learning: instead of specifying how to do it, you specify what to do, and the system optimizes the how.

Advantages

  • No manual prompt engineering: the optimizer finds the best prompts. No more hours tweaking commas.
  • Robustness: programs are less fragile than manual prompts
  • Portability: switch models (GPT-4 -> Claude -> Mistral) without rewriting prompts
  • Reproducibility: programs are verifiable and testable Python objects

Documentation: dspy-docs.vercel.app GitHub: stanfordnlp/dspy (17K+ stars) Paper: DSPy (Khattab et al., 2023)

Framework maturity

The LLM framework ecosystem evolves very rapidly. APIs change frequently, new frameworks appear, and best practices are constantly being redefined. Don't get too rigidly attached to a single framework. Understand the concepts (RAG, agents, orchestration) and the specific framework becomes interchangeable.

Choosing the Right Framework

NeedRecommended Framework
RAG and document searchLlamaIndex
Complex agents with conditional logicLangGraph
Robust production pipelineHaystack
Automatic prompt optimizationDSPy
Rapid prototyping and explorationLangChain
Simple application (basic chatbot)Direct API call (OpenAI, Anthropic)

Other Notable Tools

The ecosystem doesn't stop at these four frameworks. Here are other tools that deserve your attention:

  • Semantic Kernel (Microsoft): orchestration framework for .NET and Python. The choice if you're in the Microsoft ecosystem.
  • CrewAI: multi-role collaborative agent framework. Each agent has a "role" and they work together.
  • AutoGen (Microsoft): multi-agent conversational agent framework
  • Instructor: structured data extraction from LLMs. Simple, elegant, effective.
  • Outlines: structured generation (JSON, regex) with LLMs. Guarantees that the output follows a schema.