सरल भाषा में
LangChain और LangGraph एक ही कंपनी (LangChain Inc.) से आते हैं और एक ही package ecosystem साझा करते हैं। लेकिन दोनों अलग समस्याएं हल करते हैं। LangChain composable building blocks का संग्रह है — model clients, prompt templates, document loaders, retrievers, और वह pipe (|) operator जो इन्हें जोड़ता है। LangGraph एक lower-level orchestration engine है, जो आपके workflow को एक स्पष्ट state machine के रूप में दर्शाता है: nodes, edges, और एक साझा state object, जिसे हर node पढ़ती और अपडेट करती है।

एक अच्छी तुलना: LangChain से बनाना ऐसा है जैसे Lego की ईंटों को एक लाइन में जोड़ना। हर ईंट अगली से जुड़ती है, output आगे बहता है, और आखिर में एक साफ़ linear आकार बनता है। LangGraph ऐसा है जैसे circuit board में तार जोड़ना — components वही रहते हैं, लेकिन अब current वापस लूप कर सकता है, branches में बंट सकता है, और circuit पूरा होने से पहले एक ही node दो बार जा सकता है।
अक्टूबर 2025 तक, दोनों frameworks v1.0 (general availability) तक पहुंच चुके हैं। एक बड़ा बदलाव: LangChain के high-level agent helpers अब अंदरूनी तौर पर LangGraph runtime पर चलते हैं। ये दोनों टूल विकल्प नहीं हैं — ये layers हैं। LangChain आपको रफ़्तार देता है; LangGraph आपको नियंत्रण देता है। ज़्यादातर गैर-सरल production agents दोनों का इस्तेमाल करते हैं।
यह क्यों मायने रखता है
इन दोनों टूल के बीच की उलझन LLM ecosystem में आने वाले developers के लिए सबसे आम रुकावटों में से एक है। कई tutorials पहले LangChain chains सिखाते हैं, फिर चुपचाप "agents" के लिए LangGraph पर स्विच कर जाते हैं — यह समझाए बिना कि स्विच क्यों हुआ या उसने कौन सी समस्या हल की। इससे दो तरह की गलतियां होती हैं: developers जो हर चीज़ के लिए LangGraph इस्तेमाल करते हैं (एक सादे RAG pipeline के लिए ओवरकिल), और developers जो जटिल agent loops को chain में ठूंसने की कोशिश करते हैं (नाज़ुक और debug करने में मुश्किल)।
सही सीमा पहचानने से असली समय बचता है। सादे LCEL chains से बना RAG pipeline लगभग 30 lines का कोड होता है और आसानी से पढ़ा जा सकता है। वही pipeline LangGraph StateGraph के रूप में शायद 80 lines का होगा — काम का रखरखाव हो सकता है, लेकिन अगर आपको loops या checkpoints की ज़रूरत ही नहीं, तो अतिरिक्त जटिलता से कुछ हासिल नहीं होता। इसके उलट, एक multi-step research agent जिसे web search को दोबारा try करना है, किसी human reviewer तक escalate करना है, और server restart के बाद फिर से शुरू होना है, उसके लिए LangGraph ज़रूरी है — उसे linear chain में बनाने की कोशिश spaghetti code पैदा करती है।
LangGraph के production इस्तेमाल का मामला
LangGraph से पहले, Uber, LinkedIn, और Klarna जैसी कंपनियों की production टीमें LangChain chains के ऊपर custom retry logic की परतें डालकर नाज़ुक stateful agents बना रही थीं। LangGraph ने उसी चीज़ को औपचारिक रूप दिया जो वे पहले से कर रही थीं: स्पष्ट state, conditional routing, और persistence। v1.0 तक पहुंचने से पहले ही यह बड़े पैमाने पर battle-tested हो चुका था — इसी वजह से LangChain टीम ने agent runtime को LangGraph में मिला दिया, न कि उल्टा।
हर टूल कैसे काम करता है
LangChain: pipe operator और Runnables
LangChain का आधुनिक core LangChain Expression Language (LCEL) है। हर component — prompt template, model call, output parser — एक Runnable interface लागू करता है, जिसमें एक समान .invoke() / .stream() / .batch() API होता है। आप | pipe operator से Runnables को जोड़ते हैं, और data बाएं से दाएं बहता है। यह chain एक directed acyclic graph (DAG) है: इसमें कोई cycle नहीं होता, कोई साझा mutable state नहीं होता, और बीच के outputs के आधार पर कोई branching नहीं होती।
from langchain_anthropic import ChatAnthropic
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
prompt = ChatPromptTemplate.from_template(
"Answer using this context:\n{context}\n\nQuestion: {question}"
)
model = ChatAnthropic(model="claude-opus-4-8")
parser = StrOutputParser()
# The chain: prompt → model → parser (left to right, no loops)
chain = prompt | model | parser
result = chain.invoke({"context": docs, "question": "What is LCEL?"})Parallel execution, streaming, retries, और fallbacks सभी Runnable interface में पहले से बने हैं — आप हर component के लिए अलग से इन्हें चुनते हैं। लेकिन topology definition के समय तय हो जाती है। model ने जो कहा, उसके आधार पर आप किसी दूसरे node पर route नहीं कर सकते।
LangGraph: state, nodes, और edges
LangGraph तीन primitives पर केंद्रित है। state एक TypedDict है, जो एक साझा scratchpad का काम करता है, जिसे हर node पढ़ सकती है और आंशिक रूप से overwrite कर सकती है। Nodes सामान्य Python functions हैं, जो मौजूदा state लेते हैं और अपडेट करने वाले fields का dict लौटाते हैं। Edges या तो बिना शर्त वाले होते हैं (add_edge("a", "b")) या शर्त वाले (add_conditional_edges) — शर्त वाला edge एक routing function चलाता है जो state की जांच करके अगला node गतिशील रूप से चुनता है, और यही loops और branching को संभव बनाता है।
from typing import TypedDict, Literal
from langgraph.graph import StateGraph, START, END
class AgentState(TypedDict):
messages: list
attempts: int
def call_llm(state: AgentState) -> dict:
reply = llm.invoke(state["messages"])
return {"messages": state["messages"] + [reply],
"attempts": state["attempts"] + 1}
def route(state: AgentState) -> Literal["call_tool", "__end__"]:
last = state["messages"][-1]
return "call_tool" if last.tool_calls else END
def call_tool(state: AgentState) -> dict:
# execute tool calls, append results
...
graph = StateGraph(AgentState)
graph.add_node("llm", call_llm)
graph.add_node("call_tool", call_tool)
graph.add_edge(START, "llm")
graph.add_conditional_edges("llm", route)
graph.add_edge("call_tool", "llm") # loop back
app = graph.compile()ध्यान दें कि आखिरी add_edge call call_tool को वापस llm से जोड़ता है। यह एक cycle है — शुद्ध LCEL chain में यह संभव नहीं, लेकिन किसी भी ऐसे agent का मूल पैटर्न यही है जो tool इस्तेमाल करता है और फिर नतीजे पर विचार करता है।
- डायरेक्टेड एसाइक्लिक ग्राफ़ (DAG)
- पाइप ऑपरेटर: A | B | C
- हर step का output अगले step को मिलता है
- कोई साझा mutable state नहीं
- Topology परिभाषा के समय तय होती है
- सबसे उपयुक्त: RAG, prompt pipelines, सादे chatbots
- चक्रीय (cyclic) state machine
- Nodes + शर्त वाले edges
- सभी nodes एक typed State object साझा करते हैं
- checkpointer द्वारा सहेजी गई स्पष्ट state
- Routing runtime पर router function तय करता है
- सबसे उपयुक्त: loops, retries, और human review वाले agents
Checkpointers: LangGraph state को कैसे सहेजता है
LangGraph की सबसे महत्वपूर्ण सुविधाओं में से एक इसका checkpointer सिस्टम है। हर node execution के बाद, LangGraph पूरी state को serialize करके किसी store (in-memory, SQLite, Postgres, या Redis) में लिख सकता है। इसका मतलब है कि agent को बीच में रोका जा सकता है — server restart, rate limit, या human review step से — और वह ठीक वहीं से फिर शुरू हो सकता है जहां वह रुका था। इसके लिए कोई custom database logic नहीं चाहिए। LangChain chains में इसका कोई built-in समकक्ष नहीं है; state अस्थायी होती है और सिर्फ़ function call stack में रहती है।
दोनों में चुनाव: एक निर्णय गाइड
किस टूल को चुनना है, इसका सबसे स्पष्ट तरीका यह है। अपने workflow के बारे में खुद से ये चार सवाल, इसी क्रम में, पूछें:
- क्या यह loop करता है? अगर agent को कभी tool कॉल करना पड़ता है, नतीजा जांचना पड़ता है, और फिर से कॉल करने का फ़ैसला करना पड़ता है — तो यह एक cycle है। LangGraph इस्तेमाल करें।
- क्या यह बीच के output पर branch करता है? अगर LLM ने जो कहा उसके आधार पर आप अलग-अलग nodes पर route करते हैं (जैसे search बनाम calculate बनाम answer), तो LangGraph इस्तेमाल करें।
- क्या इसे रोकने और फिर शुरू करने की ज़रूरत है? Human-in-the-loop review, लंबे समय तक चलने वाले background jobs, कई दिनों के workflows — इन सबके लिए LangGraph checkpointers चाहिए।
- क्या यह कई specialized agents पर चलता है? जहां एक supervisor tasks को sub-agents तक route करता है, वहां multi-agent handoffs
splittopology के आसपास बना LangGraph पैटर्न हैं।
अगर आपका जवाब चारों सवालों पर नहीं है, तो LangChain LCEL chain सही विकल्प है। इसे पढ़ना, टेस्ट करना और debug करना आसान है। LangGraph तभी अपनाएं जब workflow को सच में इसकी ज़रूरत हो।
| उपयोग का मामला | सबसे अच्छा टूल | क्यों |
|---|---|---|
| RAG pipeline (retrieve → prompt → answer) | LangChain (LCEL) | सीधा DAG, loops की ज़रूरत नहीं |
| मेमोरी वाला सादा chatbot | LangChain (LCEL) | मेमोरी को context के रूप में inject किया जा सकता है; graph की ज़रूरत नहीं |
| दस्तावेज़ सारांश pipeline | LangChain (LCEL) | Map-reduce पैटर्न pipe operator में फ़िट बैठता है |
| ReAct agent (reason → act → observe loop) | LangGraph | observe से reason तक वापस cycle की ज़रूरत है |
| कार्रवाई से पहले human review वाला agent | LangGraph | Checkpointer + interrupt से graph रुक सकता है |
| विशेषज्ञ sub-agents वाला multi-agent system | LangGraph | Supervisor → specialist routing एक graph पैटर्न है |
| लंबे समय तक चलने वाला background research job | LangGraph | server restart के बाद भी टिकाऊ state चाहिए |
| Prototype / त्वरित प्रयोग | LangChain (LCEL) | लिखना तेज़ है; ज़रूरत हो तो बाद में LangGraph पर जाएं |
LangChain और LangGraph को एक साथ इस्तेमाल करना
सबसे व्यावहारिक पैटर्न है LangGraph nodes के अंदर LangChain components इस्तेमाल करना। LangGraph control-flow का ढांचा (state, routing, persistence) देता है, जबकि LangChain पहले से बने integrations (model clients, retrievers, output parsers, tool wrappers) देता है। आपको इनमें से एक को चुनना नहीं है — आप leaf स्तर पर LangChain की व्यापकता और orchestration स्तर पर LangGraph की संरचना का इस्तेमाल कर रहे हैं।
from langchain_anthropic import ChatAnthropic
from langchain_core.prompts import ChatPromptTemplate
from langgraph.graph import StateGraph, MessagesState
from langgraph.checkpoint.memory import MemorySaver
# LangChain component: model + prompt
model = ChatAnthropic(model="claude-opus-4-8")
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant."),
("placeholder", "{messages}"),
])
chain = prompt | model # LCEL chain used as a sub-component
# LangGraph node that calls the LCEL chain
def agent_node(state: MessagesState) -> dict:
response = chain.invoke({"messages": state["messages"]})
return {"messages": [response]}
# LangGraph graph that gives the node durable state
graph = StateGraph(MessagesState)
graph.add_node("agent", agent_node)
graph.set_entry_point("agent")
graph.set_finish_point("agent")
# MemorySaver checkpoints state between turns
app = graph.compile(checkpointer=MemorySaver())LangChain टीम production के लिए यही पैटर्न सुझाती है: हर node के अंदर LCEL chains prompt formatting और model calls संभालते हैं, जबकि LangGraph पूरे agent lifecycle में routing, state और persistence को मैनेज करता है।
मौजूदा LangChain agent का migration
अगर आपके पास LangChain 0.x से बना पुराना AgentExecutor-आधारित agent है, तो सुझाया गया migration तरीका है उसे एक स्पष्ट StateGraph से बदलना। यह आमतौर पर एक ही commit का बदलाव होता है: आपके tools वही रहते हैं, आपका model वही रहता है — आप बस AgentExecutor wrapper को LangGraph graph definition से बदलते हैं। इसका फ़ायदा है deterministic routing, नेटिव persistence, और executor के internals को patch किए बिना आसान human-in-the-loop injection।
गहराई से समझें
जब आप LangGraph की बुनियादी बातों में सहज हो जाएं, तो कई उन्नत क्षमताएं देखने लायक हैं जिनका LangChain chain में कोई समकक्ष नहीं है।
Subgraphs और multi-agent architectures
LangGraph subgraphs को सपोर्ट करता है — एक compiled StateGraph खुद एक बड़े graph के अंदर node के रूप में इस्तेमाल हो सकता है। यही multi-agent systems की नींव है: एक supervisor graph काम को specialist sub-agents (जिनमें से हर एक अपना compiled graph है) तक route करता है, उनके outputs इकट्ठा करता है, और तय करता है कि आगे क्या करना है। specialist sub-agents supervisor के साथ कोई code साझा नहीं कर सकते; वे सिर्फ़ साझा state schema के ज़रिए संवाद करते हैं।
Human-in-the-loop (HITL)
LangGraph का interrupt primitive आपको किसी भी node पर graph को रोकने और नियंत्रण किसी इंसान (या approval system) को सौंपने देता है। graph state को checkpointer सहेजता है, इसलिए human के review और वैकल्पिक रूप से state में बदलाव करने के बाद agent मिनटों या दिनों बाद फिर शुरू हो सकता है। यह पैटर्न उन agentic systems में खास तौर पर आम है जिनके side effects होते हैं — जैसे emails भेजना, files लिखना, external APIs कॉल करना — जहां आप अपरिवर्तनीय कार्रवाइयों से पहले इंसान की मंज़ूरी चाहते हैं।
Streaming और आंशिक outputs
दोनों frameworks streaming को सपोर्ट करते हैं, पर अलग तरीकों से। LCEL chains model calls से token-by-token output को पारदर्शी रूप से stream करते हैं — .invoke() की जगह .stream() कॉल करें। LangGraph एक दूसरा streaming mode जोड़ता है: streaming graph events — हर node के शुरू और खत्म होने पर आपको सूचना मिलती है, जिसमें हर step पर पूरी state का snapshot भी शामिल है। यह event-level stream उन agent UIs के लिए बेहद उपयोगी है जो खाली spinner की जगह "सोचने" की प्रगति दिखाना चाहते हैं।
Observability के लिए LangSmith
LangSmith वह observability और evaluation platform है जो LangChain ecosystem को पूरा करता है। यह LangGraph run में हर node execution को trace करता है — inputs, outputs, latency, token count, और हर step पर पूरी state। आप किसी भी run को replay कर सकते हैं, automated evaluators जोड़ सकते हैं, और experiments के बीच runs की तुलना कर सकते हैं। production LangGraph agents के लिए LangSmith integration लगभग अनिवार्य है: graph की स्पष्ट state traces को उन अपारदर्शी call stacks से कहीं ज़्यादा पढ़ने लायक बनाती है जो आपको raw API client से मिलते हैं।
दोनों को कब छोड़ें
LangChain और LangGraph ही विकल्प नहीं हैं। अगर आप मुख्य रूप से Anthropic models के साथ काम कर रहे हैं, तो Claude Agent SDK (जिसे Anthropic Agents SDK भी कहते हैं) built-in tool use और multi-agent सपोर्ट के साथ, LangChain dependency के बिना, एक first-party orchestration layer देता है। अगर आप Google के models से बना रहे हैं, तो Google ADK LangGraph जैसा graph-आधारित तरीका अपनाता है, लेकिन Gemini के साथ कसकर integrated है। सही टूल इस पर निर्भर करता है कि आप किस provider के साथ जुड़े हैं और LangChain ecosystem (integrations, community templates, LangSmith) का कितना हिस्सा इस्तेमाल करना चाहते हैं।
अक्सर पूछे जाने वाले प्रश्न
क्या LangChain के बिना LangGraph इस्तेमाल कर सकते हैं?
हां। LangGraph का अपना package है (langgraph) और यह high-level LangChain abstractions पर कोई कठोर निर्भरता नहीं रखता। आप nodes ऐसे लिख सकते हैं जो सीधे Anthropic या OpenAI SDK को कॉल करें, बिना किसी LangChain model wrapper के। कई टीमें LangGraph को एक standalone orchestration layer के रूप में इस्तेमाल करती हैं और सिर्फ़ ज़रूरी LangChain integrations ही जोड़ती हैं (जैसे model client के लिए langchain-anthropic)।
क्या LangGraph सीखना LangChain से कठिन है?
LangGraph की शुरुआती सीखने की curve थोड़ी खड़ी है, क्योंकि आपको एक सादे pipe के बजाय state machines के हिसाब से सोचना पड़ता है — nodes, edges, state schema, और routing functions। ज़्यादातर developers पाते हैं कि LangGraph से एक-दो agents बनाने के बाद मानसिक मॉडल साफ़ हो जाता है, और उसकी स्पष्टता बोझ नहीं बल्कि फ़ायदा लगने लगती है। आधिकारिक LangGraph tutorials अच्छी संरचना वाले हैं और एक घंटे से कम में एक काम करने वाला agent बना देते हैं।
क्या मुझे अपना मौजूदा LangChain agent LangGraph पर migrate करना चाहिए?
अगर आपका agent AgentExecutor (v1.0 से पहले का पैटर्न) इस्तेमाल करता है, तो LangGraph पर migrate करने की ज़ोरदार सिफ़ारिश है: AgentExecutor deprecated है और उसे सिर्फ़ security fixes मिलते हैं। अगर आपका agent LangChain 1.0 के नए create_agent helper का इस्तेमाल करता है, तो वह पहले से LangGraph runtime पर चलता है — आपको स्पष्ट StateGraph पर तभी जाना होगा जब आपको custom routing, checkpointing, या human-in-the-loop interrupts चाहिए।
क्या LangGraph OpenAI के अलावा दूसरे models के साथ काम करता है?
हां — LangGraph model-agnostic है। यह उन Python functions को orchestrate करता है जो आप उसके nodes में डालते हैं। Nodes Anthropic, Google Gemini, Mistral, एक local Ollama instance, या कोई भी दूसरा model कॉल कर सकते हैं। LangChain model wrappers (langchain-anthropic, langchain-google-genai आदि) provider बदलना आसान बनाते हैं, लेकिन वे वैकल्पिक हैं।
LangGraph checkpointers और LangChain memory में क्या अंतर है?
LangChain का ConversationBufferMemory और इसी तरह की classes बातचीत का इतिहास in-process स्टोर करती हैं और उसे prompts में inject करती हैं। ये server restart के बाद बने नहीं रहते और मनमाने agent state का checkpoint नहीं ले सकते। LangGraph checkpointers हर node execution के बाद पूरी State object — सिर्फ़ messages नहीं — को किसी external store (SQLite, Postgres, Redis) में serialize करते हैं। failure, restart, या कई दिनों के रुकने के बाद agent ठीक वहीं से फिर शुरू हो सकता है जहां वह रुका था।
LangChain और LangGraph का LangSmith से क्या संबंध है?
LangSmith एक अलग product है (cloud SaaS, free tier के साथ), जो LangChain या LangGraph से बने LLM applications को trace, evaluate और monitor करने के लिए है। दोनों में से कोई भी framework चलाने के लिए यह ज़रूरी नहीं है, लेकिन यह गहराई से integrated है: जब आप LANGCHAIN_TRACING_V2=true environment variable सेट करते हैं, तो हर LCEL chain और हर LangGraph node execution अपने-आप trace हो जाती है। production agents के लिए LangSmith traces debugging और evaluation को काफ़ी आसान बना देते हैं।
