AI Hints
Home›LangGraph Tutorial›Command and Multi-Agent
LangGraph · Lesson 67 of 120

Command

Update and go.

⏱ 2 min read▶ Video at 1:11:24🎓 Free
Watch this lesson in the LangGraph courseWatch on YouTube ↗Full LangGraph course

Usually, a node updates the state, and a separate edge decides where to go next. Command lets one node do both at once. Return a command, with an update, and a go to.

Command
From the video: Command
  • Update in the node
  • Route in the edge
  • Command does both

Triage with Command

The triage node returns a command. Its update saves a priority. Its go to picks the next node. The return type lists the possible targets, so LangGraph can draw the graph. Notice there are no edges out of triage at all. Run it. A large refund goes to the manager, and a small one goes straight through.

command.pypython
from typing import Literal, TypedDict
from langgraph.graph import StateGraph, START
from langgraph.types import Command

class State(TypedDict):
    amount: float
    priority: str
    handled_by: str

def triage(s) -> Command[Literal["manager", "auto"]]:
    if s["amount"] > 20:
        return Command(update={"priority": "high"}, goto="manager")
    return Command(update={"priority": "low"}, goto="auto")

b = StateGraph(State); b.add_node(triage); b.add_edge(START, "triage")
b.add_node("manager", lambda s: {"handled_by": "manager"})
b.add_node("auto", lambda s: {"handled_by": "automatic"})
for amount in [45.0, 8.0]:
    print(amount, b.compile().invoke({"amount": amount}))
Output
45.0 {'amount': 45.0, 'priority': 'high', 'handled_by': 'manager'}
8.0 {'amount': 8.0, 'priority': 'low', 'handled_by': 'automatic'}

Command vs edges

Use conditional edges when routing is simple, and separate from the work. Use command when the node that does the work also knows where to go next. Command is especially handy for agents handing control to each other.

keyvalue
Edgesrouting kept separate
Commandwork + route together
Return typelists the targets
Great foragent handoffs

Summary

A command updates the state and chooses the next node, from inside one node. Next, why we might split work across several agents.

Key takeaways
  • Command(update, goto)
  • Literal targets
  • No edges needed
Next up: Multi-Agent Systems — why split