Skip to content
LangChain: Quickly Build Apps with OpenAI API

Unleash the Potential of LangChain in Modern AI Application Development

Updated on

LangChain has evolved dramatically since 2023. What started as a simple wrapper around LLMs is now a full AI application framework with support for:

  • LCEL (LangChain Expression Language)
  • Runnables
  • LangGraph (stateful, reliable agent workflows)
  • OpenAI function calling
  • Tool calling
  • Advanced memory systems
  • Modern document pipelines for RAG

This updated guide gives you a 2025-ready understanding of LangChain and how to build real LLM applications with the latest OpenAI API.

📚

Basic Concepts for LangChain

🌟 LangChain Today: A Modern AI App Framework

Modern LLM apps need more than raw text generation:

  • tools
  • memory
  • document retrieval
  • state management
  • multi-step workflows
  • error handling
  • determinism

LangChain provides these primitives through Runnables, LCEL, and LangGraph, allowing developers to build reliable agent and workflow systems.


🔗 Sequential Workflows with LCEL (Updated)

LCEL enables you to compose LLM pipelines like functions:

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
 
model = ChatOpenAI(model="gpt-4.1")
 
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant."),
    ("user", "{question}")
])
 
chain = prompt | model
chain.invoke({"question": "Explain LangChain LCEL in one paragraph."})

This replaces older “SequentialChain” APIs.


Getting Started with LangChain

📦 Installation

pip install langchain langchain-openai langgraph faiss-cpu

If you use LangGraph:

pip install langgraph

💬 LLMs and Prompt Templates (Modern API)

LangChain now uses ChatModel interfaces:

from langchain_openai import ChatOpenAI
 
llm = ChatOpenAI(model="gpt-4.1", temperature=0)

Prompt Templates remain powerful:

from langchain_core.prompts import ChatPromptTemplate
 
prompt = ChatPromptTemplate.from_template(
    "Summarize the following text:\n\n{text}"
)

⛓️ Chains with LCEL (instead of old “Chains API”)

You build chains with LCEL:

chain = prompt | llm

⚙️ Tools & Agents (2025 Best Practices)

The old initialize_agent, “ReAct agent”, and MiniChain style are deprecated.

The recommended approach is:

Use LangGraph for agent workflows

LangGraph gives:

  • state management
  • retry policies
  • deterministic edges
  • tool calling
  • persistence

Example agent node:

from langchain_openai import ChatOpenAI
 
llm = ChatOpenAI(
    model="gpt-4.1",
    temperature=0,
    streaming=False
)

Tool calling example:

from langchain_core.tools import tool
 
@tool
def search_wiki(query: str):
    """Search Wikipedia topics."""
    import wikipedia
    return wikipedia.summary(query)
 
tools = [search_wiki]

Connect with LLM:

result = llm.invoke({
    "messages": [
        {"role": "user", "content": "Find me information about LangChain."}
    ],
    "tools": tools
})

🧠 Memory

LangChain memory now supports:

  • BufferMemory
  • ConversationSummaryMemory
  • SQL / Redis backed memory
  • Graph-state memory (LangGraph)

Example:

from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
 
memory.save_context(
    {"input": "Hello"},
    {"output": "Hi, how can I help?"}
)
 
memory.load_memory_variables({})

📄 Loading Data, Text Splitters & Retrieval (Modern RAG)

Updated example using FAISS + OpenAI embeddings:

from langchain_community.document_loaders import TextLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
 
loader = TextLoader("./state_of_the_union.txt")
docs = loader.load()
 
splitter = RecursiveCharacterTextSplitter(chunk_size=1000)
chunks = splitter.split_documents(docs)
 
emb = OpenAIEmbeddings()
db = FAISS.from_documents(chunks, emb)
 
query = "What policies did the president highlight?"
results = db.similarity_search(query)

🚀 End-to-End Example

Below is a complete, runnable LangChain pipeline using LCEL, updated OpenAI API, and retrieval.

from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_core.prompts import ChatPromptTemplate
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import TextLoader
from langchain_community.vectorstores import FAISS
 
# 1. Load LLM
llm = ChatOpenAI(model="gpt-4.1", temperature=0)
 
# 2. Load + split documents
loader = TextLoader("./state_of_the_union.txt")
docs = loader.load()
splitter = RecursiveCharacterTextSplitter(chunk_size=800)
chunks = splitter.split_documents(docs)
 
# 3. Build vectorstore
db = FAISS.from_documents(chunks, OpenAIEmbeddings())
 
# 4. Retrieval chain
prompt = ChatPromptTemplate.from_messages([
    ("system", "Answer based on the provided context."),
    ("user", "{question}\n\nContext: {context}")
])
 
def retriever(q):
    results = db.similarity_search(q, k=3)
    return "\n".join([r.page_content for r in results])
 
chain = (
    {
        "question": lambda x: x,
        "context": lambda x: retriever(x)
    }
    | prompt
    | llm
)
 
# 5. Run end-to-end
response = chain.invoke("What economic topics were mentioned?")
print(response.content)

Conclusion

LangChain in 2025 is no longer just a toolkit — it is a full AI application framework for:

  • agents
  • workflows
  • retrieval systems
  • tool integrations
  • memory-rich conversational apps

With LCEL, Runnables, and LangGraph, you can now build production-grade AI apps with clean, maintainable, scalable architecture.

Use this guide as your updated foundation for creating powerful, competitive AI applications.