Working through this module?0 of 17 done
Module 02 · Page 3 of 20 · 5 sections

LangChain Architecture and Core Components

How LangChain is structured as a bridge between LLMs, data and tools, what each core component does and a simple example that combines them.

2.1

Architecture Overview

  • LangChain provides a modular architecture to build LLM applications.
  • It acts as a bridge between LLMs, external data sources, tools and application logic.
  • It provides standard interfaces and abstractions to work with different LLM providers and components.
  • We can mix and match components as per our use case (RAG, agents, etc.).
2.2

How a Request Flows

  1. 1User provides a query to the application.
  2. 2LangChain processes the query using chains, agents or retrievers.
  3. 3It interacts with LLMs, external data sources, tools and vector stores.
  4. 4The LLM generates a response using the provided context and tools.
  5. 5LangChain returns the final response to the user.
2.3

Architecture Diagram

User Query
LangChain ApplicationChains / Agents
LLMsOpenAI, Claude, etc.
Vector DatabasesFAISS, Chroma, etc.
External APIsSerpAPI, DB, etc.
ToolsCalculators, search, etc.

Inside the application sit the LangChain components: prompts, chains, agents, memory, document loaders, vector stores and tools.

2.4

Core Components and Their Purpose

ComponentPurpose
ModelsInterface to different LLMs (OpenAI, Anthropic, etc.).
PromptsManage and create prompts, prompt templates.
ChainsCombine multiple steps to create a workflow.
AgentsUse tools to take actions and solve complex tasks.
MemoryMaintain conversation history and context.
Document LoadersLoad data from various sources (PDF, web, DB, etc.).
Text SplittersSplit large documents into smaller chunks.
Vector StoresStore and search embeddings for similarity search.
RetrieversFetch relevant documents from a vector store.
ToolsIntegrate external tools and APIs.
CallbacksTrack and monitor execution (logs, token usage, etc.).
2.5

Simple Example: Using Core Components

python
from langchain_openai import ChatOpenAI
from langchain_core.prompts import PromptTemplate
from langchain.chains import LLMChain

# 1. Define LLM
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0.7)

# 2. Define prompt template
template = PromptTemplate(
    input_variables=["topic"],
    template="Explain {topic} in simple words."
)

# 3. Create chain
chain = LLMChain(llm=llm, prompt=template)

# 4. Run chain
response = chain.invoke({"topic": "LangChain"})
print(response)
  • We create an LLM instance (OpenAI).
  • Define a prompt template with a variable.
  • Create an LLMChain with the model and prompt.
  • Invoke the chain with input to get the response.
  • We can also use memory, tools, retrievers, etc., for advanced applications.