← Back to tutorials

LangChain vs LangGraph Practical Guide: How to Choose Your Agent Framework, Explained Clearly

Starting from real project needs, telling you which framework to use

LangChain was almost synonymous with AI application development in 2023. But by 2026, many have migrated from LangChain to LangGraph—because LangGraph is better suited for building truly production-grade Agents.

How do you choose between these two frameworks?

1. Core Positioning Differences

LangChain: A toolbox for AI application development

  • Provides a large number of ready-made "chains" and components
  • Quickly combine LLM + tools + data sources
  • Suitable for rapid MVPs and simple RAG applications
  • LangGraph: A stateful Agent workflow framework

  • Models Agent behavior as a directed graph (nodes + edges)
  • Supports loops, branching, and parallel execution
  • Suitable for complex multi-step Agents requiring precise flow control
  • Simply put: LangChain helps you start quickly, LangGraph helps you get to production.

    2. Code Comparison: Same Task, Two Implementations

    Task: Automatically choose between search or calculator tools based on user question

    #### LangChain Implementation (Concise but less control)

    python
    from langchain.agents import create_react_agent, AgentExecutor
    from langchain_openai import ChatOpenAI
    from langchain.tools import tool

    @tool def search(query: str) -> str: """Search the internet for the latest information""" return web_search(query)

    @tool def calculator(expression: str) -> str: """Calculate a mathematical expression""" return eval(expression)

    llm = ChatOpenAI(model="gpt-4o") agent = create_react_agent(llm, tools=[search, calculator], prompt=...) executor = AgentExecutor(agent=agent, tools=[search, calculator])

    result = executor.invoke({"input": "What is Apple's latest stock price, and what is it times 100?"})

    #### LangGraph Implementation (Complex but controllable)

    python
    from langgraph.graph import StateGraph, END
    from typing import TypedDict, List

    class AgentState(TypedDict): messages: List[dict] tool_calls: List[dict] final_answer: str

    def call_llm(state: AgentState) -> AgentState: """Call LLM to decide next step""" response = llm.invoke(state["messages"]) return {"messages": state["messages"] + [response]}

    def execute_tools(state: AgentState) -> AgentState: """Execute tool calls""" results = [] for tool_call in state["messages"][-1].tool_calls: result = tools[tool_call["name"]].invoke(tool_call["args"]) results.append({"role": "tool", "content": result}) return {"messages": state["messages"] + results}

    def should_continue(state: AgentState) -> str: """Decide whether to continue calling tools or end""" last_message = state["messages"][-1] if last_message.tool_calls: return "tools" return END

    Build the graph

    graph = StateGraph(AgentState) graph.add_node("llm", call_llm) graph.add_node("tools", execute_tools) graph.add_edge("tools", "llm") graph.add_conditional_edges("llm", should_continue) graph.set_entry_point("llm")

    app = graph.compile() result = app.invoke({"messages": [{"role": "user", "content": "..."}]})

    LangGraph has more code, but you have complete control over every step.

    3. When to Choose Which

    Choose LangChain when

  • Building RAG applications quickly (document Q&A)
  • Need many ready-made integrations (databases, vector stores, etc.)
  • Simple tool calls (1-2 tools)
  • Learning and prototyping phase
  • Choose LangGraph when

  • Agent needs loops (retry on tool failure)
  • Need to execute multiple sub-tasks in parallel
  • Require precise error handling and rollback
  • Multi-agent collaboration
  • Production environment requiring observability
  • Recommended combination in 2026

  • RAG / Knowledge Base → LangChain + Chroma/Pinecone
  • Simple Agent → LangChain LCEL
  • Complex Agent → LangGraph
  • Multi-agent system → LangGraph multi-graph
  • 4. LangGraph Core Concepts

    State

    python
    class MyState(TypedDict):
        messages: List[BaseMessage]  # conversation history
        next_step: str               # what to do next
        attempts: int                # retry count
        result: Optional[str]        # final result
    

    State is the most core concept in LangGraph—the entire Agent execution revolves around state changes.

    Conditional Edges

    python
    def route(state) -> str:
        if state["attempts"] > 3:
            return "give_up"
        elif state["has_tool_calls"]:
            return "execute_tools"
        else:
            return "end"

    graph.add_conditional_edges("llm_node", route)

    Conditional edges allow you to implement complex branching logic, clearer than simple if-else.

    5. Persistence and Resume from Checkpoints

    LangGraph supports saving intermediate state to a database, allowing the Agent to be interrupted and resumed:

    python
    from langgraph.checkpoint.sqlite import SqliteSaver

    Save checkpoints to SQLite

    memory = SqliteSaver.from_conn_string("checkpoints.db") app = graph.compile(checkpointer=memory)

    First run

    thread = {"configurable": {"thread_id": "task_001"}} result = app.invoke(input_data, config=thread)

    Continue later (from where it left off)

    result = app.invoke(None, config=thread)

    This is crucial for long-running Agents (e.g., automated research, batch processing).


    Further Reading

  • AI Agent Prompt Engineering in Practice
  • AutoGen Multi-Agent Tutorial
  • AI Agent Workflow Automation
  • Also available in 中文.