How to beginner · 3 min read

How to build a simple LangGraph agent

Quick answer
Use the langgraph package to define a StateGraph with typed states and nodes as functions. Compile the graph and invoke it with initial state data to run a simple LangGraph agent.

PREREQUISITES

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

Setup

Install the langgraph package via pip and prepare your environment.

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

Create a typed State dictionary, define a node function that updates state, build a StateGraph, add nodes and edges, compile the graph, and invoke it 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 that appends a response
def my_node(state: State) -> State:
    return {"messages": state["messages"] + ["Hello from LangGraph agent!"]}

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

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

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

Common variations

  • Use multiple nodes and edges to create complex flows.
  • Persist state with a checkpointer for long-running agents.
  • Integrate with LLMs by calling AI APIs inside node functions.
python
from langgraph.graph import StateGraph, END
from langgraph.savers.memory import MemorySaver
from typing import TypedDict

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

# Example node calling an LLM (pseudo-code)
def llm_node(state: State) -> State:
    # Imagine calling an LLM here
    response = "AI response"
    return {"messages": state["messages"] + [response]}

graph = StateGraph(State)
graph.add_node("llm_node", llm_node)
graph.set_entry_point("llm_node")
graph.add_edge("llm_node", END)

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

result = app.invoke({"messages": []})
print(result["messages"])
output
['AI response']

Troubleshooting

  • If you see TypeError, ensure your state matches the TypedDict schema.
  • If app.invoke() hangs, check that all nodes have edges leading to END or other nodes.
  • For import errors, verify langgraph is installed in your environment.

Key Takeaways

  • Define your agent state with Python TypedDict for type safety.
  • Use StateGraph to add nodes and edges representing agent logic.
  • Compile the graph and invoke it with initial state to run the agent.
  • Persist state with checkpointers for complex or long-running agents.
  • Integrate external APIs inside node functions for powerful AI agents.
Verified 2026-04
Verify ↗