Learnixo
Back to blog
AI Systemsintermediate

What Is LangChain?

Framework overview, core abstractions (LLMs, chains, agents, memory, tools), when to use vs raw API, installation, and your first hello-world chain.

Asma Hafeez KhanMay 15, 20268 min read
LangChainPythonLLMChainsAgentsFramework
Share:š•

What Is LangChain?

LangChain is an open-source framework that makes it straightforward to build applications powered by large language models. Rather than writing raw API calls and stitching together text manipulation by hand, LangChain gives you composable building blocks — models, prompts, chains, memory, tools, and agents — that snap together into production-ready pipelines.

Why LangChain Exists

Before LangChain, building an LLM app meant:

  • Formatting prompts manually for every request
  • Writing retry logic, streaming handlers, and token counting from scratch
  • Connecting external data sources (PDFs, databases, websites) by hand
  • Building your own conversation memory management
  • Reinventing tool-calling orchestration for every project

LangChain extracts all of that boilerplate into a shared library with a consistent interface, so you spend time on your product logic rather than infrastructure.

Core Abstractions

LangChain organizes itself around six concepts. Understanding these concepts is the key to using LangChain effectively.

1. Models

Models are the interface to language model providers. LangChain wraps OpenAI, Anthropic, Azure OpenAI, Cohere, HuggingFace, and dozens of others behind a uniform API. You swap providers by changing one line.

2. Prompts

Prompt templates let you parameterize prompts. Instead of building strings with f-strings, you define a template once and fill it at runtime. This keeps prompts versioned, testable, and reusable.

3. Chains

A chain is a sequence of steps. The simplest chain is: format prompt, call model, parse output. More complex chains combine retrieval, summarization, translation, and branching logic.

4. Memory

Memory allows a chain to persist information across turns of a conversation. Without memory, every call to the LLM is stateless. With memory, you can build chatbots that remember what was said earlier in the session or across sessions.

5. Tools

Tools are functions that an LLM can call during reasoning. Examples: search the web, run Python code, query a database, look up a calendar. LangChain provides many built-in tools and makes it trivial to build custom ones.

6. Agents

An agent uses an LLM to decide which tool to call and in what order. Rather than a fixed pipeline, an agent reasons dynamically — it sees the results of one tool call and decides what to do next. This enables open-ended problem solving.

When to Use LangChain vs Raw API

Use the raw OpenAI or Anthropic SDK when:

  • You are building a single, simple completion endpoint
  • You need absolute control over every HTTP parameter
  • You have a tiny codebase that will not grow in complexity

Use LangChain when:

  • You need to combine retrieval with generation (RAG)
  • You are building a multi-turn chatbot
  • You want agents that call tools
  • You need to swap between providers without rewriting your pipeline
  • You want built-in tracing, caching, and evaluation tooling

Installation

LangChain is split into focused packages. Install only what you need.

Bash
# Core LangChain library
pip install langchain

# OpenAI integration
pip install langchain-openai

# Anthropic integration
pip install langchain-anthropic

# Community integrations (FAISS, Wikipedia, etc.)
pip install langchain-community

# For vector store work
pip install faiss-cpu chromadb

# For PDF loading
pip install pypdf

Set your API keys as environment variables (or use a .env file with python-dotenv):

Bash
export OPENAI_API_KEY="sk-..."
export ANTHROPIC_API_KEY="sk-ant-..."

Your First LangChain Chain

Let's build the canonical hello-world: a chain that takes a topic and returns a one-sentence explanation.

Python
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

# Step 1: define the model
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)

# Step 2: define a prompt template
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a concise technical educator. Explain things in one sentence."),
    ("human", "Explain {topic} to a software engineer."),
])

# Step 3: define an output parser
parser = StrOutputParser()

# Step 4: compose the chain with the pipe operator
chain = prompt | llm | parser

# Step 5: invoke the chain
result = chain.invoke({"topic": "vector embeddings"})
print(result)
# Vector embeddings are numerical representations of data that capture
# semantic meaning, enabling similarity comparisons in high-dimensional space.

This is the essence of LangChain Expression Language (LCEL). The | operator chains runnables together. Each component receives output from the previous one.

Understanding the Chain Flow

When you call chain.invoke({"topic": "vector embeddings"}):

  1. The ChatPromptTemplate receives the dict and formats the messages.
  2. The formatted messages are passed to ChatOpenAI, which calls the OpenAI API.
  3. The AIMessage response is passed to StrOutputParser, which extracts the text string.
  4. The string is returned to your code.
Python
# You can inspect each step individually
formatted = prompt.invoke({"topic": "vector embeddings"})
print(formatted.messages)
# [SystemMessage(content='You are a concise technical educator...'),
#  HumanMessage(content='Explain vector embeddings to a software engineer.')]

response = llm.invoke(formatted)
print(type(response))  # <class 'langchain_core.messages.ai.AIMessage'>
print(response.content)  # The actual text

parsed = parser.invoke(response)
print(type(parsed))  # <class 'str'>

Streaming Output

LangChain supports streaming out of the box. Replace .invoke() with .stream():

Python
for chunk in chain.stream({"topic": "transformer architecture"}):
    print(chunk, end="", flush=True)
print()  # newline at the end

Each chunk is a string fragment as tokens arrive from the API.

Batch Processing

Process multiple inputs simultaneously:

Python
topics = [
    {"topic": "attention mechanisms"},
    {"topic": "RLHF"},
    {"topic": "RAG pipelines"},
]

results = chain.batch(topics)
for topic, result in zip(topics, results):
    print(f"{topic['topic']}: {result}\n")

batch() runs calls concurrently by default, making it much faster than sequential invocation.

A More Realistic Example: Blog Post Generator

Python
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

llm = ChatOpenAI(model="gpt-4o", temperature=0.7)

outline_prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a technical blog writer. Generate a structured outline."),
    ("human", "Create a 5-point outline for a blog post about {topic}. Return only the numbered list."),
])

post_prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a technical blog writer. Write engaging, accurate content."),
    ("human", "Write a full blog post based on this outline:\n\n{outline}\n\nTopic: {topic}"),
])

parser = StrOutputParser()

# Chain 1: generate outline
outline_chain = outline_prompt | llm | parser

# Chain 2: generate full post (receives output from chain 1)
from langchain_core.runnables import RunnablePassthrough

full_chain = (
    {"outline": outline_chain, "topic": RunnablePassthrough()}
    | post_prompt
    | llm
    | parser
)

# Wait — this won't work because outline_chain expects a dict with "topic"
# but full_chain is passing a string. Let's fix it properly:

from langchain_core.runnables import RunnableParallel

# Correct approach: pass topic through and generate outline in parallel
chain = (
    RunnableParallel(
        topic=RunnablePassthrough(),
        outline=outline_chain,
    )
    | post_prompt
    | llm
    | parser
)

post = chain.invoke({"topic": "LangChain LCEL patterns"})
print(post[:500])

Environment Setup with dotenv

For local development, manage secrets with a .env file:

Python
# .env
# OPENAI_API_KEY=sk-...
# ANTHROPIC_API_KEY=sk-ant-...

from dotenv import load_dotenv
import os

load_dotenv()  # reads .env file into os.environ

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o-mini")
# ChatOpenAI automatically reads OPENAI_API_KEY from environment

LangChain Package Structure

Understanding the package layout prevents import confusion:

| Package | Purpose | |---|---| | langchain-core | Base interfaces: Runnable, BaseMessage, BaseLLM | | langchain | High-level chains, agents, memory | | langchain-openai | ChatOpenAI, OpenAIEmbeddings | | langchain-anthropic | ChatAnthropic | | langchain-community | 300+ integrations: loaders, stores, tools | | langchain-chroma | Chroma vector store | | langserve | Serve chains as REST APIs | | langsmith | Tracing and evaluation |

Key Concepts Recap

Python
from langchain_openai import ChatOpenAI           # Model
from langchain_core.prompts import ChatPromptTemplate  # Prompt
from langchain_core.output_parsers import StrOutputParser  # Parser

# These three compose into a chain:
chain = ChatPromptTemplate.from_template("Tell me about {topic}") | ChatOpenAI() | StrOutputParser()

# Invoke: single synchronous call
result = chain.invoke({"topic": "Python decorators"})

# Stream: token-by-token
for chunk in chain.stream({"topic": "Python decorators"}):
    print(chunk, end="")

# Batch: multiple inputs at once
results = chain.batch([{"topic": "decorators"}, {"topic": "generators"}])

What You Will Learn in This Course

The rest of this course builds on this foundation:

  • Chat models — comparing providers, parameters, and message types
  • Prompt templates — parameterized prompts for reuse and versioning
  • Output parsers — structured extraction from LLM responses
  • LCEL — the expression language for composing chains
  • RAG — retrieval-augmented generation from scratch to production
  • Agents and tools — autonomous reasoning with function calling
  • Memory — multi-turn conversation with persistent history
  • Callbacks — monitoring, tracing, and cost tracking
  • LangSmith — evaluation and debugging
  • Deployment — serving chains as production APIs

Summary

LangChain solves the problem of LLM application infrastructure. It wraps providers in a uniform interface, provides composable building blocks for prompts, chains, memory, tools, and agents, and integrates with the LangSmith ecosystem for observability. The LCEL pipe operator makes chains readable and composable. In the next lesson, you will go deep on chat models — the foundational building block of every LangChain application.

Enjoyed this article?

Explore the AI Systems learning path for more.

Found this helpful?

Share:š•

Leave a comment

Have a question, correction, or just found this helpful? Leave a note below.