DevInterviewMasterStart free →
AI & AutomationFree to read

LangChain

The Swiss Army Knife of LLM Application Development

LangChain is the most popular framework for building LLM-powered applications. Understand its core abstractions - Chains for sequencing, Tools for external actions, and Memory for conversation context - and know when to use (and when to avoid) it.

What is LangChain?

The Most Popular LLM Framework - For Better or Worse

What LangChain Does:

LangChain is an open-source framework that provides building blocks and abstractions for creating LLM applications. It handles the boilerplate of connecting LLMs to data sources, tools, and memory so you can focus on application logic.

Think of LangChain as the Express.js of AI applications - it gives you a structured way to build things, with middleware (chains), route handlers (tools), and session management (memory).

Real-World Analogy - Zomato Order System:

Building an LLM app without LangChain is like running a restaurant without a POS system - you manually take orders, relay to kitchen, track payments. LangChain is like installing a Zomato POS:

  • Chains = Order Flow: Customer orders -> Kitchen prepares -> Quality check -> Serve (sequenced steps)
  • Tools = Kitchen Equipment: Oven, mixer, fridge - each does a specific job when needed
  • Memory = Customer History: "Your usual paneer tikka, sir?" - remembering past interactions

LangChain Ecosystem (2025):

  • langchain-core: Base abstractions (Runnables, messages, prompts)
  • langchain: Chains, agents, and retrieval strategies
  • langchain-community: 600+ third-party integrations
  • LangGraph: Stateful agent workflows (separate package)
  • LangSmith: Tracing, debugging, evaluation (separate SaaS)
  • LangServe: Deploy chains as REST APIs

Honest Assessment:

LangChain is both the most used and the most criticized LLM framework. It is great for prototyping and has the largest ecosystem. But its abstractions can be confusing, debugging can be hard, and for production you often need to go lower-level. Know its strengths and limitations.

Note: LangChain evolves rapidly. The LCEL (LangChain Expression Language) introduced in 2024 replaced most legacy chain classes. Always check you are using the latest patterns.

Chains and LCEL - Composing LLM Workflows

Sequencing Steps Into Powerful Workflows

What are Chains?

A Chain is a sequence of steps where the output of one step feeds into the next. The simplest chain: Prompt Template -> LLM -> Output Parser. More complex chains can include retrieval, tool calls, branching, and parallel execution.

LCEL (LangChain Expression Language):

LCEL is the modern way to compose chains in LangChain. It uses the pipe operator (|) to connect Runnables:

chain = prompt | llm | output_parser

Every component in LCEL implements the Runnable interface with methods: invoke(), stream(), batch(), ainvoke() (async). This gives you streaming, batching, and async for free.

Common Chain Patterns:

  • Simple Chain: prompt | llm | parser - For straightforward LLM calls
  • Retrieval Chain: retriever | format_docs | prompt | llm - RAG pattern
  • Sequential Chain: chain1 | chain2 - Output of first feeds into second
  • Parallel Chain: RunnableParallel runs multiple chains simultaneously and combines results
  • Branching Chain: RunnableBranch routes input to different chains based on conditions

Legacy vs Modern:

Legacy (Avoid)Modern (Use This)
LLMChainprompt | llm
SequentialChainchain1 | chain2
ConversationChainRunnableWithMessageHistory
RetrievalQAretriever | prompt | llm

Note: LCEL replaced all legacy chain classes. If you see LLMChain, SequentialChain, or ConversationChain in tutorials, those are outdated. Use the pipe operator instead.

Tools and Agents - Taking Action in the Real World

Giving Your LLM Hands to Interact With External Systems

What are Tools?

A Tool in LangChain is a function that an LLM can decide to call. It has a name, description (tells the LLM when to use it), and a function (the actual code to execute). The LLM reads tool descriptions and decides which tool to use based on the user question.

Built-in Tools:

  • Search: Tavily, Google Search, Wikipedia, DuckDuckGo
  • Code: Python REPL, shell execution
  • APIs: HTTP requests, OpenAPI spec execution
  • Math: Calculator, Wolfram Alpha
  • Retrieval: Any retriever can be wrapped as a tool

Creating Custom Tools:

There are two main ways to create custom tools:

  • @tool decorator: Simplest approach. Decorate any Python function. The docstring becomes the tool description. Type hints define the schema.
  • StructuredTool: More control. Define name, description, and args_schema (Pydantic model) explicitly. Better for complex inputs.

Best practice: Write detailed descriptions that tell the LLM WHEN to use the tool, not just WHAT it does. "Use this tool to get current stock prices for Indian NSE/BSE listed companies" is better than "Gets stock prices."

Agents - LLMs That Choose Tools:

An Agent is an LLM that decides which tools to call and in what order. LangChain supports several agent types, but the recommended one for 2025+ is create_react_agent (or LangGraph for complex cases).

The agent loop: User input -> LLM decides action -> Tool executes -> LLM sees result -> Decides next action or responds.

Note: Tool descriptions are the most important part - the LLM uses them to decide when to call each tool. Invest time writing clear, specific descriptions.

Memory - Giving LLMs Conversation Context

How to Make Your LLM Remember Past Conversations

The Memory Problem:

LLMs are stateless - each API call is independent. Without memory, every message is treated as a brand new conversation. Memory solves this by storing and injecting conversation history into each new LLM call.

Memory Types in LangChain:

  • ConversationBufferMemory: Stores complete conversation history. Simple but context window fills up fast. Good for short conversations (5-10 turns).
  • ConversationSummaryMemory: Uses an LLM to summarize older messages. Keeps context small. Costs extra LLM calls for summarization.
  • ConversationBufferWindowMemory: Keeps only last k messages. Simple sliding window. Loses older context but predictable token usage.
  • ConversationTokenBufferMemory: Keeps messages up to a max token count. More precise than window-based.

Modern Approach - RunnableWithMessageHistory:

The modern LangChain way to handle memory uses RunnableWithMessageHistory wrapper instead of legacy Memory classes. It wraps any chain and automatically loads/saves message history using a configurable backend (in-memory, Redis, PostgreSQL, etc.).

Key advantage: The session_id parameter lets you maintain separate conversation threads for different users - essential for multi-user production apps.

Memory Best Practices:

  • Use Redis or PostgreSQL for production memory storage (not in-memory)
  • Set a token limit to prevent context overflow
  • For RAG chatbots, store only Q&A pairs in memory, not the full retrieved context
  • Consider using conversation summary for long sessions

Note: Memory management is crucial for chatbot applications. Without it, the LLM forgets everything between messages. With too much, you hit context window limits.

LangChain Criticisms and When to Avoid It

Honest Talk About LangChain Limitations

Criticism 1: Abstraction Overload

LangChain wraps everything in abstractions. Sometimes you just want to call the OpenAI API directly, but LangChain adds layers of Runnables, Chains, and wrappers. For simple use cases, these abstractions add complexity without value.

Criticism 2: Debugging Difficulty

When something goes wrong inside a chain, error messages can be cryptic. The deep abstraction layers make it hard to trace where exactly a failure occurred. LangSmith helps, but it is a paid service.

Criticism 3: Rapid Breaking Changes

LangChain evolves very fast. Code from 6 months ago often does not work with the latest version. Legacy patterns (LLMChain, ConversationChain) are deprecated frequently.

When LangChain Shines:

  • Prototyping: Fastest way to build a RAG demo or agent prototype
  • Integration variety: 600+ integrations for every vector DB, LLM, and tool
  • Learning: Great for understanding LLM application patterns
  • Standard patterns: When your use case fits a common pattern (RAG, chatbot, agent)

When to Skip LangChain:

  • Simple apps: Direct API calls with the openai/anthropic SDK are simpler
  • Production RAG: Consider Haystack for type safety and reliability
  • Complex agents: LangGraph (separate from LangChain) or custom solutions
  • Performance critical: The abstraction layers add latency

Note: Do not use LangChain just because it is popular. Evaluate if the abstractions help YOUR use case. For simple applications, direct SDK calls are often better.

Interview Questions

Q: What is LCEL and why did LangChain introduce it?

LCEL (LangChain Expression Language) is the modern way to compose chains using the pipe operator (|). It replaced legacy classes like LLMChain and SequentialChain. Every component implements the Runnable interface with invoke(), stream(), batch(), and async variants. LCEL was introduced to provide a consistent API, built-in streaming support, and easier composition of complex workflows. It makes chains more readable and gives free streaming/batching capabilities.

Q: How does memory work in LangChain and what are the different types?

LLMs are stateless - memory stores and injects conversation history into each call. Types: (1) Buffer - stores complete history, simple but fills context window. (2) Summary - LLM summarizes older messages, saves tokens but costs extra calls. (3) Window - keeps last k messages, predictable token usage. (4) Token Buffer - keeps messages up to max token count. Modern approach uses RunnableWithMessageHistory with session_id for multi-user support and configurable backends (Redis, PostgreSQL).

Q: What are the main criticisms of LangChain and when would you avoid using it?

Main criticisms: (1) Abstraction overload - too many wrapper layers for simple tasks. (2) Debugging difficulty - cryptic errors deep in abstraction layers. (3) Rapid breaking changes - code from 6 months ago often does not work. Avoid LangChain for: simple apps (use direct SDK), production RAG (consider Haystack), performance-critical systems (abstraction latency), and complex agents (use LangGraph). Use it for: prototyping, learning, and when you need its vast integration ecosystem.

Frequently Asked Questions

What is LangChain?

LangChain is the most popular framework for building LLM-powered applications. Understand its core abstractions - Chains for sequencing, Tools for external actions, and Memory for conversation context - and know when to use (and when to avoid) it.

How does LangChain work?

The Most Popular LLM Framework - For Better or Worse What LangChain Does: LangChain is an open-source framework that provides building blocks and abstractions for creating LLM applications. It handles the boilerplate of connecting LLMs to data sources, tools, and memory so you can focus on application logic.

Browse all AI & Automation topics →

Practice this on DevInterviewMaster

Read the full LangChain breakdown with interactive demos, quizzes, and Hinglish notes.

Open the interactive topic →

800+ system-design, LLD, coding, and design-pattern topics. Unlock everything with Pro (₹499, one-time) or Ultimate (₹999, one-time) — lifetime access, no subscription.