Combined update and routing
A node needs to write state and select the next node without a separate conditional edge.
AI Agents · Class
Command lets a LangGraph node combine a state update with routing instructions. It can target ordinary nodes, Send-based execution, or a parent graph from a subgraph.
Command packages a graph state update with execution control. A node can return it to write an update and route to one or more nodes, including a parent graph or a node receiving an explicit Send payload.
new Command<Resume = unknown, Update extends Record<string, unknown> = Record<string, unknown>, Nodes extends string = string>(args: Omit<CommandParams<Resume, Update, Nodes>, "lg_name">)| Name | Type | Description |
|---|---|---|
update | Update | State update applied as though the node returned this value directly. |
goto | Nodes | Send<Nodes, any> | (Nodes | Send<Nodes, any>)[] | Next node, nodes, or Send value(s) to execute. |
graph | string | Target graph; omitted means the current graph, while Command.PARENT targets the closest parent graph from a subgraph. |
resume | Resume | Value used to resume execution after an interrupt. |
A Command<Resume, Update, Nodes> instance that carries state updates and/or graph control instructions.
import { Annotation, Command, StateGraph, START, END } from "@langchain/langgraph";
const State = Annotation.Root({
value: Annotation<number>(),
next: Annotation<string>(),
});
const route = (state: typeof State.State): Command<"finish"> => {
if (state.value > 0) {
return new Command({ update: { next: "finish" }, goto: "finish" });
}
return new Command({ update: { next: "finish" }, goto: END });
};
const finish = (state: typeof State.State) => ({
value: state.value + 1,
});
const graph = new StateGraph(State)
.addNode("route", route)
.addNode("finish", finish)
.addEdge(START, "route")
.addEdge("finish", END)
.compile();
await graph.invoke({ value: 1, next: "" });The node returns a Command that updates state and selects the next destination in one result.
A node needs to write state and select the next node without a separate conditional edge.
A workflow must send different inputs to selected downstream nodes.
A subgraph must hand control to a node in its parent graph.
A node resumes an interrupted workflow with a supplied value.
A Command combines a state update with control flow in a single node return. Its update is written as though the node had returned that state value directly, while goto chooses the next node or nodes.
Follow-up: How can a command route across graph boundaries? Set graph to Command.PARENT when returning from a subgraph, and configure the parent graph's possible destinations.
The constructor receives an object matching CommandParams, excluding the internal lg_name field. The main options are update for state changes, goto for destinations or Send objects, graph for graph targeting, and resume for interrupt continuation.
Follow-up: What does a Send destination add? It lets the command execute a selected node with the exact input carried by the Send object.
goto may be one node name, a sequence of node names, one Send object, or a sequence of Send objects. This supports ordinary routing as well as fan-out-style execution with explicit per-node inputs.
Follow-up: When should a node use multiple destinations? Use a sequence when the same command must schedule several next steps or Send objects with distinct inputs.
CommandInstanceSendCommand.PARENTStateGraphinterrupt