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

AI Agents · Function

compile

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.

How it works

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.

Syntax

const graph = builder.compile(options?);

Parameters

NameTypeDescription
optionsCompileOptionsOptional compile configuration, including runtime settings such as a checkpointer; the API reference also lists cache and description options.

Returns

A compiled executable graph, represented by a CompiledGraph-compatible runtime object.

Key points

  • After adding the graph’s nodes and edges, call .compile() to create the executable graph.
  • Pass a checkpointer in the compile options when the graph needs persistence.
  • Compilation catches basic structural issues, including orphaned nodes.

Common mistakes

  • A graph builder is only a definition; invoking or otherwise using it before calling compile is invalid.
  • Compilation validates graph structure, so disconnected or otherwise malformed definitions can fail at this stage.

Example: Compile and invoke a graph

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); // 6

Build a StateGraph, connect its start and end nodes, compile it, and invoke the resulting runnable graph.

When to use it

Execute a workflow

Turn a completed node-and-edge workflow into a runnable graph.

Configure checkpointing

Attach persistence to graph execution.

Validate graph topology

Catch malformed graph structure before runtime execution.

Interview questions

QWhat does 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.

QWhen should compilation happen?

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.

QHow do you configure a checkpointer?

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.

  • StateGraph
  • CompiledGraph
  • checkpointer
  • breakpoints
  • invoke
  • State
  • nodes
  • edges

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