Breakpoints¶
Breakpoints pause graph execution at specific points and enable stepping through execution step by step. Breakpoints are powered by LangGraph's persistence layer, which saves the state after each graph step. Breakpoints can also be used to enable human-in-the-loop workflows, though we recommend using the interrupt
function for this purpose.
Requirements¶
To use breakpoints, you will need to:
-
Specify a checkpointer to save the graph state after each step.
-
Set breakpoints to specify where execution should pause.
-
Run the graph with a thread ID to pause execution at the breakpoint.
-
Resume execution using
invoke
/stream
(see TheCommand
primitive).
Setting breakpoints¶
There are two places where you can set breakpoints:
-
Before or after a node executes by setting breakpoints at compile time or run time. We call these static breakpoints.
-
Inside a node using the
NodeInterrupt
error.
Static breakpoints¶
Static breakpoints are triggered either before or after a node executes. You can set static breakpoints by specifying interruptBefore
and interruptAfter
at "compile" time or run time.
const graph = graphBuilder.compile({
interruptBefore: ["nodeA"],
interruptAfter: ["nodeB", "nodeC"],
checkpointer: ..., // Specify a checkpointer
});
const threadConfig = {
configurable: {
thread_id: "someThread"
}
};
// Run the graph until the breakpoint
await graph.invoke(inputs, threadConfig);
// Optionally update the graph state based on user input
await graph.updateState(update, threadConfig);
// Resume the graph
await graph.invoke(null, threadConfig);
await graph.invoke(
inputs,
{
configurable: { thread_id: "someThread" },
interruptBefore: ["nodeA"],
interruptAfter: ["nodeB", "nodeC"]
}
);
const threadConfig = {
configurable: {
thread_id: "someThread"
}
};
// Run the graph until the breakpoint
await graph.invoke(inputs, threadConfig);
// Optionally update the graph state based on user input
await graph.updateState(update, threadConfig);
// Resume the graph
await graph.invoke(null, threadConfig);
Note
You cannot set static breakpoints at runtime for sub-graphs.
If you have a sub-graph, you must set the breakpoints at compilation time.
Static breakpoints can be especially useful for debugging if you want to step through the graph execution one node at a time or if you want to pause the graph execution at specific nodes.
NodeInterrupt
error¶
We recommend that you use the interrupt
function instead of the NodeInterrupt
error if you're trying to implement
human-in-the-loop workflows. The interrupt
function is easier to use and more flexible.
NodeInterrupt
error
The developer can define some condition that must be met for a breakpoint to be triggered. This concept of dynamic breakpoints is useful when the developer wants to halt the graph under a particular condition. This uses a NodeInterrupt
, which is a special type of error that can be thrown from within a node based upon some condition. As an example, we can define a dynamic breakpoint that triggers when the input
is longer than 5 characters.
function myNode(state: typeof GraphAnnotation.State) {
if (state.input.length > 5) {
throw new NodeInterrupt(`Received input that is longer than 5 characters: ${state.input}`);
}
return state;
}
Let's assume we run the graph with an input that triggers the dynamic breakpoint and then attempt to resume the graph execution simply by passing in null
for the input.
// Attempt to continue the graph execution with no change to state after we hit the dynamic breakpoint
for await (const event of await graph.stream(null, threadConfig)) {
console.log(event);
}
The graph will interrupt again because this node will be re-run with the same graph state. We need to change the graph state such that the condition that triggers the dynamic breakpoint is no longer met. So, we can simply edit the graph state to an input that meets the condition of our dynamic breakpoint (< 5 characters) and re-run the node.
// Update the state to pass the dynamic breakpoint
await graph.updateState({ input: "foo" }, threadConfig);
for await (const event of await graph.stream(null, threadConfig)) {
console.log(event);
}
Alternatively, what if we want to keep our current input and skip the node (myNode
) that performs the check? To do this, we can simply perform the graph update with "myNode"
(the node name) as the third positional argument, and pass in null
for the values. This will make no update to the graph state, but run the update as myNode
, effectively skipping the node and bypassing the dynamic breakpoint.
Additional Resources 📚¶
-
Conceptual Guide: Persistence: Read the persistence guide for more context about persistence.
-
Conceptual Guide: Human-in-the-loop: Read the human-in-the-loop guide for more context on integrating human feedback into LangGraph applications using breakpoints.
-
How to View and Update Past Graph State: Step-by-step instructions for working with graph state that demonstrate the replay and fork actions.