LangChain Core Components
Moving from simple API calls to building sophisticated, production-ready Large Language Model (LLM) applications requires more than just sending a prompt to a model. It requires orchestration—the ability to manage state, integrate external data, and connect models to real-world tools.
LangChain is the premier framework designed to solve this orchestration challenge. By providing a modular set of components, it allows developers to assemble reusable, observable, and scalable workflows. Whether you are building a Retrieval-Augmented Generation (RAG) system, a conversational chatbot, or an autonomous agent, understanding its core components is essential for designing robust LLM architectures.
Models serve as the primary inference engine within the LangChain ecosystem. Rather than treating all models as a single type, LangChain categorizes them based on their input and output formats to suit different use cases:
- LLMs (Large Language Models): These are traditional text-in, text-out models. They take a simple string as input and return a string, making them ideal for basic text completion tasks.
- Chat Models: Designed specifically for conversational interfaces, these models take a list of structured messages (e.g.,
SystemMessage,HumanMessage,AIMessage) as input and return a message object. This allows for much more nuanced control over dialogue dynamics. - Embedding Models: These models transform text into high-dimensional vectors. This is a critical step for semantic search, clustering, and any task involving similarity calculations.
from langchain_openai import ChatOpenAI
# Initializing a Chat Model
model = ChatOpenAI(model="gpt-4o-mini", temperature=0)
resp = model.invoke("Explain LangChain in one sentence.")
print(resp.content)
Prompts
Prompting is the art of guiding a model toward a specific behavior. The Prompt component in LangChain moves beyond hard-coded strings by introducing dynamic templating. This allows developers to inject user input, system instructions, and historical context into a structured format.
Key prompt types include:
PromptTemplate: A standard template for single-string inputs.ChatPromptTemplate: A sophisticated template used to define roles (System, Human, AI) in a conversation.FewShotPromptTemplate: A template that includes a few examples of desired input-output pairs to "teach" the model via in-context learning.MessagesPlaceholder: A specialized component used to dynamically inject a list of messages, such as chat history, into a prompt.
from langchain_core.prompts import ChatPromptTemplate
# Creating a structured chat prompt
prompt = ChatPromptTemplate.from_messages([
("system", "You are a concise technical assistant."),
("human", "Explain the core concept of {concept}.")
])
# The variable {concept} is filled during invocation
formatted_prompt = prompt.invoke({"concept": "Vector Retrieval"})
Output Parsers
While LLMs are excellent at generating natural language, software applications usually require structured data (like JSON or specific object types) to function. Output Parsers bridge this gap by transforming the model's raw text output into a format your code can actually use.
Common parsers include:
StrOutputParser: The simplest parser, used to extract the raw string content from a message.JsonOutputParser: Specifically designed to parse the model's output into a JSON object.PydanticOutputParser: The most robust option, which uses Pydantic models to validate that the output conforms to a specific schema, ensuring type safety and data integrity.
from langchain_core.output_parsers import StrOutputParser
# Combining components into a simple chain
parser = StrOutputParser()
chain = prompt | model | parser
print(chain.invoke({"concept": "LCEL"}))
Chains and LCEL
A "Chain" is the fundamental unit of workflow in LangChain. It allows you to link multiple components together—such as a prompt, a model, and an output parser—into a single, executable sequence.
To make this process seamless, LangChain introduced LCEL (LangChain Expression Language). LCEL uses a declarative syntax with the pipe operator (|), similar to Unix pipes. This approach makes the code highly readable and provides built-in support for advanced features like:
- Streaming: Sending parts of the response to the user as they are generated.
- Batching: Running multiple inputs through the chain in parallel.
- Async Support: Handling multiple requests efficiently without blocking.
# A complete chain using LCEL
chain = prompt | model | parser
result = chain.invoke({"concept": "RAG"})
Memory
LLMs are inherently stateless; they do not "remember" previous interactions once a request is complete. To build a conversational agent, you must implement Memory.
Memory components allow the application to store and retrieve past interactions, providing the model with the necessary context to maintain a coherent conversation. While older versions used ConversationBufferMemory, modern LangChain implementations prefer using RunnableWithMessageHistory in conjunction with ChatMessageHistory.
The professional way to implement memory involves:
- Assigning a unique
session_idto each user conversation. - Using a
MessagesPlaceholderin the prompt to hold the history. - Wrapping the chain with
RunnableWithMessageHistoryto automatically manage the reading and writing of history to a persistent store like Redis or a database.
Retrieval
Retrieval is the backbone of Retrieval-Augmented Generation (RAG). It allows an LLM to access private or up-to-date information that was not part of its original training data. The retrieval pipeline typically consists of several stages:
- Document Loaders: Ingesting data from various sources (PDFs, HTML, databases, etc.).
- Text Splitters: Breaking large documents into smaller, manageable "chunks" to fit within the model's context window.
- Embeddings: Converting those chunks into numerical vectors.
- Vector Stores: Databases (like Chroma or Pinecone) designed to store and perform similarity searches on these vectors.
- Retrieters: The interface that takes a user query, searches the vector store, and returns the most relevant document chunks.
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_chroma import Chroma
# A typical RAG retrieval setup
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
chunks = splitter.split_documents(docs)
vectorstore = Chroma.from_documents(chunks, OpenAIEmbeddings())
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
Agents and Tools
While chains follow a predefined sequence of steps, Agents use the LLM as a reasoning engine to decide which actions to take and in what order.
An agent is given a set of Tools—functions that can perform specific tasks like searching the web, calculating math, or querying a database. The agent enters a reasoning loop: it observes the current state, thinks about what to do, selects a tool, executes it, observes the result, and repeats this process until the task is complete.
from langchain_core.tools import tool
@tool
def get_word_length(word: str) -> int:
"""Returns the length of a given word."""
return len(word)
# This tool can now be provided to an agent to solve length-related queries.
Callbacks
In a production environment, you cannot treat your LLM application as a "black box." You need to know how much it costs, how long it takes, and where it fails. Callbacks provide this observability.
Callbacks allow you to hook into the various stages of the execution lifecycle. You can use them to:
- Log inputs and outputs for debugging.
- Monitor token usage and latency.
- Stream intermediate steps to a user interface.
- Trigger alerts when errors occur.
Summary
Mastering LangChain is about understanding how these specialized components interact. Models provide the intelligence, Prompts provide the direction, Parsers provide the structure, Chains provide the flow, Memory provides the context, Retrieval provides the knowledge, Agents provide the agency, and Callbacks provide the visibility. By combining these building blocks, you can transform a simple language model into a powerful, autonomous, and context-aware application.