Execute a workflow
Turn a completed node-and-edge workflow into a runnable graph.
AI Agents · Function
StateGraph.compile() validates a graph definition and produces the executable graph. It is also the place to configure runtime options such as checkpointers and breakpoints.
Call compile on a completed StateGraph to turn its node-and-edge definition into an executable graph. Compilation performs structural checks and accepts runtime options such as a checkpointer; the returned graph can then be invoked or streamed.
const graph = builder.compile(options?);| Name | Type | Description |
|---|---|---|
options | CompileOptions | Optional compile configuration, including runtime settings such as a checkpointer; the API reference also lists cache and description options. |
A compiled executable graph, represented by a CompiledGraph-compatible runtime object.
.compile() to create the executable graph.compile is invalid.import { StateGraph, START, END, Annotation } from "@langchain/langgraph";
const State = Annotation.Root({
value: Annotation<number>(),
});
const workflow = new StateGraph(State)
.addNode("double", (state) => ({ value: state.value * 2 }))
.addEdge(START, "double")
.addEdge("double", END);
const graph = workflow.compile();
const result = await graph.invoke({ value: 3 });
console.log(result.value); // 6Build a StateGraph, connect its start and end nodes, compile it, and invoke the resulting runnable graph.
Turn a completed node-and-edge workflow into a runnable graph.
Attach persistence to graph execution.
Catch malformed graph structure before runtime execution.
StateGraph.compile() do?compile() converts the graph builder into an executable compiled graph. It also performs basic structural validation and is required before the graph can be used.
Follow-up: What runtime concerns can be configured during compilation? The documentation identifies checkpointers and breakpoints as runtime arguments, and the API also exposes cache-related configuration.
The graph should be assembled first by defining state, adding nodes, and adding edges. Calling .compile() then produces the runnable graph object.
Follow-up: Why not execute the builder directly? The builder represents the workflow definition; compilation creates the runtime object that supports execution methods such as invocation.
A checkpointer can be supplied through compile options, for example builder.compile({ checkpointer }). This attaches persistence behavior to the compiled graph.
Follow-up: What changes when a checkpointer is configured? Runtime state can be persisted and used for features such as durable execution, subject to the checkpointer's capabilities.
StateGraphCompiledGraphcheckpointerbreakpointsinvokeStatenodesedges