LangChain Core Modules: What Each One Actually Solves

LangChain Core Modules: What Each One Actually Solves

If you read the previous article, What Is LangChain?, you probably have this question: how do I actually use it?

The first article covered positioning and value. This one answers a more specific question: what real problems do each of LangChain's six core modules solve, and how do you use them in practice?

1. LLM and Prompt Management

What Problem It Solves

When calling the API directly, you have to craft prompts by hand. Want AI to play a customer service agent? You write it every time: you are a professional agent, be friendly, keep answers concise... switch scenarios and you start over. More prompts means more headaches.

LangChain prompt templates standardize this. You define a template with variable slots that get filled at runtime.

Code Example

from langchain_core.prompts import ChatPromptTemplate

# Define template
template = ChatPromptTemplate.from_messages([
    ("system", "You are a {persona}, respond in a {tone} tone."),
    ("human", "{question}")
])

# Fill variables
messages = template.format_messages(
    persona="technical support engineer",
    tone="patient and professional",
    question="How do I reset my password?"
)

# Call model
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o")
response = llm.invoke(messages)
print(response.content)

Unified Model Interface

Another pain point is vendor lock-in. Code written against openai.ChatCompletion.create() requires massive changes to switch models. LangChain's LLM abstraction layer lets you write code once and switch between providers with a single config change.

# Use OpenAI
llm = ChatOpenAI(model="gpt-4o")

# Switch to Anthropic — calling convention stays the same
llm = ChatAnthropic(model="claude-sonnet-4-20250514")

# Switch to a local open-source model
llm = ChatOllama(model="llama3")

Real-World Scenario

An e-commerce company used ChatPromptTemplate to manage over 200 different customer service prompt scenarios. Previously, changing service scripts required code changes and redeployment. Now operations edits templates in config files without touching code.

2. Chain

What Problem It Solves

Real-world AI tasks are rarely single-step. You want AI to read a PDF, summarize it, translate to English, generate a report — each step needs a model call plus intermediate processing. Writing this yourself turns your code into spaghetti.

Chain turns multi-step orchestration into a declarative pipeline. You define each step, LangChain connects them and runs them in sequence.

How Chain Works

The core idea: each step is a reusable component connected through inputs and outputs. The simplest Chain has three parts: prompt template (fills variables into a complete prompt), LLM (calls the model), output parser (converts model output into structured data).

But the real power is composition. You can feed one Chain's output as another Chain's input, forming chains of chains. Or run multiple sub-chains in parallel and merge their results.

Code Example

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

llm = ChatOpenAI(model="gpt-4o")

# First Chain: translate
translate_prompt = ChatPromptTemplate.from_template("Translate the following to Chinese: {text}")
translate_chain = translate_prompt | llm | StrOutputParser()

# Second Chain: refine
refine_prompt = Refine_prompt = ChatPromptTemplate.from_template("Refine this Chinese text to make it more fluent: {text}")
refine_chain = refine_prompt | llm | StrOutputParser()

# Compose: translate then refine
def full_pipeline(text):
    translated = translate_chain.invoke({"text": text})
    refined = refine_chain.invoke({"text": translated})
    return refined

result = full_pipeline("Artificial intelligence is transforming every industry.")
print(result)

Real-World Scenario

I helped a content team build an automated pipeline. Input: an English tech blog post. Pipeline: text cleaning, Chinese translation, generate 3 headline styles, generate image descriptions. Each step was an independent Chain. Input one blog post, output a complete Chinese content package.

3. LCEL Expressions

What Problem It Solves

Chain solved multi-step orchestration, but defining Chains was verbose — you had to instantiate components and wire them manually. LCEL (LangChain Expression Language) makes this trivially simple.

How LCEL Works

The core of LCEL is the pipe symbol. Use | to connect components, data flows left to right. For example, prompt | model | output_parser defines a complete AI pipeline in one line: format the prompt, call the model, parse the output.

This simple syntax hides powerful machinery. Between each |, LangChain automatically handles async execution, error retries, and streaming. You don't worry about the details — the framework handles them.

Code Example

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

prompt = ChatPromptTemplate.from_template("Summarize {topic} in one sentence")
model = ChatOpenAI(model="gpt-4o")
parser = StrOutputParser()

# Define a complete AI processing chain with pipe symbols
chain = prompt | model | parser

# Invoke
result = chain.invoke({"topic": "quantum computing"})
print(result)

# Batch invoke
results = chain.batch([
    {"topic": "artificial intelligence"},
    {"topic": "blockchain"},
    {"topic": "quantum computing"}
])

# Streaming output
for chunk in chain.stream({"topic": "machine learning"}):
    print(chunk, end="", flush=True)

Why Better Than Traditional Code

Traditional code is imperative: create template, call model, parse result, wrap each step in try-catch. LCEL is declarative: you describe the flow you want, the framework executes it. Fewer bugs, shorter code, easier debugging.

I can write a Chain in about 5 lines of LCEL. The same functionality takes at least 30 lines the traditional way.

4. Retrieval-Augmented Generation (RAG)

What Problem It Solves

Large language models have a knowledge cutoff. Ask them about events after May 2025 and they will probably make things up. More importantly, your internal company docs, product manuals, technical specs — these are not in the model's training data.

RAG solves this. It retrieves relevant document fragments from a knowledge base, stuffs them into the prompt, then lets the model generate an answer based on that information. Think of it as giving the model an external hard drive.

How RAG Works

The RAG pipeline goes like this: document loading (read PDFs, Word docs, web pages) → document splitting (cut long docs into chunks) → vectorization (convert text to numeric vectors in a vector database) → retrieval (find the most relevant chunks for the user's question) → generation (feed retrieval results into the prompt for the model to answer).

Each step matters. Split too coarsely and retrieval is imprecise. Split too finely and context is fragmented. Choose the wrong vector database and retrieval is slow and poor quality.

Code Example

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

# 1. Load documents
loader = PyPDFLoader("company_docs.pdf")
docs = loader.load()

# 2. Split
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
chunks = splitter.split_documents(docs)

# 3. Vectorize and store in vector database
vectorstore = Chroma.from_documents(chunks, OpenAIEmbeddings())
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})

# 4. Build RAG Chain
prompt = ChatPromptTemplate.from_template(
    "Answer the user question based on the following context. If the context does not contain relevant information, say so.\n\nContext: {context}\n\nQuestion: {question}"
)

def format_docs(docs):
    return "\n\n".join(doc.page_content for doc in docs)

rag_chain = (
    {"context": retriever | format_docs, "question": RunnablePassthrough()}
    | prompt
    | ChatOpenAI(model="gpt-4o")
    | StrOutputParser()
)

# 5. Use
answer = rag_chain.invoke("What is the company's leave policy?")
print(answer)

Real-World Scenario

A common use case is internal knowledge base QA. A company with 500 technical documents — previously, new hires had to search docs or ask senior colleagues. With RAG, they get answers by simply asking a question.

5. Agent

What Problem It Solves

Chain is a fixed workflow — you define every step in advance. But many real tasks cannot be planned ahead. You ask the AI to check tomorrow's weather and recommend outfit choices. That needs two steps, but step two depends on step one, and if the weather API fails you need to decide what to do.

Agent solves dynamic decision-making. It uses a large language model as the decision engine, equipped with a toolkit — it decides which tool to use, in what order, and what to do next based on intermediate results.

How Agent Works

The core loop: receive task → model thinks about what to do → calls a tool → gets result → model thinks about next step → calls another tool or returns the final answer. This loop repeats until the task is done.

The toolkit can include: search engines, database queries, API calls, code execution, file read/write. The key: tools are optional — the Agent decides whether to use them.

Code Example

from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
from langchain.agents import AgentExecutor, create_react_agent
from langchain_core.prompts import ChatPromptTemplate

# Define tools
@tool
def get_weather(city: str) -> str:
    """Get weather information for a specified city"""
    weather_data = {
        "Beijing": "Sunny, 25°C",
        "Shanghai": "Cloudy, 28°C",
        "Shenzhen": "Overcast, 26°C"
    }
    return weather_data.get(city, "Unknown city")

@tool
def search_web(query: str) -> str:
    """Search the web for information"""
    return f"Search results for '{query}'..."

# Create Agent
llm = ChatOpenAI(model="gpt-4o")
tools = [get_weather, search_web]

prompt = ChatPromptTemplate.from_template(
    "You are an assistant that can use the following tools: {tools}\n\nUser question: {input}\n\nThink and use tools to answer."
)

agent = create_react_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

# Use
result = agent_executor.invoke({"input": "What's the weather in Beijing tomorrow? What should I wear?"})
print(result["output"])

Real-World Scenario

A typical scenario: you ask the AI to check what new features competitors recently released. The Agent uses a search engine to find articles, a web scraper to read details, then compiles an analysis report. No flow control code needed from you.

I built a practical example: an automated ops Agent that can troubleshoot alerts — first checking logs, then monitoring data, then suggesting fixes. If a service restart is needed, the Agent asks for confirmation first.

6. Memory Module

What Problem It Solves

Large language models have no memory — every conversation starts from scratch. You tell it your name, next round it forgets. This is fatal for chatbot applications.

LangChain's memory module lets AI remember previous conversations, supporting short-term memory (within the current session) and long-term memory (persisted across sessions).

How Memory Works

LangChain offers multiple memory types:

  • ConversationBufferMemory: Stores full conversation history — best for customer service
  • ConversationSummaryMemory: Summarizes history to save tokens — best for long conversations
  • VectorStoreRetrieverMemory: Uses vector store for semantic retrieval — best for knowledge base scenarios

Code Example

from langchain.memory import ConversationBufferMemory
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain.chains import LLMChain

# Create memory module
memory = ConversationBufferMemory(memory_key="chat_history")

# Create Chain with memory
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a friendly assistant who remembers user preferences and conversation history."),
    ("human", "{input}")
])

llm = ChatOpenAI(model="gpt-4o")
chain = LLMChain(llm=llm, prompt=prompt, memory=memory)

# First conversation
response1 = chain.invoke({"input": "My name is Alice, I prefer concise answers"})
print(response1["text"])

# Second conversation — AI remembers the user's name and preference
response2 = chain.invoke({"input": "Help me write some Python code"})
print(response2["text"])

# View memory contents
print(memory.buffer)

Real-World Scenario

An AI tutoring system: the AI needs to remember each student's learning progress and weak areas. With VectorStoreMemory, store learning records in a vector database and retrieve relevant history each session. The AI can then teach to the individual.

Summary

Six modules, six different problems:

  • LLM and Prompt Management solves how to call models
  • Chain solves how to orchestrate steps
  • LCEL solves how to define flows elegantly
  • RAG solves how to supplement the model's knowledge
  • Agent solves how to let the model make autonomous decisions
  • Memory solves how to maintain context across conversations

These modules are not used in isolation. A real AI application often combines multiple modules. A customer service bot uses prompt templates for persona, RAG for knowledge base QA, Chain for conversation flow, memory for user preferences, and Agent for complex requests that need external systems.

That is the value of LangChain: a proven set of components so you do not have to build everything from scratch.


Series: