LangGraph Agents

Course Content

LangGraph Agents

7 sections · 49 lessons

What is the Command API and when is it useful for “state updates + hop”?


What you need to know

Python
from typing import Literalfrom langgraph.types import Commanddef review_claim(state) -> Command[Literal["fast_track", "adjuster", "reject"]]:    score = fraud_model.score(state["claim"])    if score > 0.9:        return Command(update={"fraud_score": score, "reason": "high fraud risk"}, goto="reject")    if state["claim"]["amount_inr"] > 200_000 or score > 0.4:        return Command(update={"fraud_score": score}, goto="adjuster")    return Command(update={"fraud_score": score}, goto="fast_track")

No add_conditional_edges is needed after review_claim. The Literal tells LangGraph the possible targets for drawing and validation.

The fields of Command

FieldMeaning
updateState update, merged through reducers
gotoNext node name, a list of names, or Send objects
graphCommand.PARENT to route in the parent graph
resumeThe value an interrupt() returns when a run continues

Command or conditional edge

Use Command

  • The node already computed the decision
  • A supervisor or handoff picks the next agent
  • You need to jump out of a subgraph

Use a conditional edge

  • Routing is a separate, reusable rule
  • Several nodes share the same routing
  • You want the node to stay unaware of the graph

Mixing both on one node is allowed but confusing: static edges from that node still fire in addition to the goto.

A real-life example

A claims team first built review_claim as a node plus a conditional edge that recomputed the fraud threshold. When actuaries changed the threshold from 0.85 to 0.9, someone updated the node but not the edge, and 140 claims were scored "safe" but routed to rejection for two days. Moving to Command put the score and the routing in one function, so there is one threshold and one unit test.

Follow-up questions to expect

  • "How does a subgraph agent hand off to a sibling?" — return Command(goto="billing_agent", graph=Command.PARENT, update={...}). Keys updated this way that the parent shares with the subgraph need reducers.
  • "Can a tool return a Command?" — Yes. A tool can return Command(update=...) to change state, which is how handoff tools and state-updating tools work; include a ToolMessage in the update.
  • "How is resume used?" — graph.invoke(Command(resume={"approved": True}), config) continues a paused thread, and that value becomes the return value of interrupt().