By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A chat agent's state is almost always the same shape: a list of messages. LangGraph ships that shape as `MessagesState`, a TypedDict with one field — messages: Annotated[list[AnyMessage], add_messages]. The add_messages reducer appends new messages, and it does one thing more: a message with an existing id replaces that message instead of duplicating it, and RemoveMessage deletes a message. That makes edits and history trimming ordinary state updates.
If your graph needs more than messages — an order_id, an attempts counter, an approval flag — define your own TypedDict and use Annotated[list, add_messages] for just the messages field. MessagesState is a convenience, not a requirement.
The LangChain path built the tool loop by hand: read tool_calls, execute each function, append a ToolMessage. LangGraph packages that mechanics as `ToolNode`, and the routing decision as `tools_condition`. ToolNode(tools) is a node that reads the last message, executes every requested tool and returns one ToolMessage per call. tools_condition is a router that sends the run to the tools node when the last AIMessage has tool calls and to END when it does not.
from typing import Annotated, TypedDict
from langchain_core.language_models.fake_chat_models import GenericFakeChatModel
from langchain_core.messages import AIMessage, HumanMessage, ToolCall
from langchain_core.tools import tool
from langgraph.graph import START, StateGraph
from langgraph.graph.message import add_messages
from langgraph.prebuilt import ToolNode, tools_condition
@tool
def lookup_order(order_id: str) -> str:
"""Read-only: look up an order's status by id."""
orders = {"4521": "delivered (ceramic mug)"}
if order_id not in orders:
raise ValueError(f"Unknown order: {order_id}")
return orders[order_id]
class AgentState(TypedDict):
messages: Annotated[list, add_messages]
model = GenericFakeChatModel(
messages=iter(
[
AIMessage(
content="",
tool_calls=[
ToolCall(
name="lookup_order",
args={"order_id": "4521"},
id="call_1",
)
],
),
AIMessage(
content="Order #4521 was delivered and contains one ceramic mug."
),
]
)
)
def call_model(state: AgentState) -> dict:
return {"messages": [model.invoke(state["messages"])]}
builder = StateGraph(AgentState)
builder.add_node("model", call_model)
builder.add_node("tools", ToolNode([lookup_order]))
builder.add_edge(START, "model")
builder.add_conditional_edges("model", tools_condition)
builder.add_edge("tools", "model")
graph = builder.compile()
result = graph.invoke(
{"messages": [HumanMessage("Where is order #4521?")]}
)
for message in result["messages"]:
print(type(message).__name__, "|", repr(message.content))AIMessage can request several tools; ToolNode executes them and returns one ToolMessage per call, each linked by tool_call_id.handle_tool_errors configures that behaviour.{'messages': [...]} and add_messages appends them in order.tools_condition, carry an attempts or step budget so a model that keeps calling tools cannot run forever.| Piece | What it is | Use it when |
|---|---|---|
MessagesState | A TypedDict with a reduced messages list | The graph's state is just the conversation |
add_messages | The reducer for a messages channel | You add messages to your own custom state |
ToolNode | A node that executes requested tools | The model can request tools and the graph should run them |
tools_condition | A router: tools when tool calls exist, otherwise END | The standard model → tools → model loop |
create_agent | A compiled graph wiring all of the above | You want the standard loop without hand-building it (Lesson 10) |
Key takeaways
MessagesState is the standard agent state: a messages list reduced by add_messages.
@tool turns a typed, documented function into a schema; ToolNode executes the model's requested calls.
tools_condition routes to the tools node on tool calls and to END otherwise.
ToolNode handles dispatch, ids and tool errors — but read-only versus write policy is still yours.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.