Langgraph & Langchain
Overview (Dec 2025 snapshot)
LangGraph and LangChain are two complementary Python ecosystems for building LLM apps. LangChain focuses on primitives (prompts, tools, retrievers) and orchestration utilities; LangGraph extends this with graph-based state machines for reliable multi-step agents.
When to use which
- Use LangChain for: quick RAG pipelines, tool calling, prompt templating, tracing via LangSmith, easy loaders/vectorstores.
- Use LangGraph for: multi-turn agents, branch/merge flows, retries/guards, long-running tools, and stateful workflows with explicit control.
Key concepts (LangGraph)
- State: typed dict (TypedDict/attrs/pydantic) defining what each node reads/writes.
- Nodes: callables that mutate state; can invoke tools or LLMs.
- Edges: control flow between nodes; conditional edges allow branching.
- Persistence: checkpointer for resumable agents (e.g., Redis, Postgres, file).
- Interrupts: yield control back to the caller (human-in-the-loop).
Install (latest)
pip install "langgraph>=0.2.0" "langchain>=0.3.0" "langchain-openai" "langchain-community"
Minimal LangGraph agent
from typing import TypedDict
from langgraph.graph import StateGraph, END
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4.1-mini")
class State(TypedDict):
question: str
answer: str
def call_llm(state: State):
resp = llm.invoke(state["question"])
return {"answer": resp.content}
graph = StateGraph(State)
graph.add_node("llm", call_llm)
graph.set_entry_point("llm")
graph.add_edge("llm", END)
app = graph.compile()
result = app.invoke({"question": "Explain RAG in one sentence"})
print(result["answer"])
LangChain RAG (concise)
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import FAISS
from langchain.chains import RetrievalQA
docs = ["LangGraph adds graph-based control to LangChain primitives."]
splitter = RecursiveCharacterTextSplitter(chunk_size=300, chunk_overlap=50)
emb = OpenAIEmbeddings(model="text-embedding-3-large")
db = FAISS.from_texts(splitter.split_text("\n".join(docs)), emb)
qa = RetrievalQA.from_chain_type(
llm=ChatOpenAI(model="gpt-4.1-mini"),
retriever=db.as_retriever(),
)
print(qa.run("What is LangGraph?"))
Patterns to prefer (2025)
- Graph over chains for multi-step agents to avoid hidden control flow.
- Typed state to prevent key typos and make nodes composable.
- Checkpointers for resumability (Redis/Postgres/file).
- Tool fallbacks: wrap tool nodes with retry/backoff and guardrails.
- Observability: enable LangSmith tracing (
LANGCHAIN_TRACING_V2=true).
Interop tips
- You can run LangChain tools inside LangGraph nodes; share embeddings/vectorstores.
- For multi-LLM strategies, route via a dispatcher node (fast model first, fallback to premium).
- Keep contexts short; use RAG + summarization instead of huge prompts.
References
- LangGraph docs: https://langchain-ai.github.io/langgraph/
- LangChain docs: https://python.langchain.com
- LangSmith tracing: https://docs.smith.langchain.com