import { ... } from "@langchain/langgraph";The Pregel class is the core runtime engine of LangGraph, implementing a message-passing graph computation model inspired by Google's Pregel system. It provides the foundation for building reliable, controllable agent workflows that can evolve state over time.
Key features:
The Pregel class is not intended to be instantiated directly by consumers. Instead, use the following higher-level APIs:
PregelPregel instance is returned by the entrypoint functionSelects the type of output you'll receive when streaming from the graph. See Streaming for more details.
Special reserved node name denoting the end of a graph.
Special channel reserved for graph interrupts
Prebuilt state annotation that combines returned messages. Can handle standard messages and special modifiers like RemoveMessage instances.
Specifically, importing and using the prebuilt MessagesAnnotation like this:
import { MessagesAnnotation, StateGraph } from "@langchain/langgraph";
const graph = new StateGraph(MessagesAnnotation)
.addNode(...)
...import { BaseMessage } from "@langchain/core/messages";
import { Annotation, StateGraph, messagesStateReducer } from "@langchain/langgraph";
export const StateAnnotation = Annotation.Root({
messages: Annotation<BaseMessage[]>({
reducer: messagesStateReducer,
default: () => [],
}),
});
const graph = new StateGraph(StateAnnotation)
.addNode(...)
...Prebuilt schema meta for Zod state definition.
import { z } from "zod/v4-mini";
import { MessagesZodState, StateGraph } from "@langchain/langgraph";
const AgentState = z.object({
messages: z.custom<BaseMessage[]>().register(registry, MessagesZodMeta),
});Prebuilt state object that uses Zod to combine returned messages.
This utility is synonymous with the MessagesAnnotation annotation,
but uses Zod as the way to express messages state.
You can use import and use this prebuilt schema like this:
import { MessagesZodState, StateGraph } from "@langchain/langgraph";
const graph = new StateGraph(MessagesZodState)
.addNode(...)
...import { z } from "zod";
import type { BaseMessage, BaseMessageLike } from "@langchain/core/messages";
import { StateGraph, messagesStateReducer } from "@langchain/langgraph";
import "@langchain/langgraph/zod";
const AgentState = z.object({
messages: z
.custom<BaseMessage[]>()
.default(() => [])
.langgraph.reducer(
messagesStateReducer,
z.custom<BaseMessageLike | BaseMessageLike[]>()
),
});
const graph = new StateGraph(AgentState)
.addNode(...)
...Special value that signifies the intent to remove all previous messages in the state reducer.
Used as the unique identifier for a RemoveMessage instance which, when encountered,
causes all prior messages to be discarded, leaving only those following this marker.
Special reserved node name denoting the start of a graph.
Helper that instantiates channels within a StateGraph state.
Can be used as a field in an Annotation.Root wrapper in one of two ways:
import { StateGraph, Annotation } from "@langchain/langgraph";
// Define a state with a single string key named "currentOutput"
const SimpleAnnotation = Annotation.Root({
currentOutput: Annotation<string>,
});
const graphBuilder = new StateGraph(SimpleAnnotation);
// A node in the graph that returns an object with a "currentOutput" key
// replaces the value in the state. You can get the state type as shown below:
const myNode = (state: typeof SimpleAnnotation.State) => {
return {
currentOutput: "some_new_value",
};
}
const graph = graphBuilder
.addNode("myNode", myNode)
...
.compile();import { type BaseMessage, AIMessage } from "@langchain/core/messages";
import { StateGraph, Annotation } from "@langchain/langgraph";
// Define a state with a single key named "messages" that will
// combine a returned BaseMessage or arrays of BaseMessages
const AnnotationWithReducer = Annotation.Root({
messages: Annotation<BaseMessage[]>({
// Different types are allowed for updates
reducer: (left: BaseMessage[], right: BaseMessage | BaseMessage[]) => {
if (Array.isArray(right)) {
return left.concat(right);
}
return left.concat([right]);
},
default: () => [],
}),
});
const graphBuilder = new StateGraph(AnnotationWithReducer);
// A node in the graph that returns an object with a "messages" key
// will update the state by combining the existing value with the returned one.
const myNode = (state: typeof AnnotationWithReducer.State) => {
return {
messages: [new AIMessage("Some new response")],
};
};
const graph = graphBuilder
.addNode("myNode", myNode)
...
.compile();Define a LangGraph workflow using the entrypoint function.
The wrapped function must accept at most two parameters. The first parameter is the input to the function. The second (optional) parameter is a LangGraphRunnableConfig object. If you wish to pass multiple parameters to the function, you can pass them as an object.
To write data to the "custom" stream, use the getWriter function, or the LangGraphRunnableConfig.writer property.
The getPreviousState function can be used to access the previous state that was returned from the last invocation of the entrypoint on the same thread id.
If you wish to save state other than the return value, you can use the entrypoint.final function.
A helper utility function that returns the LangGraphRunnableConfig that was set when the graph was initialized.
Note: This only works when running in an environment that supports node:async_hooks and AsyncLocalStorage. If you're running this in a web environment, access the LangGraphRunnableConfig from the node function directly.
A helper utility function that returns the input for the currently executing task
Get the JSON schema from a SerializableSchema.
A helper utility function for use with the functional API that returns the previous state from the checkpoint from the last invocation of the current thread.
This function allows workflows to access state that was saved in previous runs using entrypoint.final.
Detect if a schema has a default value by validating undefined.
Uses the Standard Schema ~standard.validate API to detect defaults.
If the schema accepts undefined and returns a value, that value is the default.
This approach is library-agnostic and works with any Standard Schema compliant library (Zod, Valibot, ArkType, etc.) without needing to introspect internals.
A helper utility function that returns the BaseStore that was set when the graph was initialized
Used for subgraph detection.
A helper utility function that returns the LangGraphRunnableConfig#writer if "custom" stream mode is enabled, otherwise undefined.
Interrupts the execution of a graph node.
This function can be used to pause execution of a node, and return the value of the resume
input when the graph is re-invoked using Command.
Multiple interrupts can be called within a single node, and each will be handled sequentially.
When an interrupt is called:
resume value available (from a previous Command), it returns that value.GraphInterrupt with the provided valueCommand with a resume valueBecause the interrupt function propagates by throwing a special GraphInterrupt error,
you should avoid using try/catch blocks around the interrupt function,
or if you do, ensure that the GraphInterrupt error is thrown again within your catch block.
A type guard to check if the given value is a Command.
Useful for type narrowing when working with the Command object.
Checks if the given graph invoke / stream chunk contains interrupt.
Type guard to check if a given value is a SerializableSchema, i.e.
both a Standard Schema and a Standard JSON Schema object.
Type guard to check if a given value is a Standard Schema V1 object.
Reducer function for combining two sets of messages in LangGraph's state system.
This reducer handles several tasks:
left and right message inputs to arrays.BaseMessage instances.RemoveMessage instance is encountered in right with the ID REMOVE_ALL_MESSAGES,
all previous messages are discarded and only the subsequent messages in right are returned.left and right messages together following these rules:
right shares an ID with a message in left:
RemoveMessage, that message (by ID) is marked for removal.left.right does not exist in left:
RemoveMessage, this is considered an error (cannot remove non-existent ID).Manually push a message to a message stream.
This is useful when you need to push a manually created message before the node has finished executing.
When a message is pushed, it will be automatically persisted to the state after the node has finished executing.
To disable persisting, set options.stateKey to null.
Define a LangGraph task using the task function.
Tasks can only be called from within an entrypoint or from within a StateGraph. A task can be called like a regular function with the following differences:
Reducer function for combining two sets of messages in LangGraph's state system.
This reducer handles several tasks:
left and right message inputs to arrays.BaseMessage instances.RemoveMessage instance is encountered in right with the ID REMOVE_ALL_MESSAGES,
all previous messages are discarded and only the subsequent messages in right are returned.left and right messages together following these rules:
right shares an ID with a message in left:
RemoveMessage, that message (by ID) is marked for removal.left.right does not exist in left:
RemoveMessage, this is considered an error (cannot remove non-existent ID).Abstract base class for persistent key-value stores.
Stores enable persistence and memory that can be shared across threads, scoped to user IDs, assistant IDs, or other arbitrary namespaces.
Features:
Abstract base class for persistent key-value stores.
Stores enable persistence and memory that can be shared across threads, scoped to user IDs, assistant IDs, or other arbitrary namespaces.
Features:
Stores the result of applying a binary operator to the current value and each new value.
One or more commands to update the graph's state and send messages to nodes. Can be used to combine routing logic with state updates in lieu of conditional edges
Final result from building and compiling a StateGraph.
Should not be instantiated directly, only using the StateGraph .compile()
instance method.
In-memory key-value store with optional vector search.
A lightweight store implementation using JavaScript Maps. Supports basic key-value operations and vector search when configured with embeddings.
Raised by a node to interrupt execution.
Represents a state field whose value is computed and updated using a reducer function.
ReducedValue allows you to define accumulators, counters, aggregators, or other fields whose value is determined incrementally by applying a reducer to incoming updates.
Each time a new input is provided, the reducer function is called with the current output and the new input, producing an updated value. Input validation can be controlled separately from output validation by providing an explicit input schema.
Exception raised when an error occurs in the remote graph.
A message or packet to send to a specific node in the graph.
The Send class is used within a StateGraph's conditional edges to
dynamically invoke a node with a custom state at the next step.
Importantly, the sent state can differ from the core graph's state, allowing for flexible and dynamic workflow management.
One such example is a "map-reduce" workflow where your graph invokes the same node multiple times in parallel with different states, before aggregating the results back into the main graph's state.
A graph whose nodes communicate by reading and writing to a shared state.
Each node takes a defined State as input and returns a Partial<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.
Error thrown when invalid input is provided to a StateGraph.
This typically means that the input to the StateGraph constructor or builder did not match the required types. A valid input should be a StateDefinition, an Annotation.Root, or a Zod schema.
StateSchema provides a unified API for defining LangGraph state schemas.
Represents a state field whose value is transient and never checkpointed.
Use UntrackedValue for state fields that should be tracked for the lifetime of the process, but should not participate in durable checkpoints or recovery.
The Pregel class is the core runtime engine of LangGraph, implementing a message-passing graph computation model inspired by Google's Pregel system. It provides the foundation for building reliable, controllable agent workflows that can evolve state over time.
Key features:
The Pregel class is not intended to be instantiated directly by consumers. Instead, use the following higher-level APIs:
PregelPregel instance is returned by the entrypoint function