How to beginner · 3 min read

How to build AI workflow with LangGraph

Quick answer
Use LangGraph to build stateful AI workflows by defining nodes as Python functions and connecting them in a StateGraph. Compile the graph into an app and invoke it with input state to run the workflow.

PREREQUISITES

  • Python 3.8+
  • pip install langgraph
  • Basic Python knowledge

Setup

Install the langgraph package via pip and import the necessary classes. No API keys are required as LangGraph runs locally.

bash
pip install langgraph
output
Collecting langgraph
  Downloading langgraph-0.1.0-py3-none-any.whl (10 kB)
Installing collected packages: langgraph
Successfully installed langgraph-0.1.0

Step by step

Define a typed state dictionary, create node functions that accept and return state, add nodes and edges to a StateGraph, compile it, and invoke the app with initial state.

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

# Define the state schema
class State(TypedDict):
    messages: list[str]

# Define a node function

def greet_node(state: State) -> State:
    new_messages = state["messages"] + ["Hello from LangGraph!"]
    return {"messages": new_messages}

# Create the graph and add nodes
graph = StateGraph(State)
graph.add_node("greet", greet_node)
graph.set_entry_point("greet")
graph.add_edge("greet", END)

# Compile the graph into an app
app = graph.compile()

# Invoke the app with initial state
result = app.invoke({"messages": []})
print(result["messages"])
output
['Hello from LangGraph!']

Common variations

You can build multi-node workflows by adding more nodes and edges. Use async functions for asynchronous workflows. Persist state between runs by integrating a checkpointer. Customize state types with TypedDict for complex data.

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

class State(TypedDict):
    messages: list[str]

async def greet_node(state: State) -> State:
    new_messages = state["messages"] + ["Hello asynchronously!"]
    return {"messages": new_messages}

def farewell_node(state: State) -> State:
    new_messages = state["messages"] + ["Goodbye!"]
    return {"messages": new_messages}

graph = StateGraph(State)
graph.add_node("greet", greet_node)
graph.add_node("farewell", farewell_node)
graph.set_entry_point("greet")
graph.add_edge("greet", "farewell")
graph.add_edge("farewell", END)

# Use a memory saver for persistence
app = graph.compile(checkpointer=MemorySaver())

import asyncio

async def run():
    result = await app.invoke({"messages": []})
    print(result["messages"])

asyncio.run(run())
output
['Hello asynchronously!', 'Goodbye!']

Troubleshooting

  • If you see TypeError, ensure your node functions accept and return the correct TypedDict state.
  • If the graph does not run, verify you set the entry point with set_entry_point().
  • For async nodes, ensure you await the app.invoke() call.

Key Takeaways

  • LangGraph uses typed Python functions as nodes to build AI workflows.
  • Compile your graph with StateGraph.compile() and invoke it with initial state.
  • Support for async nodes and state persistence enables complex workflows.
  • Always define and use a typed state dictionary for clarity and type safety.
Verified 2026-04
Verify ↗