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
1from typing import Literal2from langgraph.types import Command34def review_claim(state) -> Command[Literal["fast_track", "adjuster", "reject"]]:5 score = fraud_model.score(state["claim"])6 if score > 0.9:7 return Command(update={"fraud_score": score, "reason": "high fraud risk"}, goto="reject")8 if state["claim"]["amount_inr"] > 200_000 or score > 0.4:9 return Command(update={"fraud_score": score}, goto="adjuster")10 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
| Field | Meaning |
|---|---|
update | State update, merged through reducers |
goto | Next node name, a list of names, or Send objects |
graph | Command.PARENT to route in the parent graph |
resume | The 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 returnCommand(update=...)to change state, which is how handoff tools and state-updating tools work; include aToolMessagein the update. - "How is
resumeused?" —graph.invoke(Command(resume={"approved": True}), config)continues a paused thread, and that value becomes the return value ofinterrupt().