GenAIHub
← Back to Technical Section

LangChain

The Framework for Building LLM-Powered Applications

What is LangChain?

LangChain is an open-source framework designed to simplify the development of applications powered by large language models (LLMs). It provides a modular architecture that connects LLMs with external data sources, APIs, databases, and computational tools—acting as a "Swiss Army knife" for building sophisticated AI applications.

"LangChain is a versatile framework for building complex LLM-powered workflows, focusing on multi-step reasoning, dynamic agents, and extensive integrations with external tools and data sources."

LangChain

Core Framework

LangGraph

Stateful Agents

LangSmith

Observability

LangServe

Deployment

Core Concepts

Chains

Chains are sequences of operations that process data in a specific order. They combine prompts, LLM calls, and various functionalities into structured pipelines. Think of them as building blocks that you can compose together.

Sequential Chains Map/Reduce Chains Router Chains

Agents

Unlike chains with hardcoded sequences, agents use LLMs to dynamically decide which actions to take. They reason about the problem and choose appropriate tools in real-time—essential for complex, context-aware tasks.

ReAct Agent OpenAI Functions Tool Calling

Memory

Memory enables LLM applications to retain conversational context across interactions. Crucial for building stateful chatbots that remember previous exchanges and maintain coherent dialogues.

Buffer Memory Summary Memory Entity Memory

Tools

Tools are functions or capabilities that agents can invoke. They extend LLM capabilities beyond text generation—enabling web search, database queries, API calls, calculations, and more.

Web Search Vector DB APIs Custom Functions

LangChain Expression Language (LCEL)

LCEL is the modern, declarative way to compose LangChain components. Using the pipe (|) operator, you can build complex workflows that support streaming, async, batching, and parallel execution out of the box.

# Simple LCEL Chain Example
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

# Define components
prompt = ChatPromptTemplate.from_template("Explain {topic} in simple terms")
model = ChatOpenAI(model="gpt-4")
parser = StrOutputParser()

# Compose with LCEL pipe operator
chain = prompt | model | parser

# Invoke the chain
result = chain.invoke({"topic": "quantum computing"})
print(result)
Streaming
Async Support
Batching
Parallel Execution
Retries & Fallbacks
Dynamic Routing

Memory Example

Memory allows your chatbot to remember previous messages in the conversation. Here's a simple example using ConversationBufferMemory:

# Chatbot with Memory
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.messages import HumanMessage, AIMessage

# Create the model
llm = ChatOpenAI(model="gpt-4")

# Create prompt with chat history placeholder
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant."),
    MessagesPlaceholder(variable_name="chat_history"),
    ("human", "{question}"),
])

chain = prompt | llm

# Maintain chat history manually
chat_history = []

# First interaction
response = chain.invoke({
    "chat_history": chat_history,
    "question": "My name is Carlos"
})
chat_history.append(HumanMessage(content="My name is Carlos"))
chat_history.append(AIMessage(content=response.content))

# Second interaction - remembers the name!
response = chain.invoke({
    "chat_history": chat_history,
    "question": "What's my name?"
})
print(response.content)  # "Your name is Carlos!"

Tip: For more advanced memory (summary, entity extraction, vector-based), check ConversationSummaryMemory or VectorStoreRetrieverMemory.

Tools Example

Tools extend LLM capabilities by allowing them to execute functions. Here's how to create a simple calculator tool that the agent can use:

# Agent with Custom Tools
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
from langgraph.prebuilt import create_react_agent

# Define a custom tool with @tool decorator
@tool
def calculate(expression: str) -> str:
    """Calculate a math expression. Use this for any math operations."""
    try:
        result = eval(expression)
        return f"Result: {result}"
    except:
        return "Error: Invalid expression"

@tool
def get_weather(city: str) -> str:
    """Get current weather for a city."""
    # In production, call a real weather API
    return f"Weather in {city}: 25°C, Sunny"

# Create the model with tools
llm = ChatOpenAI(model="gpt-4")
tools = [calculate, get_weather]

# Create a ReAct agent
agent = create_react_agent(llm, tools)

# Ask something that requires a tool
result = agent.invoke({
    "messages": [{"role": "user", "content": "What is 15 * 23 + 100?"}]
})
print(result["messages"][-1].content)  # Uses calculate tool!

Built-in Tools

LangChain includes tools for: Wikipedia, DuckDuckGo, Python REPL, Tavily Search, and many more.

MCP Integration

Use Model Context Protocol (MCP) for standardized tool connections.

Modular Architecture

LangChain's layered architecture allows swapping components easily. Change your LLM provider, vector store, or embedding model without rewriting your application logic.

Package Purpose Examples
langchain-core Base abstractions & LCEL Runnables, Messages, Prompts
langchain-openai OpenAI integrations ChatOpenAI, OpenAIEmbeddings
langchain-anthropic Anthropic/Claude integrations ChatAnthropic
langchain-community Community integrations Vector stores, tools, loaders
langgraph Stateful agent graphs StateGraph, cycles, persistence

LangChain vs LlamaIndex

Both frameworks serve LLM development but with different focuses. Choose based on your primary use case:

LangChain

"Swiss Army Knife" for LLM apps

  • Complex multi-step workflows
  • Dynamic agentic reasoning
  • Extensive tool integrations
  • General-purpose applications

LlamaIndex

"Precision Scalpel" for RAG

  • Data ingestion & indexing
  • Optimized retrieval performance
  • Multiple index types
  • RAG-focused applications

💡 Pro Tip: Many production systems combine both—LlamaIndex for optimized data retrieval, LangChain for intelligent response generation.

Common Use Cases

Chatbots

Conversational assistants with memory and context

Autonomous Agents

Task automation with reasoning capabilities

RAG Systems

Q&A over private documents

Document Processing

Summarization, extraction, analysis

Code Assistants

Code generation and debugging helpers

Workflow Automation

Multi-step business processes

Quick Start

# Install LangChain
pip install langchain langchain-openai langchain-community

# Set your API key
export OPENAI_API_KEY="your-api-key"

# Create a simple chain
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate

llm = ChatOpenAI(model="gpt-4")
prompt = ChatPromptTemplate.from_template("You are a helpful assistant. {question}")

chain = prompt | llm
response = chain.invoke({"question": "What is LangChain?"})
print(response.content)

Resources & References

Related Topics

Test Your Knowledge

Score 8/10 or higher to pass