Intro
LangChain is a framework for composing LLM applications: prompts, models, retrievers, tools, memory, and agents in a single chain abstraction.
LangChain — what it is
EXAMPLE
# ===== The values =====
# - Provider-agnostic LLM interface (OpenAI, Anthropic, Google, local)
# - Prompt + model + parser as composable building blocks
# - Retrieval (RAG), tools / agents, memory, evaluation
# - Big ecosystem: LangSmith for tracing, LangServe for deploys
# ===== Hello, chain =====
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
llm = ChatOpenAI(model='gpt-4o-mini', temperature=0)
prompt = ChatPromptTemplate.from_messages([
('system', 'You are a concise editor.'),
('human', 'Rewrite: {text}'),
])
chain = prompt | llm | StrOutputParser()
print(chain.invoke({'text': 'their are 5 cars'}))
# ===== Retrieval-Augmented Generation (RAG) =====
from langchain_community.vectorstores import Chroma
from langchain_openai import OpenAIEmbeddings
emb = OpenAIEmbeddings(model='text-embedding-3-small')
store = Chroma.from_texts(['cats meow', 'dogs bark'], emb)
retriever = store.as_retriever()
docs = retriever.invoke('what do dogs do?')
# ===== Tools / agents =====
from langchain_core.tools import tool
@tool
def calculator(expr: str) -> str:
"""Evaluate a math expression."""
return str(eval(expr)) # demo only
# Bind tools to model:
llm_with_tools = llm.bind_tools([calculator])
res = llm_with_tools.invoke('what is 12 * 7?')
# ===== When LangChain wins =====
# - You need to swap providers easily
# - Chains have many stages (prompt + model + parser + retriever + ...)
# - You want LangSmith for tracing + eval
# - Standard patterns (RAG, agents, summarisation)
# ===== When LangChain hurts =====
# - Single-call apps where direct SDK is simpler
# - You need maximum control + minimum abstractions
# - Rapidly changing APIs cause version churn
# ===== Patterns to internalise =====
# - Compose with the pipe operator (prompt | llm | parser)
# - Use structured output for non-prose responses
# - Cache during dev to save money
# - Trace with LangSmith from day one
# ===== Pitfalls =====
# - Wrapping too much in custom subclasses -> upgrade pain
# - No timeouts -> stuck requests stall pipelines
# - Memory and tool calls without cost tracking
# - Treating chains as static; expect to rewrite as the use case evolves
Why it matters
LangChain is the framework version of "build LLM apps". Compose prompts + models + parsers + retrievers + tools through a clean pipe API. Reach for it when chains get long or providers need to be swappable. For small one-shot calls, the SDK is fine.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Example
Example
# LangChain: a framework for composing LLM apps. # Chains, agents, RAG, tools, memory — all behind one interface.Try it Yourself »
Discussion
Loading…