0% found this document useful (0 votes)
216 views24 pages

LangGraph Agents Tutorial Guide

The LangGraph Agents Hands-On Tutorial teaches users how to build scalable AI agents using LangGraph's graph-based framework, focusing on core components like state, nodes, edges, and memory management. It covers the creation of single and ReAct agents, the integration of tools, and the management of memory through external storage and native support for short-term and long-term memory. The tutorial emphasizes the importance of structured workflows and real-time decision-making in developing complex multi-agent systems.

Uploaded by

webdevrohitvish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
216 views24 pages

LangGraph Agents Tutorial Guide

The LangGraph Agents Hands-On Tutorial teaches users how to build scalable AI agents using LangGraph's graph-based framework, focusing on core components like state, nodes, edges, and memory management. It covers the creation of single and ReAct agents, the integration of tools, and the management of memory through external storage and native support for short-term and long-term memory. The tutorial emphasizes the importance of structured workflows and real-time decision-making in developing complex multi-agent systems.

Uploaded by

webdevrohitvish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Buy Now

EN

T U TO R I A L S category

Home Tutorials AI Agents

LangGraph Agents Hands-On Tutorial


Master LangGraph fundamentals — state, nodes, edges, memory —
and build scalable AI agents with ReAct patterns, custom tools, and
persistent state management.

Contents Jul 15, 2025

Vaibhav Mehra

TO P I C S

AI Agents

Artificial Intelligence

Large Language Models

LangGraph is a graph-based framework for building AI agents that can reason,


plan, and act through interconnected steps. Its distinguishing factor is that it lets
us create cyclical, branching workflows with ease, where each node can run an
LLM, tool, or function — while managing the state automatically.

[Link] 15/10/25, 9 00 PM
Page 1 of 24
:
This makes LangGraph ideal for building complex, robust, multi-agent systems
that need memory, error handling, and real-time decision-making.

In this tutorial, I’ll walk you through the fundamentals and advanced features of
LangGraph, from understanding its core components to building stateful, tool-
augmented AI agents. If you’re interested in building multi-agent systems with
LangGraph, be sure to check out our hands-on course. You can also view our
video tutorial on building LangGraph agents below.

LangGraph Fundamentals
Let’s start by discussing the fundamental building blocks of LangGraph. These
elements allow us to build structured, stateful, and scalable workflows for
building advanced AI agents.

State
The state is a shared memory object that flows through the graph. It stores all
the relevant information such as messages, variables, intermediate results, and
decision history. LangGraph manages the state automatically, making
development more efficient.

[Link] 15/10/25, 9 00 PM
Page 2 of 24
:
To complement the State, there are more advanced supporting features like
checkpointing, thread-local memory, and cross-session persistence. Throughout
the execution, the state is continuously executed, as shown in the diagram below:

State management cycle in LangGraph showing continuous updates across


nodes

Node
A node is a single functional unit in the workflow. It can perform a variety of
actions, such as:

Invoking a large language model (LLM)

Calling a tool or API

[Link] 15/10/25, 9 00 PM
Page 3 of 24
:
Running a custom Python function

Routing logic or branching decisions

Each node takes in the current state and returns an updated state.

Edges and conditional edges


Edges define the transitions between nodes. They determine the control flow of
the graph and can support:

Static connections (for linear progression)

Cyclical paths (for iterative behaviors)

Dynamic branching (based on state conditions - created through


Conditional Edges)

Different types of Edges define the flow between nodes and control the
information through the graph.

Edges are essential for orchestrating complex agent behaviors.

Graph and stategraph


A graph in LangGraph defines the structure of the agentic workflow, and it
consists of nodes connected by edges.

[Link] 15/10/25, 9 00 PM
Page 4 of 24
:
Graph Structure is a combination of Nodes and Edges.

The StateGraph is a specialized graph that maintains and updates a shared


state throughout execution. It enables context-aware decision-making and
persistent memory across steps. We can think of this as the fusion of State and
Graph .

Tool and toolnode


A tool is any external or internal function an agent can call, such as a web
search, calculator, or custom utility. There are two types of Tools:

In-built Tools: Tools which are pre-made and ready to use by Langchain,
and can be accessed in the Documentation.

Custom Tools: Tools that we can make ourselves and use in our application
(done through the assignment of a decorator and DocString - this will be
shown in code later).

A ToolNode is a dedicated node type for executing tools within the graph. It
allows us developers to integrate tools without writing additional wrapper logic.

[Link] 15/10/25, 9 00 PM
Page 5 of 24
:
We can think of this as the fusion of Tool and Node .

Message types
Messages are structured data elements (such as the user input, system output,
intermediate responses, etc.) that move through the graph and are stored in the
state. They allow for traceability, memory, and context for agent decisions, which
helps in making more informed decisions.

There are multiple MessageTypes in LangGraph:

HumanMessage: Represents input from a user. This is the most common


message type used to start or continue a conversation within the graph
with the agent.

AIMessage: Represents the responses generated by the underlying


language models. These are stored to maintain conversational memory
and inform future actions.

SystemMessage: Provides context or behavior setup instructions to the


LLM (e.g., “You are my personal assistant.”). This message influences how
the model responds.

ToolMessage: Encapsulates the output from a tool. These are crucial when
your agent relies on external operations (like calculations or searches) to
decide its next steps.

RemoveMessage: Used to programmatically delete or undo previously


added messages from the state. This is helpful for error correction or
pruning irrelevant context.

BaseMessage: The main parent for all message types in LangChain and
LangGraph. Every specific message type — like HumanMessage ,
AIMessage , or SystemMessage — inherits from BaseMessage .

LangGraph Agents
Now we will be coding these Agents in LangGraph, focusing on understanding
the different methods and steps involved when coding as well as visualizing
these agents.

[Link] 15/10/25, 9 00 PM
Page 6 of 24
:
Single-agent workflow
Let’s start by creating a single agent with no tools or memory. Let's start off with
the imports:

from typing import TypedDict, List


from langchain_core.messages import HumanMessage
from langchain_openai import ChatOpenAI
from [Link] import StateGraph, START, END
from dotenv import load_dotenv

Explain code POWERED BY

The first line states TypedDict , which is a Type-Annotation that is defined like
this in Python:

from typing import TypedDict


class Student(TypedDict):
name: str
grade: float
s1: Student = {"name": "Sam", "grade": 92.5}

Explain code POWERED BY

Essentially, a TypedDict is a dictionary that has robust Type-Safety measures,


crucial for AI Agent Development.

HumanMessage should be familiar to you now, as we discussed this earlier.

ChatOpenAI , however, is new. Let's take a slight detour to explain this.

Through Langchain, we can use the Chat family, which essentially allows us to
use models from many different LLM providers, such as OpenAI, Anthropic,
Ollama, etc. through their respective libraries such as ChatOpenAI ,
ChatAnthropic , ChatOllama , etc.

Fundamentally, their parameters and usage are similar, allowing for modularity in
our code. More detailed steps can be found in the LangChain Documentation.

[Link] 15/10/25, 9 00 PM
Page 7 of 24
:
Lastly, the StateGraph we have covered earlier, and the Start and End are the
start and end points of our graph.

Let's proceed onwards:

load_dotenv() # Obtaining over secret keys


# Creation of the state using a Typed Dictionary
class AgentState(TypedDict):
messages: List[HumanMessage] # We are going to be storing Human Messages (the u
llm = ChatOpenAI(model="gpt-4o") # Our model choice
# This is an action - the underlying function of our node
def process(state: AgentState) -> AgentState:
response = [Link](state["messages"])
print(f"\nAI: {[Link]}")
return state
graph = StateGraph(AgentState) # Initialization of a Graph
graph.add_node("process_node", process) # Adding nodes
graph.add_edge(START, "process_node") # Adding edges
graph.add_edge("process_node", END)
agent = [Link]() # Compiling the graph

Explain code POWERED BY

Let's discuss the .add_node() and .add_edge() methods in more detail.

[Link]() : There are two main parameters you need to focus on; the
name of the node and the underlying action. The underlying action, in our
case - process() is the main logic behind the node, i.e., what actions are
completed when we get to this stage. The name of the node can be
anything, even the same as the function, although that could be quite
confusing when debugging!

add_edge.() : There are again two main parameters you need to focus
on, and these are the start and the end nodes of the Edge. Note that the
name of the node is what gets inputted, not the underlying action.

Let’s look at a practical example:

[Link] 15/10/25, 9 00 PM
Page 8 of 24
:
user_input = input("Enter: ")
while user_input != "exit":
[Link]({"messages": [HumanMessage(content=user_input)]})
user_input = input("Enter: ")

Explain code POWERED BY

The above piece of code allows us to call the Agent multiple times. Visually
speaking, the AI Agent we have constructed looks like this:

A Sequential Agent completes tasks step-by-step in a predefined order, without


branching or reasoning.

To obtain such a visualization, we can use the following code (Note: we use the
compiled graph):

from [Link] import Image, display


display(Image(agent.get_graph().draw_mermaid_png()))

Explain code POWERED BY

React agents
ReAct Agents, Reasoning and Acting Agents, are extremely common in industry,
due to their ease of creation but robustness. Since they are so common,

[Link] 15/10/25, 9 00 PM
Page 9 of 24
:
LangGraph has an inbuilt method we can leverage to create such Agents.

Fortunately, we only need two additional imports:

from [Link] import create_react_agent


from langchain_google_community import GmailToolkit

Explain code POWERED BY

The create_react_agent method is what will create the ReAct agents for us.

tools = [GmailToolkit()] # We are using the Inbuilt Gmail tool


llm = ChatOllama(model="qwen2.5:latest") # Leveraging Ollama Models
agent = create_react_agent(
model = llm, # Choice of the LLM
tools = tools, # Tools we want our LLM to have
name = "email_agent", # Name of our agent
prompt = "You are my AI assistant that has access to certain tools. Use the
)

Explain code POWERED BY

The best part about using this is that we can create multiple agents and create
more complex architectures.

We can also create our own custom tools.

[Link] 15/10/25, 9 00 PM
Page 10 of 24
:
@tool
def send_email(email_address: str, email_content: str) -> str:
"""
Sends an email to a specified recipient with the given content.
Args:
email_address (str): The recipient's email address (e.g., 'example@[Link]'
email_content (str): The body of the email message to be sent
Returns:
str: A confirmation message indicating success or failure
Example:
>>> send_email('[Link]@[Link]', 'Hello John, just checking in!')
'Email successfully sent to [Link]@[Link]'
"""
# Tool Logic goes here
return "Done!"

Explain code POWERED BY

As stated above, there are two main things that are important when creating
custom tools:

Decorator: This tells LangGraph that this particular function is a


specialized function - a tool.

Docstring: This is needed to provide the context of what the tool does to
the LLM. It is useful to provide examples and descriptions of each
parameter since it makes tool-calling more robust.

ReAct Agents look like this:

[Link] 15/10/25, 9 00 PM
Page 11 of 24
:
A ReAct Agent alternates between reasoning (thoughts) and actions to solve
tasks step-by-step.

Reducer functions for state growth


When defining the State for more complicated, robust agentic systems, it is a
good idea to use Reducer Functions or simply reducers . reducers are data
annotations that make sure that each time a node returns a message, it is
appended to the existing list in the state, rather than overwriting the previous
value. This allows our state to build up the context.

# State with reducer function


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

Explain code POWERED BY

This code integrates the reducer function [Link] with the state, ensuring
all of the messages are preserved.

Memory Management in LangGraph


In this section we will be focusing on a core component of every agent - Memory.
Memory is extremely important to an agent since it provides the context needed

[Link] 15/10/25, 9 00 PM
Page 12 of 24
:
to make it more robust and reliable. Let’s explore the different ways we can
manage memory in our agents.

External checkpoint stores


The State is the most important element in LangGraph, and naturally, there are
multiple ways to store the state externally. Some of the popular external storage
are:

SQLite

PostgreSQL

Amazon S3

Azure Blob Storage

Google Cloud Storage

Mem0

All of these libraries can be found in the documentation.

For now, we will look at the SQLite:

from [Link] import SqliteSaver


memory = SqliteSaver.from_conn_string(":memory:") # This is for connecting to t
graph = graph_builder.compile(checkpointer=memory) # Compiling graph with check

Explain code POWERED BY

Using SQLite is an easy way to add Persistence to our Agents. Persistence is the
ability to maintain context across different interactions.

All of this is for storing and updating the state. Now let's look at how to use and
manage that state effectively, especially the messages list, which becomes
quite large as the conversation continues.

LangGraph provides native support for short-term and long-term memory. Let's
explore these.

[Link] 15/10/25, 9 00 PM
Page 13 of 24
:
Short-term memory
Short-term memory allows our agents to remember message history during a
session, making it useful for multi-turn conversations.

from [Link] import InMemorySaver


from [Link] import StateGraph

checkpointer = InMemorySaver()

builder = StateGraph(...)
graph = [Link](checkpointer=checkpointer)

# Invoking the Graph with our message


agent_graph.invoke(
{"messages": [{"role": "user", "content": "What's the weather today?"}]},
{"configurable": {"thread_id": "session_42"}},
)

Explain code POWERED BY

In the above example, the thread_id is used for uniquely identifying a


conversation or session.

Long-term memory
Long-term memory persists across multiple sessions and is perfect for
remembering things like names, goals, or settings - important things which are
required across sessions.

[Link] 15/10/25, 9 00 PM
Page 14 of 24
:
from [Link] import InMemoryStore
from [Link] import StateGraph

long_term_store = InMemoryStore()
builder = StateGraph(...)
agent = [Link](store=long_term_store)

Explain code POWERED BY

However, since our conversation can get large, we need a way to limit our
message history. This is where trimming comes in.

Trimming
In the below example, we are removing tokens from the beginning of the
message history (due to the line: strategy="first" ) so that after trimming, we are
left with at most 150 tokens.

from langchain_core.[Link] import trim_messages, count_tokens_approxima

trimmed = trim_messages(
messages=state["messages"],
strategy="first", # remove the messages from beginning
token_counter=count_tokens_approximately,
max_tokens=150
)

Explain code POWERED BY

Summarization-based pruning
Another way to reduce message history is by summarization. This allows for
preservation of important information throughout the message history, rather
than just cutting out tokens from a part of the conversation.

[Link] 15/10/25, 9 00 PM
Page 15 of 24
:
from langmem.short_term import SummarizationNode
from langchain_core.[Link] import count_tokens_approximately

summary_node = SummarizationNode(
model=summary_llm, # Our summarization LLM
max_tokens=300, # Total token limit
max_tokens_before_summary=150, # when to start summarizing
max_summary_tokens=150, # summary size
token_counter=count_tokens_approximately
)

Explain code POWERED BY

It is important to note that the summarization is performed by an underlying LLM,


in this case summary_llm , which can be any LLM.

Selective deletion
The last main operation needed in Memory Management is Delete. To delete
selected messages, we can use this code:

from langchain_core.messages import RemoveMessage

def clean_state(state):
# Remove all tool-related messages
to_remove = [RemoveMessage(id=[Link]) for msg in state["messages"] if msg.r
return {"messages": to_remove}

Explain code POWERED BY

In the above code, we have deleted all of the tool-related messages, which could
be redundant (since realistically, the Tool-Message is for the LLM to craft its
answer after using the tool).

However, if we want to delete the entire message history, we can do this:

[Link] 15/10/25, 9 00 PM
Page 16 of 24
:
from [Link] import REMOVE_ALL_MESSAGES
from langchain_core.messages import RemoveMessage

def reset_history(state):
# Remove entire conversation history
return {"messages": [RemoveMessage(id=REMOVE_ALL_MESSAGES)]}

Explain code POWERED BY

Conclusion
Overall, LangGraph is a powerful library that offers a structured and scalable
approach to building agentic systems. Modeling logic as a graph of nodes and
edges, with shared state and persistent memory, allows us to develop robust
agents that can reason, interact, and adapt over time.

As systems grow in complexity, this framework provides the tools needed to


manage memory, coordinate tool use, and maintain long-term context — all
while remaining modular and extensible.

With the core components now in place, you're equipped to design and deploy
robust AI agents capable of handling real-world workflows.

To keep learning, be sure to check out our course, Building Multi-Agent Systems
With LangGraph. You can also view our Designing Agentic Systems with
LangChain course, and our tutorial on Agentic RAG.

LangGraph Agents FAQs

What exactly is LangGraph?


A Python framework that models an AI agent’s logic as a graph of nodes
and edges, allowing for reasoning, tool calls, and memory.

[Link] 15/10/25, 9 00 PM
Page 17 of 24
:
Why should I choose LangGraph over other agent libraries?

How does LangGraph manage and persist memory?

Can I use local models instead?

How do I reduce the size of long message histories?

AUTHOR

Vaibhav Mehra

TO P I C S

AI Agents Artificial Intelligence Large Language Models

Training more people?


Get your team access to the full DataCamp for business platform.

For Business

For a bespoke solution book a demo.

Top DataCamp Courses

[Link] 15/10/25, 9 00 PM
Page 18 of 24
:
COURSE

Designing Agentic Systems with LangChain


3 hr 6.5K

Get to grips with the foundational components of LangChain agents and


build custom chat agents.

See Details Start Course

See More

Related
T U TO R I A L

LangGraph Tutorial: What Is


LangGraph and How to Use It?

T U TO R I A L

Getting Started with Gemini


Fullstack LangGraph

T U TO R I A L
LangGraph Studio Guide:
Installation, Set Up, Use Cases

See More

[Link] 15/10/25, 9 00 PM
Page 19 of 24
:
Grow your data skills with DataCamp for
Mobile
Make progress on the go with our mobile courses and daily 5-minute coding
challenges.

LEARN

Learn Python

Learn AI

Learn Power BI

Learn Data Engineering

Assessments

Career Tracks

Skill Tracks

Courses

Data Science Roadmap

DATA C O U R S E S

Python Courses

[Link] 15/10/25, 9 00 PM
Page 20 of 24
:
R Courses

SQL Courses

Power BI Courses

Tableau Courses

Alteryx Courses

Azure Courses

AWS Courses

Google Sheets Courses

Excel Courses

AI Courses

Data Analysis Courses

Data Visualization Courses

Machine Learning Courses

Data Engineering Courses

Probability & Statistics Courses

DATA L A B

Get Started

Pricing

Security

Documentation

[Link] 15/10/25, 9 00 PM
Page 21 of 24
:
C E R T I F I C AT I O N

Certifications

Data Scientist

Data Analyst

Data Engineer

SQL Associate

Power BI Data Analyst

Tableau Certified Data Analyst

Azure Fundamentals

AI Fundamentals

RESOURCES

Resource Center

Upcoming Events

Blog

Code-Alongs

Tutorials

Docs

Open Source

RDocumentation

Book a Demo with DataCamp for Business

[Link] 15/10/25, 9 00 PM
Page 22 of 24
:
Data Portfolio

PLANS

Pricing

For Students

For Business

For Universities

Discounts, Promos & Sales

Expense DataCamp

DataCamp Donates

FO R B U S I N E S S

Business Pricing

Teams Plan

Data & AI Unlimited Plan

Customer Stories

Partner Program

ABOUT

About Us

Learner Stories

Careers

Become an Instructor

[Link] 15/10/25, 9 00 PM
Page 23 of 24
:
Press

Leadership

Contact Us

DataCamp Español

DataCamp Português

DataCamp Deutsch

DataCamp Français

S U P PO R T

Help Center

Become an Affiliate

Privacy Policy Cookie Notice Do Not Sell My Personal Information Accessibility

Security Terms of Use

© 2025 DataCamp, Inc. All Rights Reserved.

[Link] 15/10/25, 9 00 PM
Page 24 of 24
:

You might also like