Skip to content
Learni
View all tutorials
Intelligence Artificielle

How to Build an Agent with LangGraph in 2026

Lire en français

Introduction

LangGraph lets you model LLM applications as graphs, giving you complete control over agent execution flow. Unlike linear chains, it handles states, loops, and conditional decisions explicitly. This tutorial covers the foundations for building a simple agent that answers questions while preserving conversation history. It's ideal for projects that require reliability and traceability.

Prerequisites

  • Python 3.10 or higher
  • Basic knowledge of Python and LLM APIs
  • OpenAI API key or equivalent
  • Virtual environment recommended

Installing Dependencies

terminal
python -m venv venv
source venv/bin/activate
pip install langgraph langchain langchain-openai

This command creates an isolated environment and installs LangGraph along with the required LangChain dependencies for interacting with LLM models.

Defining Graph State

state.py
from typing import TypedDict, Annotated

from langgraph.graph import add_messages

class AgentState(TypedDict):
    messages: Annotated[list, add_messages]

The AgentState defines the shared structure passed between nodes. add_messages automatically concatenates messages without duplication.

Creating the First Node

nodes.py
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o-mini")

def chatbot(state: AgentState):
    response = llm.invoke(state["messages"])
    return {"messages": [response]}

This simple node calls the LLM and returns the response in the state. It serves as the entry point for text generation.

Building and Compiling the Graph

graph.py
from langgraph.graph import StateGraph, START, END

builder = StateGraph(AgentState)
builder.add_node("chatbot", chatbot)
builder.add_edge(START, "chatbot")
builder.add_edge("chatbot", END)

graph = builder.compile()

StateGraph assembles nodes and connections. START and END mark the beginning and end of the flow. compile() produces an executable graph.

Running the Graph

main.py
from state import AgentState
from graph import graph

initial_state = {"messages": [("user", "Bonjour, comment ça va ?")]}
result = graph.invoke(initial_state)
print(result["messages"][-1].content)

This script initializes the state with a user message, executes the graph, and prints the model's final response.

Best Practices

  • Always explicitly type the state with TypedDict
  • Use descriptive node names
  • Test each node independently before assembling the graph
  • Version prompts in separate files
  • Add checkpoints for persistence as early as possible

Common Mistakes to Avoid

  • Forgetting to annotate messages with add_messages causes state overwrites
  • Failing to handle LLM API errors can block the entire graph
  • Creating infinite loops without exit conditions
  • Ignoring user input validation

Going Further

Check out our advanced LangGraph courses to master multi-agent systems and state persistence.