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 functionclass PregelPartialRunnable<InputType | CommandType | null, OutputType, PregelOptions<Nodes, Channels, ContextType>>// Using StateGraph API
const graph = new StateGraph(annotation)
.addNode("nodeA", myNodeFunction)
.addEdge("nodeA", "nodeB")
.compile();
// The compiled graph is a Pregel instance
const result = await graph.invoke(input);// Using Functional API
import { task, entrypoint } from "@langchain/langgraph";
import { MemorySaver } from "@langchain/langgraph-checkpoint";
// Define tasks that can be composed
const addOne = task("add", async (x: number) => x + 1);
// Create a workflow using the entrypoint function
const workflow = entrypoint({
name: "workflow",
checkpointer: new MemorySaver()
}, async (numbers: number[]) => {
// Tasks can be run in parallel
const results = await Promise.all(numbers.map(n => addOne(n)));
return results;
});
// The workflow is a Pregel instance
const result = await workflow.invoke([1, 2, 3]); // Returns [2, 3, 4]Whether to automatically validate the graph structure when it is compiled. Defaults to true.
The channels in the graph, mapping channel names to their BaseChannel or ManagedValueSpec instances
Optional checkpointer for persisting graph state. When provided, saves a checkpoint of the graph state at every superstep. When false or undefined, checkpointing is disabled, and the graph will not be able to save or restore state.
The default configuration for graph execution, can be overridden on a per-invocation basis
Whether to enable debug logging. Defaults to false.
The input channels for the graph. These channels receive the initial input when the graph is invoked. Can be a single channel key or an array of channel keys.
Optional array of node names or "all" to interrupt after executing these nodes. Used for implementing human-in-the-loop workflows.
Optional array of node names or "all" to interrupt before executing these nodes. Used for implementing human-in-the-loop workflows.
The name of the task, analogous to the node name in StateGraph.
The nodes in the graph, mapping node names to their PregelNode instances
The output channels for the graph. These channels contain the final output when the graph completes. Can be a single channel key or an array of channel keys.
Optional retry policy for handling failures in node execution
Optional timeout in milliseconds for the execution of each superstep
Optional long-term memory store for the graph, allows for persistence & retrieval of data across threads
Optional channels to stream. If not specified, all channels will be streamed. Can be a single channel key or an array of channel keys.
The streaming modes enabled for this graph. Defaults to ["values"]. Supported modes:
Execute multiple operations in a single batch. This is more efficient than executing operations individually.
Returns a drawable representation of the computation graph.
Gets the current state of the graph. Requires a checkpointer to be configured.
Gets the history of graph states. Requires a checkpointer to be configured. Useful for:
Gets all subgraphs within this graph asynchronously. A subgraph is a Pregel instance that is nested within a node of this graph.
Run the graph with a single input and config.
Create a new runnable sequence that runs each individual runnable in series, piping the output of one runnable into another runnable or runnable-like.
Streams the execution of the graph, emitting state updates as they occur. This is the primary method for observing graph execution in real-time.
Stream modes:
Updates the state of the graph with new values. Requires a checkpointer to be configured.
This method can be used for:
Validates the graph structure to ensure it is well-formed. Checks for:
Creates a new instance of the Pregel graph with updated configuration. This method follows the immutable pattern - instead of modifying the current instance, it returns a new instance with the merged configuration.
Returns a drawable representation of the computation graph.
Gets all subgraphs within this graph. A subgraph is a Pregel instance that is nested within a node of this graph.