Skip to main content

Quickstart

This guide will get you up and running with LangGraph in under 5 minutes. You’ll create a simple stateful workflow that demonstrates the core concepts of graphs, nodes, edges, and state.

Prerequisites

Before you begin, make sure you have:
  • Python 3.10 or higher installed
  • Basic familiarity with Python and type hints
If you haven’t installed LangGraph yet, check out the Installation Guide.

Your First LangGraph Application

Let’s build a simple workflow that processes text through multiple nodes.
1
Define Your State
2
First, define the state schema using a TypedDict. The state represents the data that flows through your graph:
3
from typing_extensions import TypedDict

class State(TypedDict):
    text: str
4
Create Node Functions
5
Nodes are the building blocks of your graph. Each node is a function that receives the current state and returns an update:
6
def node_a(state: State) -> dict:
    return {"text": state["text"] + "a"}

def node_b(state: State) -> dict:
    return {"text": state["text"] + "b"}
7
Node functions can return a partial state update. LangGraph automatically merges the update with the existing state.
8
Build the Graph
9
Now create a StateGraph and add your nodes and edges:
10
from langgraph.graph import START, StateGraph

graph = StateGraph(State)
graph.add_node("node_a", node_a)
graph.add_node("node_b", node_b)
graph.add_edge(START, "node_a")
graph.add_edge("node_a", "node_b")
11
Here’s what’s happening:
12
  • StateGraph(State) creates a graph with your state schema
  • add_node() registers functions as nodes in the graph
  • add_edge() defines the execution flow between nodes
  • START is a special constant representing the entry point
  • 13
    Compile and Run
    14
    Compile the graph and execute it with initial state:
    15
    app = graph.compile()
    
    result = app.invoke({"text": ""})
    print(result)
    

    Complete Example

    Here’s the full code in one place:
    from langgraph.graph import START, StateGraph
    from typing_extensions import TypedDict
    
    
    class State(TypedDict):
        text: str
    
    
    def node_a(state: State) -> dict:
        return {"text": state["text"] + "a"}
    
    
    def node_b(state: State) -> dict:
        return {"text": state["text"] + "b"}
    
    
    graph = StateGraph(State)
    graph.add_node("node_a", node_a)
    graph.add_node("node_b", node_b)
    graph.add_edge(START, "node_a")
    graph.add_edge("node_a", "node_b")
    
    app = graph.compile()
    
    print(app.invoke({"text": ""}))
    # Output: {'text': 'ab'}
    

    Expected Output

    When you run this code, you’ll see:
    {'text': 'ab'}
    
    The workflow:
    1. Starts with an empty string ""
    2. node_a appends "a""a"
    3. node_b appends "b""ab"

    Understanding the Flow

    1
    State Initialization
    2
    The graph begins with the initial state you provide: {"text": ""}
    3
    Node Execution
    4
    Each node receives the current state and returns updates. LangGraph automatically merges these updates.
    5
    Sequential Processing
    6
    Edges determine execution order. In this example, node_a always runs before node_b.
    7
    Final State
    8
    The invoke() method returns the final state after all nodes complete.

    Building a Conditional Workflow

    Let’s extend the example with conditional routing:
    from langgraph.graph import START, END, StateGraph
    from typing_extensions import TypedDict
    
    
    class State(TypedDict):
        text: str
        count: int
    
    
    def increment(state: State) -> dict:
        return {"count": state["count"] + 1}
    
    
    def should_continue(state: State) -> str:
        if state["count"] < 3:
            return "increment"
        return END
    
    
    graph = StateGraph(State)
    graph.add_node("increment", increment)
    graph.add_edge(START, "increment")
    graph.add_conditional_edges("increment", should_continue)
    
    app = graph.compile()
    
    result = app.invoke({"text": "hello", "count": 0})
    print(result)
    # Output: {'text': 'hello', 'count': 3}
    
    This creates a loop that increments count until it reaches 3.

    Next Steps

    Now that you’ve built your first LangGraph application, explore more advanced features:

    Core Concepts

    Learn about state management, reducers, and graph patterns

    Checkpointing

    Add persistence and enable time-travel debugging

    Human-in-the-Loop

    Pause execution for human approval or input

    Examples

    Explore real-world LangGraph applications
    Want to build AI agents quickly? Check out create_react_agent for a high-level API built on LangGraph.

    Troubleshooting

    Import Errors

    If you see ModuleNotFoundError, ensure LangGraph is installed:
    pip install langgraph
    

    Type Errors

    Make sure you’re using typing_extensions.TypedDict (not typing.TypedDict) for Python 3.10 compatibility:
    from typing_extensions import TypedDict
    

    State Not Updating

    Remember that node functions should return dictionaries with the keys to update:
    # Correct
    def my_node(state: State) -> dict:
        return {"text": "new value"}
    
    # Incorrect - returns the full state
    def my_node(state: State) -> State:
        state["text"] = "new value"
        return state
    

    Get Help

    Need assistance? Here are some resources: