The state definition used to construct the graph. Can be an
AnnotationRoot, StateSchema, or Zod object schema. This is the
primary generic from which S and U are derived.
The full state type representing the complete shape of your graph's
state after all reducers have been applied. Automatically inferred from SD.
The update type representing what nodes can return to modify state.
Typically a partial of the state type. Automatically inferred from SD.
Union of all node names in the graph (e.g., "agent" | "tool").
Accumulated as you call .addNode(). Used for type-safe routing.
The input schema definition. Set via the input option in the
constructor to restrict what data the graph accepts when invoked.
The output schema definition. Set via the output option in the
constructor to restrict what data the graph returns after execution.
The config/context schema definition. Set via the context option
to define additional configuration passed at runtime.
Constrains what types nodes in this graph can return.
The type for interrupt resume values. Set via
the interrupt option for typed human-in-the-loop patterns.
The type for custom stream writers. Set via the writer
option to enable typed custom streaming from within nodes.
Create a new StateGraph for building stateful, multi-step workflows.
Accepts state definitions via Annotation.Root, StateSchema, or Zod schemas.
Optionaloptions: const StateAnnotation = Annotation.Root({
messages: Annotation<string[]>({ reducer: (a, b) => [...a, ...b] }),
});
const graph = new StateGraph(StateAnnotation);
const graph = new StateGraph(StateAnnotation, {
input: InputSchema,
output: OutputSchema,
});
OptionalcontextSchema: C | AnnotationRoot<ToStateDefinition<C>>OptionalcontextSchema: C | AnnotationRoot<ToStateDefinition<C>>OptionalcontextSchema: C | AnnotationRoot<ToStateDefinition<C>>OptionalcontextSchema: C | AnnotationRoot<ToStateDefinition<C>>OptionalentryOptionaloptions: StateGraphAddNodeOptions<string, undefined | StateDefinitionInit>Optionaloptions: StateGraphAddNodeOptions<string, undefined | StateDefinitionInit>Optionalinterrupt: string[]Protectedwarn
A graph whose nodes communicate by reading and writing to a shared state. Each node takes a defined
Stateas input and returns aPartial<State>.Each state key can optionally be annotated with a reducer function that will be used to aggregate the values of that key received from multiple nodes. The signature of a reducer function is (left: Value, right: UpdateValue) => Value.
See Annotation for more on defining state.
After adding nodes and edges to your graph, you must call
.compile()on it before you can use it.Example