KEMBAR78
Command | LangGraph.js API Reference

Class Command<Resume, Update, Nodes>

One or more commands to update the graph's state and send messages to nodes. Can be used to combine routing logic with state updates in lieu of conditional edges

import { Annotation, Command } from "@langchain/langgraph";

// Define graph state
const StateAnnotation = Annotation.Root({
foo: Annotation<string>,
});

// Define the nodes
const nodeA = async (_state: typeof StateAnnotation.State) => {
console.log("Called A");
// this is a replacement for a real conditional edge function
const goto = Math.random() > .5 ? "nodeB" : "nodeC";
// note how Command allows you to BOTH update the graph state AND route to the next node
return new Command({
// this is the state update
update: {
foo: "a",
},
// this is a replacement for an edge
goto,
});
};

// Nodes B and C are unchanged
const nodeB = async (state: typeof StateAnnotation.State) => {
console.log("Called B");
return {
foo: state.foo + "|b",
};
}

const nodeC = async (state: typeof StateAnnotation.State) => {
console.log("Called C");
return {
foo: state.foo + "|c",
};
}

import { StateGraph } from "@langchain/langgraph";

// NOTE: there are no edges between nodes A, B and C!
const graph = new StateGraph(StateAnnotation)
.addNode("nodeA", nodeA, {
ends: ["nodeB", "nodeC"],
})
.addNode("nodeB", nodeB)
.addNode("nodeC", nodeC)
.addEdge("__start__", "nodeA")
.compile();

await graph.invoke({ foo: "" });

// Randomly oscillates between
// { foo: 'a|c' } and { foo: 'a|b' }

Type Parameters

  • Resume = unknown
  • Update extends Record<string, unknown> = Record<string, unknown>
  • Nodes extends string = string

Hierarchy

Index

Constructors

Properties

"[COMMAND_SYMBOL]": CommandParams<Resume, Update, Nodes>
goto?: Nodes | Send<Nodes, any> | (Nodes | Send<Nodes, any>)[]

Can be one of the following:

  • name of the node to navigate to next (any node that belongs to the specified graph)
  • sequence of node names to navigate to next
  • Send object (to execute a node with the exact input provided in the Send object)
  • sequence of Send objects
graph?: string

Graph to send the command to. Supported values are:

  • None: the current graph (default)
  • The specific name of the graph to send the command to
  • Command.PARENT: closest parent graph (only supported when returned from a node in a subgraph)
lc_direct_tool_output: boolean
lg_name: string
resume?: Resume

Value to resume execution with. To be used together with interrupt.

update?: Update | [string, unknown][]

Update to apply to the graph's state as a result of executing the node that is returning the command. Written to the state as if the node had simply returned this value instead of the Command object.

PARENT: string

Methods

  • Returns {
        goto:
            | undefined
            | Nodes
            | (Nodes | { args: any; lg_name: string; node: Nodes })[]
            | { args: any; lg_name: string; node: Nodes };
        lg_name: string;
        resume: undefined | Resume;
        update: undefined | Update | [string, unknown][];
    }