LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
LangGraph
  • Web
  • Channels
  • Pregel
  • Prebuilt
  • Remote
LangGraph SDK
  • Client
  • Auth
  • React
  • Logging
  • React Ui
  • Server
LangGraph Checkpoint
LangGraph Checkpoint MongoDB
LangGraph Checkpoint Postgres
  • Store
LangGraph Checkpoint Redis
  • Shallow
  • Store
LangGraph Checkpoint SQLite
LangGraph Checkpoint Validation
  • Cli
LangGraph API
LangGraph CLI
LangGraph CUA
  • Utils
LangGraph Supervisor
LangGraph Swarm
⌘I

LangChain Assistant

Ask a question to get started

Enter to send•Shift+Enter new line

Menu

LangGraph
WebChannelsPregelPrebuiltRemote
LangGraph SDK
ClientAuthReactLoggingReact UiServer
LangGraph Checkpoint
LangGraph Checkpoint MongoDB
LangGraph Checkpoint Postgres
Store
LangGraph Checkpoint Redis
ShallowStore
LangGraph Checkpoint SQLite
LangGraph Checkpoint Validation
Cli
LangGraph API
LangGraph CLI
LangGraph CUA
Utils
LangGraph Supervisor
LangGraph Swarm
Language
Theme
JavaScript@langchain/langgraphindexCommand
Class●Since v0.3

Command

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

Copy
class Command

Bases

CommandInstance<Resume, Update, Nodes>

Used in Docs

  • Build a custom SQL agent
  • Build a personal assistant with subagents
  • Build a SQL assistant with on-demand skills
  • Build customer support with handoffs
  • Graph API overview

Example

Copy
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' }

Constructors

constructor
constructor

Properties

property
[COMMAND_SYMBOL]: CommandParams<Resume, Update, Nodes>
property
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
property
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)
property
lc_direct_tool_output: boolean
property
lg_name: "Command"
property
resume: Resume

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

property
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.

property
PARENT: string

Methods

method
toJSON→ __type
View source on GitHub