A LangGraph agent can be understood through four ideas:
This lesson starts with state and nodes. Edges and execution come next.
The Paris run needs one typed record of everything it knows so far.
Field in TripState |
What it holds | Merge rule |
|---|---|---|
messages |
Conversation so far | Append |
budget_left |
Rupees still unspent | Overwrite |
policy_clauses |
Retrieved policy text | Accumulate |
candidates |
Flight and hotel options | Accumulate |
attempts |
Replans so far | Add one |
verdict |
Latest compliance result | Overwrite |
One moment in the trip might look like this:
budget_left: ₹45,100
candidates: AI142 · Hotel Lumière
verdict: FAIL · §5.1
A reducer says how a new value joins the old value.
By default, a new value replaces the old one. Some fields need another rule:
1.A small typed-state example:
import operator
from typing import Annotated, TypedDict
from langgraph.graph.message import add_messages
class TripState(TypedDict):
messages: Annotated[list, add_messages]
budget_left: int
candidates: Annotated[list, operator.add]
attempts: Annotated[int, operator.add]
verdict: dict | None
The annotation is the merge rule. It prevents two useful updates from silently overwriting each other.
A node:
def retrieve_policy(state: TripState) -> dict:
clauses = policy_retriever.invoke(state["messages"][-1])
return {"policy_clauses": clauses}
The node does not copy and return the whole state. LangGraph merges its partial update using the reducers.
| Node | Reads | Returns |
|---|---|---|
retrieve_policy |
Goal | Cabin rule and hotel cap |
llm_call |
Messages | One new model message, possibly with tool calls |
tool_node |
Tool calls in the last message | One result message per tool |
retrieve_policy may contain a complete RAG pipeline, but the graph sees it as one node with one input and one update.
LangGraph state is a typed shared record; small nodes return partial updates, and reducers merge those updates without accidental data loss.