Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

AI Agents · Const

State Management

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.

How it works

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.

Syntax

const graph = new StateGraph(State).addNode(name, node).addEdge(from, to).compile();

Returns

A compiled graph that can be invoked to execute nodes and produce state updates.

Key points

  • Use StateGraph when constructing a graph around user-defined state.
  • Reducers control how each emitted update is applied to its state key.
  • StateSchema defines state fields using standard schemas and special value types.
  • Command supports combining a state update with routing from a node.

Example: Shared state with StateGraph

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.

When to use it

Share context between nodes

Multi-step agent workflows

Aggregate updates with reducers

Parallel or concurrent node execution

Update and route with Command

Conditional workflow transitions

Interview questions

QWhat is state in LangGraph?

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.

QWhat does StateGraph provide?

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.

QHow do node updates affect state?

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.

  • StateGraph
  • StateSchema
  • Command
  • reducer
  • CompiledGraph

Source: Graph API overview. Checked 2026-10-15.