Share context between nodes
Multi-step agent workflows
AI Agents · Const
State Management is LangGraph's shared-state model for agent workflows. Define a state schema, let nodes return partial updates, and use reducers to determine how updates are combined.
State is a shared data structure that represents the current application snapshot. Nodes read the state and return partial updates; LangGraph applies those updates through each field's reducer, then edges determine what runs next.
const graph = new StateGraph(State).addNode(name, node).addEdge(from, to).compile();A compiled graph that can be invoked to execute nodes and produce state updates.
import { Annotation, StateGraph, START, END } from "@langchain/langgraph";
const State = Annotation.Root({
input: Annotation<string>(),
result: Annotation<string>(),
});
const graph = new StateGraph(State)
.addNode("process", (state) => ({
result: state.input.toUpperCase(),
}))
.addEdge(START, "process")
.addEdge("process", END)
.compile();
const output = await graph.invoke({ input: "hello" });
console.log(output.result);Defines shared state, lets a node read it and return a partial update, then compiles and invokes the graph.
Multi-step agent workflows
Parallel or concurrent node execution
Conditional workflow transitions
State is the shared data structure representing the application's current snapshot. It is typically defined with a schema, and nodes read it while returning updates to it.
Follow-up: How are concurrent updates combined? Each state key can define a reducer that aggregates values received from multiple nodes.
StateGraph is the main graph class for building an agent workflow. It is parameterized by a user-defined State object, and its nodes communicate by reading and writing shared state.
Follow-up: What must happen before execution? Add the required nodes and edges, then compile the StateGraph into an executable graph.
Nodes emit updates to state, and the configured reducer for each key determines how an update is applied. A node can therefore return only the fields it wants to change rather than reconstructing the full state.
Follow-up: How can one operation both update state and choose the next node? Return a Command from the node function to update state and route in one step.
StateGraphStateSchemaCommandreducerCompiledGraph