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

AI Agents · Class

Command

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.

How it works

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.

Syntax

new Command<Resume = unknown, Update extends Record<string, unknown> = Record<string, unknown>, Nodes extends string = string>(args: Omit<CommandParams<Resume, Update, Nodes>, "lg_name">)

Parameters

NameTypeDescription
updateUpdateState update applied as though the node returned this value directly.
gotoNodes | Send<Nodes, any> | (Nodes | Send<Nodes, any>)[]Next node, nodes, or Send value(s) to execute.
graphstringTarget graph; omitted means the current graph, while Command.PARENT targets the closest parent graph from a subgraph.
resumeResumeValue used to resume execution after an interrupt.

Returns

A Command<Resume, Update, Nodes> instance that carries state updates and/or graph control instructions.

Key points

  • Command can combine state changes with messages or navigation to downstream nodes.
  • Return Command when a node must both modify state and decide the next route, instead of separating those concerns into a conditional edge.
  • The update field supplies the state patch produced by the command-returning node.
  • A string in goto names the next node to execute.
  • A Send value in goto directs execution to a node with explicitly supplied input.
  • The graph field chooses where the command is delivered: the current graph by default or another supported graph target.

Example: Update state and route with Command

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.

When to use it

Combined update and routing

A node needs to write state and select the next node without a separate conditional edge.

Explicit-input dispatch with Send

A workflow must send different inputs to selected downstream nodes.

Cross-graph navigation

A subgraph must hand control to a node in its parent graph.

Interrupt resumption

A node resumes an interrupted workflow with a supplied value.

Interview questions

QWhat problem does Command solve?

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.

QWhat does the Command constructor accept?

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.

QWhat forms can goto take?

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.

  • CommandInstance
  • Send
  • Command.PARENT
  • StateGraph
  • interrupt

Source: Command | LangGraph.js API Reference. Checked 2026-10-15.